import { describe, expect, it, vi } from "vitest"; import type { ExtensionContext, ModelRegistry, SessionManager } from "@earendil-works/pi-coding-agent"; import { DEFAULT_AUTO_REVIEW_CONFIG, type AutoReviewConfig } from "#src/auto-review/config"; import { installBuiltinAutoReview } from "#src/auto-review/runtime"; import type { ConfigReader } from "#src/config-store"; import type { PermissionsService } from "#src/service"; function harness(autoReview: AutoReviewConfig, hasUI = true) { const notify = vi.fn(); const registerAuthorizer = vi.fn(() => () => {}); const controller = installBuiltinAutoReview({ service: { registerAuthorizer } as unknown as PermissionsService, config: { current: () => ({ debugLog: false, permissionReviewLog: true, yoloMode: false, doublePressToConfirm: true, authorizerChain: ["auto-review"], autoReview, }), } as ConfigReader, }); const ctx = { hasUI, ui: { notify }, modelRegistry: {} as ModelRegistry, sessionManager: { getBranch: () => [] } as unknown as Pick, } as unknown as ExtensionContext; controller.handleSessionStart(ctx); return { controller, notify, registerAuthorizer }; } describe("built-in AutoReview lifecycle", () => { it("registers the auto-review authorizer", () => { const { registerAuthorizer } = harness({ ...DEFAULT_AUTO_REVIEW_CONFIG }); expect(registerAuthorizer).toHaveBeenCalledWith("auto-review", expect.any(Function)); }); it("warns once at session start when provider and model are unset", () => { const { notify } = harness({ ...DEFAULT_AUTO_REVIEW_CONFIG }); expect(notify).toHaveBeenCalledTimes(1); expect(notify.mock.calls[0]?.[1]).toBe("warning"); expect(notify.mock.calls[0]?.[0]).toMatch(/provider.*model/iu); }); it("stays silent when both provider and model are configured", () => { const { notify } = harness({ ...DEFAULT_AUTO_REVIEW_CONFIG, provider: "vendor", model: "m" }); expect(notify).not.toHaveBeenCalled(); }); it("stays silent when AutoReview is disabled", () => { const { notify } = harness({ ...DEFAULT_AUTO_REVIEW_CONFIG, enabled: false }); expect(notify).not.toHaveBeenCalled(); }); it("does not notify without a UI", () => { const { notify } = harness({ ...DEFAULT_AUTO_REVIEW_CONFIG }, false); expect(notify).not.toHaveBeenCalled(); }); it("stays silent when auto-review is not in the authorizer chain", () => { const notify = vi.fn(); const controller = installBuiltinAutoReview({ service: { registerAuthorizer: vi.fn(() => () => {}) } as unknown as PermissionsService, config: { current: () => ({ debugLog: false, permissionReviewLog: true, yoloMode: false, doublePressToConfirm: true, authorizerChain: [], autoReview: { ...DEFAULT_AUTO_REVIEW_CONFIG }, }), } as ConfigReader, }); controller.handleSessionStart({ hasUI: true, ui: { notify }, } as unknown as ExtensionContext); expect(notify).not.toHaveBeenCalled(); }); });