import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import { chatsListPath, projectChatsPath } from "./chats-query.js";

test("opening a chat forwards its safe project label", async () => {
  const source = await readFile(new URL("./chats-api.js", import.meta.url), "utf8");
  assert.match(source, /projectLabel: thread\.workspaceLabel/);
});

test("project chat can send its initial message", async () => {
  const source = await readFile(new URL("./chats-api.js", import.meta.url), "utf8");
  assert.match(source, /sendInitialChatMessage/);
  assert.match(source, /uploadChatAttachment/);
  assert.match(source, /body: payload/);
});

test("chat list path encodes search and cursor", () => {
  assert.equal(
    chatsListPath("next/page", "로그인 오류"),
    "/threads?cursor=next%2Fpage&search=%EB%A1%9C%EA%B7%B8%EC%9D%B8+%EC%98%A4%EB%A5%98"
  );
});

test("chat list path omits empty search and cursor", () => {
  assert.equal(chatsListPath(undefined, "  "), "/threads");
});

test("chat list path can request archived chats", () => {
  assert.equal(chatsListPath(undefined, "", true), "/threads?archived=true");
});

test("project history uses its own endpoint", async () => {
  const source = await readFile(new URL("./chats-api.js", import.meta.url), "utf8");
  assert.match(source, /listProjectChats/);
  assert.match(source, /projectChatsPath\(cursor\)/);
  assert.equal(projectChatsPath(undefined), "/thread-projects");
  assert.equal(projectChatsPath("next/page"), "/thread-projects?cursor=next%2Fpage");
});

test("project cards can be hidden without a delete request", async () => {
  const source = await readFile(new URL("./chats-api.js", import.meta.url), "utf8");
  assert.match(source, /hideProject/);
  assert.match(source, /request\("\/thread-projects\/hide", \{[\s\S]*method: "POST"[\s\S]*projectKey/);
});

test("archived chat delete uses the local-delete endpoint", async () => {
  const source = await readFile(new URL("./chats-api.js", import.meta.url), "utf8");
  assert.match(source, /deleteArchivedChat/);
  assert.match(source, /\/local-delete/);
});
