@gotgenes/pi-permission-system 10.2.0 → 10.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/package.json +1 -1
- package/src/config-modal.ts +7 -10
- package/src/config-store.ts +227 -0
- package/src/index.ts +65 -38
- package/src/permission-prompter.ts +3 -3
- package/src/permission-session.ts +10 -14
- package/src/permissions-service.ts +3 -5
- package/src/session-logger.ts +46 -9
- package/test/composition-root.test.ts +85 -1
- package/test/config-modal.test.ts +15 -10
- package/test/config-store.test.ts +428 -0
- package/test/permission-prompter.test.ts +12 -5
- package/test/permission-session.test.ts +57 -26
- package/test/session-logger.test.ts +151 -64
- package/src/runtime.ts +0 -280
- package/test/runtime.test.ts +0 -487
package/src/session-logger.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { DEBUG_LOG_FILENAME, REVIEW_LOG_FILENAME } from "./config-paths";
|
|
3
|
+
import {
|
|
4
|
+
ensurePermissionSystemLogsDirectory,
|
|
5
|
+
type PermissionSystemExtensionConfig,
|
|
6
|
+
} from "./extension-config";
|
|
7
|
+
import { createPermissionSystemLogger } from "./logging";
|
|
2
8
|
|
|
3
9
|
/**
|
|
4
10
|
* Unified logging + notification surface for handler deps.
|
|
@@ -13,17 +19,48 @@ export interface SessionLogger {
|
|
|
13
19
|
warn(message: string): void;
|
|
14
20
|
}
|
|
15
21
|
|
|
22
|
+
/** Narrow dependencies for constructing a {@link SessionLogger}. */
|
|
23
|
+
export interface SessionLoggerDeps {
|
|
24
|
+
/** Root logs directory; the debug + review log file paths derive from it. */
|
|
25
|
+
globalLogsDir: string;
|
|
26
|
+
/** Reads current config for the debug/review write toggles (call-time). */
|
|
27
|
+
getConfig: () => PermissionSystemExtensionConfig;
|
|
28
|
+
/** Surfaces a warning message to the user; called at warn/IO-failure time. */
|
|
29
|
+
notify: (message: string) => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
16
32
|
/**
|
|
17
|
-
* Create a SessionLogger
|
|
33
|
+
* Create a SessionLogger from narrow dependencies.
|
|
18
34
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
35
|
+
* Composes the JSONL log writer, owns the IO-failure warning dedup Set,
|
|
36
|
+
* and routes both IO-failure warnings and explicit warn() calls through
|
|
37
|
+
* the injected notify sink. No ExtensionRuntime reference required.
|
|
22
38
|
*/
|
|
23
|
-
export function createSessionLogger(
|
|
39
|
+
export function createSessionLogger(deps: SessionLoggerDeps): SessionLogger {
|
|
40
|
+
const writer = createPermissionSystemLogger({
|
|
41
|
+
getConfig: deps.getConfig,
|
|
42
|
+
debugLogPath: join(deps.globalLogsDir, DEBUG_LOG_FILENAME),
|
|
43
|
+
reviewLogPath: join(deps.globalLogsDir, REVIEW_LOG_FILENAME),
|
|
44
|
+
ensureLogsDirectory: () =>
|
|
45
|
+
ensurePermissionSystemLogsDirectory(deps.globalLogsDir),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const reported = new Set<string>();
|
|
49
|
+
const reportOnce = (warning: string): void => {
|
|
50
|
+
if (reported.has(warning)) return;
|
|
51
|
+
reported.add(warning);
|
|
52
|
+
deps.notify(warning);
|
|
53
|
+
};
|
|
54
|
+
|
|
24
55
|
return {
|
|
25
|
-
debug: (event, details) =>
|
|
26
|
-
|
|
27
|
-
|
|
56
|
+
debug: (event, details) => {
|
|
57
|
+
const warning = writer.debug(event, details);
|
|
58
|
+
if (warning) reportOnce(warning);
|
|
59
|
+
},
|
|
60
|
+
review: (event, details) => {
|
|
61
|
+
const warning = writer.review(event, details);
|
|
62
|
+
if (warning) reportOnce(warning);
|
|
63
|
+
},
|
|
64
|
+
warn: (message) => deps.notify(message),
|
|
28
65
|
};
|
|
29
66
|
}
|
|
@@ -31,7 +31,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
31
31
|
import { getGlobalConfigPath } from "#src/config-paths";
|
|
32
32
|
import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
|
|
33
33
|
import piPermissionSystemExtension from "#src/index";
|
|
34
|
-
import {
|
|
34
|
+
import {
|
|
35
|
+
PERMISSIONS_READY_CHANNEL,
|
|
36
|
+
PERMISSIONS_RPC_CHECK_CHANNEL,
|
|
37
|
+
} from "#src/permission-events";
|
|
35
38
|
import {
|
|
36
39
|
createPermissionForwardingLocation,
|
|
37
40
|
type ForwardedPermissionRequest,
|
|
@@ -359,6 +362,87 @@ describe("ready emitted after service publication", () => {
|
|
|
359
362
|
});
|
|
360
363
|
});
|
|
361
364
|
|
|
365
|
+
describe("single source of truth for session state", () => {
|
|
366
|
+
// Regression guard for the split-brain bug: before the fix, the gate path
|
|
367
|
+
// recorded session approvals into a private SessionRules instance that the
|
|
368
|
+
// RPC check and the service never saw. After the fix, both readers use the
|
|
369
|
+
// same SessionRules the gate writes into.
|
|
370
|
+
it("gate session-approval is visible to the RPC check and the service", async () => {
|
|
371
|
+
writeGlobalConfig({
|
|
372
|
+
permission: { "*": "allow", demo: "ask" },
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
const cwd = mkdtempSync(join(tmpdir(), "pi-perm-sot-cwd-"));
|
|
376
|
+
const pi = makeFakePi({ toolNames: ["demo"] });
|
|
377
|
+
piPermissionSystemExtension(pi as unknown as ExtensionAPI);
|
|
378
|
+
|
|
379
|
+
// UI ctx that approves the gate prompt for this session (options[1]).
|
|
380
|
+
const ctx = {
|
|
381
|
+
cwd,
|
|
382
|
+
hasUI: true,
|
|
383
|
+
sessionManager: {
|
|
384
|
+
getEntries: (): unknown[] => [],
|
|
385
|
+
getSessionId: (): string => "sot-session",
|
|
386
|
+
getSessionDir: (): string => cwd,
|
|
387
|
+
},
|
|
388
|
+
ui: {
|
|
389
|
+
notify: (): void => {},
|
|
390
|
+
setStatus: (): void => {},
|
|
391
|
+
// Return the second option label-agnostically — always the
|
|
392
|
+
// "for this session" choice regardless of the exact label text.
|
|
393
|
+
select: async (
|
|
394
|
+
_title: string,
|
|
395
|
+
options: string[],
|
|
396
|
+
): Promise<string | undefined> => options[1],
|
|
397
|
+
input: async (): Promise<string | undefined> => undefined,
|
|
398
|
+
},
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
await fireSessionStart(pi, ctx);
|
|
402
|
+
|
|
403
|
+
// Drive a tool_call on "demo"; the gate prompts and the mock selects
|
|
404
|
+
// options[1], recording a session-scoped approval.
|
|
405
|
+
await pi.fire(
|
|
406
|
+
"tool_call",
|
|
407
|
+
{
|
|
408
|
+
toolName: "demo",
|
|
409
|
+
toolCallId: "demo-for-session",
|
|
410
|
+
input: { foo: "bar" },
|
|
411
|
+
},
|
|
412
|
+
ctx,
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
// RPC check — the deprecated channel must now reflect the session approval.
|
|
416
|
+
// eslint-disable-next-line @typescript-eslint/no-deprecated -- intentionally testing the deprecated RPC channel's session-rules visibility
|
|
417
|
+
const rpcCheckChannel: string = PERMISSIONS_RPC_CHECK_CHANNEL;
|
|
418
|
+
const requestId = "sot-rpc-1";
|
|
419
|
+
const replyPromise = new Promise<unknown>((resolve) => {
|
|
420
|
+
const unsub = pi.events.on(
|
|
421
|
+
`${rpcCheckChannel}:reply:${requestId}`,
|
|
422
|
+
(data) => {
|
|
423
|
+
unsub();
|
|
424
|
+
resolve(data);
|
|
425
|
+
},
|
|
426
|
+
);
|
|
427
|
+
});
|
|
428
|
+
pi.events.emit(rpcCheckChannel, { requestId, surface: "demo" });
|
|
429
|
+
const reply = (await replyPromise) as {
|
|
430
|
+
success: boolean;
|
|
431
|
+
data?: { result: string };
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
expect(reply.success).toBe(true);
|
|
435
|
+
// Before the fix this was "ask" — the RPC channel read an empty SessionRules.
|
|
436
|
+
expect(reply.data?.result).toBe("allow");
|
|
437
|
+
|
|
438
|
+
// Service accessor must also see the session approval.
|
|
439
|
+
const serviceResult = getPermissionsService()!.checkPermission("demo");
|
|
440
|
+
expect(serviceResult.state).toBe("allow");
|
|
441
|
+
|
|
442
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
|
|
362
446
|
describe("multi-instance global service interplay", () => {
|
|
363
447
|
// The fix (#302) scopes the process-global service slot to the publishing
|
|
364
448
|
// instance. The parent publishes at its session_start; an in-process child
|
|
@@ -3,6 +3,7 @@ import { tmpdir } from "node:os";
|
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { expect, test, vi } from "vitest";
|
|
5
5
|
import { registerPermissionSystemCommand } from "#src/config-modal";
|
|
6
|
+
import type { CommandConfigStore } from "#src/config-store";
|
|
6
7
|
import {
|
|
7
8
|
DEFAULT_EXTENSION_CONFIG,
|
|
8
9
|
normalizePermissionSystemConfig,
|
|
@@ -79,11 +80,14 @@ test("permission-system command completions expose top-level config actions", ()
|
|
|
79
80
|
let config: PermissionSystemExtensionConfig = { ...DEFAULT_EXTENSION_CONFIG };
|
|
80
81
|
|
|
81
82
|
try {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
const configStore: CommandConfigStore = {
|
|
84
|
+
current: () => config,
|
|
85
|
+
save: (next) => {
|
|
85
86
|
config = next;
|
|
86
87
|
},
|
|
88
|
+
};
|
|
89
|
+
const controller = {
|
|
90
|
+
config: configStore,
|
|
87
91
|
getConfigPath: () => configPath,
|
|
88
92
|
};
|
|
89
93
|
|
|
@@ -136,9 +140,9 @@ test("permission-system command handlers manage config summary, persistence, and
|
|
|
136
140
|
"utf-8",
|
|
137
141
|
);
|
|
138
142
|
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
143
|
+
const configStore: CommandConfigStore = {
|
|
144
|
+
current: () => config,
|
|
145
|
+
save: (next) => {
|
|
142
146
|
const currentConfig = normalizePermissionSystemConfig(
|
|
143
147
|
JSON.parse(readFileSync(configPath, "utf-8")) as unknown,
|
|
144
148
|
);
|
|
@@ -153,6 +157,9 @@ test("permission-system command handlers manage config summary, persistence, and
|
|
|
153
157
|
);
|
|
154
158
|
expect(config).not.toEqual(currentConfig);
|
|
155
159
|
},
|
|
160
|
+
};
|
|
161
|
+
const controller = {
|
|
162
|
+
config: configStore,
|
|
156
163
|
getConfigPath: () => configPath,
|
|
157
164
|
};
|
|
158
165
|
|
|
@@ -249,8 +256,7 @@ test("show output includes rule origins when getComposedRules is provided", asyn
|
|
|
249
256
|
];
|
|
250
257
|
|
|
251
258
|
const controller = {
|
|
252
|
-
|
|
253
|
-
setConfig: () => {},
|
|
259
|
+
config: { current: () => config, save: () => {} } as CommandConfigStore,
|
|
254
260
|
getConfigPath: () => "/fake/config.json",
|
|
255
261
|
getComposedRules: () => composedRules,
|
|
256
262
|
};
|
|
@@ -282,8 +288,7 @@ test("show output omits rule summary when getComposedRules is not provided", asy
|
|
|
282
288
|
const config = { ...DEFAULT_EXTENSION_CONFIG, yoloMode: true };
|
|
283
289
|
|
|
284
290
|
const controller = {
|
|
285
|
-
|
|
286
|
-
setConfig: () => {},
|
|
291
|
+
config: { current: () => config, save: () => {} } as CommandConfigStore,
|
|
287
292
|
getConfigPath: () => "/fake/config.json",
|
|
288
293
|
// no getComposedRules
|
|
289
294
|
};
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
// ── Module mocks (hoisted) ─────────────────────────────────────────────────
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
mockLoadAndMergeConfigs,
|
|
7
|
+
mockLoadUnifiedConfig,
|
|
8
|
+
mockSyncPermissionSystemStatus,
|
|
9
|
+
mockBuildResolvedConfigLogEntry,
|
|
10
|
+
mockExistsSync,
|
|
11
|
+
mockMkdirSync,
|
|
12
|
+
mockWriteFileSync,
|
|
13
|
+
mockRenameSync,
|
|
14
|
+
mockUnlinkSync,
|
|
15
|
+
} = vi.hoisted(() => ({
|
|
16
|
+
mockLoadAndMergeConfigs: vi.fn(),
|
|
17
|
+
mockLoadUnifiedConfig: vi.fn(),
|
|
18
|
+
mockSyncPermissionSystemStatus: vi.fn(),
|
|
19
|
+
mockBuildResolvedConfigLogEntry: vi.fn(),
|
|
20
|
+
mockExistsSync: vi.fn<(path: string) => boolean>(),
|
|
21
|
+
mockMkdirSync: vi.fn(),
|
|
22
|
+
mockWriteFileSync: vi.fn(),
|
|
23
|
+
mockRenameSync: vi.fn(),
|
|
24
|
+
mockUnlinkSync: vi.fn(),
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
vi.mock("../src/config-loader", () => ({
|
|
28
|
+
loadAndMergeConfigs: mockLoadAndMergeConfigs,
|
|
29
|
+
loadUnifiedConfig: mockLoadUnifiedConfig,
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
vi.mock("../src/status", () => ({
|
|
33
|
+
syncPermissionSystemStatus: mockSyncPermissionSystemStatus,
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
vi.mock("../src/config-reporter", () => ({
|
|
37
|
+
buildResolvedConfigLogEntry: mockBuildResolvedConfigLogEntry,
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
vi.mock("node:fs", () => ({
|
|
41
|
+
existsSync: mockExistsSync,
|
|
42
|
+
mkdirSync: mockMkdirSync,
|
|
43
|
+
writeFileSync: mockWriteFileSync,
|
|
44
|
+
renameSync: mockRenameSync,
|
|
45
|
+
unlinkSync: mockUnlinkSync,
|
|
46
|
+
default: {
|
|
47
|
+
existsSync: mockExistsSync,
|
|
48
|
+
mkdirSync: mockMkdirSync,
|
|
49
|
+
writeFileSync: mockWriteFileSync,
|
|
50
|
+
renameSync: mockRenameSync,
|
|
51
|
+
unlinkSync: mockUnlinkSync,
|
|
52
|
+
},
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
// ── Imports ────────────────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
import type {
|
|
58
|
+
ExtensionCommandContext,
|
|
59
|
+
ExtensionContext,
|
|
60
|
+
} from "@earendil-works/pi-coding-agent";
|
|
61
|
+
import {
|
|
62
|
+
ConfigStore,
|
|
63
|
+
type ConfigStoreDeps,
|
|
64
|
+
type ResolvedPolicyPathProvider,
|
|
65
|
+
} from "#src/config-store";
|
|
66
|
+
import { DEFAULT_EXTENSION_CONFIG } from "#src/extension-config";
|
|
67
|
+
import type { ResolvedPolicyPaths } from "#src/policy-loader";
|
|
68
|
+
|
|
69
|
+
// ── Helpers ────────────────────────────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
function makePolicyPathProvider(
|
|
72
|
+
paths?: Partial<ResolvedPolicyPaths>,
|
|
73
|
+
): ResolvedPolicyPathProvider {
|
|
74
|
+
return {
|
|
75
|
+
getResolvedPolicyPaths: vi.fn(
|
|
76
|
+
(): ResolvedPolicyPaths => ({
|
|
77
|
+
globalConfigPath: "/agent/config.json",
|
|
78
|
+
globalConfigExists: false,
|
|
79
|
+
projectConfigPath: null,
|
|
80
|
+
projectConfigExists: false,
|
|
81
|
+
agentsDir: "/agent/agents",
|
|
82
|
+
agentsDirExists: false,
|
|
83
|
+
projectAgentsDir: null,
|
|
84
|
+
projectAgentsDirExists: false,
|
|
85
|
+
...paths,
|
|
86
|
+
}),
|
|
87
|
+
),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function makeLogger() {
|
|
92
|
+
return {
|
|
93
|
+
writeDebugLog:
|
|
94
|
+
vi.fn<(event: string, details?: Record<string, unknown>) => void>(),
|
|
95
|
+
writeReviewLog:
|
|
96
|
+
vi.fn<(event: string, details?: Record<string, unknown>) => void>(),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function makeCtx(overrides: Partial<ExtensionContext> = {}): ExtensionContext {
|
|
101
|
+
return {
|
|
102
|
+
cwd: "/test/project",
|
|
103
|
+
hasUI: false,
|
|
104
|
+
ui: { notify: vi.fn(), setStatus: vi.fn() },
|
|
105
|
+
sessionManager: { getEntries: vi.fn(), addEntry: vi.fn() },
|
|
106
|
+
...overrides,
|
|
107
|
+
} as unknown as ExtensionContext;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function makeCommandCtx(
|
|
111
|
+
overrides: Partial<ExtensionCommandContext> = {},
|
|
112
|
+
): ExtensionCommandContext {
|
|
113
|
+
return {
|
|
114
|
+
cwd: "/test/project",
|
|
115
|
+
ui: { notify: vi.fn(), setStatus: vi.fn() },
|
|
116
|
+
...overrides,
|
|
117
|
+
} as unknown as ExtensionCommandContext;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function makeStore(overrides: Partial<ConfigStoreDeps> = {}): {
|
|
121
|
+
store: ConfigStore;
|
|
122
|
+
logger: ReturnType<typeof makeLogger>;
|
|
123
|
+
} {
|
|
124
|
+
const logger = makeLogger();
|
|
125
|
+
const deps: ConfigStoreDeps = {
|
|
126
|
+
agentDir: "/test/agent",
|
|
127
|
+
policyPaths: makePolicyPathProvider(),
|
|
128
|
+
logger,
|
|
129
|
+
...overrides,
|
|
130
|
+
};
|
|
131
|
+
return { store: new ConfigStore(deps), logger };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ── Tests ──────────────────────────────────────────────────────────────────
|
|
135
|
+
|
|
136
|
+
describe("ConfigStore", () => {
|
|
137
|
+
beforeEach(() => {
|
|
138
|
+
mockLoadAndMergeConfigs.mockReset().mockReturnValue({
|
|
139
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
140
|
+
issues: [],
|
|
141
|
+
});
|
|
142
|
+
mockLoadUnifiedConfig.mockReset().mockReturnValue({ config: {} });
|
|
143
|
+
mockSyncPermissionSystemStatus.mockReset();
|
|
144
|
+
mockBuildResolvedConfigLogEntry
|
|
145
|
+
.mockReset()
|
|
146
|
+
.mockReturnValue({ resolved: true });
|
|
147
|
+
mockExistsSync.mockReset().mockReturnValue(false);
|
|
148
|
+
mockMkdirSync.mockReset();
|
|
149
|
+
mockWriteFileSync.mockReset();
|
|
150
|
+
mockRenameSync.mockReset();
|
|
151
|
+
mockUnlinkSync.mockReset();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// ── current() ─────────────────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
describe("current()", () => {
|
|
157
|
+
it("returns DEFAULT_EXTENSION_CONFIG before any refresh", () => {
|
|
158
|
+
const { store } = makeStore();
|
|
159
|
+
expect(store.current()).toEqual(DEFAULT_EXTENSION_CONFIG);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// ── refresh() ─────────────────────────────────────────────────────────
|
|
164
|
+
|
|
165
|
+
describe("refresh()", () => {
|
|
166
|
+
it("uses the passed ctx cwd for loadAndMergeConfigs", () => {
|
|
167
|
+
const { store } = makeStore();
|
|
168
|
+
store.refresh(makeCtx({ cwd: "/my/project" }));
|
|
169
|
+
expect(mockLoadAndMergeConfigs).toHaveBeenCalledWith(
|
|
170
|
+
"/test/agent",
|
|
171
|
+
"/my/project",
|
|
172
|
+
expect.any(String),
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("uses empty string cwd when no ctx is provided", () => {
|
|
177
|
+
const { store } = makeStore();
|
|
178
|
+
store.refresh();
|
|
179
|
+
expect(mockLoadAndMergeConfigs).toHaveBeenCalledWith(
|
|
180
|
+
"/test/agent",
|
|
181
|
+
"",
|
|
182
|
+
expect.any(String),
|
|
183
|
+
);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("updates current() with normalized merged result", () => {
|
|
187
|
+
const { store } = makeStore();
|
|
188
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
189
|
+
merged: { debugLog: true, permissionReviewLog: false, yoloMode: false },
|
|
190
|
+
issues: [],
|
|
191
|
+
});
|
|
192
|
+
store.refresh();
|
|
193
|
+
expect(store.current().debugLog).toBe(true);
|
|
194
|
+
expect(store.current().permissionReviewLog).toBe(false);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("writes config.loaded debug log", () => {
|
|
198
|
+
const { store, logger } = makeStore();
|
|
199
|
+
store.refresh();
|
|
200
|
+
expect(logger.writeDebugLog).toHaveBeenCalledWith(
|
|
201
|
+
"config.loaded",
|
|
202
|
+
expect.objectContaining({ debugLog: false }),
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("sets warning when issues are present", () => {
|
|
207
|
+
const { store } = makeStore();
|
|
208
|
+
const ctx = makeCtx({ hasUI: false });
|
|
209
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
210
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
211
|
+
issues: ["legacy config detected"],
|
|
212
|
+
});
|
|
213
|
+
store.refresh(ctx);
|
|
214
|
+
// Verify the warning is tracked (next identical call should not re-notify)
|
|
215
|
+
const mockNotify = vi.fn();
|
|
216
|
+
const ctx2 = makeCtx({
|
|
217
|
+
hasUI: true,
|
|
218
|
+
ui: { notify: mockNotify } as never,
|
|
219
|
+
});
|
|
220
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
221
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
222
|
+
issues: ["legacy config detected"],
|
|
223
|
+
});
|
|
224
|
+
store.refresh(ctx2);
|
|
225
|
+
// Same warning — should not re-notify
|
|
226
|
+
expect(mockNotify).not.toHaveBeenCalled();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("notifies UI when a new warning appears and hasUI is true", () => {
|
|
230
|
+
const mockNotify = vi.fn();
|
|
231
|
+
const { store } = makeStore();
|
|
232
|
+
const ctx = makeCtx({ hasUI: true, ui: { notify: mockNotify } as never });
|
|
233
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
234
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
235
|
+
issues: ["new warning"],
|
|
236
|
+
});
|
|
237
|
+
store.refresh(ctx);
|
|
238
|
+
expect(mockNotify).toHaveBeenCalledWith("new warning", "warning");
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("does not re-notify the same warning on subsequent calls", () => {
|
|
242
|
+
const mockNotify = vi.fn();
|
|
243
|
+
const { store } = makeStore();
|
|
244
|
+
const ctx = makeCtx({ hasUI: true, ui: { notify: mockNotify } as never });
|
|
245
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
246
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
247
|
+
issues: ["persistent warning"],
|
|
248
|
+
});
|
|
249
|
+
store.refresh(ctx);
|
|
250
|
+
store.refresh(ctx);
|
|
251
|
+
expect(mockNotify).toHaveBeenCalledTimes(1);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it("clears warning when no issues on next refresh", () => {
|
|
255
|
+
const mockNotify = vi.fn();
|
|
256
|
+
const { store } = makeStore();
|
|
257
|
+
// First call: set a warning
|
|
258
|
+
const ctxWithUI = makeCtx({
|
|
259
|
+
hasUI: true,
|
|
260
|
+
ui: { notify: mockNotify } as never,
|
|
261
|
+
});
|
|
262
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
263
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
264
|
+
issues: ["warning"],
|
|
265
|
+
});
|
|
266
|
+
store.refresh(ctxWithUI);
|
|
267
|
+
// Second call: no issues — warning should clear
|
|
268
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
269
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
270
|
+
issues: [],
|
|
271
|
+
});
|
|
272
|
+
store.refresh();
|
|
273
|
+
// Third call: same warning reappears — should notify again (dedup cleared)
|
|
274
|
+
mockLoadAndMergeConfigs.mockReturnValue({
|
|
275
|
+
merged: { ...DEFAULT_EXTENSION_CONFIG },
|
|
276
|
+
issues: ["warning"],
|
|
277
|
+
});
|
|
278
|
+
store.refresh(ctxWithUI);
|
|
279
|
+
expect(mockNotify).toHaveBeenCalledTimes(2);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("calls syncPermissionSystemStatus when hasUI is true", () => {
|
|
283
|
+
const { store } = makeStore();
|
|
284
|
+
const ctx = makeCtx({ hasUI: true });
|
|
285
|
+
store.refresh(ctx);
|
|
286
|
+
expect(mockSyncPermissionSystemStatus).toHaveBeenCalledWith(
|
|
287
|
+
ctx,
|
|
288
|
+
expect.any(Object),
|
|
289
|
+
);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("does not call syncPermissionSystemStatus when hasUI is false", () => {
|
|
293
|
+
const { store } = makeStore();
|
|
294
|
+
const ctx = makeCtx({ hasUI: false });
|
|
295
|
+
store.refresh(ctx);
|
|
296
|
+
expect(mockSyncPermissionSystemStatus).not.toHaveBeenCalled();
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
// ── save() ─────────────────────────────────────────────────────────────
|
|
301
|
+
|
|
302
|
+
describe("save()", () => {
|
|
303
|
+
it("writes merged config to the global path", () => {
|
|
304
|
+
const { store } = makeStore();
|
|
305
|
+
mockLoadUnifiedConfig.mockReturnValue({
|
|
306
|
+
config: { permission: { "*": "ask" } },
|
|
307
|
+
});
|
|
308
|
+
const next = { ...DEFAULT_EXTENSION_CONFIG, debugLog: true };
|
|
309
|
+
const ctx = makeCommandCtx();
|
|
310
|
+
store.save(next, ctx);
|
|
311
|
+
expect(mockWriteFileSync).toHaveBeenCalledWith(
|
|
312
|
+
expect.stringContaining(".tmp"),
|
|
313
|
+
expect.stringContaining('"debugLog": true'),
|
|
314
|
+
"utf-8",
|
|
315
|
+
);
|
|
316
|
+
expect(mockRenameSync).toHaveBeenCalled();
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it("updates current() after a successful save", () => {
|
|
320
|
+
const { store } = makeStore();
|
|
321
|
+
const next = { ...DEFAULT_EXTENSION_CONFIG, debugLog: true };
|
|
322
|
+
store.save(next, makeCommandCtx());
|
|
323
|
+
expect(store.current().debugLog).toBe(true);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("calls syncPermissionSystemStatus after a successful save", () => {
|
|
327
|
+
const { store } = makeStore();
|
|
328
|
+
const ctx = makeCommandCtx();
|
|
329
|
+
store.save({ ...DEFAULT_EXTENSION_CONFIG }, ctx);
|
|
330
|
+
expect(mockSyncPermissionSystemStatus).toHaveBeenCalledWith(
|
|
331
|
+
ctx,
|
|
332
|
+
expect.any(Object),
|
|
333
|
+
);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it("writes config.saved debug log after a successful save", () => {
|
|
337
|
+
const { store, logger } = makeStore();
|
|
338
|
+
store.save({ ...DEFAULT_EXTENSION_CONFIG }, makeCommandCtx());
|
|
339
|
+
expect(logger.writeDebugLog).toHaveBeenCalledWith(
|
|
340
|
+
"config.saved",
|
|
341
|
+
expect.objectContaining({ debugLog: false }),
|
|
342
|
+
);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("notifies with error and returns early when write fails", () => {
|
|
346
|
+
const mockNotify = vi.fn();
|
|
347
|
+
const ctx = makeCommandCtx({ ui: { notify: mockNotify } as never });
|
|
348
|
+
const { store, logger } = makeStore();
|
|
349
|
+
mockMkdirSync.mockImplementation(() => {
|
|
350
|
+
throw new Error("disk full");
|
|
351
|
+
});
|
|
352
|
+
store.save({ ...DEFAULT_EXTENSION_CONFIG }, ctx);
|
|
353
|
+
expect(mockNotify).toHaveBeenCalledWith(
|
|
354
|
+
expect.stringContaining("Failed to save"),
|
|
355
|
+
"error",
|
|
356
|
+
);
|
|
357
|
+
// current() is not updated on failure
|
|
358
|
+
expect(store.current()).toEqual(DEFAULT_EXTENSION_CONFIG);
|
|
359
|
+
// no debug log on failure
|
|
360
|
+
expect(logger.writeDebugLog).not.toHaveBeenCalledWith(
|
|
361
|
+
"config.saved",
|
|
362
|
+
expect.anything(),
|
|
363
|
+
);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it("attempts cleanup of tmp file when write fails and tmp exists", () => {
|
|
367
|
+
const ctx = makeCommandCtx();
|
|
368
|
+
const { store } = makeStore();
|
|
369
|
+
mockMkdirSync.mockImplementation(() => {
|
|
370
|
+
throw new Error("disk full");
|
|
371
|
+
});
|
|
372
|
+
mockExistsSync.mockReturnValue(true);
|
|
373
|
+
store.save({ ...DEFAULT_EXTENSION_CONFIG }, ctx);
|
|
374
|
+
expect(mockUnlinkSync).toHaveBeenCalled();
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
// ── logResolvedPaths() ─────────────────────────────────────────────────
|
|
379
|
+
|
|
380
|
+
describe("logResolvedPaths()", () => {
|
|
381
|
+
it("writes config.resolved to both review and debug logs", () => {
|
|
382
|
+
const { store, logger } = makeStore();
|
|
383
|
+
store.logResolvedPaths();
|
|
384
|
+
expect(logger.writeReviewLog).toHaveBeenCalledWith(
|
|
385
|
+
"config.resolved",
|
|
386
|
+
expect.any(Object),
|
|
387
|
+
);
|
|
388
|
+
expect(logger.writeDebugLog).toHaveBeenCalledWith(
|
|
389
|
+
"config.resolved",
|
|
390
|
+
expect.any(Object),
|
|
391
|
+
);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it("calls getResolvedPolicyPaths from the provider", () => {
|
|
395
|
+
const mockProvider = makePolicyPathProvider();
|
|
396
|
+
const { store } = makeStore({ policyPaths: mockProvider });
|
|
397
|
+
store.logResolvedPaths();
|
|
398
|
+
expect(mockProvider.getResolvedPolicyPaths).toHaveBeenCalled();
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it("passes legacy detection results to buildResolvedConfigLogEntry", () => {
|
|
402
|
+
const { store } = makeStore();
|
|
403
|
+
// Make one legacy path exist
|
|
404
|
+
mockExistsSync.mockImplementation((p: string) =>
|
|
405
|
+
p.includes("policies.json"),
|
|
406
|
+
);
|
|
407
|
+
store.logResolvedPaths("/some/project");
|
|
408
|
+
expect(mockBuildResolvedConfigLogEntry).toHaveBeenCalledWith(
|
|
409
|
+
expect.objectContaining({
|
|
410
|
+
legacyGlobalPolicyDetected: expect.any(Boolean),
|
|
411
|
+
legacyProjectPolicyDetected: expect.any(Boolean),
|
|
412
|
+
legacyExtensionConfigDetected: expect.any(Boolean),
|
|
413
|
+
}),
|
|
414
|
+
);
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it("does not check project legacy path when no cwd is provided", () => {
|
|
418
|
+
const { store } = makeStore();
|
|
419
|
+
store.logResolvedPaths(); // no cwd
|
|
420
|
+
// existsSync called for global and ext-config legacy paths only (not project)
|
|
421
|
+
const calls = mockExistsSync.mock.calls.map(([p]: [string]) => p);
|
|
422
|
+
const projectCalls = calls.filter(
|
|
423
|
+
(p) => p.includes("/null/") || p.includes("null"),
|
|
424
|
+
);
|
|
425
|
+
expect(projectCalls).toHaveLength(0);
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
});
|