cognitive-modules-cli 2.2.0 → 2.2.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.
@@ -0,0 +1,455 @@
1
+ /**
2
+ * Tests for Policy Enforcement
3
+ *
4
+ * Tests all policy enforcement functionality:
5
+ * - Tool policy checking (allowed/denied lists)
6
+ * - General policy checking (network, filesystem, etc.)
7
+ * - Tool call interception
8
+ * - Policy-aware executors
9
+ */
10
+
11
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
12
+ import {
13
+ checkToolPolicy,
14
+ checkPolicy,
15
+ checkToolAllowed,
16
+ validateToolsAllowed,
17
+ getDeniedActions,
18
+ getDeniedTools,
19
+ getAllowedTools,
20
+ ToolCallInterceptor,
21
+ createPolicyAwareExecutor,
22
+ type PolicyAction,
23
+ } from './runner.js';
24
+ import type { CognitiveModule } from '../types.js';
25
+
26
+ // =============================================================================
27
+ // Test Fixtures
28
+ // =============================================================================
29
+
30
+ function createMockModule(overrides: Partial<CognitiveModule> = {}): CognitiveModule {
31
+ return {
32
+ name: 'test-module',
33
+ version: '1.0.0',
34
+ responsibility: 'Test module',
35
+ excludes: [],
36
+ prompt: 'Test prompt',
37
+ location: '/test',
38
+ format: 'v2',
39
+ ...overrides,
40
+ };
41
+ }
42
+
43
+ // =============================================================================
44
+ // checkToolPolicy Tests
45
+ // =============================================================================
46
+
47
+ describe('checkToolPolicy', () => {
48
+ it('should allow all tools when no policy defined', () => {
49
+ const module = createMockModule();
50
+
51
+ expect(checkToolPolicy('write_file', module).allowed).toBe(true);
52
+ expect(checkToolPolicy('shell', module).allowed).toBe(true);
53
+ expect(checkToolPolicy('any_tool', module).allowed).toBe(true);
54
+ });
55
+
56
+ it('should deny tools in denied list', () => {
57
+ const module = createMockModule({
58
+ tools: {
59
+ policy: 'allow_by_default',
60
+ allowed: [],
61
+ denied: ['write_file', 'shell', 'network'],
62
+ },
63
+ });
64
+
65
+ const result = checkToolPolicy('write_file', module);
66
+ expect(result.allowed).toBe(false);
67
+ expect(result.reason).toContain('explicitly denied');
68
+ expect(result.policy).toBe('tools.denied');
69
+ });
70
+
71
+ it('should handle case-insensitive tool names', () => {
72
+ const module = createMockModule({
73
+ tools: {
74
+ policy: 'allow_by_default',
75
+ allowed: [],
76
+ denied: ['Write_File'],
77
+ },
78
+ });
79
+
80
+ expect(checkToolPolicy('write_file', module).allowed).toBe(false);
81
+ expect(checkToolPolicy('WRITE_FILE', module).allowed).toBe(false);
82
+ expect(checkToolPolicy('write-file', module).allowed).toBe(false);
83
+ });
84
+
85
+ it('should enforce deny_by_default policy', () => {
86
+ const module = createMockModule({
87
+ tools: {
88
+ policy: 'deny_by_default',
89
+ allowed: ['read_file', 'list_dir'],
90
+ },
91
+ });
92
+
93
+ expect(checkToolPolicy('read_file', module).allowed).toBe(true);
94
+ expect(checkToolPolicy('list_dir', module).allowed).toBe(true);
95
+
96
+ const result = checkToolPolicy('write_file', module);
97
+ expect(result.allowed).toBe(false);
98
+ expect(result.reason).toContain('not in allowed list');
99
+ expect(result.policy).toBe('tools.policy');
100
+ });
101
+
102
+ it('should allow tools in allow_by_default mode (not in denied)', () => {
103
+ const module = createMockModule({
104
+ tools: {
105
+ policy: 'allow_by_default',
106
+ allowed: [],
107
+ denied: ['shell'],
108
+ },
109
+ });
110
+
111
+ expect(checkToolPolicy('read_file', module).allowed).toBe(true);
112
+ expect(checkToolPolicy('write_file', module).allowed).toBe(true);
113
+ expect(checkToolPolicy('shell', module).allowed).toBe(false);
114
+ });
115
+ });
116
+
117
+ // =============================================================================
118
+ // checkPolicy Tests
119
+ // =============================================================================
120
+
121
+ describe('checkPolicy', () => {
122
+ it('should allow all actions when no policies defined', () => {
123
+ const module = createMockModule();
124
+
125
+ expect(checkPolicy('network', module).allowed).toBe(true);
126
+ expect(checkPolicy('filesystem_write', module).allowed).toBe(true);
127
+ expect(checkPolicy('side_effects', module).allowed).toBe(true);
128
+ expect(checkPolicy('code_execution', module).allowed).toBe(true);
129
+ });
130
+
131
+ it('should deny actions marked as deny', () => {
132
+ const module = createMockModule({
133
+ policies: {
134
+ network: 'deny',
135
+ filesystem_write: 'deny',
136
+ side_effects: 'allow',
137
+ code_execution: 'deny',
138
+ },
139
+ });
140
+
141
+ const networkResult = checkPolicy('network', module);
142
+ expect(networkResult.allowed).toBe(false);
143
+ expect(networkResult.reason).toContain("'network' is denied");
144
+ expect(networkResult.policy).toBe('policies.network');
145
+
146
+ expect(checkPolicy('filesystem_write', module).allowed).toBe(false);
147
+ expect(checkPolicy('side_effects', module).allowed).toBe(true);
148
+ expect(checkPolicy('code_execution', module).allowed).toBe(false);
149
+ });
150
+ });
151
+
152
+ // =============================================================================
153
+ // checkToolAllowed Tests (Combined Check)
154
+ // =============================================================================
155
+
156
+ describe('checkToolAllowed', () => {
157
+ it('should check both tool policy and general policies', () => {
158
+ const module = createMockModule({
159
+ policies: {
160
+ filesystem_write: 'deny',
161
+ side_effects: 'deny',
162
+ },
163
+ tools: {
164
+ policy: 'allow_by_default',
165
+ allowed: [],
166
+ },
167
+ });
168
+
169
+ // write_file maps to filesystem_write and side_effects
170
+ const result = checkToolAllowed('write_file', module);
171
+ expect(result.allowed).toBe(false);
172
+ expect(result.reason).toContain('filesystem_write');
173
+ });
174
+
175
+ it('should block tools that require denied actions', () => {
176
+ const module = createMockModule({
177
+ policies: {
178
+ network: 'deny',
179
+ },
180
+ });
181
+
182
+ // Network tools should be blocked
183
+ expect(checkToolAllowed('fetch', module).allowed).toBe(false);
184
+ expect(checkToolAllowed('http', module).allowed).toBe(false);
185
+ expect(checkToolAllowed('curl', module).allowed).toBe(false);
186
+
187
+ // Non-network tools should be allowed
188
+ expect(checkToolAllowed('read_file', module).allowed).toBe(true);
189
+ });
190
+
191
+ it('should block shell/exec when code_execution denied', () => {
192
+ const module = createMockModule({
193
+ policies: {
194
+ code_execution: 'deny',
195
+ },
196
+ });
197
+
198
+ expect(checkToolAllowed('shell', module).allowed).toBe(false);
199
+ expect(checkToolAllowed('exec', module).allowed).toBe(false);
200
+ expect(checkToolAllowed('code_interpreter', module).allowed).toBe(false);
201
+ });
202
+
203
+ it('should check explicit tools policy first', () => {
204
+ const module = createMockModule({
205
+ policies: {
206
+ network: 'allow', // Allow network in general
207
+ },
208
+ tools: {
209
+ policy: 'allow_by_default',
210
+ allowed: [],
211
+ denied: ['fetch'], // But explicitly deny fetch
212
+ },
213
+ });
214
+
215
+ const result = checkToolAllowed('fetch', module);
216
+ expect(result.allowed).toBe(false);
217
+ expect(result.policy).toBe('tools.denied');
218
+ });
219
+ });
220
+
221
+ // =============================================================================
222
+ // validateToolsAllowed Tests
223
+ // =============================================================================
224
+
225
+ describe('validateToolsAllowed', () => {
226
+ it('should return empty array when all tools allowed', () => {
227
+ const module = createMockModule();
228
+
229
+ const violations = validateToolsAllowed(['read_file', 'write_file', 'shell'], module);
230
+ expect(violations).toHaveLength(0);
231
+ });
232
+
233
+ it('should return all violations', () => {
234
+ const module = createMockModule({
235
+ policies: {
236
+ network: 'deny',
237
+ code_execution: 'deny',
238
+ },
239
+ });
240
+
241
+ const violations = validateToolsAllowed(['fetch', 'shell', 'read_file'], module);
242
+ expect(violations).toHaveLength(2);
243
+ expect(violations.some(v => v.reason?.includes('fetch'))).toBe(true);
244
+ expect(violations.some(v => v.reason?.includes('shell'))).toBe(true);
245
+ });
246
+ });
247
+
248
+ // =============================================================================
249
+ // Helper Functions Tests
250
+ // =============================================================================
251
+
252
+ describe('getDeniedActions', () => {
253
+ it('should return list of denied actions', () => {
254
+ const module = createMockModule({
255
+ policies: {
256
+ network: 'deny',
257
+ filesystem_write: 'deny',
258
+ side_effects: 'allow',
259
+ },
260
+ });
261
+
262
+ const denied = getDeniedActions(module);
263
+ expect(denied).toContain('network');
264
+ expect(denied).toContain('filesystem_write');
265
+ expect(denied).not.toContain('side_effects');
266
+ });
267
+
268
+ it('should return empty array when no policies', () => {
269
+ const module = createMockModule();
270
+ expect(getDeniedActions(module)).toHaveLength(0);
271
+ });
272
+ });
273
+
274
+ describe('getDeniedTools', () => {
275
+ it('should return denied tools list', () => {
276
+ const module = createMockModule({
277
+ tools: {
278
+ policy: 'allow_by_default',
279
+ allowed: [],
280
+ denied: ['shell', 'network', 'write_file'],
281
+ },
282
+ });
283
+
284
+ const denied = getDeniedTools(module);
285
+ expect(denied).toContain('shell');
286
+ expect(denied).toContain('network');
287
+ expect(denied).toContain('write_file');
288
+ });
289
+ });
290
+
291
+ describe('getAllowedTools', () => {
292
+ it('should return null for allow_by_default', () => {
293
+ const module = createMockModule({
294
+ tools: {
295
+ policy: 'allow_by_default',
296
+ allowed: ['read_file'],
297
+ },
298
+ });
299
+
300
+ expect(getAllowedTools(module)).toBeNull();
301
+ });
302
+
303
+ it('should return allowed list for deny_by_default', () => {
304
+ const module = createMockModule({
305
+ tools: {
306
+ policy: 'deny_by_default',
307
+ allowed: ['read_file', 'list_dir'],
308
+ },
309
+ });
310
+
311
+ const allowed = getAllowedTools(module);
312
+ expect(allowed).toEqual(['read_file', 'list_dir']);
313
+ });
314
+ });
315
+
316
+ // =============================================================================
317
+ // ToolCallInterceptor Tests
318
+ // =============================================================================
319
+
320
+ describe('ToolCallInterceptor', () => {
321
+ let module: CognitiveModule;
322
+ let interceptor: ToolCallInterceptor;
323
+
324
+ beforeEach(() => {
325
+ module = createMockModule({
326
+ policies: {
327
+ network: 'deny',
328
+ filesystem_write: 'deny',
329
+ },
330
+ tools: {
331
+ policy: 'deny_by_default',
332
+ allowed: ['read_file', 'list_dir'],
333
+ },
334
+ });
335
+ interceptor = new ToolCallInterceptor(module);
336
+ });
337
+
338
+ it('should check if tool is allowed', () => {
339
+ expect(interceptor.checkAllowed('read_file').allowed).toBe(true);
340
+ expect(interceptor.checkAllowed('write_file').allowed).toBe(false);
341
+ expect(interceptor.checkAllowed('fetch').allowed).toBe(false);
342
+ });
343
+
344
+ it('should execute allowed tool', async () => {
345
+ const mockExecutor = vi.fn().mockResolvedValue('file content');
346
+ interceptor.registerTool('read_file', mockExecutor);
347
+
348
+ const result = await interceptor.execute({
349
+ name: 'read_file',
350
+ arguments: { path: '/test.txt' },
351
+ });
352
+
353
+ expect(result.success).toBe(true);
354
+ expect(result.result).toBe('file content');
355
+ expect(mockExecutor).toHaveBeenCalledWith({ path: '/test.txt' });
356
+ });
357
+
358
+ it('should block denied tool', async () => {
359
+ const mockExecutor = vi.fn().mockResolvedValue('done');
360
+ interceptor.registerTool('write_file', mockExecutor);
361
+
362
+ const result = await interceptor.execute({
363
+ name: 'write_file',
364
+ arguments: { path: '/test.txt', content: 'hello' },
365
+ });
366
+
367
+ expect(result.success).toBe(false);
368
+ expect(result.error?.code).toBe('TOOL_NOT_ALLOWED');
369
+ expect(mockExecutor).not.toHaveBeenCalled();
370
+ });
371
+
372
+ it('should log all calls', async () => {
373
+ interceptor.registerTool('read_file', vi.fn().mockResolvedValue('ok'));
374
+ interceptor.registerTool('write_file', vi.fn().mockResolvedValue('ok'));
375
+
376
+ await interceptor.execute({ name: 'read_file', arguments: {} });
377
+ await interceptor.execute({ name: 'write_file', arguments: {} });
378
+ await interceptor.execute({ name: 'read_file', arguments: {} });
379
+
380
+ const log = interceptor.getCallLog();
381
+ expect(log).toHaveLength(3);
382
+ expect(log[0].tool).toBe('read_file');
383
+ expect(log[0].allowed).toBe(true);
384
+ expect(log[1].tool).toBe('write_file');
385
+ expect(log[1].allowed).toBe(false);
386
+ });
387
+
388
+ it('should get denied calls', async () => {
389
+ interceptor.registerTool('read_file', vi.fn().mockResolvedValue('ok'));
390
+
391
+ await interceptor.execute({ name: 'read_file', arguments: {} });
392
+ await interceptor.execute({ name: 'write_file', arguments: {} });
393
+ await interceptor.execute({ name: 'shell', arguments: {} });
394
+
395
+ const denied = interceptor.getDeniedCalls();
396
+ expect(denied).toHaveLength(2);
397
+ expect(denied.some(d => d.tool === 'write_file')).toBe(true);
398
+ expect(denied.some(d => d.tool === 'shell')).toBe(true);
399
+ });
400
+
401
+ it('should execute many and stop on policy violation', async () => {
402
+ interceptor.registerTool('read_file', vi.fn().mockResolvedValue('ok'));
403
+ interceptor.registerTool('list_dir', vi.fn().mockResolvedValue(['a', 'b']));
404
+
405
+ const results = await interceptor.executeMany([
406
+ { name: 'read_file', arguments: {} },
407
+ { name: 'write_file', arguments: {} }, // Blocked
408
+ { name: 'list_dir', arguments: {} }, // Should not execute
409
+ ]);
410
+
411
+ expect(results).toHaveLength(2);
412
+ expect(results[0].success).toBe(true);
413
+ expect(results[1].success).toBe(false);
414
+ });
415
+
416
+ it('should provide policy summary', () => {
417
+ const summary = interceptor.getPolicySummary();
418
+
419
+ expect(summary.deniedActions).toContain('network');
420
+ expect(summary.deniedActions).toContain('filesystem_write');
421
+ expect(summary.allowedTools).toEqual(['read_file', 'list_dir']);
422
+ expect(summary.toolsPolicy).toBe('deny_by_default');
423
+ });
424
+ });
425
+
426
+ // =============================================================================
427
+ // createPolicyAwareExecutor Tests
428
+ // =============================================================================
429
+
430
+ describe('createPolicyAwareExecutor', () => {
431
+ it('should execute allowed tool', async () => {
432
+ const module = createMockModule();
433
+ const executor = vi.fn().mockResolvedValue('result');
434
+
435
+ const safeExecutor = createPolicyAwareExecutor(module, 'read_file', executor);
436
+ const result = await safeExecutor({ path: '/test.txt' });
437
+
438
+ expect(result).toBe('result');
439
+ expect(executor).toHaveBeenCalledWith({ path: '/test.txt' });
440
+ });
441
+
442
+ it('should throw on policy violation', async () => {
443
+ const module = createMockModule({
444
+ policies: {
445
+ filesystem_write: 'deny',
446
+ },
447
+ });
448
+ const executor = vi.fn().mockResolvedValue('result');
449
+
450
+ const safeExecutor = createPolicyAwareExecutor(module, 'write_file', executor);
451
+
452
+ await expect(safeExecutor({ path: '/test.txt' })).rejects.toThrow('Policy violation');
453
+ expect(executor).not.toHaveBeenCalled();
454
+ });
455
+ });