-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
1512 lines (1274 loc) · 55.9 KB
/
test.js
File metadata and controls
1512 lines (1274 loc) · 55.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { readFile, writeFile, mkdir, rm } from "fs/promises";
import { join } from "path";
import { tmpdir } from "os";
import {
redactSensitive, parseLine, extractDisplayMessage,
validateShareTokenEntries, hashPassword, generatePassword,
BASE_SENSITIVE_PATTERNS, loadCustomPatterns,
listSessions, getProjectMessages, listProjects, computeProjectStats,
formatDuration, formatModel,
} from "./lib.js";
import { esc, isDiffContent, renderDiff, detectContentType } from "./public/js/utils.js";
// ── redactSensitive ──────────────────────────────────────
describe("redactSensitive", () => {
it("returns non-string input unchanged", () => {
assert.equal(redactSensitive(null), null);
assert.equal(redactSensitive(undefined), undefined);
assert.equal(redactSensitive(42), 42);
});
it("returns empty string unchanged", () => {
assert.equal(redactSensitive(""), "");
});
it("returns clean text unchanged", () => {
assert.equal(redactSensitive("hello world"), "hello world");
});
it("redacts OpenAI API keys", () => {
const input = "my key is sk-proj-abc123def456ghi789jkl012mno345";
const result = redactSensitive(input);
assert.ok(!result.includes("sk-proj-abc"));
assert.ok(result.includes("sk-***REDACTED***"));
});
it("redacts Anthropic API keys", () => {
const input = "key: sk-ant-api03-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const result = redactSensitive(input);
assert.ok(!result.includes("sk-ant-api03-"));
assert.ok(result.includes("sk-***REDACTED***"));
});
it("redacts AWS Access Key IDs", () => {
const input = "AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE";
const result = redactSensitive(input);
assert.ok(result.includes("AKIA***REDACTED***"));
assert.ok(!result.includes("AKIAIOSFODNN7EXAMPLE"));
});
it("redacts GitHub tokens", () => {
const input = "token=ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn";
const result = redactSensitive(input);
assert.ok(result.includes("gh*_***REDACTED***"));
assert.ok(!result.includes("ghp_ABCDEF"));
});
it("redacts Slack tokens", () => {
const input = "SLACK_TOKEN=xoxb-AAAAAAAAAA-BBBBBBBBBB-CCCCCCCCCCCCCCCCCCCCCCCC";
const result = redactSensitive(input);
assert.ok(result.includes("xox*_***REDACTED***"));
assert.ok(!result.includes("xoxb-"));
});
it("redacts Google API keys", () => {
// Without assignment context, the AIza-specific pattern fires
const input = "key is AIzaSyA1234567890abcdefghijklmnopqrstuv";
const result = redactSensitive(input);
assert.ok(result.includes("AIza***REDACTED***"));
assert.ok(!result.includes("AIzaSyA1234567890"));
});
it("redacts Bearer JWT tokens", () => {
const input = "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIn0.abcdef";
const result = redactSensitive(input);
assert.ok(result.includes("Bearer ***REDACTED***"));
assert.ok(!result.includes("eyJhbGciOiJIUzI1NiJ9"));
});
it("redacts generic password assignments", () => {
const input = 'password="supersecret123"';
const result = redactSensitive(input);
assert.ok(result.includes("password=***REDACTED***"));
assert.ok(!result.includes("supersecret123"));
});
it("redacts api_key assignments", () => {
const input = "api_key=myApiKey12345678";
const result = redactSensitive(input);
assert.ok(result.includes("api_key=***REDACTED***"));
assert.ok(!result.includes("myApiKey12345678"));
});
it("redacts PEM private keys", () => {
const input = "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA...\n-----END RSA PRIVATE KEY-----";
const result = redactSensitive(input);
assert.equal(result, "-----BEGIN REDACTED PRIVATE KEY-----");
});
it("redacts multiple secrets in one string", () => {
const input = "key1=sk-proj-aaaaaaaaaaaaaaaaaaaaaaaaaa and key2=sk-ant-bbbbbbbbbbbbbbbbbbbbbbbbbb";
const result = redactSensitive(input);
assert.ok(!result.includes("sk-proj-aaa"));
assert.ok(!result.includes("sk-ant-bbb"));
assert.ok(result.includes("sk-***REDACTED***"));
});
// ── Database connection strings ──
it("redacts MongoDB connection string", () => {
const result = redactSensitive("mongodb://admin:secretpass@cluster0.example.mongodb.net/mydb");
assert.ok(!result.includes("secretpass"));
assert.ok(result.includes("***REDACTED***"));
assert.ok(result.includes("@cluster0.example.mongodb.net"));
});
it("redacts MongoDB connection string with @ in password", () => {
const result = redactSensitive("mongodb://admin:S3cretP@ss@cluster0.example.mongodb.net/mydb");
assert.ok(!result.includes("S3cretP"));
assert.ok(result.includes("***REDACTED***"));
assert.ok(result.includes("@cluster0.example.mongodb.net"));
});
it("redacts MongoDB SRV connection string", () => {
const result = redactSensitive("mongodb+srv://admin:secretpass@cluster.mongodb.net/mydb");
assert.ok(!result.includes("secretpass"));
assert.ok(result.includes("@cluster.mongodb.net"));
});
it("does not redact MongoDB URL without password", () => {
const input = "mongodb://localhost:27017/mydb";
assert.equal(redactSensitive(input), input);
});
it("redacts PostgreSQL connection string", () => {
const result = redactSensitive("postgres://user:p@ssw0rd@db.example.com:5432/myapp");
assert.ok(!result.includes("p@ssw0rd"));
assert.ok(result.includes("***REDACTED***"));
assert.ok(result.includes("@db.example.com"));
});
it("redacts postgresql:// long form", () => {
const result = redactSensitive("postgresql://admin:s3cret@localhost:5432/production");
assert.ok(!result.includes("s3cret"));
assert.ok(result.includes("@localhost"));
});
it("redacts MySQL connection string", () => {
const result = redactSensitive("mysql://root:password123@localhost:3306/mydb");
assert.ok(!result.includes("password123"));
assert.ok(result.includes("@localhost"));
});
it("redacts Redis URL with password", () => {
const result = redactSensitive("redis://:s3cret_password@redis.example.com:6379");
assert.ok(!result.includes("s3cret_password"));
assert.ok(result.includes("@redis.example.com"));
});
it("does not redact Redis URL without password", () => {
const input = "redis://localhost:6379";
assert.equal(redactSensitive(input), input);
});
it("redacts JDBC connection string", () => {
const result = redactSensitive("jdbc:postgresql://user:password123@db.example.com:5432/mydb");
assert.ok(!result.includes("password123"));
assert.ok(result.includes("@db.example.com"));
});
// ── Stripe ── (prefix split to avoid GitHub push protection false positive)
it("redacts Stripe live secret key", () => {
const prefix = "sk" + "_live_"; // split to avoid secret scanner
const result = redactSensitive(prefix + "FAKEfakeFAKEfakeFAKEfakeFAKEfakeFAKE");
assert.ok(!result.includes("FAKEfake"));
assert.ok(result.includes("sk_***REDACTED***"));
});
it("redacts Stripe test secret key", () => {
const prefix = "sk" + "_test_";
const result = redactSensitive(prefix + "FAKEfakeFAKEfakeFAKEfakeFAKEfakeFAKE");
assert.ok(!result.includes("FAKEfake"));
assert.ok(result.includes("sk_***REDACTED***"));
});
it("redacts Stripe publishable key", () => {
const prefix = "pk" + "_live_";
const result = redactSensitive(prefix + "FAKEfakeFAKEfakeFAKEfakeFAKEfakeFAKE");
assert.ok(!result.includes("FAKEfake"));
assert.ok(result.includes("pk_***REDACTED***"));
});
it("redacts Stripe webhook secret", () => {
const prefix = "wh" + "sec_";
const result = redactSensitive(prefix + "FAKEfakeFAKEfakeFAKEfakeFAKEfakeFAKE");
assert.ok(!result.includes("FAKEfake"));
assert.ok(result.includes("whsec_***REDACTED***"));
});
// ── Messaging / Bot tokens ──
it("redacts Telegram bot token", () => {
const result = redactSensitive("123456789:ABCdefGHIjklMNOpqrsTUVwxyz1234567890ab");
assert.ok(!result.includes("ABCdefGHI"));
assert.ok(result.includes("***TELEGRAM_REDACTED***"));
});
it("redacts Discord bot token", () => {
const result = redactSensitive("MTIzNDU2Nzg5MDEy.MTQ1Ng.GhXYZ_abc123def456ghi789jkl012mno345");
assert.ok(!result.includes("MTIzNDU2"));
assert.ok(result.includes("***DISCORD_REDACTED***"));
});
// ── Platform tokens ──
it("redacts Vercel token", () => {
const result = redactSensitive("vrtx_abc123def456ghi789jkl012mno345pqr678");
assert.ok(!result.includes("abc123def"));
assert.ok(result.includes("vrtx_***REDACTED***"));
});
it("redacts PyPI token", () => {
const result = redactSensitive("pypi-AgEIcHlwS2VydkBtYWlsLmNvbQABCDEF123456");
assert.ok(!result.includes("AgEIcHlw"));
assert.ok(result.includes("pypi-***REDACTED***"));
});
it("redacts Cloudflare API token assignment", () => {
const result = redactSensitive("CLOUDFLARE_API_TOKEN=abc123def456ghi789jkl012mno345pqr678stu");
assert.ok(!result.includes("abc123def"));
assert.ok(result.includes("***REDACTED***"));
});
it("redacts secret= assignments", () => {
const result = redactSensitive("secret=mySecretValue123!");
assert.ok(result.includes("secret=***REDACTED***"));
assert.ok(!result.includes("mySecretValue123"));
});
it("redacts access_key assignments", () => {
const result = redactSensitive("access_key=myAccessKeyValue12345");
assert.ok(result.includes("access_key=***REDACTED***"));
assert.ok(!result.includes("myAccessKeyValue12345"));
});
it("redacts private_key assignments", () => {
const result = redactSensitive("private_key=myPrivateKeyData12345");
assert.ok(result.includes("private_key=***REDACTED***"));
assert.ok(!result.includes("myPrivateKeyData12345"));
});
it("redacts auth_token assignments", () => {
const result = redactSensitive("auth_token=myAuthTokenValue1234");
assert.ok(result.includes("auth_token=***REDACTED***"));
assert.ok(!result.includes("myAuthTokenValue1234"));
});
it("redacts TOKEN= assignments", () => {
const result = redactSensitive("TOKEN=abcdefghijklmnop12345678");
assert.ok(result.includes("TOKEN=***REDACTED***"));
assert.ok(!result.includes("abcdefghijklmnop"));
});
it("redacts token= assignments", () => {
const result = redactSensitive("token=abcdefghijklmnop12345678");
assert.ok(result.includes("token=***REDACTED***"));
assert.ok(!result.includes("abcdefghijklmnop"));
});
it("redacts AWS Secret Access Key", () => {
const result = redactSensitive("AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY");
assert.ok(result.includes("***REDACTED***"));
assert.ok(!result.includes("wJalrXUtnFEMI"));
});
it("redacts AWS SecretAccessKey with colon", () => {
const result = redactSensitive("AWSSecretAccessKey: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY");
assert.ok(result.includes("***REDACTED***"));
assert.ok(!result.includes("wJalrXUtnFEMI"));
});
it("redacts mysql2:// connection string", () => {
const result = redactSensitive("mysql2://root:password123@localhost:3306/mydb");
assert.ok(!result.includes("password123"));
assert.ok(result.includes("@localhost"));
});
it("redacts redis:// with password containing special chars", () => {
const result = redactSensitive("redis://:p@ss_w0rd@redis.example.com:6379");
assert.ok(!result.includes("p@ss_w0rd"));
assert.ok(result.includes("@redis.example.com"));
});
// ── Private keys (expanded types) ──
it("redacts OpenSSH private key", () => {
const input = "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAA\n-----END OPENSSH PRIVATE KEY-----";
assert.equal(redactSensitive(input), "-----BEGIN REDACTED PRIVATE KEY-----");
});
it("redacts EC private key", () => {
const input = "-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEIOBX\n-----END EC PRIVATE KEY-----";
assert.equal(redactSensitive(input), "-----BEGIN REDACTED PRIVATE KEY-----");
});
it("redacts DSA private key", () => {
const input = "-----BEGIN DSA PRIVATE KEY-----\nMIIBuwIBAAJBAK\n-----END DSA PRIVATE KEY-----";
assert.equal(redactSensitive(input), "-----BEGIN REDACTED PRIVATE KEY-----");
});
it("redacts PGP private key block", () => {
const input = "-----BEGIN PGP PRIVATE KEY BLOCK-----\nlQIVBGXXXXXXX\n-----END PGP PRIVATE KEY BLOCK-----";
assert.equal(redactSensitive(input), "-----BEGIN REDACTED PRIVATE KEY-----");
});
});
// ── parseLine ────────────────────────────────────────────
describe("parseLine", () => {
it("parses valid JSON lines", () => {
const result = parseLine('{"type":"user","message":{"content":"hi"}}');
assert.deepEqual(result, { type: "user", message: { content: "hi" } });
});
it("returns null for invalid JSON", () => {
assert.equal(parseLine("not json"), null);
assert.equal(parseLine(""), null);
assert.equal(parseLine("{broken"), null);
});
it("returns null for skipped types", () => {
assert.equal(parseLine('{"type":"queue-operation"}'), null);
assert.equal(parseLine('{"type":"file-history-snapshot"}'), null);
assert.equal(parseLine('{"type":"change"}'), null);
assert.equal(parseLine('{"type":"last-prompt"}'), null);
});
it("returns object for non-skipped types", () => {
const result = parseLine('{"type":"user","data":"test"}');
assert.deepEqual(result, { type: "user", data: "test" });
});
});
// ── extractDisplayMessage ────────────────────────────────
describe("extractDisplayMessage", () => {
const noRedact = (t) => t; // identity for easier assertions
it("extracts summary messages", () => {
const raw = { type: "summary", uuid: "abc", timestamp: "2026-01-01T00:00:00Z", message: { summary: "Test summary" } };
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.role, "system");
assert.equal(result.display.type, "summary");
assert.equal(result.display.text, "Test summary");
});
it("extracts simple user text messages", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "Hello" } };
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.role, "user");
assert.equal(result.display.type, "text");
assert.equal(result.display.text, "Hello");
});
it("filters out local-command-caveat messages", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "<local-command-caveat>stuff</local-command-caveat>" } };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("parses command-name messages as structured command display", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "<command-name>help</command-name>" } };
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.role, "user");
assert.deepEqual(result.display, { type: "command", name: "help", args: "" });
});
it("filters out /clear command-name messages", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "<command-name>/clear</command-name>" } };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("filters out local-command-clear messages", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "<local-command-clear>stuff</local-command-clear>" } };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("extracts user messages with tool_result blocks only → role tool_response", () => {
const raw = {
type: "user", uuid: "u2", timestamp: "t2",
message: { content: [{ type: "tool_result", tool_use_id: "tu1", content: "result text" }] },
};
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.role, "tool_response");
assert.equal(result.display.parts[0].type, "tool_result");
assert.equal(result.display.parts[0].text, "result text");
});
it("extracts user messages with mixed text+tool_result → role user", () => {
const raw = {
type: "user", uuid: "u3", timestamp: "t3",
message: { content: [
{ type: "text", text: "Here is the result" },
{ type: "tool_result", tool_use_id: "tu2", content: "output" },
] },
};
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.role, "user");
assert.equal(result.display.parts.length, 2);
});
it("extracts assistant messages with text blocks", () => {
const raw = {
type: "assistant", uuid: "a1", timestamp: "t4",
message: { model: "claude-4", content: [{ type: "text", text: "Hello!" }] },
};
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.role, "assistant");
assert.equal(result.display.parts[0].text, "Hello!");
assert.equal(result.display.model, "claude-4");
});
it("extracts assistant messages with thinking blocks", () => {
const raw = {
type: "assistant", uuid: "a2", timestamp: "t5",
message: { model: "claude-4", content: [{ type: "thinking", thinking: "hmm..." }] },
};
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.display.parts[0].type, "thinking");
assert.equal(result.display.parts[0].text, "hmm...");
});
it("extracts assistant messages with tool_use blocks", () => {
const raw = {
type: "assistant", uuid: "a3", timestamp: "t6",
message: { model: "claude-4", content: [{ type: "tool_use", name: "Read", id: "tu3", input: { path: "/foo" } }] },
};
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.display.parts[0].type, "tool_use");
assert.equal(result.display.parts[0].toolName, "Read");
assert.equal(result.display.parts[0].toolCallId, "tu3");
});
it("returns null for empty assistant content", () => {
const raw = { type: "assistant", uuid: "a4", timestamp: "t7", message: { content: [] } };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("returns null for assistant with non-array content", () => {
const raw = { type: "assistant", uuid: "a5", timestamp: "t8", message: { content: "just a string" } };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("returns null for unknown types", () => {
const raw = { type: "unknown", uuid: "x", timestamp: "t" };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("passes isSidechain and cwd through", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "hi" }, isSidechain: true, cwd: "/home/project" };
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.isSidechain, true);
assert.equal(result.cwd, "/home/project");
});
it("applies redaction to extracted text", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "my key is sk-proj-aaaaaaaaaaaaaaaaaaaaaaaaaa" } };
const result = extractDisplayMessage(raw); // uses default redactSensitive
assert.ok(!result.display.text.includes("sk-proj-aaa"));
assert.ok(result.display.text.includes("sk-***REDACTED***"));
});
it("handles tool_result with array content", () => {
const raw = {
type: "user", uuid: "u4", timestamp: "t9",
message: { content: [{ type: "tool_result", tool_use_id: "tu4", content: [
{ type: "text", text: "file contents" },
{ type: "tool_reference", tool_name: "Read" },
] }] },
};
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.display.parts[0].text, "file contents\n[Read]");
});
it("handles tool_result with non-string non-array content", () => {
const raw = {
type: "user", uuid: "u5", timestamp: "t10",
message: { content: [{ type: "tool_result", tool_use_id: "tu5", content: 42 }] },
};
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.display.parts[0].text, "42");
});
// ── Additional coverage for processUserText paths ──
it("shows non-empty local-command-stdout content", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "<local-command-stdout>build output here</local-command-stdout>" } };
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.role, "user");
assert.equal(result.display.text, "build output here");
});
it("filters out empty local-command-stdout content", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "<local-command-stdout> </local-command-stdout>" } };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("filters out 'Base directory for this skill:' messages", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "Base directory for this skill: /path/to/skill" } };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("parses command-name with command-args", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: "<command-name>help</command-name><command-args>me please</command-args>" } };
const result = extractDisplayMessage(raw, noRedact);
assert.deepEqual(result.display, { type: "command", name: "help", args: "me please" });
});
it("returns null for user with non-string non-array content", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: 42 } };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("returns null for user with null content", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: { content: null } };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("returns null for user with undefined content", () => {
const raw = { type: "user", uuid: "u1", timestamp: "t1", message: {} };
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("returns null when all array blocks are filtered", () => {
const raw = {
type: "user", uuid: "u1", timestamp: "t1",
message: { content: [
{ type: "text", text: "<local-command-caveat>skip me</local-command-caveat>" },
] },
};
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
it("handles structured command result from array text block", () => {
const raw = {
type: "user", uuid: "u1", timestamp: "t1",
message: { content: [
{ type: "text", text: "<command-name>commit</command-name><command-args>-m fix</command-args>" },
] },
};
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.role, "user");
assert.equal(result.display.parts[0].type, "command");
assert.equal(result.display.parts[0].name, "commit");
assert.equal(result.display.parts[0].args, "-m fix");
});
it("extracts summary with empty message.summary", () => {
const raw = { type: "summary", uuid: "abc", timestamp: "t1", message: {} };
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.display.text, "");
});
it("extracts summary with no message object", () => {
const raw = { type: "summary", uuid: "abc", timestamp: "t1" };
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.display.text, "");
});
it("assistant with no model defaults to empty string", () => {
const raw = {
type: "assistant", uuid: "a1", timestamp: "t1",
message: { content: [{ type: "text", text: "hi" }] },
};
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.display.model, "");
});
it("assistant skips unknown block types", () => {
const raw = {
type: "assistant", uuid: "a1", timestamp: "t1",
message: { content: [{ type: "unknown_block", data: "x" }, { type: "text", text: "actual text" }] },
};
const result = extractDisplayMessage(raw, noRedact);
assert.equal(result.display.parts.length, 1);
assert.equal(result.display.parts[0].text, "actual text");
});
it("assistant with only unknown blocks returns null", () => {
const raw = {
type: "assistant", uuid: "a1", timestamp: "t1",
message: { content: [{ type: "custom_block" }] },
};
assert.equal(extractDisplayMessage(raw, noRedact), null);
});
});
// ── loadCustomPatterns ───────────────────────────────────
describe("loadCustomPatterns", () => {
it("returns empty array when no CC_LIVE_REDACT env vars", () => {
assert.deepEqual(loadCustomPatterns({}), []);
});
it("loads plain string patterns", () => {
const patterns = loadCustomPatterns({ CC_LIVE_REDACT_1: "my-secret-word" });
assert.equal(patterns.length, 1);
assert.equal(patterns[0].replacement, "***REDACTED***");
assert.equal("my-secret-word is here".replace(patterns[0].pattern, patterns[0].replacement), "***REDACTED*** is here");
});
it("loads regex patterns with custom replacement", () => {
const patterns = loadCustomPatterns({ CC_LIVE_REDACT_1: "/foo\\d+/→[HIDDEN]" });
assert.equal(patterns.length, 1);
assert.equal(patterns[0].replacement, "[HIDDEN]");
assert.equal("foo123 bar".replace(patterns[0].pattern, patterns[0].replacement), "[HIDDEN] bar");
});
it("loads multiple patterns", () => {
const patterns = loadCustomPatterns({ CC_LIVE_REDACT_1: "secret1", CC_LIVE_REDACT_2: "secret2" });
assert.equal(patterns.length, 2);
});
it("loads regex pattern that matches literally", () => {
const patterns = loadCustomPatterns({ CC_LIVE_REDACT_1: "/FOO/→[MASKED]" });
assert.equal(patterns.length, 1);
assert.equal(patterns[0].replacement, "[MASKED]");
assert.equal("FOO bar".replace(patterns[0].pattern, patterns[0].replacement), "[MASKED] bar");
});
it("silently skips invalid regex patterns", () => {
const patterns = loadCustomPatterns({ CC_LIVE_REDACT_1: "/[invalid/→redacted" });
assert.equal(patterns.length, 0);
});
it("stops loading at first missing index", () => {
const patterns = loadCustomPatterns({ CC_LIVE_REDACT_1: "alpha", CC_LIVE_REDACT_3: "gamma" });
assert.equal(patterns.length, 1);
});
it("escapes special regex chars in plain string patterns", () => {
const patterns = loadCustomPatterns({ CC_LIVE_REDACT_1: "file.txt" });
assert.equal(patterns.length, 1);
// The dot should be escaped, so "file_txt" won't match
assert.equal("file_txt".replace(patterns[0].pattern, patterns[0].replacement), "file_txt");
// But "file.txt" should match
assert.equal("file.txt".replace(patterns[0].pattern, patterns[0].replacement), "***REDACTED***");
});
});
// ── validateShareTokenEntries ─────────────────────────────
describe("validateShareTokenEntries", () => {
it("accepts valid entries", () => {
const result = validateShareTokenEntries({
abc123: { project: "/Users/nick/foo", createdAt: 1712500000000 },
def456: { project: "/Users/nick/bar", createdAt: 1712600000000 },
});
assert.equal(result.size, 2);
assert.equal(result.get("abc123").project, "/Users/nick/foo");
assert.equal(result.get("def456").createdAt, 1712600000000);
});
it("rejects entries with missing project", () => {
const result = validateShareTokenEntries({
abc: { createdAt: 1712500000000 },
});
assert.equal(result.size, 0);
});
it("rejects entries with non-string project", () => {
const result = validateShareTokenEntries({
abc: { project: 123, createdAt: 1712500000000 },
});
assert.equal(result.size, 0);
});
it("rejects entries with missing createdAt", () => {
const result = validateShareTokenEntries({
abc: { project: "/foo" },
});
assert.equal(result.size, 0);
});
it("rejects entries with non-number createdAt", () => {
const result = validateShareTokenEntries({
abc: { project: "/foo", createdAt: "2026-04-08" },
});
assert.equal(result.size, 0);
});
it("rejects null info", () => {
const result = validateShareTokenEntries({ abc: null });
assert.equal(result.size, 0);
});
it("rejects undefined info", () => {
const result = validateShareTokenEntries({ abc: undefined });
assert.equal(result.size, 0);
});
it("handles mixed valid and invalid entries", () => {
const result = validateShareTokenEntries({
good1: { project: "/foo", createdAt: 1000 },
bad1: { project: "/foo" },
bad2: null,
good2: { project: "/bar", createdAt: 2000 },
});
assert.equal(result.size, 2);
assert.ok(result.has("good1"));
assert.ok(result.has("good2"));
assert.ok(!result.has("bad1"));
assert.ok(!result.has("bad2"));
});
it("returns empty Map for empty object", () => {
const result = validateShareTokenEntries({});
assert.equal(result.size, 0);
assert.ok(result instanceof Map);
});
it("preserves passwordHash from persisted entries", () => {
const result = validateShareTokenEntries({
abc: { project: "/foo", createdAt: 1000, passwordHash: "abc123def" },
});
assert.equal(result.get("abc").passwordHash, "abc123def");
});
it("defaults passwordHash to null when missing", () => {
const result = validateShareTokenEntries({
abc: { project: "/foo", createdAt: 1000 },
});
assert.equal(result.get("abc").passwordHash, null);
});
it("defaults passwordHash to null when falsy", () => {
const result = validateShareTokenEntries({
abc: { project: "/foo", createdAt: 1000, passwordHash: "" },
});
assert.equal(result.get("abc").passwordHash, null);
});
});
// ── hashPassword ─────────────────────────────────────────
describe("hashPassword", () => {
it("produces a 64-char hex string", () => {
const result = hashPassword("test");
assert.equal(result.length, 64);
assert.ok(/^[a-f0-9]{64}$/.test(result));
});
it("is deterministic — same input produces same output", () => {
assert.equal(hashPassword("hello"), hashPassword("hello"));
});
it("different inputs produce different outputs", () => {
assert.notEqual(hashPassword("password1"), hashPassword("password2"));
});
it("handles empty string", () => {
const result = hashPassword("");
assert.equal(result.length, 64);
});
});
// ── generatePassword ────────────────────────────────────
describe("generatePassword", () => {
it("produces a 6-character string", () => {
for (let i = 0; i < 20; i++) {
assert.equal(generatePassword().length, 6);
}
});
it("only contains valid characters", () => {
const valid = new Set("abcdefghijkmnpqrstuvwxyz23456789");
for (let i = 0; i < 20; i++) {
const pwd = generatePassword();
for (const ch of pwd) {
assert.ok(valid.has(ch), `Invalid char: ${ch}`);
}
}
});
it("excludes ambiguous characters (0, o, l, 1)", () => {
const ambiguous = new Set(["0", "o", "l", "1"]);
for (let i = 0; i < 50; i++) {
const pwd = generatePassword();
for (const ch of pwd) {
assert.ok(!ambiguous.has(ch), `Ambiguous char: ${ch}`);
}
}
});
it("generates different passwords across calls", () => {
const passwords = new Set();
for (let i = 0; i < 20; i++) passwords.add(generatePassword());
// With 6 chars from ~30 char alphabet, collisions in 20 tries are astronomically unlikely
assert.ok(passwords.size > 15, "Expected mostly unique passwords");
});
});
// ── esc ──────────────────────────────────────────────────
describe("esc", () => {
it("returns empty string for falsy input", () => {
assert.equal(esc(null), "");
assert.equal(esc(undefined), "");
assert.equal(esc(""), "");
assert.equal(esc(0), "");
assert.equal(esc(false), "");
});
it("converts non-string input to string then escapes", () => {
assert.equal(esc(42), "42");
assert.equal(esc(true), "true");
});
it("escapes &", () => {
assert.equal(esc("a&b"), "a&b");
});
it("escapes <", () => {
assert.equal(esc("a<b"), "a<b");
});
it("escapes >", () => {
assert.equal(esc("a>b"), "a>b");
});
it("escapes double quotes", () => {
assert.equal(esc('a"b'), "a"b");
});
it("escapes all special chars in one string", () => {
assert.equal(esc('<div class="x">&</div>'), "<div class="x">&</div>");
});
it("returns clean text unchanged", () => {
assert.equal(esc("hello world 123"), "hello world 123");
});
});
// ── isDiffContent ────────────────────────────────────────
describe("isDiffContent", () => {
it("returns true for lang=diff", () => {
assert.equal(isDiffContent("diff", "anything"), true);
});
it("returns false for other explicit languages", () => {
assert.equal(isDiffContent("javascript", "var x = 1"), false);
assert.equal(isDiffContent("python", "print('hi')"), false);
assert.equal(isDiffContent("bash", "echo hi"), false);
});
it("detects diff with @@ hunk headers and changes", () => {
const text = "@@ -1,3 +1,3 @@\n-old line\n+new line\n context";
assert.equal(isDiffContent(undefined, text), true);
});
it("detects diff with hunk header but only adds", () => {
const text = "@@ -1 +1,2 @@\n+added line";
assert.equal(isDiffContent(undefined, text), true);
});
it("does not detect diff with hunk but no changes", () => {
const text = "@@ -1,3 +1,3 @@\n context\n more context";
assert.equal(isDiffContent(undefined, text), false);
});
it("detects diff without hunk headers when add+del > 30%", () => {
const lines = ["-removed", "+added", "-removed2"];
assert.equal(isDiffContent(undefined, lines.join("\n")), true);
});
it("does not detect diff when only additions (no deletions)", () => {
const text = "+added line\n+another added\ncontext line";
assert.equal(isDiffContent(undefined, text), false);
});
it("does not detect diff when only deletions (no additions)", () => {
const text = "-removed line\n-another removed\ncontext line";
assert.equal(isDiffContent(undefined, text), false);
});
it("ignores +++ and --- lines as add/del", () => {
const text = "--- a/file.txt\n+++ b/file.txt\ncontext";
assert.equal(isDiffContent(undefined, text), false);
});
it("treats lang=plaintext same as no lang", () => {
const text = "@@ -1 +1 @@\n-old\n+new";
assert.equal(isDiffContent("plaintext", text), true);
});
it("returns false for normal text", () => {
assert.equal(isDiffContent(undefined, "hello world\nfoo bar"), false);
});
});
// ── renderDiff ───────────────────────────────────────────
describe("renderDiff", () => {
it("wraps output in pre.diff-block", () => {
const result = renderDiff("hello");
assert.ok(result.startsWith('<pre class="diff-block"><code class="hljs language-diff">'));
assert.ok(result.endsWith("</code></pre>"));
});
it("renders @@ lines as diff-hunk", () => {
const result = renderDiff("@@ -1,3 +1,3 @@");
assert.ok(result.includes("diff-hunk"));
assert.ok(result.includes("@@ -1,3 +1,3 @@"));
});
it("renders + lines as diff-add", () => {
const result = renderDiff("+added line");
assert.ok(result.includes("diff-add"));
assert.ok(result.includes("+added line"));
});
it("renders - lines as diff-del", () => {
const result = renderDiff("-removed line");
assert.ok(result.includes("diff-del"));
assert.ok(result.includes("-removed line"));
});
it("renders context lines without add/del/hunk class", () => {
const result = renderDiff("just a context line");
assert.ok(result.includes("diff-line"));
assert.ok(!result.includes("diff-add"));
assert.ok(!result.includes("diff-del"));
assert.ok(!result.includes("diff-hunk"));
});
it("escapes HTML in diff content", () => {
const result = renderDiff('+<script>alert("xss")</script>');
assert.ok(!result.includes("<script>"));
assert.ok(result.includes("<script>"));
});
it("renders multi-line diff with correct classes", () => {
const result = renderDiff("@@ -1 +1 @@\n-old\n+new\n ctx");
assert.ok(result.includes("diff-hunk"));
assert.ok(result.includes("diff-del"));
assert.ok(result.includes("diff-add"));
// context line gets diff-line but not diff-add/del/hunk
});
});
// ── detectContentType ────────────────────────────────────
describe("detectContentType", () => {
it("detects fenced code blocks as code", () => {
assert.equal(detectContentType("```js\nconsole.log('hi')\n```"), "code");
assert.equal(detectContentType("```\nbare code\n```"), "code");
});
it("detects valid JSON object as json", () => {
assert.equal(detectContentType('{"key": "value"}'), "json");
});
it("detects valid JSON array as json", () => {
assert.equal(detectContentType('[1, 2, 3]'), "json");
});
it("does not detect invalid JSON as json", () => {
assert.equal(detectContentType("{not valid json}"), "text");
});
it("detects mostly-indented content as code", () => {
const text = "line1\n indented1\n indented2\n indented3\n indented4";
assert.equal(detectContentType(text), "code");
});
it("returns text for normal prose", () => {
assert.equal(detectContentType("hello world\nthis is text"), "text");
});
it("returns text for short content", () => {
assert.equal(detectContentType("hi"), "text");
});
it("handles whitespace-only input", () => {
assert.equal(detectContentType(" \n \n "), "text");
});
it("does not detect code with low indentation ratio", () => {
const text = "line1\n indented\nline3\nline4\nline5";
assert.equal(detectContentType(text), "text");
});
it("requires >3 lines for indentation-based code detection", () => {
const text = " a\n b\n c";
assert.equal(detectContentType(text), "text");
});