diff --git a/src/agents/compaction.test.ts b/src/agents/compaction.test.ts index ce273e67e92..f4e04852cd1 100644 --- a/src/agents/compaction.test.ts +++ b/src/agents/compaction.test.ts @@ -2,38 +2,7 @@ import type { AgentMessage } from "@mariozechner/pi-agent-core"; import type { AssistantMessage, ToolResultMessage } from "@mariozechner/pi-ai"; import { beforeAll, describe, expect, it, vi } from "vitest"; import { makeAgentAssistantMessage } from "./test-helpers/agent-message-fixtures.js"; - -const piCodingAgentMocks = vi.hoisted(() => ({ - estimateTokens: vi.fn((message: unknown) => estimateTokenish(message)), -})); - -function readText(value: unknown): string { - if (typeof value === "string") { - return value; - } - if (Array.isArray(value)) { - return value.map(readText).join(""); - } - if (value && typeof value === "object") { - const record = value as { text?: unknown; content?: unknown; arguments?: unknown }; - return `${readText(record.text)}${readText(record.content)}${readText(record.arguments)}`; - } - return ""; -} - -function estimateTokenish(message: unknown): number { - return Math.max(1, Math.ceil(readText(message).length / 4)); -} - -vi.mock("@mariozechner/pi-coding-agent", async () => { - const actual = await vi.importActual( - "@mariozechner/pi-coding-agent", - ); - return { - ...actual, - estimateTokens: piCodingAgentMocks.estimateTokens, - }; -}); +import "./test-helpers/pi-coding-agent-token-mock.js"; let estimateMessagesTokens: typeof import("./compaction.js").estimateMessagesTokens; let pruneHistoryForContextShare: typeof import("./compaction.js").pruneHistoryForContextShare; diff --git a/src/agents/pi-embedded-runner/run/preemptive-compaction.test.ts b/src/agents/pi-embedded-runner/run/preemptive-compaction.test.ts index 5fcecfb97f7..ab9a0be6470 100644 --- a/src/agents/pi-embedded-runner/run/preemptive-compaction.test.ts +++ b/src/agents/pi-embedded-runner/run/preemptive-compaction.test.ts @@ -1,39 +1,8 @@ import type { AgentMessage } from "@mariozechner/pi-agent-core"; import { beforeAll, describe, expect, it, vi } from "vitest"; +import "../../test-helpers/pi-coding-agent-token-mock.js"; import { estimateToolResultReductionPotential } from "../tool-result-truncation.js"; -const piCodingAgentMocks = vi.hoisted(() => ({ - estimateTokens: vi.fn((message: unknown) => estimateTokenish(message)), -})); - -function readText(value: unknown): string { - if (typeof value === "string") { - return value; - } - if (Array.isArray(value)) { - return value.map(readText).join(""); - } - if (value && typeof value === "object") { - const record = value as { text?: unknown; content?: unknown }; - return `${readText(record.text)}${readText(record.content)}`; - } - return ""; -} - -function estimateTokenish(message: unknown): number { - return Math.max(1, Math.ceil(readText(message).length / 4)); -} - -vi.mock("@mariozechner/pi-coding-agent", async () => { - const actual = await vi.importActual( - "@mariozechner/pi-coding-agent", - ); - return { - ...actual, - estimateTokens: piCodingAgentMocks.estimateTokens, - }; -}); - let PREEMPTIVE_OVERFLOW_ERROR_TEXT: typeof import("./preemptive-compaction.js").PREEMPTIVE_OVERFLOW_ERROR_TEXT; let estimatePrePromptTokens: typeof import("./preemptive-compaction.js").estimatePrePromptTokens; let shouldPreemptivelyCompactBeforePrompt: typeof import("./preemptive-compaction.js").shouldPreemptivelyCompactBeforePrompt; diff --git a/src/agents/test-helpers/pi-coding-agent-token-mock.ts b/src/agents/test-helpers/pi-coding-agent-token-mock.ts new file mode 100644 index 00000000000..a9062ddbfd4 --- /dev/null +++ b/src/agents/test-helpers/pi-coding-agent-token-mock.ts @@ -0,0 +1,35 @@ +import { vi } from "vitest"; + +const piCodingAgentTokenMocks = vi.hoisted(() => { + function readText(value: unknown): string { + if (typeof value === "string") { + return value; + } + if (Array.isArray(value)) { + return value.map(readText).join(""); + } + if (value && typeof value === "object") { + const record = value as { text?: unknown; content?: unknown; arguments?: unknown }; + return `${readText(record.text)}${readText(record.content)}${readText(record.arguments)}`; + } + return ""; + } + + function estimateTokenish(message: unknown): number { + return Math.max(1, Math.ceil(readText(message).length / 4)); + } + + return { + estimateTokens: vi.fn((message: unknown) => estimateTokenish(message)), + }; +}); + +vi.mock("@mariozechner/pi-coding-agent", async () => { + const actual = await vi.importActual( + "@mariozechner/pi-coding-agent", + ); + return { + ...actual, + estimateTokens: piCodingAgentTokenMocks.estimateTokens, + }; +});