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

import { createReviewDemoState, reviewDemoReducer } from "./review-demo-state.js";

test("review demo seed is deterministic and contains only synthetic public data", () => {
  const first = createReviewDemoState();
  const second = createReviewDemoState();
  const serialized = JSON.stringify(first);

  assert.deepEqual(first, second);
  assert.equal(first.mode, "chats");
  assert.equal(first.account.label, "Demo account");
  assert.equal(first.workspace.label, "Sample project");
  assert.match(serialized, /synthetic/i);
  assert.doesNotMatch(serialized, /auth\.json|access[_-]?token|refresh[_-]?token|api[_-]?key|CODEX_HOME/i);
  assert.doesNotMatch(serialized, /[A-Za-z]:\\|\\Users\\|\/Users\/|\/home\//);
});

test("review demo supports the reviewer's core interaction path without an API", () => {
  let state = createReviewDemoState();
  state = reviewDemoReducer(state, { type: "open-chat" });
  state = reviewDemoReducer(state, { type: "send", message: "Summarize the sample change" });
  state = reviewDemoReducer(state, { type: "complete-response" });
  state = reviewDemoReducer(state, { type: "decide-approval", decision: "approved" });
  state = reviewDemoReducer(state, { type: "navigate", mode: "changes" });

  assert.equal(state.mode, "changes");
  assert.equal(state.approval.status, "approved");
  assert.equal(state.messages.at(-2).role, "user");
  assert.equal(state.messages.at(-1).status, "complete");
  assert.equal(state.files.length > 0, true);

  assert.deepEqual(reviewDemoReducer(state, { type: "reset" }), createReviewDemoState());
});

test("native and authentication connection screens expose the review demo", async () => {
  const [app, nativeGate, authGate, demo] = await Promise.all([
    readFile(new URL("../../App.jsx", import.meta.url), "utf8"),
    readFile(new URL("../connection/NativeConnectionGate.jsx", import.meta.url), "utf8"),
    readFile(new URL("../auth/AuthenticationGate.jsx", import.meta.url), "utf8"),
    readFile(new URL("./ReviewDemo.jsx", import.meta.url), "utf8")
  ]);

  assert.match(app, /ReviewDemoProvider/);
  assert.match(nativeGate, /startReviewDemo/);
  assert.match(nativeGate, /reviewDemo\.start/);
  assert.match(authGate, /startReviewDemo/);
  assert.match(authGate, /reviewDemo\.start/);
  assert.match(demo, /reviewDemo\.syntheticNotice/);
  assert.match(demo, /reviewDemo\.exit/);
  assert.match(demo, /reviewDemo\.reset/);
  assert.doesNotMatch(demo, /\brequest\s*\(|EventSource|fetch\s*\(/);
});
