import test from "node:test";
import assert from "node:assert/strict";
import { chatRowStatus } from "./chat-row-status.js";

test("chat row status marks unread completed notifications red", () => {
  const thread = { id: "thread-1", ownerId: "account-a" };
  const sessions = [{ id: "session-1", accountId: "account-a", codexThreadId: "thread-1", status: "completed" }];
  const notifications = [{ type: "completed", sessionId: "session-1" }];

  assert.equal(chatRowStatus(thread, sessions, notifications), "notification");
});

test("chat row status marks unread approval notifications red before active state", () => {
  const thread = { id: "thread-1", ownerId: "account-a" };
  const sessions = [{ id: "session-1", accountId: "account-a", codexThreadId: "thread-1", status: "waiting_approval" }];
  const notifications = [{ type: "approval", sessionId: "session-1" }];

  assert.equal(chatRowStatus(thread, sessions, notifications), "notification");
});

test("chat row status marks failed session notifications red", () => {
  const thread = { id: "thread-1", ownerId: "account-a" };
  const sessions = [{ id: "session-1", accountId: "account-a", codexThreadId: "thread-1", status: "failed" }];
  const notifications = [{ type: "usage-exhausted", sessionId: "session-1" }];

  assert.equal(chatRowStatus(thread, sessions, notifications), "notification");
});

test("chat row status marks active responding sessions orange", () => {
  const thread = { id: "thread-1", ownerId: "account-a" };
  const sessions = [{ id: "session-1", accountId: "account-a", codexThreadId: "thread-1", status: "running" }];

  assert.equal(chatRowStatus(thread, sessions, []), "active");
});

test("chat row status prefers the live current session before the monitor polls", () => {
  const thread = { id: "thread-1", ownerId: "account-a" };
  const sessions = [{ id: "session-1", accountId: "account-a", codexThreadId: "thread-1", status: "completed" }];
  const currentSession = { id: "session-1", accountId: "account-a", codexThreadId: "thread-1", status: "running" };

  assert.equal(chatRowStatus(thread, sessions, [], currentSession), "active");
});

test("chat row status stays active while the live session waits for a response", () => {
  const thread = { id: "thread-1", ownerId: "account-a" };
  const currentSession = { id: "session-1", accountId: "account-a", codexThreadId: "thread-1", status: "waiting_response" };

  assert.equal(chatRowStatus(thread, [], [], currentSession), "active");
});

test("chat row status ignores sessions from other accounts", () => {
  const thread = { id: "thread-1", ownerId: "account-a" };
  const sessions = [{ id: "session-1", accountId: "account-b", codexThreadId: "thread-1", status: "running" }];

  assert.equal(chatRowStatus(thread, sessions, []), "idle");
});
