-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.mjs
More file actions
1101 lines (948 loc) · 49.1 KB
/
tests.mjs
File metadata and controls
1101 lines (948 loc) · 49.1 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
/**
* DeepLore unit tests
* Run with: node tests.mjs
*
* Tests shared core functions (imported from core/) and base-specific logic.
*/
import {
parseFrontmatter, extractWikiLinks, cleanContent, extractTitle,
truncateToSentence, simpleHash, escapeRegex,
buildScanText, buildAiChatContext, validateSettings,
} from './core/utils.js';
import { testEntryMatch, countKeywordOccurrences, applyGating, resolveLinks, formatAndGroup } from './core/matching.js';
import { parseVaultFile, clearPrompts } from './core/pipeline.js';
import { takeIndexSnapshot, detectChanges } from './core/sync.js';
// Settings constraints (base DeepLore version)
const settingsConstraints = {
obsidianPort: { min: 1, max: 65535 },
scanDepth: { min: 0, max: 100 },
maxEntries: { min: 1, max: 100 },
maxTokensBudget: { min: 100, max: 100000 },
injectionDepth: { min: 0, max: 9999 },
maxRecursionSteps: { min: 1, max: 10 },
cacheTTL: { min: 0, max: 86400 },
reviewResponseTokens: { min: 0, max: 100000 },
syncPollingInterval: { min: 0, max: 3600 },
reinjectionCooldown: { min: 0, max: 50 },
newChatThreshold: { min: 1, max: 20 },
stripLookbackDepth: { min: 1, max: 10 },
};
// ============================================================================
// Test runner
// ============================================================================
let passed = 0;
let failed = 0;
function assert(condition, message) {
if (condition) {
passed++;
} else {
failed++;
console.error(` FAIL: ${message}`);
}
}
function assertEqual(actual, expected, message) {
if (JSON.stringify(actual) === JSON.stringify(expected)) {
passed++;
} else {
failed++;
console.error(` FAIL: ${message}`);
console.error(` expected: ${JSON.stringify(expected)}`);
console.error(` actual: ${JSON.stringify(actual)}`);
}
}
function test(name, fn) {
console.log(`\n${name}`);
fn();
}
// ============================================================================
// Helper
// ============================================================================
function makeEntry(title, opts = {}) {
return {
title,
requires: opts.requires || [],
excludes: opts.excludes || [],
constant: opts.constant || false,
seed: opts.seed || false,
bootstrap: opts.bootstrap || false,
keys: opts.keys || [],
content: opts.content || '',
summary: opts.summary || '',
priority: opts.priority || 100,
tokenEstimate: opts.tokenEstimate || 50,
scanDepth: opts.scanDepth ?? null,
excludeRecursion: opts.excludeRecursion || false,
links: opts.links || [],
resolvedLinks: opts.resolvedLinks || [],
tags: opts.tags || [],
injectionPosition: opts.injectionPosition ?? null,
injectionDepth: opts.injectionDepth ?? null,
injectionRole: opts.injectionRole ?? null,
cooldown: opts.cooldown ?? null,
warmup: opts.warmup ?? null,
refineKeys: opts.refineKeys || [],
cascadeLinks: opts.cascadeLinks || [],
filename: opts.filename || `${title}.md`,
};
}
// ============================================================================
// Tests: parseFrontmatter
// ============================================================================
test('parseFrontmatter: basic key-value pairs', () => {
const input = '---\ntitle: Test Note\npriority: 10\nenabled: true\n---\n# Body';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter.title, 'Test Note', 'should parse string value');
assertEqual(result.frontmatter.priority, 10, 'should parse number value');
assertEqual(result.frontmatter.enabled, true, 'should parse boolean true');
assertEqual(result.body, '# Body', 'should extract body');
});
test('parseFrontmatter: arrays', () => {
const input = '---\ntags:\n - lorebook\n - character\nkeys:\n - Eris\n - goddess\n---\nContent';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter.tags, ['lorebook', 'character'], 'should parse tags array');
assertEqual(result.frontmatter.keys, ['Eris', 'goddess'], 'should parse keys array');
});
test('parseFrontmatter: empty arrays', () => {
const input = '---\nkeys: []\n---\nContent';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter.keys, [], 'should parse empty array');
});
test('parseFrontmatter: boolean false', () => {
const input = '---\nenabled: false\n---\nContent';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter.enabled, false, 'should parse boolean false');
});
test('parseFrontmatter: quoted strings', () => {
const input = '---\ntitle: "Hello World"\n---\nContent';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter.title, 'Hello World', 'should strip quotes');
});
test('parseFrontmatter: no frontmatter', () => {
const input = '# Just a heading\nSome content';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter, {}, 'should return empty frontmatter');
assertEqual(result.body, input, 'should return full content as body');
});
test('parseFrontmatter: inline arrays', () => {
const input = '---\nkeys: [Wren, wren, The Bird]\n---\nContent';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter.keys, ['Wren', 'wren', 'The Bird'], 'should parse inline array');
});
test('parseFrontmatter: inline arrays with quotes', () => {
const input = '---\nkeys: ["Wren Smith", \'The Bird\']\n---\nContent';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter.keys, ['Wren Smith', 'The Bird'], 'should strip quotes from inline array items');
});
test('parseFrontmatter: inline array with spaces', () => {
const input = '---\ntags: [ lorebook , character ]\n---\nContent';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter.tags, ['lorebook', 'character'], 'should trim whitespace in inline array items');
});
test('parseFrontmatter: requires and excludes arrays', () => {
const input = '---\nrequires:\n - Eris\n - Dark Council\nexcludes:\n - Draft\n---\nContent';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter.requires, ['Eris', 'Dark Council'], 'should parse requires array');
assertEqual(result.frontmatter.excludes, ['Draft'], 'should parse excludes array');
});
test('parseFrontmatter: position, depth, role fields', () => {
const input = '---\nposition: before\ndepth: 2\nrole: user\n---\nContent';
const result = parseFrontmatter(input);
assertEqual(result.frontmatter.position, 'before', 'should parse position string');
assertEqual(result.frontmatter.depth, 2, 'should parse depth number');
assertEqual(result.frontmatter.role, 'user', 'should parse role string');
});
// ============================================================================
// Tests: cleanContent
// ============================================================================
test('cleanContent: strips image embeds', () => {
assertEqual(cleanContent('Before ![[image.png]] after'), 'Before after', 'should strip wiki image embeds');
assertEqual(cleanContent('Before  after'), 'Before after', 'should strip markdown image embeds');
});
test('cleanContent: converts wiki links', () => {
assertEqual(cleanContent('See [[Target Page]]'), 'See Target Page', 'should convert simple wiki links');
assertEqual(cleanContent('See [[Target|Display Text]]'), 'See Display Text', 'should convert aliased wiki links');
});
test('cleanContent: collapses blank lines', () => {
assertEqual(cleanContent('Line 1\n\n\n\n\nLine 2'), 'Line 1\n\nLine 2', 'should collapse 5 newlines to 2');
});
test('cleanContent: strips deeplore-exclude regions', () => {
assertEqual(
cleanContent('Before\n%%deeplore-exclude%%\nHidden stuff\n%%/deeplore-exclude%%\nAfter'),
'Before\n\nAfter',
'should strip deeplore-exclude region and contents',
);
assertEqual(
cleanContent('Start %%deeplore-exclude%%secret%%/deeplore-exclude%% end'),
'Start end',
'should strip inline deeplore-exclude',
);
});
test('cleanContent: strips Obsidian %% comment blocks', () => {
assertEqual(cleanContent('Before %%inline comment%% after'), 'Before after',
'should strip inline %% blocks');
assertEqual(
cleanContent('Before\n%%aat-inline-event\nstart-date: 2025\ntimelines: [test]\n%%\nAfter'),
'Before\n\nAfter',
'should strip multiline %% blocks',
);
assertEqual(cleanContent('%%aat-event-end-of-body%%'), '',
'should strip standalone %% markers');
});
test('cleanContent: strips HTML div tags', () => {
assertEqual(
cleanContent('<div class="meta-block">[Species: vampire]</div>'),
'[Species: vampire]',
'should strip div tags but keep content',
);
assertEqual(cleanContent('Text <div>inner</div> more'), 'Text inner more',
'should strip plain div tags');
});
test('cleanContent: strips H1 heading', () => {
assertEqual(cleanContent('# Eris\nContent here'), 'Content here',
'should strip H1 heading');
assertEqual(cleanContent('## Subheading\nContent'), '## Subheading\nContent',
'should NOT strip H2 headings');
});
// ============================================================================
// Tests: extractTitle
// ============================================================================
test('extractTitle: from H1', () => {
assertEqual(extractTitle('# My Title\nContent', 'test.md'), 'My Title', 'should extract H1');
});
test('extractTitle: from filename', () => {
assertEqual(extractTitle('No heading here', 'folder/My Note.md'), 'My Note', 'should fall back to filename');
});
// ============================================================================
// Tests: extractWikiLinks
// ============================================================================
test('extractWikiLinks: basic links', () => {
const links = extractWikiLinks('See [[Eris]] and [[Dark Council]].');
assertEqual(links.length, 2, 'should find two links');
assert(links.includes('Eris'), 'should include Eris');
assert(links.includes('Dark Council'), 'should include Dark Council');
});
test('extractWikiLinks: aliased links', () => {
const links = extractWikiLinks('She joined [[Dark Council|the council]] recently.');
assertEqual(links.length, 1, 'should find one link');
assertEqual(links[0], 'Dark Council', 'should extract target, not display text');
});
test('extractWikiLinks: ignores image embeds', () => {
const links = extractWikiLinks('![[portrait.png]] and [[Eris]] and ![[map.jpg]]');
assertEqual(links.length, 1, 'should skip image embeds');
assertEqual(links[0], 'Eris', 'should only include non-image link');
});
test('extractWikiLinks: deduplicates', () => {
const links = extractWikiLinks('[[Eris]] met [[Eris]] at the [[Temple]].');
assertEqual(links.length, 2, 'should deduplicate Eris');
assert(links.includes('Eris'), 'should include Eris once');
assert(links.includes('Temple'), 'should include Temple');
});
test('extractWikiLinks: empty body', () => {
assertEqual(extractWikiLinks('No links here.').length, 0, 'should return empty for no links');
assertEqual(extractWikiLinks('').length, 0, 'should handle empty string');
});
test('extractWikiLinks: trims whitespace in link targets', () => {
const links = extractWikiLinks('See [[ Eris ]] here.');
assertEqual(links[0], 'Eris', 'should trim whitespace from link target');
});
// ============================================================================
// Tests: testEntryMatch
// ============================================================================
test('testEntryMatch: case insensitive substring', () => {
const entry = { keys: ['Eris'] };
const settings = { caseSensitive: false, matchWholeWords: false };
assertEqual(testEntryMatch(entry, 'I met eris today', settings), 'Eris', 'should match case-insensitively');
assertEqual(testEntryMatch(entry, 'No match here', settings), null, 'should return null for no match');
});
test('testEntryMatch: case sensitive', () => {
const entry = { keys: ['Eris'] };
const settings = { caseSensitive: true, matchWholeWords: false };
assertEqual(testEntryMatch(entry, 'I met eris today', settings), null, 'should not match wrong case');
assertEqual(testEntryMatch(entry, 'I met Eris today', settings), 'Eris', 'should match exact case');
});
test('testEntryMatch: whole words', () => {
const entry = { keys: ['war'] };
const settings = { caseSensitive: false, matchWholeWords: true };
assertEqual(testEntryMatch(entry, 'The warning was clear', settings), null, 'should not match partial word');
assertEqual(testEntryMatch(entry, 'The war began', settings), 'war', 'should match whole word');
});
test('testEntryMatch: empty keys', () => {
const entry = { keys: [] };
const settings = { caseSensitive: false, matchWholeWords: false };
assertEqual(testEntryMatch(entry, 'any text', settings), null, 'should return null for empty keys');
});
test('testEntryMatch: regex special chars in key', () => {
const entry = { keys: ['C++ programming'] };
const settings = { caseSensitive: false, matchWholeWords: false };
assertEqual(testEntryMatch(entry, 'I love c++ programming', settings), 'C++ programming', 'should handle regex special chars');
});
// ============================================================================
// Tests: truncateToSentence
// ============================================================================
test('truncateToSentence: short text unchanged', () => {
assertEqual(truncateToSentence('Hello world.', 200), 'Hello world.', 'should not truncate short text');
});
test('truncateToSentence: cuts at sentence boundary', () => {
const text = 'First sentence. Second sentence. Third sentence that is very long and keeps going on.';
const result = truncateToSentence(text, 40);
assertEqual(result, 'First sentence. Second sentence.', 'should cut at last sentence boundary');
});
test('truncateToSentence: falls back to ellipsis', () => {
const text = 'This is a single very long sentence without any periods that just keeps going on and on and on and on';
const result = truncateToSentence(text, 30);
assertEqual(result, 'This is a single very long sen...', 'should add ellipsis when no sentence boundary');
});
test('truncateToSentence: respects exclamation marks', () => {
const text = 'Watch out! This is dangerous. And more text that is very long and keeps going.';
const result = truncateToSentence(text, 35);
assertEqual(result, 'Watch out! This is dangerous.', 'should cut at exclamation/period');
});
// ============================================================================
// Tests: simpleHash
// ============================================================================
test('simpleHash: deterministic', () => {
const hash1 = simpleHash('test string');
const hash2 = simpleHash('test string');
assertEqual(hash1, hash2, 'same input should produce same hash');
});
test('simpleHash: different for different inputs', () => {
const hash1 = simpleHash('hello');
const hash2 = simpleHash('world');
assert(hash1 !== hash2, 'different inputs should produce different hashes');
});
// ============================================================================
// Tests: validateSettings
// ============================================================================
test('validateSettings: clamps values', () => {
const settings = { obsidianPort: 99999, scanDepth: -5, cacheTTL: 100000 };
validateSettings(settings, settingsConstraints);
assertEqual(settings.obsidianPort, 65535, 'should clamp port to max');
assertEqual(settings.scanDepth, 0, 'should clamp scanDepth to min');
assertEqual(settings.cacheTTL, 86400, 'should clamp cacheTTL to max');
});
test('validateSettings: rounds floats', () => {
const settings = { scanDepth: 4.7 };
validateSettings(settings, settingsConstraints);
assertEqual(settings.scanDepth, 5, 'should round float to integer');
});
test('validateSettings: trims lorebook tag', () => {
const settings = { lorebookTag: ' custom-tag ' };
validateSettings(settings, settingsConstraints);
assertEqual(settings.lorebookTag, 'custom-tag', 'should trim whitespace');
});
test('validateSettings: defaults empty lorebook tag', () => {
const settings = { lorebookTag: ' ' };
validateSettings(settings, settingsConstraints);
assertEqual(settings.lorebookTag, 'lorebook', 'should default empty tag to lorebook');
});
test('validateSettings: clamps reinjection cooldown', () => {
const settings = { reinjectionCooldown: 100 };
validateSettings(settings, settingsConstraints);
assertEqual(settings.reinjectionCooldown, 50, 'should clamp reinjection cooldown to max');
});
test('validateSettings: clamps sync polling interval', () => {
const settings = { syncPollingInterval: 5000 };
validateSettings(settings, settingsConstraints);
assertEqual(settings.syncPollingInterval, 3600, 'should clamp sync interval to max');
});
// ============================================================================
// Tests: buildScanText / buildAiChatContext
// ============================================================================
test('buildAiChatContext: annotates roles', () => {
const chat = [
{ name: 'Alice', is_user: true, mes: 'Hello' },
{ name: 'Bob', is_user: false, mes: 'Hi there' },
];
const result = buildAiChatContext(chat, 10);
assert(result.includes('Alice (user): Hello'), 'should mark Alice as user');
assert(result.includes('Bob (character): Hi there'), 'should mark Bob as character');
});
test('buildAiChatContext: respects depth', () => {
const chat = [
{ name: 'A', is_user: true, mes: 'msg1' },
{ name: 'B', is_user: false, mes: 'msg2' },
{ name: 'C', is_user: true, mes: 'msg3' },
];
const result = buildAiChatContext(chat, 2);
assert(!result.includes('msg1'), 'should exclude messages beyond depth');
assert(result.includes('msg2'), 'should include recent messages');
assert(result.includes('msg3'), 'should include most recent message');
});
test('buildAiChatContext: handles missing name', () => {
const chat = [{ is_user: false, mes: 'Hello' }];
const result = buildAiChatContext(chat, 10);
assert(result.includes('Unknown (character)'), 'should use Unknown for missing name');
});
test('buildScanText: depth 0 returns empty string', () => {
const chat = [
{ name: 'Alice', mes: 'Hello world' },
{ name: 'Bob', mes: 'Greetings' },
];
assertEqual(buildScanText(chat, 0), '', 'should return empty string for depth 0');
});
test('buildScanText: depth 1 returns last message', () => {
const chat = [
{ name: 'Alice', mes: 'First' },
{ name: 'Bob', mes: 'Second' },
];
const result = buildScanText(chat, 1);
assert(!result.includes('First'), 'should exclude first message');
assert(result.includes('Second'), 'should include last message');
});
test('buildAiChatContext: depth 0 returns empty string', () => {
const chat = [{ name: 'Alice', is_user: true, mes: 'Hello' }];
assertEqual(buildAiChatContext(chat, 0), '', 'should return empty string for depth 0');
});
// ============================================================================
// Tests: applyGating
// ============================================================================
test('applyGating: all requires present passes', () => {
const entries = [
makeEntry('Eris'),
makeEntry('Dark Council'),
makeEntry('Secret', { requires: ['Eris', 'Dark Council'] }),
];
const result = applyGating(entries);
assertEqual(result.length, 3, 'all three should pass');
});
test('applyGating: missing requires removes entry', () => {
const entries = [
makeEntry('Eris'),
makeEntry('Secret', { requires: ['Eris', 'Dark Council'] }),
];
const result = applyGating(entries);
assertEqual(result.length, 1, 'Secret should be removed (Dark Council missing)');
assertEqual(result[0].title, 'Eris', 'Eris should remain');
});
test('applyGating: excludes blocks entry', () => {
const entries = [
makeEntry('Eris'),
makeEntry('Draft Notes'),
makeEntry('Secret', { excludes: ['Draft Notes'] }),
];
const result = applyGating(entries);
assertEqual(result.length, 2, 'Secret should be excluded');
assert(!result.find(e => e.title === 'Secret'), 'Secret should not be in results');
});
test('applyGating: cascading removal', () => {
const entries = [
makeEntry('A', { requires: ['B'] }),
makeEntry('B', { requires: ['C'] }),
];
const result = applyGating(entries);
assertEqual(result.length, 0, 'both should be removed by cascading');
});
test('applyGating: empty requires/excludes passes', () => {
const entries = [makeEntry('Eris'), makeEntry('Plain Entry')];
const result = applyGating(entries);
assertEqual(result.length, 2, 'entries without gating rules should pass');
});
test('applyGating: case insensitive title matching', () => {
const entries = [
makeEntry('Eris'),
makeEntry('Secret', { requires: ['eris'] }),
];
const result = applyGating(entries);
assertEqual(result.length, 2, 'should match case-insensitively');
});
test('applyGating: constants still subject to gating', () => {
const entries = [
makeEntry('Draft Notes'),
makeEntry('Always Entry', { constant: true, excludes: ['Draft Notes'] }),
];
const result = applyGating(entries);
assertEqual(result.length, 1, 'constant entry should still be gated');
assertEqual(result[0].title, 'Draft Notes', 'Draft Notes should remain');
});
// ============================================================================
// Tests: formatAndGroup
// ============================================================================
const defaultTestSettings = {
injectionPosition: 1,
injectionDepth: 4,
injectionRole: 0,
injectionTemplate: '<{{title}}>\n{{content}}\n</{{title}}>',
unlimitedEntries: true,
unlimitedBudget: true,
maxEntries: 10,
maxTokensBudget: 2048,
};
test('formatAndGroup: single group when no overrides', () => {
const entries = [
makeEntry('Eris', { content: 'A goddess' }),
makeEntry('Temple', { content: 'A sacred place' }),
];
const result = formatAndGroup(entries, defaultTestSettings, 'deeplore_');
assertEqual(result.groups.length, 1, 'should produce one group');
assertEqual(result.groups[0].tag, 'deeplore_p1_d4_r0', 'should use global settings in tag');
assertEqual(result.count, 2, 'should count 2 entries');
});
test('formatAndGroup: multiple groups with different positions', () => {
const entries = [
makeEntry('World Lore', { content: 'Background', injectionPosition: 2 }),
makeEntry('Eris', { content: 'A goddess' }),
makeEntry('Dialogue Hint', { content: 'Speak softly', injectionPosition: 1, injectionDepth: 0, injectionRole: 1 }),
];
const result = formatAndGroup(entries, defaultTestSettings, 'deeplore_');
assertEqual(result.groups.length, 3, 'should produce three groups');
assert(result.groups.some(g => g.tag === 'deeplore_p2_d4_r0'), 'should have before_prompt group');
assert(result.groups.some(g => g.tag === 'deeplore_p1_d4_r0'), 'should have default in_chat group');
assert(result.groups.some(g => g.tag === 'deeplore_p1_d0_r1'), 'should have custom in_chat group');
});
test('formatAndGroup: budget applied globally across groups', () => {
const entries = [
makeEntry('A', { content: 'Content A', tokenEstimate: 500, injectionPosition: 2 }),
makeEntry('B', { content: 'Content B', tokenEstimate: 500 }),
makeEntry('C', { content: 'Content C', tokenEstimate: 500 }),
];
const settings = { ...defaultTestSettings, unlimitedBudget: false, maxTokensBudget: 1200 };
const result = formatAndGroup(entries, settings, 'deeplore_');
assertEqual(result.count, 2, 'should only accept 2 entries within budget');
assertEqual(result.totalTokens, 1000, 'total should be 1000');
});
test('formatAndGroup: per-entry depth override', () => {
const entries = [
makeEntry('Near', { content: 'Close to action', injectionDepth: 1 }),
makeEntry('Far', { content: 'Background info', injectionDepth: 8 }),
];
const result = formatAndGroup(entries, defaultTestSettings, 'deeplore_');
assertEqual(result.groups.length, 2, 'should produce two groups at different depths');
assert(result.groups.some(g => g.depth === 1), 'should have depth 1 group');
assert(result.groups.some(g => g.depth === 8), 'should have depth 8 group');
});
test('formatAndGroup: fallback to global settings', () => {
const entries = [makeEntry('Test', { content: 'Body' })];
const result = formatAndGroup(entries, defaultTestSettings, 'deeplore_');
assertEqual(result.groups[0].position, 1, 'should use global position');
assertEqual(result.groups[0].depth, 4, 'should use global depth');
assertEqual(result.groups[0].role, 0, 'should use global role');
});
// ============================================================================
// Tests: detectChanges
// ============================================================================
function makeSnapshot(entries) {
const snapshot = {
contentHashes: new Map(),
titleMap: new Map(),
keyMap: new Map(),
timestamp: Date.now(),
};
for (const e of entries) {
snapshot.contentHashes.set(e.filename, simpleHash(e.content));
snapshot.titleMap.set(e.filename, e.title);
snapshot.keyMap.set(e.filename, JSON.stringify(e.keys || []));
}
return snapshot;
}
test('detectChanges: no previous snapshot returns empty', () => {
const newSnap = makeSnapshot([{ filename: 'a.md', title: 'A', content: 'hello', keys: [] }]);
const changes = detectChanges(null, newSnap);
assertEqual(changes.hasChanges, false, 'should report no changes');
});
test('detectChanges: detects new entries', () => {
const old = makeSnapshot([{ filename: 'a.md', title: 'A', content: 'hello', keys: [] }]);
const now = makeSnapshot([
{ filename: 'a.md', title: 'A', content: 'hello', keys: [] },
{ filename: 'b.md', title: 'B', content: 'world', keys: [] },
]);
const changes = detectChanges(old, now);
assertEqual(changes.added, ['B'], 'should detect B as new');
assertEqual(changes.hasChanges, true, 'should report changes');
});
test('detectChanges: detects removed entries', () => {
const old = makeSnapshot([
{ filename: 'a.md', title: 'A', content: 'hello', keys: [] },
{ filename: 'b.md', title: 'B', content: 'world', keys: [] },
]);
const now = makeSnapshot([{ filename: 'a.md', title: 'A', content: 'hello', keys: [] }]);
const changes = detectChanges(old, now);
assertEqual(changes.removed, ['B'], 'should detect B as removed');
});
test('detectChanges: detects modified content', () => {
const old = makeSnapshot([{ filename: 'a.md', title: 'A', content: 'hello', keys: [] }]);
const now = makeSnapshot([{ filename: 'a.md', title: 'A', content: 'hello changed', keys: [] }]);
const changes = detectChanges(old, now);
assertEqual(changes.modified, ['A'], 'should detect A as modified');
});
test('detectChanges: detects keyword changes', () => {
const old = makeSnapshot([{ filename: 'a.md', title: 'A', content: 'same', keys: ['foo'] }]);
const now = makeSnapshot([{ filename: 'a.md', title: 'A', content: 'same', keys: ['foo', 'bar'] }]);
const changes = detectChanges(old, now);
assertEqual(changes.keysChanged, ['A'], 'should detect keyword change');
assertEqual(changes.modified.length, 0, 'should not appear in modified (content unchanged)');
});
test('detectChanges: no changes returns hasChanges false', () => {
const old = makeSnapshot([{ filename: 'a.md', title: 'A', content: 'hello', keys: ['x'] }]);
const now = makeSnapshot([{ filename: 'a.md', title: 'A', content: 'hello', keys: ['x'] }]);
const changes = detectChanges(old, now);
assertEqual(changes.hasChanges, false, 'should report no changes');
});
// ============================================================================
// Tests: parseVaultFile
// ============================================================================
test('parseVaultFile: parses valid lorebook entry', () => {
const file = {
filename: 'Characters/Eris.md',
content: '---\ntags:\n - lorebook\nkeys:\n - Eris\n - goddess\npriority: 20\nsummary: "A powerful goddess"\n---\n# Eris\n\nShe is a goddess.',
};
const tagConfig = { lorebookTag: 'lorebook', constantTag: 'lorebook-always', neverInsertTag: 'lorebook-never' };
const entry = parseVaultFile(file, tagConfig);
assert(entry !== null, 'should return an entry');
assertEqual(entry.title, 'Eris', 'should extract title');
assertEqual(entry.keys, ['Eris', 'goddess'], 'should extract keys');
assertEqual(entry.priority, 20, 'should extract priority');
assertEqual(entry.summary, 'A powerful goddess', 'should extract summary');
assertEqual(entry.constant, false, 'should not be constant');
});
test('parseVaultFile: skips non-lorebook files', () => {
const file = { filename: 'notes.md', content: '---\ntags:\n - misc\n---\nContent' };
const tagConfig = { lorebookTag: 'lorebook', constantTag: '', neverInsertTag: '' };
assertEqual(parseVaultFile(file, tagConfig), null, 'should return null for non-lorebook');
});
test('parseVaultFile: skips disabled entries', () => {
const file = { filename: 'test.md', content: '---\ntags:\n - lorebook\nenabled: false\n---\nContent' };
const tagConfig = { lorebookTag: 'lorebook', constantTag: '', neverInsertTag: '' };
assertEqual(parseVaultFile(file, tagConfig), null, 'should return null for disabled');
});
test('parseVaultFile: skips never-insert tag', () => {
const file = { filename: 'test.md', content: '---\ntags:\n - lorebook\n - lorebook-never\n---\nContent' };
const tagConfig = { lorebookTag: 'lorebook', constantTag: '', neverInsertTag: 'lorebook-never' };
assertEqual(parseVaultFile(file, tagConfig), null, 'should return null for never-insert');
});
test('parseVaultFile: detects constant tag', () => {
const file = { filename: 'test.md', content: '---\ntags:\n - lorebook\n - lorebook-always\n---\nContent' };
const tagConfig = { lorebookTag: 'lorebook', constantTag: 'lorebook-always', neverInsertTag: '' };
const entry = parseVaultFile(file, tagConfig);
assertEqual(entry.constant, true, 'should detect constant from tag');
});
// ============================================================================
// Tests: clearPrompts
// ============================================================================
test('clearPrompts: removes matching prompts', () => {
const prompts = {
'deeplore_p1_d4_r0': 'test',
'deeplore': 'test',
'other_prompt': 'keep',
};
clearPrompts(prompts, 'deeplore_', 'deeplore');
assertEqual(Object.keys(prompts), ['other_prompt'], 'should remove deeplore prompts only');
});
// ============================================================================
// Tests: resolveLinks
// ============================================================================
test('resolveLinks: resolves matching titles', () => {
const index = [
makeEntry('Eris', { links: ['Dark Council', 'Unknown Page'] }),
makeEntry('Dark Council', { links: ['Eris'] }),
];
resolveLinks(index);
assertEqual(index[0].resolvedLinks, ['Dark Council'], 'should resolve matching link');
assertEqual(index[1].resolvedLinks, ['Eris'], 'should resolve reverse link');
});
// ============================================================================
// Tests: countKeywordOccurrences
// ============================================================================
test('countKeywordOccurrences: counts multiple hits', () => {
const entry = { keys: ['Eris'] };
const settings = { caseSensitive: false, matchWholeWords: false };
assertEqual(countKeywordOccurrences(entry, 'Eris met eris and ERIS', settings), 3, 'should count 3 occurrences');
});
test('countKeywordOccurrences: whole words', () => {
const entry = { keys: ['war'] };
const settings = { caseSensitive: false, matchWholeWords: true };
assertEqual(countKeywordOccurrences(entry, 'The war and warning of warfare', settings), 1, 'should count only whole word matches');
});
// ============================================================================
// Tests: takeIndexSnapshot
// ============================================================================
test('takeIndexSnapshot: creates snapshot from vault index', () => {
const index = [
makeEntry('Eris', { content: 'A goddess', keys: ['Eris'] }),
makeEntry('Temple', { content: 'Sacred', keys: ['temple'] }),
];
const snapshot = takeIndexSnapshot(index);
assertEqual(snapshot.contentHashes.size, 2, 'should hash both entries');
assertEqual(snapshot.titleMap.get('Eris.md'), 'Eris', 'should map filename to title');
});
// ============================================================================
// Tests: parseVaultFile seed/bootstrap tags
// ============================================================================
test('parseVaultFile: detects seed tag', () => {
const file = { filename: 'test.md', content: '---\ntags:\n - lorebook\n - lorebook-seed\nkeys:\n - test\n---\n# Test\nContent' };
const tagConfig = { lorebookTag: 'lorebook', constantTag: 'lorebook-always', neverInsertTag: 'lorebook-never', seedTag: 'lorebook-seed', bootstrapTag: 'lorebook-bootstrap' };
const entry = parseVaultFile(file, tagConfig);
assert(entry !== null, 'should return an entry');
assertEqual(entry.seed, true, 'should detect seed from tag');
assertEqual(entry.bootstrap, false, 'should not be bootstrap');
});
test('parseVaultFile: detects bootstrap tag', () => {
const file = { filename: 'test.md', content: '---\ntags:\n - lorebook\n - lorebook-bootstrap\nkeys:\n - test\n---\n# Test\nContent' };
const tagConfig = { lorebookTag: 'lorebook', constantTag: 'lorebook-always', neverInsertTag: 'lorebook-never', seedTag: 'lorebook-seed', bootstrapTag: 'lorebook-bootstrap' };
const entry = parseVaultFile(file, tagConfig);
assert(entry !== null, 'should return an entry');
assertEqual(entry.bootstrap, true, 'should detect bootstrap from tag');
assertEqual(entry.seed, false, 'should not be seed');
});
test('parseVaultFile: no seed/bootstrap without tags', () => {
const file = { filename: 'test.md', content: '---\ntags:\n - lorebook\nkeys:\n - test\n---\n# Test\nContent' };
const tagConfig = { lorebookTag: 'lorebook', constantTag: 'lorebook-always', neverInsertTag: 'lorebook-never', seedTag: 'lorebook-seed', bootstrapTag: 'lorebook-bootstrap' };
const entry = parseVaultFile(file, tagConfig);
assertEqual(entry.seed, false, 'should not be seed without tag');
assertEqual(entry.bootstrap, false, 'should not be bootstrap without tag');
});
test('parseVaultFile: seed/bootstrap false when tags not in config', () => {
const file = { filename: 'test.md', content: '---\ntags:\n - lorebook\n - lorebook-seed\nkeys:\n - test\n---\n# Test\nContent' };
const tagConfig = { lorebookTag: 'lorebook', constantTag: 'lorebook-always', neverInsertTag: 'lorebook-never' };
const entry = parseVaultFile(file, tagConfig);
assertEqual(entry.seed, false, 'seed should be false when seedTag not configured');
});
// ============================================================================
// Tests: validateSettings newChatThreshold
// ============================================================================
test('validateSettings: clamps newChatThreshold', () => {
const settings = { newChatThreshold: 100 };
const constraints = { newChatThreshold: { min: 1, max: 20 } };
validateSettings(settings, constraints);
assertEqual(settings.newChatThreshold, 20, 'should clamp newChatThreshold to max');
});
// ============================================================================
// Tests: testEntryMatch with refineKeys
// ============================================================================
test('testEntryMatch: refine keys AND_ANY mode', () => {
const entry = { keys: ['Eris'], refineKeys: ['goddess', 'divine'] };
const settings = { caseSensitive: false, matchWholeWords: false };
assertEqual(testEntryMatch(entry, 'I met eris today', settings), null, 'should NOT match when no refine key present');
assertEqual(testEntryMatch(entry, 'eris the goddess', settings), 'Eris', 'should match when refine key present');
assertEqual(testEntryMatch(entry, 'eris is divine', settings), 'Eris', 'should match with second refine key');
});
test('testEntryMatch: empty refine keys passes through', () => {
const entry = { keys: ['Eris'], refineKeys: [] };
const settings = { caseSensitive: false, matchWholeWords: false };
assertEqual(testEntryMatch(entry, 'I met eris today', settings), 'Eris', 'should match normally with empty refineKeys');
});
test('testEntryMatch: no refineKeys property passes through', () => {
const entry = { keys: ['Eris'] };
const settings = { caseSensitive: false, matchWholeWords: false };
assertEqual(testEntryMatch(entry, 'I met eris today', settings), 'Eris', 'should match when refineKeys is undefined');
});
// ============================================================================
// Tests: parseVaultFile advanced frontmatter
// ============================================================================
test('parseVaultFile: parses refine_keys', () => {
const file = {
filename: 'test.md',
content: '---\ntags:\n - lorebook\nkeys:\n - Eris\nrefine_keys:\n - goddess\n - divine\n---\n# Eris\nContent',
};
const tagConfig = { lorebookTag: 'lorebook', constantTag: '', neverInsertTag: '' };
const entry = parseVaultFile(file, tagConfig);
assert(entry !== null, 'should return an entry');
assertEqual(entry.refineKeys, ['goddess', 'divine'], 'should parse refine_keys');
});
test('parseVaultFile: parses cascade_links', () => {
const file = {
filename: 'test.md',
content: '---\ntags:\n - lorebook\nkeys:\n - Council\ncascade_links:\n - Eris\n - Temple\n---\n# Dark Council\nContent',
};
const tagConfig = { lorebookTag: 'lorebook', constantTag: '', neverInsertTag: '' };
const entry = parseVaultFile(file, tagConfig);
assert(entry !== null, 'should return an entry');
assertEqual(entry.cascadeLinks, ['Eris', 'Temple'], 'should parse cascade_links');
});
test('parseVaultFile: parses cooldown and warmup', () => {
const file = {
filename: 'test.md',
content: '---\ntags:\n - lorebook\nkeys:\n - test\ncooldown: 3\nwarmup: 2\n---\n# Test\nContent',
};
const tagConfig = { lorebookTag: 'lorebook', constantTag: '', neverInsertTag: '' };
const entry = parseVaultFile(file, tagConfig);
assert(entry !== null, 'should return an entry');
assertEqual(entry.cooldown, 3, 'should parse cooldown');
assertEqual(entry.warmup, 2, 'should parse warmup');
});
test('parseVaultFile: parses injection overrides', () => {
const file = {
filename: 'test.md',
content: '---\ntags:\n - lorebook\nkeys:\n - test\nposition: before\ndepth: 2\nrole: user\n---\n# Test\nContent',
};
const tagConfig = { lorebookTag: 'lorebook', constantTag: '', neverInsertTag: '' };
const entry = parseVaultFile(file, tagConfig);
assert(entry !== null, 'should return an entry');
assertEqual(entry.injectionPosition, 2, 'should map "before" to position 2');
assertEqual(entry.injectionDepth, 2, 'should parse depth');
assertEqual(entry.injectionRole, 1, 'should map "user" to role 1');
});
test('parseVaultFile: strips lorebook tag from entry tags', () => {
const file = {
filename: 'test.md',
content: '---\ntags:\n - lorebook\n - character\n - important\nkeys:\n - test\n---\n# Test\nContent',
};
const tagConfig = { lorebookTag: 'lorebook', constantTag: '', neverInsertTag: '' };
const entry = parseVaultFile(file, tagConfig);
assert(entry !== null, 'should return an entry');
assert(!entry.tags.includes('lorebook'), 'should not include lorebook tag');
assert(entry.tags.includes('character'), 'should include character tag');
assert(entry.tags.includes('important'), 'should include important tag');
});
// ============================================================================
// Tests: countKeywordOccurrences additional
// ============================================================================
test('countKeywordOccurrences: case sensitive', () => {
const entry = { keys: ['Eris'] };
const settings = { caseSensitive: true, matchWholeWords: false };
assertEqual(countKeywordOccurrences(entry, 'Eris met eris and Eris', settings), 2, 'should count only exact case matches');
});
test('countKeywordOccurrences: multiple keys', () => {
const entry = { keys: ['war', 'battle'] };
const settings = { caseSensitive: false, matchWholeWords: false };
assertEqual(countKeywordOccurrences(entry, 'The war led to a battle in the war zone', settings), 3, 'should count across all keys');
});
// ============================================================================
// Tests: formatAndGroup max entries limit
// ============================================================================
test('formatAndGroup: respects max entries limit', () => {
const entries = [
makeEntry('A', { content: 'Content A', tokenEstimate: 50 }),
makeEntry('B', { content: 'Content B', tokenEstimate: 50 }),
makeEntry('C', { content: 'Content C', tokenEstimate: 50 }),
];
const settings = { ...defaultTestSettings, unlimitedEntries: false, maxEntries: 2 };
const result = formatAndGroup(entries, settings, 'deeplore_');
assertEqual(result.count, 2, 'should limit to max entries');
assertEqual(result.totalTokens, 100, 'total should be 100');
});
test('formatAndGroup: first entry always accepted even if over budget', () => {
const entries = [
makeEntry('Big', { content: 'Huge content', tokenEstimate: 5000 }),
];
const settings = { ...defaultTestSettings, unlimitedBudget: false, maxTokensBudget: 100 };
const result = formatAndGroup(entries, settings, 'deeplore_');
assertEqual(result.count, 1, 'first entry should always be accepted');
assertEqual(result.totalTokens, 5000, 'should include the entry tokens');
});
// ============================================================================
// Tests: validateSettings complete constraints from settings.js
// ============================================================================
test('validateSettings: clamps stripLookbackDepth', () => {
const settings = { stripLookbackDepth: 50 };
validateSettings(settings, settingsConstraints);
assertEqual(settings.stripLookbackDepth, 10, 'should clamp stripLookbackDepth to max');
});
test('validateSettings: clamps stripLookbackDepth min', () => {
const settings = { stripLookbackDepth: 0 };
validateSettings(settings, settingsConstraints);
assertEqual(settings.stripLookbackDepth, 1, 'should clamp stripLookbackDepth to min');
});
test('validateSettings: clamps newChatThreshold min', () => {
const settings = { newChatThreshold: 0 };
validateSettings(settings, settingsConstraints);
assertEqual(settings.newChatThreshold, 1, 'should clamp newChatThreshold to min');
});
// ============================================================================
// Tests: parseVaultFile edge cases
// ============================================================================
test('parseVaultFile: frontmatter with no content after separator', () => {
const file = { filename: 'empty.md', content: '---\ntags:\n - lorebook\nkeys:\n - test\n---\n' };
const tagConfig = { lorebookTag: 'lorebook', constantTag: '', neverInsertTag: '' };
const entry = parseVaultFile(file, tagConfig);
assert(entry !== null, 'should return an entry even with empty body');
assertEqual(entry.content, '', 'content should be empty string');
});
test('parseVaultFile: constant via frontmatter boolean (not tag)', () => {
const file = { filename: 'test.md', content: '---\ntags:\n - lorebook\nconstant: true\nkeys:\n - test\n---\n# Test\nContent' };
const tagConfig = { lorebookTag: 'lorebook', constantTag: 'lorebook-always', neverInsertTag: '' };
const entry = parseVaultFile(file, tagConfig);
assert(entry !== null, 'should return an entry');
assertEqual(entry.constant, true, 'should detect constant from frontmatter boolean');
});
// ============================================================================
// Tests: applyGating edge cases
// ============================================================================
test('applyGating: self-requiring entry is removed', () => {
const entries = [makeEntry('A', { requires: ['A'] })];
const result = applyGating(entries);
// A requires itself — it's in the active set, so it should pass