test(agent): avoid global pause gate spy

This commit is contained in:
Victor Araújo
2026-07-21 23:29:13 -03:00
parent b927c4d0c5
commit eee940c1c3
+5 -9
View File
@@ -1,4 +1,4 @@
import { afterEach, describe, expect, it, vi } from "bun:test";
import { afterEach, describe, expect, it } from "bun:test";
import { agentLoop, agentPauseGate } from "@oh-my-pi/pi-agent-core";
import type { AgentContext, AgentLoopConfig, AgentMessage, AgentTool } from "@oh-my-pi/pi-agent-core/types";
import type { Message } from "@oh-my-pi/pi-ai";
@@ -29,7 +29,6 @@ describe("agentPauseGate", () => {
afterEach(() => {
// The gate is process-global: never leak an engaged pause into other files.
agentPauseGate.resume();
vi.restoreAllMocks();
});
it("holds the next model call while paused and releases it on resume", async () => {
@@ -66,14 +65,11 @@ describe("agentPauseGate", () => {
const context: AgentContext = { systemPrompt: ["Test"], messages: [], tools: [makeEchoTool(executed)] };
const config: AgentLoopConfig = { model: mock.model, convertToLlm: identityConverter };
const toolBoundary = Promise.withResolvers<void>();
const waitUntilResumed = agentPauseGate.waitUntilResumed.bind(agentPauseGate);
vi.spyOn(agentPauseGate, "waitUntilResumed").mockImplementation(signal => {
toolBoundary.resolve();
return waitUntilResumed(signal);
});
// Let queued microtasks consume the scripted tool response. This verifies the
// observable contract without a process-global spy that sibling test files can
// restore while this turn is suspended.
const result = agentLoop([createUserMessage("run echo")], context, config, undefined, mock.stream).result();
await toolBoundary.promise;
for (let tick = 0; tick < 10; tick++) await Promise.resolve();
expect(executed).toEqual([]); // tool parked, not started
expect(mock.calls.length).toBe(1); // and no follow-up model call either