import test from "node:test"; import assert from "node:assert/strict"; import { applyChatEvent, newChatThread, threadPhase, turnPhaseLabel, settledThread, resumedThread, historyResync, runningAfterFrame, queuedAfterFrame } from "../src/chat.js"; const compact = (status, id="compact-1", session="session-1") => ({session_id:session,kind:"compaction",compaction:{status,id}}); test("silence or model-authored text never invents compaction activity", () => { const thread={...newChatThread("session-1"),running:true,runningSince:1,messages:[{role:"assistant",parts:[{type:"text",text:"Compacting context"}]}]}; assert.equal(threadPhase(thread,10_000_000),"thinking"); }); test("native compaction shows an explicit phase without inventing a tool or message", () => { const original={...newChatThread("session-1"),loaded:true,queued:true}; const current=applyChatEvent(original,compact("running"),100); assert.equal(current.running,true); assert.equal(current.queued,true); assert.equal(threadPhase(current,200),"compacting"); assert.equal(turnPhaseLabel(threadPhase(current,200)),"Compacting context…"); assert.deepEqual(current.messages,[]); assert.equal(runningAfterFrame(false,compact("running")),true); assert.equal(queuedAfterFrame(true,compact("running")),true); }); test("compaction takes priority over old reasoning and tool activity", () => { const current=applyChatEvent({...newChatThread("session-1"),running:true,messages:[{role:"assistant",parts:[{type:"tool",state:{status:"running"}}]}]},compact("running"),100); assert.equal(threadPhase(current,200),"compacting"); const completed=applyChatEvent(current,compact("completed"),300); assert.equal(completed.compaction,null); assert.equal(completed.running,true); assert.equal(threadPhase(completed,400),"tool"); }); test("a terminal compaction update does not complete the surrounding model turn", () => { for (const status of ["completed","failed"]) { const current=applyChatEvent(newChatThread("session-1"),compact("running"),100); const next=applyChatEvent(current,compact(status),200); assert.equal(next.compaction,null); assert.equal(next.running,true); assert.equal(threadPhase(next,250),"thinking"); assert.equal(runningAfterFrame(true,compact(status)),true); } }); test("other-session events and late completions cannot clear a newer compaction", () => { const current=applyChatEvent(newChatThread("session-1"),compact("running","new"),100); assert.equal(applyChatEvent(current,compact("completed","old"),200),current); assert.equal(applyChatEvent(current,compact("running","other","session-2"),200),current); assert.equal(applyChatEvent(current,compact("unrecognised"),200),current); }); test("turn completion, interruption, and reconnect clear stale compaction state", () => { const current=applyChatEvent(newChatThread("session-1"),compact("running"),100); for (const next of [settledThread(current),resumedThread(current),historyResync(current,{messages:[]}),applyChatEvent(current,{kind:"step",step:{status:"ended"}},200)]) { assert.equal(next.compaction,null); assert.equal(threadPhase(next,300),null); } const intermediate=applyChatEvent(current,{kind:"step",step:{status:"ended",intermediate:true}},200); assert.equal(threadPhase(intermediate,300),"compacting"); }); test("authoritative history cannot erase fresh compaction replayed after reconnect reset", () => { const old=applyChatEvent(newChatThread("session-1"),compact("running","old"),100); const reset=resumedThread(old); const replayed=applyChatEvent(reset,compact("running","fresh"),200); const synced=historyResync(replayed,{messages:[{id:"native-history",role:"user",parts:[]}]},{preserveActivity:true}); assert.equal(threadPhase(synced,300),"compacting"); assert.equal(synced.compaction.id,"fresh"); assert.deepEqual(synced.messages,[{id:"native-history",role:"user",parts:[]}]); assert.equal(historyResync(reset,{messages:[]},{preserveActivity:true}).compaction,null); }); test("native turn identity protects compaction and allows the reattached turn to settle", () => { const original={...newChatThread("session-1"),running:true,nativeTurnId:"new-turn"}; const old={...compact("running"),native_turn_id:"old-turn"}; assert.equal(applyChatEvent(original,old,100),original); const reset=resumedThread(original); assert.equal(reset.nativeTurnId,""); const replay=applyChatEvent(reset,{...compact("running"),native_turn_id:"current-turn"},200); assert.equal(replay.nativeTurnId,"current-turn"); assert.equal(applyChatEvent(replay,{...compact("completed"),native_turn_id:"old-turn"},300),replay); const ended=applyChatEvent(replay,{kind:"step",step:{status:"ended",turn_id:"current-turn"}},400); assert.equal(ended.running,false); assert.equal(ended.compaction,null); });