diff --git a/src/agents/pi-embedded-runner/system-prompt.test.ts b/src/agents/pi-embedded-runner/system-prompt.test.ts index 355b2c67ae9..8e20a95bda7 100644 --- a/src/agents/pi-embedded-runner/system-prompt.test.ts +++ b/src/agents/pi-embedded-runner/system-prompt.test.ts @@ -2,6 +2,11 @@ import type { AgentSession } from "@mariozechner/pi-coding-agent"; import { describe, expect, it, vi } from "vitest"; import { applySystemPromptOverrideToSession, createSystemPromptOverride } from "./system-prompt.js"; +type MutableSession = AgentSession & { + _baseSystemPrompt?: string; + _rebuildSystemPrompt?: (toolNames: string[]) => string; +}; + function createMockSession() { const setSystemPrompt = vi.fn(); const session = { @@ -10,42 +15,41 @@ function createMockSession() { return { session, setSystemPrompt }; } +function applyAndGetMutableSession( + prompt: Parameters[1], +) { + const { session, setSystemPrompt } = createMockSession(); + applySystemPromptOverrideToSession(session, prompt); + return { + mutable: session as MutableSession, + setSystemPrompt, + }; +} + describe("applySystemPromptOverrideToSession", () => { it("applies a string override to the session system prompt", () => { - const { session, setSystemPrompt } = createMockSession(); const prompt = "You are a helpful assistant with custom context."; - - applySystemPromptOverrideToSession(session, prompt); + const { mutable, setSystemPrompt } = applyAndGetMutableSession(prompt); expect(setSystemPrompt).toHaveBeenCalledWith(prompt); - const mutable = session as unknown as { _baseSystemPrompt?: string }; expect(mutable._baseSystemPrompt).toBe(prompt); }); it("trims whitespace from string overrides", () => { - const { session, setSystemPrompt } = createMockSession(); - - applySystemPromptOverrideToSession(session, " padded prompt "); + const { setSystemPrompt } = applyAndGetMutableSession(" padded prompt "); expect(setSystemPrompt).toHaveBeenCalledWith("padded prompt"); }); it("applies a function override to the session system prompt", () => { - const { session, setSystemPrompt } = createMockSession(); const override = createSystemPromptOverride("function-based prompt"); - - applySystemPromptOverrideToSession(session, override); + const { setSystemPrompt } = applyAndGetMutableSession(override); expect(setSystemPrompt).toHaveBeenCalledWith("function-based prompt"); }); it("sets _rebuildSystemPrompt that returns the override", () => { - const { session } = createMockSession(); - applySystemPromptOverrideToSession(session, "rebuild test"); - - const mutable = session as unknown as { - _rebuildSystemPrompt?: (toolNames: string[]) => string; - }; + const { mutable } = applyAndGetMutableSession("rebuild test"); expect(mutable._rebuildSystemPrompt?.(["tool1"])).toBe("rebuild test"); }); });