@gotgenes/pi-permission-system 5.16.0 → 5.18.0

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.
@@ -0,0 +1,144 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { buildInputForSurface } from "../src/input-normalizer";
3
+ import type { PermissionsService } from "../src/service";
4
+ import {
5
+ getPermissionsService,
6
+ publishPermissionsService,
7
+ unpublishPermissionsService,
8
+ } from "../src/service";
9
+ import type { PermissionCheckResult } from "../src/types";
10
+
11
+ // ── helpers ────────────────────────────────────────────────────────────────
12
+
13
+ function makeService(
14
+ overrides: Partial<PermissionsService> = {},
15
+ ): PermissionsService {
16
+ return {
17
+ checkPermission: vi.fn(),
18
+ ...overrides,
19
+ };
20
+ }
21
+
22
+ // ── globalThis accessor ────────────────────────────────────────────────────
23
+
24
+ describe("globalThis accessor", () => {
25
+ afterEach(() => {
26
+ unpublishPermissionsService();
27
+ });
28
+
29
+ it("returns undefined when nothing has been published", () => {
30
+ expect(getPermissionsService()).toBeUndefined();
31
+ });
32
+
33
+ it("returns the published service", () => {
34
+ const service = makeService();
35
+ publishPermissionsService(service);
36
+ expect(getPermissionsService()).toBe(service);
37
+ });
38
+
39
+ it("overwrites a previously published service", () => {
40
+ const first = makeService();
41
+ const second = makeService();
42
+ publishPermissionsService(first);
43
+ publishPermissionsService(second);
44
+ expect(getPermissionsService()).toBe(second);
45
+ });
46
+
47
+ it("returns undefined after unpublish", () => {
48
+ const service = makeService();
49
+ publishPermissionsService(service);
50
+ unpublishPermissionsService();
51
+ expect(getPermissionsService()).toBeUndefined();
52
+ });
53
+
54
+ it("unpublish is safe to call when nothing was published", () => {
55
+ expect(() => unpublishPermissionsService()).not.toThrow();
56
+ expect(getPermissionsService()).toBeUndefined();
57
+ });
58
+ });
59
+
60
+ // ── service adapter delegation ─────────────────────────────────────────────
61
+
62
+ describe("service adapter delegation", () => {
63
+ afterEach(() => {
64
+ unpublishPermissionsService();
65
+ });
66
+
67
+ const fakeResult: PermissionCheckResult = {
68
+ toolName: "bash",
69
+ state: "allow",
70
+ matchedPattern: "git *",
71
+ source: "bash",
72
+ origin: "global",
73
+ };
74
+
75
+ it("checkPermission delegates surface and value through buildInputForSurface", () => {
76
+ const checkPermission = vi.fn().mockReturnValue(fakeResult);
77
+ const sessionRules = [
78
+ {
79
+ surface: "bash",
80
+ pattern: "*",
81
+ action: "allow" as const,
82
+ layer: "session" as const,
83
+ origin: "session" as const,
84
+ },
85
+ ];
86
+
87
+ // Build the adapter the same way index.ts will
88
+ const service: PermissionsService = {
89
+ checkPermission(surface, value, agentName) {
90
+ const input = buildInputForSurface(surface, value);
91
+ return checkPermission(surface, input, agentName, sessionRules);
92
+ },
93
+ };
94
+
95
+ publishPermissionsService(service);
96
+ const retrieved = getPermissionsService()!;
97
+ const result = retrieved.checkPermission("bash", "git push");
98
+
99
+ expect(result).toBe(fakeResult);
100
+ expect(checkPermission).toHaveBeenCalledWith(
101
+ "bash",
102
+ { command: "git push" },
103
+ undefined,
104
+ sessionRules,
105
+ );
106
+ });
107
+
108
+ it("checkPermission passes agentName through", () => {
109
+ const checkPermission = vi.fn().mockReturnValue(fakeResult);
110
+
111
+ const service: PermissionsService = {
112
+ checkPermission(surface, value, agentName) {
113
+ const input = buildInputForSurface(surface, value);
114
+ return checkPermission(surface, input, agentName, []);
115
+ },
116
+ };
117
+
118
+ publishPermissionsService(service);
119
+ getPermissionsService()!.checkPermission("skill", "my-skill", "Explore");
120
+
121
+ expect(checkPermission).toHaveBeenCalledWith(
122
+ "skill",
123
+ { name: "my-skill" },
124
+ "Explore",
125
+ [],
126
+ );
127
+ });
128
+
129
+ it("checkPermission uses empty object for unknown surfaces", () => {
130
+ const checkPermission = vi.fn().mockReturnValue(fakeResult);
131
+
132
+ const service: PermissionsService = {
133
+ checkPermission(surface, value, agentName) {
134
+ const input = buildInputForSurface(surface, value);
135
+ return checkPermission(surface, input, agentName, []);
136
+ },
137
+ };
138
+
139
+ publishPermissionsService(service);
140
+ getPermissionsService()!.checkPermission("read", "/tmp/file");
141
+
142
+ expect(checkPermission).toHaveBeenCalledWith("read", {}, undefined, []);
143
+ });
144
+ });