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

test("chat list receives monitored sessions and renders row status dots", async () => {
  const [app, panel, hook, css] = await Promise.all([
    readFile(new URL("../../App.jsx", import.meta.url), "utf8"),
    readFile(new URL("./ChatsPanel.jsx", import.meta.url), "utf8"),
    readFile(new URL("../notifications/useNotifications.js", import.meta.url), "utf8"),
    readFile(new URL("./chats.css", import.meta.url), "utf8")
  ]);

  assert.match(hook, /sessions,/);
  assert.match(app, /sessions=\{notifications\.sessions\}/);
  assert.match(app, /currentSession=\{displaySession\}/);
  assert.match(app, /notifications=\{notifications\.notifications\}/);
  assert.match(panel, /chatRowStatus\(item, sessions, notifications, currentSession\)/);
  assert.match(panel, /thread-status-dot--notification/);
  assert.match(panel, /thread-status-dot--active/);
  assert.match(css, /\.thread-status-dot--notification\{[^}]*#ef5350/);
  assert.match(css, /\.thread-status-dot--active\{[^}]*#f0a33a/);
});

test("opening a chat row dismisses every unread notification for its session", async () => {
  const app = await readFile(new URL("../../App.jsx", import.meta.url), "utf8");

  assert.match(app, /async function handleChatOpen\(thread\)/);
  assert.match(app, /notifications\.sessions\.find/);
  assert.match(app, /item\.accountId === thread\.ownerId && item\.codexThreadId === thread\.id/);
  assert.match(app, /if \(session\) notifications\.dismissSession\(session\.id\)/);
  assert.match(app, /onOpen=\{handleChatOpen\}/);
});

test("app returns to Chats when the active chat is archived", async () => {
  const app = await readFile(new URL("../../App.jsx", import.meta.url), "utf8");
  assert.match(app, /async function handleChatArchive\(thread\)/);
  assert.match(app, /await chats\.archive\(thread\)/);
  assert.match(app, /app\.selectTab\("threads"\)/);
  assert.match(app, /onArchive=\{handleChatArchive\}/);
});
