bench / static / structural.test.ts
bench / static / structural.test.ts
import { describe, expect, test } from "vitest";
import {
allFixtures,
codingAgentFixture,
contradictoryUpdatesFixture,
mulberry32,
toolSpamFixture,
} from "../fixtures";
import {
checkViewStructure,
gradeAnswer,
latencyStats,
reductionPercent,
} from "../metrics";
import { BenchRun, renderMarkdown, resultFileBase } from "../report";
import { planCompaction } from "../../src/plan";
import {
CanonMessage,
prefixHashes,
roundBoundaries,
} from "../../src/view";
describe("mulberry32", () => {
test("same seed yields the same sequence, different seeds differ", () => {
const a1 = mulberry32(42);
const a2 = mulberry32(42);
const b = mulberry32(43);
const seqA1 = [a1(), a1(), a1()];
const seqA2 = [a2(), a2(), a2()];
const seqB = [b(), b(), b()];
expect(seqA1).toEqual(seqA2);
expect(seqA1).not.toEqual(seqB);
for (const v of seqA1) {
expect(v).toBeGreaterThanOrEqual(0);
expect(v).toBeLessThan(1);
}
});
});
describe("fixtures", () => {
test("deterministic: same seed produces identical messages and hashes", () => {
for (const [f1, f2] of allFixtures(7).map(
(f, i) => [f, allFixtures(7)[i]] as const,
)) {
expect(f1.msgs).toEqual(f2.msgs);
expect(prefixHashes(f1.msgs)).toEqual(prefixHashes(f2.msgs));
}
});
test("every fixture has messages, facts, and starts with a user message", () => {
for (const f of allFixtures()) {
expect(f.msgs.length).toBeGreaterThan(10);
expect(f.facts.length).toBeGreaterThanOrEqual(3);
expect(f.msgs[0].role).toBe("user");
}
});
test("tool results always directly follow their assistant tool call", () => {
for (const f of allFixtures()) {
f.msgs.forEach((m, i) => {
if (m.role === "tool") {
const prev = f.msgs[i - 1];
const prevOk =
prev.role === "tool" ||
(prev.role === "assistant" && (prev.toolCalls ?? []).length > 0);
expect(prevOk, `${f.name}[${i}]`).toBe(true);
}
});
}
});
test("seeded facts appear verbatim in the transcript", () => {
for (const f of allFixtures()) {
const text = f.msgs
.map(
(m) =>
m.text +
(m.toolResults ?? []).map((r) => r.content).join(" "),
)
.join("\n");
for (const fact of f.facts) {
const found = fact.accepted.some((a) => text.includes(a));
expect(found, `${f.name}: ${fact.id}`).toBe(true);
}
}
});
test("contradictory fixture accepts only the latest value", () => {
const f = contradictoryUpdatesFixture();
const revised = f.facts.find((fact) => fact.id.includes("revised"));
expect(revised).toBeDefined();
});
test("tool spam fixture carries oversized tool results", () => {
const f = toolSpamFixture();
const biggest = Math.max(
...f.msgs.flatMap((m) =>
(m.toolResults ?? []).map((r) => r.content.length),
),
);
expect(biggest).toBeGreaterThan(5000);
});
test("planCompaction cuts every fixture at valid boundaries", () => {
for (const f of allFixtures()) {
const tokens = f.msgs.map((m) => Math.ceil((m.text.length + 40) / 4) + 50);
const boundaries = roundBoundaries(f.msgs);
const plan = planCompaction({
msgs: f.msgs,
tokens,
coveredUpTo: 0,
summaryTokens: 0,
estSummaryTokensPerChunk: 300,
limit: 500,
autoCompact: true,
force: false,
keepRecentTokens: 300,
chunkTokens: 800,
});
expect(plan.cuts.length).toBeGreaterThan(0);
for (const cut of plan.cuts) {
expect(boundaries).toContain(cut);
}
}
});
});
describe("gradeAnswer", () => {
test("normalizes case and whitespace for substring matching", () => {
expect(gradeAnswer("The codename is VELVET-otter-19.", ["VELVET-OTTER-19"])).toBe(true);
expect(gradeAnswer("no idea", ["VELVET-OTTER-19"])).toBe(false);
expect(gradeAnswer("budget was $47,250 total", ["$47,250", "47250"])).toBe(true);
});
});
describe("checkViewStructure", () => {
const u = (text: string): CanonMessage => ({ role: "user", text });
const a = (text: string): CanonMessage => ({ role: "assistant", text });
test("passes a well-formed view", () => {
const report = checkViewStructure("system+summary", [u("q"), a("r")]);
expect(report.ok).toBe(true);
});
test("fails when the tail has no user message", () => {
const report = checkViewStructure("sys", [a("r")]);
expect(report.hasUserMessage).toBe(false);
expect(report.ok).toBe(false);
});
test("fails on an orphan tool result at the tail start", () => {
const tail: CanonMessage[] = [
{ role: "tool", text: "", toolResults: [{ content: "x" }] },
u("q"),
];
const report = checkViewStructure("sys", tail);
expect(report.noOrphanToolResults).toBe(false);
expect(report.ok).toBe(false);
});
test("fails on a system message inside the tail", () => {
const tail: CanonMessage[] = [u("q"), { role: "system", text: "sneaky" }];
expect(checkViewStructure("sys", tail).singleLeadingSystem).toBe(false);
});
});
describe("reductionPercent and latencyStats", () => {
test("reduction math", () => {
expect(reductionPercent(1000, 250)).toBe(75);
expect(reductionPercent(0, 0)).toBe(0);
});
test("latency stats", () => {
const stats = latencyStats([100, 200, 300]);
expect(stats.mean).toBe(200);
expect(stats.p50).toBe(200);
expect(stats.max).toBe(300);
});
});
describe("report", () => {
const run: BenchRun = {
date: "2026-08-20",
modelId: "qwen/qwen3.8-27b",
settings: { chunkTokens: 800 },
fixtures: [
{
fixture: "coding-agent",
asked: 5,
correctAfterCompaction: 5,
correctAfterConsolidation: 4,
reductionPercent: 82,
latency: { mean: 1200, p50: 1100, max: 2000 },
structural: {
singleLeadingSystem: true,
noOrphanToolResults: true,
hasUserMessage: true,
ok: true,
},
cacheDeterministic: true,
},
],
};
test("markdown contains the essentials", () => {
const md = renderMarkdown(run);
expect(md).toContain("qwen/qwen3.8-27b");
expect(md).toContain("coding-agent");
expect(md).toContain("5/5");
expect(md).toContain("82");
});
test("file base is filesystem-safe and stamped", () => {
const base = resultFileBase(run);
expect(base).toContain("2026-08-20");
expect(base).not.toMatch(/[\/\\:]/);
});
});
import { describe, expect, test } from "vitest";
import {
allFixtures,
codingAgentFixture,
contradictoryUpdatesFixture,
mulberry32,
toolSpamFixture,
} from "../fixtures";
import {
checkViewStructure,
gradeAnswer,
latencyStats,
reductionPercent,
} from "../metrics";
import { BenchRun, renderMarkdown, resultFileBase } from "../report";
import { planCompaction } from "../../src/plan";
import {
CanonMessage,
prefixHashes,
roundBoundaries,
} from "../../src/view";
describe("mulberry32", () => {
test("same seed yields the same sequence, different seeds differ", () => {
const a1 = mulberry32(42);
const a2 = mulberry32(42);
const b = mulberry32(43);
const seqA1 = [a1(), a1(), a1()];
const seqA2 = [a2(), a2(), a2()];
const seqB = [b(), b(), b()];
expect(seqA1).toEqual(seqA2);
expect(seqA1).not.toEqual(seqB);
for (const v of seqA1) {
expect(v).toBeGreaterThanOrEqual(0);
expect(v).toBeLessThan(1);
}
});
});
describe("fixtures", () => {
test("deterministic: same seed produces identical messages and hashes", () => {
for (const [f1, f2] of allFixtures(7).map(
(f, i) => [f, allFixtures(7)[i]] as const,
)) {
expect(f1.msgs).toEqual(f2.msgs);
expect(prefixHashes(f1.msgs)).toEqual(prefixHashes(f2.msgs));
}
});
test("every fixture has messages, facts, and starts with a user message", () => {
for (const f of allFixtures()) {
expect(f.msgs.length).toBeGreaterThan(10);
expect(f.facts.length).toBeGreaterThanOrEqual(3);
expect(f.msgs[0].role).toBe("user");
}
});
test("tool results always directly follow their assistant tool call", () => {
for (const f of allFixtures()) {
f.msgs.forEach((m, i) => {
if (m.role === "tool") {
const prev = f.msgs[i - 1];
const prevOk =
prev.role === "tool" ||
(prev.role === "assistant" && (prev.toolCalls ?? []).length > 0);
expect(prevOk, `${f.name}[${i}]`).toBe(true);
}
});
}
});
test("seeded facts appear verbatim in the transcript", () => {
for (const f of allFixtures()) {
const text = f.msgs
.map(
(m) =>
m.text +
(m.toolResults ?? []).map((r) => r.content).join(" "),
)
.join("\n");
for (const fact of f.facts) {
const found = fact.accepted.some((a) => text.includes(a));
expect(found, `${f.name}: ${fact.id}`).toBe(true);
}
}
});
test("contradictory fixture accepts only the latest value", () => {
const f = contradictoryUpdatesFixture();
const revised = f.facts.find((fact) => fact.id.includes("revised"));
expect(revised).toBeDefined();
});
test("tool spam fixture carries oversized tool results", () => {
const f = toolSpamFixture();
const biggest = Math.max(
...f.msgs.flatMap((m) =>
(m.toolResults ?? []).map((r) => r.content.length),
),
);
expect(biggest).toBeGreaterThan(5000);
});
test("planCompaction cuts every fixture at valid boundaries", () => {
for (const f of allFixtures()) {
const tokens = f.msgs.map((m) => Math.ceil((m.text.length + 40) / 4) + 50);
const boundaries = roundBoundaries(f.msgs);
const plan = planCompaction({
msgs: f.msgs,
tokens,
coveredUpTo: 0,
summaryTokens: 0,
estSummaryTokensPerChunk: 300,
limit: 500,
autoCompact: true,
force: false,
keepRecentTokens: 300,
chunkTokens: 800,
});
expect(plan.cuts.length).toBeGreaterThan(0);
for (const cut of plan.cuts) {
expect(boundaries).toContain(cut);
}
}
});
});
describe("gradeAnswer", () => {
test("normalizes case and whitespace for substring matching", () => {
expect(gradeAnswer("The codename is VELVET-otter-19.", ["VELVET-OTTER-19"])).toBe(true);
expect(gradeAnswer("no idea", ["VELVET-OTTER-19"])).toBe(false);
expect(gradeAnswer("budget was $47,250 total", ["$47,250", "47250"])).toBe(true);
});
});
describe("checkViewStructure", () => {
const u = (text: string): CanonMessage => ({ role: "user", text });
const a = (text: string): CanonMessage => ({ role: "assistant", text });
test("passes a well-formed view", () => {
const report = checkViewStructure("system+summary", [u("q"), a("r")]);
expect(report.ok).toBe(true);
});
test("fails when the tail has no user message", () => {
const report = checkViewStructure("sys", [a("r")]);
expect(report.hasUserMessage).toBe(false);
expect(report.ok).toBe(false);
});
test("fails on an orphan tool result at the tail start", () => {
const tail: CanonMessage[] = [
{ role: "tool", text: "", toolResults: [{ content: "x" }] },
u("q"),
];
const report = checkViewStructure("sys", tail);
expect(report.noOrphanToolResults).toBe(false);
expect(report.ok).toBe(false);
});
test("fails on a system message inside the tail", () => {
const tail: CanonMessage[] = [u("q"), { role: "system", text: "sneaky" }];
expect(checkViewStructure("sys", tail).singleLeadingSystem).toBe(false);
});
});
describe("reductionPercent and latencyStats", () => {
test("reduction math", () => {
expect(reductionPercent(1000, 250)).toBe(75);
expect(reductionPercent(0, 0)).toBe(0);
});
test("latency stats", () => {
const stats = latencyStats([100, 200, 300]);
expect(stats.mean).toBe(200);
expect(stats.p50).toBe(200);
expect(stats.max).toBe(300);
});
});
describe("report", () => {
const run: BenchRun = {
date: "2026-08-20",
modelId: "qwen/qwen3.8-27b",
settings: { chunkTokens: 800 },
fixtures: [
{
fixture: "coding-agent",
asked: 5,
correctAfterCompaction: 5,
correctAfterConsolidation: 4,
reductionPercent: 82,
latency: { mean: 1200, p50: 1100, max: 2000 },
structural: {
singleLeadingSystem: true,
noOrphanToolResults: true,
hasUserMessage: true,
ok: true,
},
cacheDeterministic: true,
},
],
};
test("markdown contains the essentials", () => {
const md = renderMarkdown(run);
expect(md).toContain("qwen/qwen3.8-27b");
expect(md).toContain("coding-agent");
expect(md).toContain("5/5");
expect(md).toContain("82");
});
test("file base is filesystem-safe and stamped", () => {
const base = resultFileBase(run);
expect(base).toContain("2026-08-20");
expect(base).not.toMatch(/[\/\\:]/);
});
});