import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { expect, test } from "vitest"; import { buildResolvedConfigLogEntry } from "#src/config-reporter"; import { createPermissionSystemLogger } from "#src/logging"; import type { ResolvedPolicyPaths } from "#src/permission-manager"; import { PermissionManager } from "#src/permission-manager"; test("buildResolvedConfigLogEntry reports global and agent policy paths", () => { const policyPaths: ResolvedPolicyPaths = { globalConfigPath: "/home/user/.pi/agent/my-pi/permission-system/policy.jsonc", globalConfigExists: true, agentsDir: "/home/user/.pi/agent/agents", agentsDirExists: true, projectAgentsDir: null, projectAgentsDirExists: false, }; const result = buildResolvedConfigLogEntry({ policyPaths }); expect(result).toEqual({ ...policyPaths, legacyGlobalPolicyDetected: false, legacyExtensionConfigDetected: false, }); }); test("config.resolved entry appears in review log via logger", () => { const tempDir = mkdtempSync(join(tmpdir(), "config-resolved-log-")); try { const logsDir = join(tempDir, "logs"); mkdirSync(logsDir, { recursive: true }); const reviewLogPath = join(logsDir, "review.jsonl"); const debugLogPath = join(logsDir, "debug.jsonl"); const globalConfigPath = join(tempDir, "pi-permissions.jsonc"); writeFileSync(globalConfigPath, "{}", "utf-8"); const agentsDir = join(tempDir, "agents"); const pm = new PermissionManager({ globalConfigPath, agentsDir, }); const logger = createPermissionSystemLogger({ getConfig: () => ({ debugLog: false, permissionReviewLog: true, yoloMode: false, doublePressToConfirm: true, }), debugLogPath, reviewLogPath, ensureLogsDirectory: () => undefined, }); const policyPaths = pm.getResolvedPolicyPaths(); const entry = buildResolvedConfigLogEntry({ policyPaths }); logger.review( "config.resolved", entry as unknown as Record, ); const logContent = readFileSync(reviewLogPath, "utf-8").trim(); const parsed = JSON.parse(logContent) as Record; expect(parsed.event).toBe("config.resolved"); expect(parsed.globalConfigPath).toBe(globalConfigPath); expect(parsed.globalConfigExists).toBe(true); expect(parsed.agentsDir).toBe(agentsDir); expect(parsed.agentsDirExists).toBe(false); expect(parsed.projectAgentsDir).toBe(null); expect(parsed.projectAgentsDirExists).toBe(false); expect(parsed.legacyGlobalPolicyDetected).toBe(false); expect(parsed.legacyExtensionConfigDetected).toBe(false); } finally { rmSync(tempDir, { recursive: true, force: true }); } });