import { describe, expect, it, vi } from "vitest"; import { AgentPrepHandler } from "#src/handlers/before-agent-start"; import { makeCheckResult, makeCtx } from "#test/helpers/handler-fixtures"; import { makeRealResolver, makeRealSession, } from "#test/helpers/session-fixtures"; function makeSetup() { const { session, permissionManager, sessionRules, configStore, forwarding } = makeRealSession(); const { resolver } = makeRealResolver(permissionManager, sessionRules); vi.mocked(permissionManager.check).mockReturnValue(makeCheckResult()); const warmParser = vi.fn(); const handler = new AgentPrepHandler(session, resolver, warmParser); return { handler, session, configStore, forwarding, warmParser, }; } describe("AgentPrepHandler", () => { it("warms the parser and activates the permission session", async () => { const { handler, forwarding, warmParser } = makeSetup(); const ctx = makeCtx(); await handler.handle({ systemPrompt: "You are an assistant." }, ctx); expect(warmParser).toHaveBeenCalledTimes(1); expect(forwarding.start).toHaveBeenCalledWith(ctx); }); it("refreshes global permission configuration", async () => { const { handler, configStore } = makeSetup(); const ctx = makeCtx({ isProjectTrusted: () => false }); await handler.handle({ systemPrompt: "You are an assistant." }, ctx); expect(configStore.refresh).toHaveBeenCalledWith(ctx, false); }); it("does not rewrite a prompt without skill entries", async () => { const { handler, session } = makeSetup(); const result = await handler.handle( { systemPrompt: "You are an assistant." }, makeCtx(), ); expect(result).toEqual({}); expect(session.getActiveSkillEntries()).toEqual([]); }); });