refactor(compaction-tests): share snapshot assertions

This commit is contained in:
Peter Steinberger
2026-03-17 07:27:52 +00:00
parent 168fa9d433
commit e4287e0938

View File

@@ -7,6 +7,30 @@ import {
shouldFlagCompactionTimeout,
} from "./compaction-timeout.js";
function expectSelectedSnapshot(params: {
currentSessionId: string;
currentSnapshot: Parameters<typeof selectCompactionTimeoutSnapshot>[0]["currentSnapshot"];
expectedSessionIdUsed: string;
expectedSnapshot: ReadonlyArray<ReturnType<typeof castAgentMessage>>;
expectedSource: "current" | "pre-compaction";
preCompactionSessionId: string;
preCompactionSnapshot: Parameters<
typeof selectCompactionTimeoutSnapshot
>[0]["preCompactionSnapshot"];
timedOutDuringCompaction: boolean;
}) {
const selected = selectCompactionTimeoutSnapshot({
timedOutDuringCompaction: params.timedOutDuringCompaction,
preCompactionSnapshot: params.preCompactionSnapshot,
preCompactionSessionId: params.preCompactionSessionId,
currentSnapshot: params.currentSnapshot,
currentSessionId: params.currentSessionId,
});
expect(selected.source).toBe(params.expectedSource);
expect(selected.sessionIdUsed).toBe(params.expectedSessionIdUsed);
expect(selected.messagesSnapshot).toEqual(params.expectedSnapshot);
}
describe("compaction-timeout helpers", () => {
it("flags compaction timeout consistently for internal and external timeout sources", () => {
const internalTimer = shouldFlagCompactionTimeout({
@@ -75,29 +99,29 @@ describe("compaction-timeout helpers", () => {
it("uses pre-compaction snapshot when compaction timeout occurs", () => {
const pre = [castAgentMessage({ role: "assistant", content: "pre" })] as const;
const current = [castAgentMessage({ role: "assistant", content: "current" })] as const;
const selected = selectCompactionTimeoutSnapshot({
expectSelectedSnapshot({
timedOutDuringCompaction: true,
preCompactionSnapshot: [...pre],
preCompactionSessionId: "session-pre",
currentSnapshot: [...current],
currentSessionId: "session-current",
expectedSource: "pre-compaction",
expectedSessionIdUsed: "session-pre",
expectedSnapshot: pre,
});
expect(selected.source).toBe("pre-compaction");
expect(selected.sessionIdUsed).toBe("session-pre");
expect(selected.messagesSnapshot).toEqual(pre);
});
it("falls back to current snapshot when pre-compaction snapshot is unavailable", () => {
const current = [castAgentMessage({ role: "assistant", content: "current" })] as const;
const selected = selectCompactionTimeoutSnapshot({
expectSelectedSnapshot({
timedOutDuringCompaction: true,
preCompactionSnapshot: null,
preCompactionSessionId: "session-pre",
currentSnapshot: [...current],
currentSessionId: "session-current",
expectedSource: "current",
expectedSessionIdUsed: "session-current",
expectedSnapshot: current,
});
expect(selected.source).toBe("current");
expect(selected.sessionIdUsed).toBe("session-current");
expect(selected.messagesSnapshot).toEqual(current);
});
});