// Tests for the Claude context shaper. node test/context.test.mjs import assert from "node:assert/strict"; import { trimContext } from "../agent/context.mjs"; let failures = 0; function test(name, fn) { try { fn(); console.log(`PASS ${name}`); } catch (e) { failures++; console.log(`FAIL ${name} — ${e && e.message}`); } } const u = (content = "hi") => ({ role: "user", content }); const a = (content = "hi") => ({ role: "assistant", content }); test("drops a leading assistant turn (API needs user first)", () => { assert.deepEqual(trimContext([a("greeting"), u("q"), a("ans")]), [u("q")]); }); test("drops a trailing assistant turn (no prefill under thinking)", () => { // The 400 case: newest message we can see is our own reply. assert.deepEqual(trimContext([u("q"), a("ans")]), [u("q")]); }); test("keeps a conversation that already ends on a user turn", () => { const c = [u("a"), a("b"), u("c")]; assert.deepEqual(trimContext(c), c); }); test("all-assistant collapses to empty (caller falls back to a hello)", () => { assert.deepEqual(trimContext([a(), a()]), []); }); test("trims both ends at once", () => { assert.deepEqual(trimContext([a(), u("x"), a("y"), u("z"), a("w")]), [u("x"), a("y"), u("z")]); }); test("empty / nullish input is safe and returns a fresh array", () => { assert.deepEqual(trimContext([]), []); assert.deepEqual(trimContext(null), []); const src = [u("x")]; assert.notEqual(trimContext(src), src); // never mutates or aliases the caller's array }); console.log(failures === 0 ? "\nALL PASSED" : `\n${failures} FAILED`); process.exit(failures === 0 ? 0 : 1);