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

test("composer typing stays local and sends the latest local value", async () => {
  const input = await readFile(new URL("./ComposerTextInput.jsx", import.meta.url), "utf8").catch(() => "");
  const panel = await readFile(new URL("./ConversationPanel.jsx", import.meta.url), "utf8");

  assert.match(input, /const \[draft, setDraft\] = useState\(\(\) => value\)/);
  assert.match(input, /value=\{draft\}/);
  assert.match(input, /committerRef\.current\.schedule\(nextValue, sessionId\)/);
  assert.doesNotMatch(input, /onDraftChange\(event\.target\.value\)/);

  assert.match(panel, /const composerMessageRef = useRef\(message\)/);
  assert.match(panel, /async function submit\(messageOverride = composerMessageRef\.current\)/);
  assert.match(panel, /<ComposerTextInput/);
  assert.doesNotMatch(panel, /<textarea className="composer-input"/);
});

test("composer flushes its draft on blur, page hide, and session replacement", async () => {
  const input = await readFile(new URL("./ComposerTextInput.jsx", import.meta.url), "utf8").catch(() => "");

  assert.match(input, /onBlur=\{[^}]*committerRef\.current\.flush\(\)/);
  assert.match(input, /globalThis\.addEventListener\?\.\("pagehide", flushDraft\)/);
  assert.match(input, /committerRef\.current\.replace\(value, sessionId\)/);
  assert.match(input, /\[sessionId, value\]/);
});
