// Native compaction fixture through the real chat fold/view; no harness calls.
import assert from "node:assert/strict";
import { createServer } from "node:http";
import { readFile, mkdir } from "node:fs/promises";
import { resolve, extname, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { launchChrome } from "./browser.mjs";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const out = resolve(process.argv[2] ?? "captures-chat-compaction");
await mkdir(out, {recursive:true});
const html = `
${["design/tokens/tokens","base","shell","lists","run-detail","chat","dialogs"].map(n=>``).join("")}
`;
const server=createServer(async(req,res)=>{try{
const path=new URL(req.url,"http://fixture").pathname;
if(path==="/"){res.setHeader("Content-Type","text/html");res.end(html);return;}
const file=resolve(root,`.${path}`);if(!file.startsWith(root+"/"))throw Error("outside fixture");
res.setHeader("Content-Type",extname(file)===".css"?"text/css":"text/javascript");res.end(await readFile(file));
}catch{res.writeHead(404);res.end();}});
await new Promise(r=>server.listen(0,"127.0.0.1",r));
let browser;
try {
browser=await launchChrome();
const page=await browser.openPage(`http://127.0.0.1:${server.address().port}/`);
await page.waitForSelector("#chatInput");
for(const width of [390,1100]) {
await page.resize(width,844);
for(const history of [false,true]) {
await page.evaluate(`setup(${history});event('running')`);
await page.waitForSelector(".chat-compaction-status");
assert.equal(await page.evaluate("document.querySelector('.chat-compaction-status').textContent"),"Compacting context…");
assert.equal(await page.evaluate("document.querySelector('.chat-compaction-status').getAttribute('role')"),"status");
assert.equal(await page.evaluate("messageCount()"),history?2:1);
assert.equal(await page.evaluate("document.querySelectorAll('#chatStop').length"),1);
if(history) assert.equal(await page.evaluate("document.querySelector('.chat-reasoning-label').textContent"),"Reasoning");
assert.equal(await page.evaluate("document.documentElement.scrollWidth>innerWidth"),false);
await page.screenshot(resolve(out,`compacting-${history?'with-history':'before-answer'}-${width}.png`));
await page.evaluate("event('completed')");
assert.equal(await page.evaluate("document.querySelectorAll('.chat-compaction-status').length"),0);
assert.equal(await page.evaluate("document.querySelectorAll('#chatStop').length"),1);
await page.evaluate("end()");
assert.equal(await page.evaluate("document.querySelectorAll('#chatStop').length"),0);
}
}
await page.evaluate("setup(true);event('running');drop()");
assert.equal(await page.evaluate("document.querySelectorAll('.chat-compaction-status').length"),0);
await page.screenshot(resolve(out,"compaction-clears-on-disconnect.png"));
await page.evaluate("reconnect()");
assert.equal(await page.evaluate("document.querySelector('.chat-compaction-status')?.textContent"),"Compacting context…");
await page.screenshot(resolve(out,"compaction-replay-survives-history-refresh.png"));
console.log("PASS: native compaction is visible before/alongside answers, clears on completion/disconnect, survives reconnect history refresh, and creates no fake message or premature turn completion.");
} finally {await browser?.close();await new Promise(r=>server.close(r));}