From 5db579f2e0d99564cb6180500737257217104149 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 21:39:58 +0000 Subject: [PATCH] refactor(test): reuse sanitize session history fixtures --- ...unner.sanitize-session-history.e2e.test.ts | 40 +++------- ...r.sanitize-session-history.test-harness.ts | 58 ++++++++++++++ ...ed-runner.sanitize-session-history.test.ts | 75 +++++-------------- 3 files changed, 89 insertions(+), 84 deletions(-) create mode 100644 src/agents/pi-embedded-runner.sanitize-session-history.test-harness.ts diff --git a/src/agents/pi-embedded-runner.sanitize-session-history.e2e.test.ts b/src/agents/pi-embedded-runner.sanitize-session-history.e2e.test.ts index 0ef9b35811e..c3f58100662 100644 --- a/src/agents/pi-embedded-runner.sanitize-session-history.e2e.test.ts +++ b/src/agents/pi-embedded-runner.sanitize-session-history.e2e.test.ts @@ -2,6 +2,11 @@ import type { AgentMessage } from "@mariozechner/pi-agent-core"; import type { SessionManager } from "@mariozechner/pi-coding-agent"; import { beforeEach, describe, expect, it, vi } from "vitest"; import * as helpers from "./pi-embedded-helpers.js"; +import { + makeInMemorySessionManager, + makeModelSnapshotEntry, + makeReasoningAssistantMessages, +} from "./pi-embedded-runner.sanitize-session-history.test-harness.js"; type SanitizeSessionHistory = typeof import("./pi-embedded-runner/google.js").sanitizeSessionHistory; @@ -70,36 +75,15 @@ describe("sanitizeSessionHistory e2e smoke", () => { }); it("downgrades openai reasoning blocks when the model snapshot changed", async () => { - const sessionEntries: Array<{ type: string; customType: string; data: unknown }> = [ - { - type: "custom", - customType: "model-snapshot", - data: { - timestamp: Date.now(), - provider: "anthropic", - modelApi: "anthropic-messages", - modelId: "claude-3-7", - }, - }, - ]; - const sessionManager = { - getEntries: vi.fn(() => sessionEntries), - appendCustomEntry: vi.fn((customType: string, data: unknown) => { - sessionEntries.push({ type: "custom", customType, data }); + const sessionEntries = [ + makeModelSnapshotEntry({ + provider: "anthropic", + modelApi: "anthropic-messages", + modelId: "claude-3-7", }), - } as unknown as SessionManager; - const messages: AgentMessage[] = [ - { - role: "assistant", - content: [ - { - type: "thinking", - thinking: "reasoning", - thinkingSignature: { id: "rs_test", type: "reasoning" }, - }, - ], - }, ]; + const sessionManager = makeInMemorySessionManager(sessionEntries); + const messages = makeReasoningAssistantMessages({ thinkingSignature: "object" }); const result = await sanitizeSessionHistory({ messages, diff --git a/src/agents/pi-embedded-runner.sanitize-session-history.test-harness.ts b/src/agents/pi-embedded-runner.sanitize-session-history.test-harness.ts new file mode 100644 index 00000000000..ec5ff65c54f --- /dev/null +++ b/src/agents/pi-embedded-runner.sanitize-session-history.test-harness.ts @@ -0,0 +1,58 @@ +import type { AgentMessage } from "@mariozechner/pi-agent-core"; +import type { SessionManager } from "@mariozechner/pi-coding-agent"; +import { vi } from "vitest"; + +export type SessionEntry = { type: string; customType: string; data: unknown }; + +export function makeModelSnapshotEntry(data: { + timestamp?: number; + provider: string; + modelApi: string; + modelId: string; +}): SessionEntry { + return { + type: "custom", + customType: "model-snapshot", + data: { + timestamp: data.timestamp ?? Date.now(), + provider: data.provider, + modelApi: data.modelApi, + modelId: data.modelId, + }, + }; +} + +export function makeInMemorySessionManager(entries: SessionEntry[]): SessionManager { + return { + getEntries: vi.fn(() => entries), + appendCustomEntry: vi.fn((customType: string, data: unknown) => { + entries.push({ type: "custom", customType, data }); + }), + } as unknown as SessionManager; +} + +export function makeReasoningAssistantMessages(opts?: { + thinkingSignature?: "object" | "json"; +}): AgentMessage[] { + const thinkingSignature: unknown = + opts?.thinkingSignature === "json" + ? JSON.stringify({ id: "rs_test", type: "reasoning" }) + : { id: "rs_test", type: "reasoning" }; + + // Intentional: we want to build message payloads that can carry non-string + // signatures, but core typing currently expects a string. + const messages = [ + { + role: "assistant", + content: [ + { + type: "thinking", + thinking: "reasoning", + thinkingSignature, + }, + ], + }, + ]; + + return messages as unknown as AgentMessage[]; +} diff --git a/src/agents/pi-embedded-runner.sanitize-session-history.test.ts b/src/agents/pi-embedded-runner.sanitize-session-history.test.ts index 6fca101c07a..098ade469d1 100644 --- a/src/agents/pi-embedded-runner.sanitize-session-history.test.ts +++ b/src/agents/pi-embedded-runner.sanitize-session-history.test.ts @@ -2,6 +2,11 @@ import type { AgentMessage } from "@mariozechner/pi-agent-core"; import type { SessionManager } from "@mariozechner/pi-coding-agent"; import { beforeEach, describe, expect, it, vi } from "vitest"; import * as helpers from "./pi-embedded-helpers.js"; +import { + makeInMemorySessionManager, + makeModelSnapshotEntry, + makeReasoningAssistantMessages, +} from "./pi-embedded-runner.sanitize-session-history.test-harness.js"; type SanitizeSessionHistory = typeof import("./pi-embedded-runner/google.js").sanitizeSessionHistory; @@ -216,36 +221,15 @@ describe("sanitizeSessionHistory", () => { }); it("does not downgrade openai reasoning when the model has not changed", async () => { - const sessionEntries: Array<{ type: string; customType: string; data: unknown }> = [ - { - type: "custom", - customType: "model-snapshot", - data: { - timestamp: Date.now(), - provider: "openai", - modelApi: "openai-responses", - modelId: "gpt-5.2-codex", - }, - }, - ]; - const sessionManager = { - getEntries: vi.fn(() => sessionEntries), - appendCustomEntry: vi.fn((customType: string, data: unknown) => { - sessionEntries.push({ type: "custom", customType, data }); + const sessionEntries = [ + makeModelSnapshotEntry({ + provider: "openai", + modelApi: "openai-responses", + modelId: "gpt-5.2-codex", }), - } as unknown as SessionManager; - const messages: AgentMessage[] = [ - { - role: "assistant", - content: [ - { - type: "thinking", - thinking: "reasoning", - thinkingSignature: JSON.stringify({ id: "rs_test", type: "reasoning" }), - }, - ], - }, ]; + const sessionManager = makeInMemorySessionManager(sessionEntries); + const messages = makeReasoningAssistantMessages({ thinkingSignature: "json" }); const result = await sanitizeSessionHistory({ messages, @@ -260,36 +244,15 @@ describe("sanitizeSessionHistory", () => { }); it("downgrades openai reasoning only when the model changes", async () => { - const sessionEntries: Array<{ type: string; customType: string; data: unknown }> = [ - { - type: "custom", - customType: "model-snapshot", - data: { - timestamp: Date.now(), - provider: "anthropic", - modelApi: "anthropic-messages", - modelId: "claude-3-7", - }, - }, - ]; - const sessionManager = { - getEntries: vi.fn(() => sessionEntries), - appendCustomEntry: vi.fn((customType: string, data: unknown) => { - sessionEntries.push({ type: "custom", customType, data }); + const sessionEntries = [ + makeModelSnapshotEntry({ + provider: "anthropic", + modelApi: "anthropic-messages", + modelId: "claude-3-7", }), - } as unknown as SessionManager; - const messages: AgentMessage[] = [ - { - role: "assistant", - content: [ - { - type: "thinking", - thinking: "reasoning", - thinkingSignature: { id: "rs_test", type: "reasoning" }, - }, - ], - }, ]; + const sessionManager = makeInMemorySessionManager(sessionEntries); + const messages = makeReasoningAssistantMessages({ thinkingSignature: "object" }); const result = await sanitizeSessionHistory({ messages,