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

test("context load rewrites stale saved account and workspace ids", async () => {
  const source = await readFile(new URL("./useAppController.js", import.meta.url), "utf8");

  assert.match(source, /resolveInitialContext/);
  assert.match(source, /const initialContext = resolveInitialContext/);
  assert.match(source, /writeSavedContext\(undefined, initialContext\)/);
  assert.match(source, /savedContext: initialContext/);
});

test("settled account and workspace selection stays persisted", async () => {
  const source = await readFile(new URL("./useAppController.js", import.meta.url), "utf8");

  assert.match(source, /if \(!state\.contextLoaded\) return;/);
  assert.match(source, /writeSavedContext\(undefined, \{ accountId: state\.accountId, workspaceId: state\.workspaceId \}\)/);
});

test("failed saved session restore clears storage and returns to chats", async () => {
  const source = await readFile(new URL("./useAppController.js", import.meta.url), "utf8");

  assert.match(source, /\.catch\(\(\) => \{\s*clearSavedSession\(\);\s*if \(active\) dispatch\(\{ type: "session\.restore\.failed", restoreSessionId: saved\.sessionId \}\);\s*\}\)/s);
});

test("context load keeps partial results when one list request fails", async () => {
  const source = await readFile(new URL("./useAppController.js", import.meta.url), "utf8");

  assert.match(source, /Promise\.allSettled\(\[request\("\/accounts"\), request\("\/workspaces"\)\]\)/);
  assert.doesNotMatch(source, /Promise\.all\(\[request\("\/accounts"\), request\("\/workspaces"\)\]\)/);
  assert.match(source, /accountResult\.status === "fulfilled"/);
  assert.match(source, /workspaceResult\.status === "fulfilled"/);
  assert.match(source, /accountsLoaded/);
  assert.match(source, /workspacesLoaded/);
});

test("unknown workspace sessions do not persist the previous workspace as attached", async () => {
  const source = await readFile(new URL("./useAppController.js", import.meta.url), "utf8");

  assert.match(source, /workspaceId: session\.workspaceId \|\| ""/);
});

test("context load preserves a safe PC reachability code for localized status", async () => {
  const source = await readFile(new URL("./useAppController.js", import.meta.url), "utf8");
  const app = await readFile(new URL("../App.jsx", import.meta.url), "utf8");
  const css = await readFile(new URL("../features/status/status.css", import.meta.url), "utf8");

  assert.match(source, /contextLoadError/);
  assert.match(source, /return "pc_unreachable"/);
  assert.match(app, /connectionStatusKey/);
  assert.match(app, /connectionError=\{connectionMessageKey \? t\(connectionMessageKey\) : ""\}/);
  assert.match(css, /\.status-connection-error/);
});

test("cold start stays gated until saved view and session restoration settle", async () => {
  const [controller, state, app] = await Promise.all([
    readFile(new URL("./useAppController.js", import.meta.url), "utf8"),
    readFile(new URL("./app-state.js", import.meta.url), "utf8"),
    readFile(new URL("../App.jsx", import.meta.url), "utf8")
  ]);

  assert.match(state, /bootStatus: "loading"/);
  assert.match(state, /action\.type === "bootstrap\.ready"/);
  assert.match(controller, /dispatch\(\{ type: "bootstrap\.ready" \}\)/);
  assert.match(app, /app\.bootStatus === "loading"/);
  assert.match(app, /<StartupScreen/);
});
