import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";

test("app uses native connection recovery and existing status row", async () => {
  const app = await readFile(new URL("../../App.jsx", import.meta.url), "utf8");
  const hook = await readFile(new URL("./useConnectionRecovery.js", import.meta.url), "utf8");

  assert.match(app, /useConnectionRecovery\(\)/);
  assert.match(app, /connectionStatusKey\(\{/);
  assert.match(app, /connectivityState: connectivity\.state/);
  assert.match(hook, /Network\.addListener\("networkStatusChange"/);
  assert.match(hook, /CapacitorApp\.addListener\("appStateChange"/);
  assert.match(hook, /recoveryVersion/);
  assert.doesNotMatch(hook, /globalThis\.location\.reload\(\)/);
});

test("recovery preserves the visible app while resubscribing live data", async () => {
  const app = await readFile(new URL("../../App.jsx", import.meta.url), "utf8");

  assert.match(app, /useConversation\(\{[\s\S]*recoveryVersion: connectivity\.recoveryVersion/);
  assert.match(app, /chats\.reload\(\{ silent: true \}\)/);
  assert.match(app, /status\.reload\(\{ silent: true \}\)/);
});
