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

test("starting a new chat search aborts the previous network request", async () => {
  const [controller, api] = await Promise.all([
    readFile(new URL("./useChats.js", import.meta.url), "utf8"),
    readFile(new URL("./chats-api.js", import.meta.url), "utf8")
  ]);

  assert.match(controller, /new AbortController\(\)/);
  assert.match(controller, /controller\.abort\(\)/);
  assert.match(controller, /load\(undefined, activeSearch, archived, \{ signal: controller\.signal \}\)/);
  assert.match(controller, /if \(signal\?\.aborted\) return/);
  assert.match(api, /listChats\(cursor, search, archived = false, options = \{\}\)/);
  assert.match(api, /request\(chatsListPath\(cursor, search, archived\), options\)/);
});
