@syntesseraai/opencode-feature-factory 0.2.3 → 0.2.4

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.
@@ -1,655 +0,0 @@
1
- /**
2
- * Unit tests for stop-quality-gate module
3
- *
4
- * Tests focus on pure functions that can be tested in isolation:
5
- * - sanitizeOutput: redacts secrets from CI output
6
- * - isSessionReadOnly: determines if session has write permissions
7
- */
8
-
9
- import { sanitizeOutput, truncateOutput } from './stop-quality-gate';
10
-
11
- interface PermissionRule {
12
- permission: string;
13
- pattern: string;
14
- action: 'allow' | 'deny' | 'ask';
15
- }
16
-
17
- function isSessionReadOnly(permission?: PermissionRule[]): boolean {
18
- if (!permission) return false;
19
- return permission.some(
20
- (rule) => (rule.permission === 'edit' || rule.permission === 'bash') && rule.action === 'deny'
21
- );
22
- }
23
-
24
- describe('sanitizeOutput', () => {
25
- describe('AWS credentials', () => {
26
- it('should redact AWS Access Key IDs', () => {
27
- const input = 'Found credentials: AKIAIOSFODNN7EXAMPLE in config';
28
- const result = sanitizeOutput(input);
29
- expect(result).toBe('Found credentials: [REDACTED_AWS_KEY] in config');
30
- });
31
-
32
- it('should redact multiple AWS keys', () => {
33
- const input = 'Key1: AKIAIOSFODNN7EXAMPLE Key2: AKIAI44QH8DHBEXAMPLE';
34
- const result = sanitizeOutput(input);
35
- expect(result).toBe('Key1: [REDACTED_AWS_KEY] Key2: [REDACTED_AWS_KEY]');
36
- });
37
-
38
- it('should not redact partial AWS key patterns', () => {
39
- const input = 'AKIA123'; // Too short
40
- const result = sanitizeOutput(input);
41
- expect(result).toBe('AKIA123');
42
- });
43
- });
44
-
45
- describe('GitHub tokens', () => {
46
- it('should redact GitHub Personal Access Tokens (classic)', () => {
47
- const input = 'Using ghp_aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789';
48
- const result = sanitizeOutput(input);
49
- expect(result).toBe('Using [REDACTED_GH_TOKEN]');
50
- });
51
-
52
- it('should redact GitHub Personal Access Tokens (fine-grained)', () => {
53
- const input = 'Using github_pat_11ABCDEFG_abcdefghijklmnopqrstuvwxyz';
54
- const result = sanitizeOutput(input);
55
- expect(result).toBe('Using [REDACTED_GH_TOKEN]');
56
- });
57
-
58
- it('should redact GitHub OAuth tokens', () => {
59
- const input = 'oauth=gho_aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789';
60
- const result = sanitizeOutput(input);
61
- expect(result).toBe('oauth=[REDACTED_GH_TOKEN]');
62
- });
63
-
64
- it('should redact GitHub App user-to-server tokens', () => {
65
- const input = 'Using ghu_aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789';
66
- const result = sanitizeOutput(input);
67
- expect(result).toBe('Using [REDACTED_GH_TOKEN]');
68
- });
69
-
70
- it('should redact GitHub App server-to-server tokens', () => {
71
- const input = 'Using ghs_aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789';
72
- const result = sanitizeOutput(input);
73
- expect(result).toBe('Using [REDACTED_GH_TOKEN]');
74
- });
75
-
76
- it('should not redact partial GitHub token patterns', () => {
77
- const input = 'ghp_abc'; // Too short
78
- const result = sanitizeOutput(input);
79
- expect(result).toBe('ghp_abc');
80
- });
81
- });
82
-
83
- describe('GitLab tokens', () => {
84
- it('should redact GitLab Personal Access Tokens', () => {
85
- const input = 'Using glpat-abcdefghij1234567890';
86
- const result = sanitizeOutput(input);
87
- expect(result).toBe('Using [REDACTED_GITLAB_TOKEN]');
88
- });
89
-
90
- it('should redact GitLab tokens with hyphens', () => {
91
- const input = 'Using glpat-abc-def-ghi-jkl-mnop-qrs';
92
- const result = sanitizeOutput(input);
93
- expect(result).toBe('Using [REDACTED_GITLAB_TOKEN]');
94
- });
95
-
96
- it('should not redact partial GitLab token patterns', () => {
97
- const input = 'glpat-short'; // Too short
98
- const result = sanitizeOutput(input);
99
- expect(result).toBe('glpat-short');
100
- });
101
- });
102
-
103
- describe('npm tokens', () => {
104
- it('should redact npm tokens', () => {
105
- const input = 'Using npm_aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789';
106
- const result = sanitizeOutput(input);
107
- expect(result).toBe('Using [REDACTED_NPM_TOKEN]');
108
- });
109
-
110
- it('should not redact partial npm token patterns', () => {
111
- const input = 'npm_abc'; // Too short
112
- const result = sanitizeOutput(input);
113
- expect(result).toBe('npm_abc');
114
- });
115
- });
116
-
117
- describe('Bearer tokens', () => {
118
- it('should redact Bearer tokens', () => {
119
- const input = 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9';
120
- const result = sanitizeOutput(input);
121
- expect(result).toBe('Authorization: Bearer [REDACTED]');
122
- });
123
-
124
- it('should handle case-insensitive Bearer', () => {
125
- const input = 'bearer abc123-token.value';
126
- const result = sanitizeOutput(input);
127
- expect(result).toBe('Bearer [REDACTED]');
128
- });
129
- });
130
-
131
- describe('API keys', () => {
132
- it('should redact api_key assignments', () => {
133
- const input = 'api_key=sk-1234567890abcdef';
134
- const result = sanitizeOutput(input);
135
- expect(result).toBe('api_key=[REDACTED]');
136
- });
137
-
138
- it('should redact api-key with hyphen', () => {
139
- const input = 'api-key: my-secret-key';
140
- const result = sanitizeOutput(input);
141
- expect(result).toBe('api_key=[REDACTED]');
142
- });
143
-
144
- it('should redact apikey without separator', () => {
145
- const input = 'apikey=abc123';
146
- const result = sanitizeOutput(input);
147
- expect(result).toBe('api_key=[REDACTED]');
148
- });
149
-
150
- it('should redact quoted api keys', () => {
151
- const input = "api_key='secret-value'";
152
- const result = sanitizeOutput(input);
153
- expect(result).toBe('api_key=[REDACTED]');
154
- });
155
- });
156
-
157
- describe('tokens', () => {
158
- it('should redact token assignments with 8+ char values', () => {
159
- const input = 'token=ghp_xxxxxxxxxxxxxxxxxxxx';
160
- const result = sanitizeOutput(input);
161
- expect(result).toBe('token=[REDACTED]');
162
- });
163
-
164
- it('should redact tokens plural with 8+ char values', () => {
165
- const input = 'tokens: secret12345';
166
- const result = sanitizeOutput(input);
167
- expect(result).toBe('token=[REDACTED]');
168
- });
169
-
170
- it('should NOT redact token with short values (less than 8 chars)', () => {
171
- const input = 'token=abc123';
172
- const result = sanitizeOutput(input);
173
- expect(result).toBe('token=abc123');
174
- });
175
-
176
- it('should NOT redact phrases like "token count: 5" (value too short)', () => {
177
- const input = 'token count: 5';
178
- const result = sanitizeOutput(input);
179
- expect(result).toBe('token count: 5');
180
- });
181
-
182
- it('should NOT redact "token: abc" (value too short)', () => {
183
- const input = 'token: abc';
184
- const result = sanitizeOutput(input);
185
- expect(result).toBe('token: abc');
186
- });
187
-
188
- it('should redact actual token values that are 8+ chars', () => {
189
- const input = 'token=abcd1234efgh';
190
- const result = sanitizeOutput(input);
191
- expect(result).toBe('token=[REDACTED]');
192
- });
193
-
194
- it('should redact quoted tokens with 8+ char values', () => {
195
- const input = 'token="my-secret-token-value"';
196
- const result = sanitizeOutput(input);
197
- expect(result).toBe('token=[REDACTED]');
198
- });
199
- });
200
-
201
- describe('passwords', () => {
202
- it('should redact password assignments', () => {
203
- const input = 'password=super-secret-123!';
204
- const result = sanitizeOutput(input);
205
- expect(result).toBe('password=[REDACTED]');
206
- });
207
-
208
- it('should redact passwords plural', () => {
209
- const input = 'passwords: "admin123"';
210
- const result = sanitizeOutput(input);
211
- expect(result).toBe('password=[REDACTED]');
212
- });
213
-
214
- it('should handle special characters in passwords', () => {
215
- const input = 'password=P@$$w0rd!#%';
216
- const result = sanitizeOutput(input);
217
- expect(result).toBe('password=[REDACTED]');
218
- });
219
- });
220
-
221
- describe('generic secrets', () => {
222
- it('should redact secret assignments', () => {
223
- const input = 'secret=my-app-secret-key';
224
- const result = sanitizeOutput(input);
225
- expect(result).toBe('secret=[REDACTED]');
226
- });
227
-
228
- it('should redact secrets plural', () => {
229
- const input = 'secrets: "confidential-data"';
230
- const result = sanitizeOutput(input);
231
- expect(result).toBe('secret=[REDACTED]');
232
- });
233
- });
234
-
235
- describe('base64 strings', () => {
236
- it('should redact long base64-like strings', () => {
237
- const base64 = 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODk=';
238
- const input = `Encoded value: ${base64}`;
239
- const result = sanitizeOutput(input);
240
- expect(result).toBe('Encoded value: [REDACTED_BASE64]');
241
- });
242
-
243
- it('should not redact short base64 strings', () => {
244
- const input = 'Short: abc123';
245
- const result = sanitizeOutput(input);
246
- expect(result).toBe('Short: abc123');
247
- });
248
-
249
- it('should redact base64 without padding', () => {
250
- const base64 = 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY3ODkw';
251
- const result = sanitizeOutput(base64);
252
- expect(result).toBe('[REDACTED_BASE64]');
253
- });
254
-
255
- it('should redact base64 strings up to 500 chars (ReDoS prevention)', () => {
256
- // Generate a 500-char base64 string
257
- const base64 = 'A'.repeat(500);
258
- const result = sanitizeOutput(base64);
259
- expect(result).toBe('[REDACTED_BASE64]');
260
- });
261
-
262
- it('should handle base64 strings over 500 chars by matching only first 500 (ReDoS prevention)', () => {
263
- // Generate a 501-char base64 string - only first 500 chars match
264
- const base64 = 'A'.repeat(501);
265
- const result = sanitizeOutput(base64);
266
- // Pattern matches the first 500 chars, leaving 1 char behind
267
- expect(result).toBe('[REDACTED_BASE64]A');
268
- });
269
-
270
- it('should handle base64 at exactly 40 chars (minimum threshold)', () => {
271
- const base64 = 'A'.repeat(40);
272
- const result = sanitizeOutput(base64);
273
- expect(result).toBe('[REDACTED_BASE64]');
274
- });
275
-
276
- it('should NOT redact base64 strings under 40 chars', () => {
277
- const base64 = 'A'.repeat(39);
278
- const result = sanitizeOutput(base64);
279
- expect(result).toBe(base64);
280
- });
281
- });
282
-
283
- describe('multiple secrets', () => {
284
- it('should redact multiple different secret types', () => {
285
- const input = `
286
- AWS_KEY: AKIAIOSFODNN7EXAMPLE
287
- Auth: Bearer my-jwt-token
288
- password=admin123
289
- `;
290
- const result = sanitizeOutput(input);
291
- expect(result).toContain('[REDACTED_AWS_KEY]');
292
- expect(result).toContain('Bearer [REDACTED]');
293
- expect(result).toContain('password=[REDACTED]');
294
- expect(result).not.toContain('AKIAIOSFODNN7EXAMPLE');
295
- expect(result).not.toContain('my-jwt-token');
296
- expect(result).not.toContain('admin123');
297
- });
298
- });
299
-
300
- describe('private keys', () => {
301
- it('should redact RSA private keys', () => {
302
- const input = `-----BEGIN RSA PRIVATE KEY-----
303
- MIIEowIBAAKCAQEA0Z3VS5JJcds3xfn/ygWyF8PbnGy
304
- -----END RSA PRIVATE KEY-----`;
305
- const result = sanitizeOutput(input);
306
- expect(result).toBe('[REDACTED_PRIVATE_KEY]');
307
- });
308
-
309
- it('should redact OpenSSH private keys', () => {
310
- const input = `-----BEGIN OPENSSH PRIVATE KEY-----
311
- b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAA
312
- -----END OPENSSH PRIVATE KEY-----`;
313
- const result = sanitizeOutput(input);
314
- expect(result).toBe('[REDACTED_PRIVATE_KEY]');
315
- });
316
-
317
- it('should redact EC private keys', () => {
318
- const input = `-----BEGIN EC PRIVATE KEY-----
319
- MHQCAQEEICg7E4NN6YPWoU6/FXa5ON6Pt6LKBfA8WL
320
- -----END EC PRIVATE KEY-----`;
321
- const result = sanitizeOutput(input);
322
- expect(result).toBe('[REDACTED_PRIVATE_KEY]');
323
- });
324
-
325
- it('should redact generic private keys', () => {
326
- const input = `-----BEGIN PRIVATE KEY-----
327
- MIIEvgIBADANBgkqhkiG9w0BAQEFAASC
328
- -----END PRIVATE KEY-----`;
329
- const result = sanitizeOutput(input);
330
- expect(result).toBe('[REDACTED_PRIVATE_KEY]');
331
- });
332
-
333
- it('should redact private keys embedded in output', () => {
334
- const input = `Loading configuration...
335
- -----BEGIN RSA PRIVATE KEY-----
336
- MIIEowIBAAKCAQEA0Z3VS5JJcds3xfn/ygWyF8PbnGy
337
- -----END RSA PRIVATE KEY-----
338
- Done loading.`;
339
- const result = sanitizeOutput(input);
340
- expect(result).toBe(`Loading configuration...
341
- [REDACTED_PRIVATE_KEY]
342
- Done loading.`);
343
- });
344
- });
345
-
346
- describe('GCP credentials', () => {
347
- it('should redact GCP API keys', () => {
348
- const input = 'Using GCP key: AIzaSyDaGmWKa4JsXZ-HjGw7ISLn_3namBGewQe';
349
- const result = sanitizeOutput(input);
350
- expect(result).toBe('Using GCP key: [REDACTED_GCP_KEY]');
351
- });
352
-
353
- it('should redact multiple GCP API keys', () => {
354
- const input =
355
- 'Key1: AIzaSyDaGmWKa4JsXZ-HjGw7ISLn_3namBGewQe Key2: AIzaSyB-1234567890abcdefghijklmnopqrstu';
356
- const result = sanitizeOutput(input);
357
- expect(result).toBe('Key1: [REDACTED_GCP_KEY] Key2: [REDACTED_GCP_KEY]');
358
- });
359
-
360
- it('should not redact partial GCP API key patterns', () => {
361
- const input = 'AIza123'; // Too short
362
- const result = sanitizeOutput(input);
363
- expect(result).toBe('AIza123');
364
- });
365
-
366
- it('should redact GCP OAuth tokens', () => {
367
- const input = 'Authorization: ya29.a0AfH6SMBx-example-token-value_123';
368
- const result = sanitizeOutput(input);
369
- expect(result).toBe('Authorization: [REDACTED_GCP_TOKEN]');
370
- });
371
-
372
- it('should redact GCP OAuth tokens with various characters', () => {
373
- const input = 'token=ya29.Gl-abc_XYZ-123';
374
- const result = sanitizeOutput(input);
375
- expect(result).toBe('token=[REDACTED_GCP_TOKEN]');
376
- });
377
- });
378
-
379
- describe('Slack tokens', () => {
380
- it('should redact Slack bot tokens', () => {
381
- const input = 'SLACK_BOT_TOKEN=xoxb-123456789012-1234567890123-AbCdEfGhIjKlMnOpQrStUvWx';
382
- const result = sanitizeOutput(input);
383
- expect(result).toBe('SLACK_BOT_TOKEN=[REDACTED_SLACK_TOKEN]');
384
- });
385
-
386
- it('should redact Slack user tokens', () => {
387
- const input =
388
- 'Using xoxp-123456789012-123456789012-123456789012-abcdef1234567890abcdef1234567890';
389
- const result = sanitizeOutput(input);
390
- expect(result).toBe('Using [REDACTED_SLACK_TOKEN]');
391
- });
392
-
393
- it('should redact Slack app tokens', () => {
394
- const input =
395
- 'APP_TOKEN=xapp-1-A0123BCDEFG-1234567890123-abcdefghijklmnopqrstuvwxyz0123456789';
396
- const result = sanitizeOutput(input);
397
- expect(result).toBe('APP_TOKEN=[REDACTED_SLACK_TOKEN]');
398
- });
399
-
400
- it('should redact Slack webhook URLs', () => {
401
- const input =
402
- 'Webhook: https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX';
403
- const result = sanitizeOutput(input);
404
- expect(result).toBe('Webhook: https://[REDACTED_SLACK_WEBHOOK]');
405
- });
406
-
407
- it('should redact multiple different Slack token types', () => {
408
- const input = `
409
- Bot: xoxb-123-456-abc
410
- User: xoxp-789-012-def
411
- App: xapp-345-ghi
412
- `;
413
- const result = sanitizeOutput(input);
414
- expect(result).toContain('[REDACTED_SLACK_TOKEN]');
415
- expect(result).not.toContain('xoxb-');
416
- expect(result).not.toContain('xoxp-');
417
- expect(result).not.toContain('xapp-');
418
- });
419
- });
420
-
421
- describe('Stripe keys', () => {
422
- it('should redact Stripe live secret keys', () => {
423
- // 24 chars after sk_live_: 51ABC123DEF456GHI789JKLM
424
- const input = 'STRIPE_SECRET_KEY=sk_live_51ABC123DEF456GHI789JKLM';
425
- const result = sanitizeOutput(input);
426
- expect(result).toBe('STRIPE_SECRET_KEY=[REDACTED_STRIPE_KEY]');
427
- });
428
-
429
- it('should redact Stripe test secret keys', () => {
430
- // 24 chars after sk_test_: 51ABC123DEF456GHI789JKLM
431
- const input = 'Using sk_test_51ABC123DEF456GHI789JKLM for testing';
432
- const result = sanitizeOutput(input);
433
- expect(result).toBe('Using [REDACTED_STRIPE_KEY] for testing');
434
- });
435
-
436
- it('should redact Stripe live restricted keys', () => {
437
- // 24 chars after rk_live_: 51ABC123DEF456GHI789JKLM
438
- const input = 'RESTRICTED_KEY=rk_live_51ABC123DEF456GHI789JKLM';
439
- const result = sanitizeOutput(input);
440
- expect(result).toBe('RESTRICTED_KEY=[REDACTED_STRIPE_KEY]');
441
- });
442
-
443
- it('should redact Stripe test restricted keys', () => {
444
- // 24 chars after rk_test_: 51ABC123DEF456GHI789JKLM
445
- const input = 'Using rk_test_51ABC123DEF456GHI789JKLM for testing';
446
- const result = sanitizeOutput(input);
447
- expect(result).toBe('Using [REDACTED_STRIPE_KEY] for testing');
448
- });
449
-
450
- it('should not redact Stripe keys that are too short', () => {
451
- const input = 'sk_live_short'; // Less than 24 characters after prefix
452
- const result = sanitizeOutput(input);
453
- expect(result).toBe('sk_live_short');
454
- });
455
-
456
- it('should redact multiple Stripe keys', () => {
457
- // 24 chars after each prefix
458
- const input = 'Live: sk_live_51ABC123DEF456GHI789JKLM Test: sk_test_51XYZ789ABC123DEF456GHIJ';
459
- const result = sanitizeOutput(input);
460
- expect(result).toBe('Live: [REDACTED_STRIPE_KEY] Test: [REDACTED_STRIPE_KEY]');
461
- });
462
-
463
- it('should redact long Stripe keys', () => {
464
- const input = 'sk_live_51ABC123DEF456GHI789JKLmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOP';
465
- const result = sanitizeOutput(input);
466
- expect(result).toBe('[REDACTED_STRIPE_KEY]');
467
- });
468
- });
469
-
470
- describe('database connection strings', () => {
471
- it('should redact PostgreSQL connection strings', () => {
472
- const input = 'DATABASE_URL=postgres://admin:secretpass123@db.example.com:5432/mydb';
473
- const result = sanitizeOutput(input);
474
- expect(result).toBe('DATABASE_URL=[REDACTED_CONNECTION_STRING]');
475
- });
476
-
477
- it('should redact MongoDB connection strings with +srv', () => {
478
- const input = 'Connecting to mongodb+srv://user:p@ssw0rd@cluster.mongodb.net/database';
479
- const result = sanitizeOutput(input);
480
- expect(result).toBe('Connecting to [REDACTED_CONNECTION_STRING]');
481
- });
482
-
483
- it('should redact Redis connection strings', () => {
484
- const input = 'REDIS_URL=redis://default:myredispassword@redis.example.com:6379';
485
- const result = sanitizeOutput(input);
486
- expect(result).toBe('REDIS_URL=[REDACTED_CONNECTION_STRING]');
487
- });
488
-
489
- it('should redact MySQL connection strings', () => {
490
- const input = 'mysql://root:rootpassword@localhost:3306/testdb';
491
- const result = sanitizeOutput(input);
492
- expect(result).toBe('[REDACTED_CONNECTION_STRING]');
493
- });
494
-
495
- it('should redact rediss (TLS) connection strings', () => {
496
- const input = 'rediss://user:password@secure-redis.example.com:6380';
497
- const result = sanitizeOutput(input);
498
- expect(result).toBe('[REDACTED_CONNECTION_STRING]');
499
- });
500
-
501
- it('should redact connection strings with URL-encoded passwords', () => {
502
- const input = 'mongodb://user:p%40ss%23word@host/db';
503
- const result = sanitizeOutput(input);
504
- expect(result).toBe('[REDACTED_CONNECTION_STRING]');
505
- });
506
-
507
- it('should redact PostgreSQL with URL-encoded @ in password', () => {
508
- const input = 'postgres://admin:secret%40pass@db.example.com:5432/mydb';
509
- const result = sanitizeOutput(input);
510
- expect(result).toBe('[REDACTED_CONNECTION_STRING]');
511
- });
512
-
513
- it('should redact connection strings with multiple URL-encoded characters', () => {
514
- const input = 'mysql://root:p%40ss%3Dw%26rd%21@localhost:3306/testdb';
515
- const result = sanitizeOutput(input);
516
- expect(result).toBe('[REDACTED_CONNECTION_STRING]');
517
- });
518
-
519
- it('should redact MongoDB+srv with URL-encoded password', () => {
520
- const input = 'mongodb+srv://user:my%40complex%23pass@cluster.mongodb.net/database';
521
- const result = sanitizeOutput(input);
522
- expect(result).toBe('[REDACTED_CONNECTION_STRING]');
523
- });
524
- });
525
-
526
- describe('non-secret content', () => {
527
- it('should preserve normal log output', () => {
528
- const input = 'Build completed successfully in 2.5s';
529
- const result = sanitizeOutput(input);
530
- expect(result).toBe(input);
531
- });
532
-
533
- it('should preserve error messages without secrets', () => {
534
- const input = 'Error: Cannot find module "./missing-file"';
535
- const result = sanitizeOutput(input);
536
- expect(result).toBe(input);
537
- });
538
-
539
- it('should preserve stack traces', () => {
540
- const input = `
541
- Error: Test failed
542
- at Object.<anonymous> (/app/test.js:10:5)
543
- at Module._compile (internal/modules/cjs/loader.js:1085:14)
544
- `;
545
- const result = sanitizeOutput(input);
546
- expect(result).toBe(input);
547
- });
548
- });
549
- });
550
-
551
- describe('isSessionReadOnly', () => {
552
- it('should return false when no permissions provided', () => {
553
- expect(isSessionReadOnly(undefined)).toBe(false);
554
- });
555
-
556
- it('should return false for empty permissions array', () => {
557
- expect(isSessionReadOnly([])).toBe(false);
558
- });
559
-
560
- it('should return true when edit permission is denied', () => {
561
- const permissions: PermissionRule[] = [{ permission: 'edit', pattern: '*', action: 'deny' }];
562
- expect(isSessionReadOnly(permissions)).toBe(true);
563
- });
564
-
565
- it('should return true when bash permission is denied', () => {
566
- const permissions: PermissionRule[] = [{ permission: 'bash', pattern: '*', action: 'deny' }];
567
- expect(isSessionReadOnly(permissions)).toBe(true);
568
- });
569
-
570
- it('should return false when edit permission is allowed', () => {
571
- const permissions: PermissionRule[] = [{ permission: 'edit', pattern: '*', action: 'allow' }];
572
- expect(isSessionReadOnly(permissions)).toBe(false);
573
- });
574
-
575
- it('should return false when edit permission requires ask', () => {
576
- const permissions: PermissionRule[] = [{ permission: 'edit', pattern: '*', action: 'ask' }];
577
- expect(isSessionReadOnly(permissions)).toBe(false);
578
- });
579
-
580
- it('should return false for other denied permissions', () => {
581
- const permissions: PermissionRule[] = [{ permission: 'read', pattern: '*', action: 'deny' }];
582
- expect(isSessionReadOnly(permissions)).toBe(false);
583
- });
584
-
585
- it('should check all permissions and return true if any edit/bash is denied', () => {
586
- const permissions: PermissionRule[] = [
587
- { permission: 'read', pattern: '*', action: 'allow' },
588
- { permission: 'write', pattern: '*', action: 'allow' },
589
- { permission: 'edit', pattern: '*.ts', action: 'deny' },
590
- ];
591
- expect(isSessionReadOnly(permissions)).toBe(true);
592
- });
593
-
594
- it('should return true if bash is denied even if edit is allowed', () => {
595
- const permissions: PermissionRule[] = [
596
- { permission: 'edit', pattern: '*', action: 'allow' },
597
- { permission: 'bash', pattern: '*', action: 'deny' },
598
- ];
599
- expect(isSessionReadOnly(permissions)).toBe(true);
600
- });
601
- });
602
-
603
- describe('truncateOutput', () => {
604
- it('should return output unchanged if under maxLines', () => {
605
- const input = 'line1\nline2\nline3';
606
- expect(truncateOutput(input, 20)).toBe(input);
607
- });
608
-
609
- it('should return output unchanged if exactly at maxLines', () => {
610
- const lines = Array.from({ length: 20 }, (_, i) => `line${i + 1}`);
611
- const input = lines.join('\n');
612
- expect(truncateOutput(input, 20)).toBe(input);
613
- });
614
-
615
- it('should truncate to last 20 lines by default', () => {
616
- const lines = Array.from({ length: 30 }, (_, i) => `line${i + 1}`);
617
- const input = lines.join('\n');
618
- const result = truncateOutput(input);
619
-
620
- expect(result).toContain('... (10 lines omitted)');
621
- expect(result).toContain('line11');
622
- expect(result).toContain('line30');
623
- expect(result).not.toContain('line1\n');
624
- expect(result).not.toContain('line10\n');
625
- });
626
-
627
- it('should truncate to custom maxLines', () => {
628
- const lines = Array.from({ length: 15 }, (_, i) => `line${i + 1}`);
629
- const input = lines.join('\n');
630
- const result = truncateOutput(input, 5);
631
-
632
- expect(result).toContain('... (10 lines omitted)');
633
- expect(result).toContain('line11');
634
- expect(result).toContain('line15');
635
- expect(result).not.toContain('line10\n');
636
- });
637
-
638
- it('should handle single line output', () => {
639
- const input = 'single line';
640
- expect(truncateOutput(input, 20)).toBe(input);
641
- });
642
-
643
- it('should handle empty output', () => {
644
- expect(truncateOutput('', 20)).toBe('');
645
- });
646
-
647
- it('should preserve line content exactly', () => {
648
- const lines = Array.from({ length: 25 }, (_, i) => `Error at line ${i + 1}: something failed`);
649
- const input = lines.join('\n');
650
- const result = truncateOutput(input, 10);
651
-
652
- expect(result).toContain('Error at line 16: something failed');
653
- expect(result).toContain('Error at line 25: something failed');
654
- });
655
- });