-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
1018 lines (897 loc) · 29 KB
/
integration_test.go
File metadata and controls
1018 lines (897 loc) · 29 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
package integration_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/GrayCodeAI/yaad/internal/bench"
"github.com/GrayCodeAI/yaad/config"
"github.com/GrayCodeAI/yaad/embeddings"
"github.com/GrayCodeAI/yaad/engine"
"github.com/GrayCodeAI/yaad/exportimport"
"github.com/GrayCodeAI/yaad/graph"
"github.com/GrayCodeAI/yaad/hooks"
"github.com/GrayCodeAI/yaad/ingest"
intentpkg "github.com/GrayCodeAI/yaad/intent"
"github.com/GrayCodeAI/yaad/profile"
"github.com/GrayCodeAI/yaad/internal/server"
"github.com/GrayCodeAI/yaad/skill"
"github.com/GrayCodeAI/yaad/storage"
yaadtls "github.com/GrayCodeAI/yaad/internal/tls"
"github.com/GrayCodeAI/yaad/utils"
)
func setup(t *testing.T) (*engine.Engine, func()) {
t.Helper()
dir := t.TempDir()
store, err := storage.NewStore(filepath.Join(dir, "test.db"))
if err != nil {
t.Fatal(err)
}
eng := engine.New(store, graph.New(store, store.DB()))
return eng, func() { store.Close(); os.RemoveAll(dir) }
}
func TestRememberRecallContext(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// --- Remember ---
nodes := []engine.RememberInput{
{Type: "convention", Content: "Use jose not jsonwebtoken for Edge compatibility", Scope: "project"},
{Type: "decision", Content: "Chose RS256 over HS256 for JWT compliance", Scope: "project"},
{Type: "bug", Content: "Token refresh race: use mutex in src/middleware/auth.ts", Scope: "project", Tags: "auth"},
{Type: "task", Content: "Add rate limiting to /auth/token", Scope: "project"},
}
var ids []string
for _, in := range nodes {
n, err := eng.Remember(context.Background(), in)
if err != nil {
t.Fatalf("Remember(%q): %v", in.Content, err)
}
ids = append(ids, n.ID)
}
if len(ids) != 4 {
t.Fatalf("expected 4 nodes, got %d", len(ids))
}
// --- Dedup: same content returns same ID ---
n2, err := eng.Remember(context.Background(), engine.RememberInput{
Type: "convention", Content: "Use jose not jsonwebtoken for Edge compatibility", Scope: "project",
})
if err != nil {
t.Fatal(err)
}
if n2.ID != ids[0] {
t.Errorf("dedup failed: expected id %s, got %s", ids[0], n2.ID)
}
// --- Recall ---
result, err := eng.Recall(context.Background(), engine.RecallOpts{Query: "auth JWT", Depth: 2, Limit: 10})
if err != nil {
t.Fatal(err)
}
if len(result.Nodes) == 0 {
t.Error("recall returned no nodes")
}
// Should find the bug and decision nodes
found := map[string]bool{}
for _, n := range result.Nodes {
found[n.Type] = true
}
if !found["bug"] && !found["decision"] {
t.Errorf("recall missing expected types, got: %v", found)
}
// --- Context (hot tier) ---
ctx, err := eng.Context(context.Background(), "")
if err != nil {
t.Fatal(err)
}
// Hot tier should include convention (tier=1) and task (tier=1)
hotTypes := map[string]bool{}
for _, n := range ctx.Nodes {
hotTypes[n.Type] = true
}
if !hotTypes["convention"] {
t.Errorf("context missing convention nodes, got: %v", hotTypes)
}
if !hotTypes["task"] {
t.Errorf("context missing task nodes, got: %v", hotTypes)
}
// --- Forget ---
if err := eng.Forget(context.Background(), ids[3]); err != nil {
t.Fatal(err)
}
forgotten, _ := eng.Store().GetNode(context.Background(), ids[3])
if forgotten.Confidence != 0 {
t.Errorf("forget: expected confidence=0, got %f", forgotten.Confidence)
}
}
func TestGraphLinkAndSubgraph(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
a, _ := eng.Remember(context.Background(), engine.RememberInput{Type: "decision", Content: "Use NATS for event bus", Scope: "project"})
b, _ := eng.Remember(context.Background(), engine.RememberInput{Type: "convention", Content: "Use NATS client v2", Scope: "project"})
// Link: decision led_to convention
err := eng.Graph().AddEdge(context.Background(), &storage.Edge{
ID: "e1", FromID: a.ID, ToID: b.ID, Type: "led_to", Weight: 1.0,
})
if err != nil {
t.Fatalf("AddEdge: %v", err)
}
// Subgraph from decision should include convention
sg, err := eng.Graph().ExtractSubgraph(context.Background(), a.ID, 1)
if err != nil {
t.Fatal(err)
}
if len(sg.Nodes) < 2 {
t.Errorf("subgraph: expected ≥2 nodes, got %d", len(sg.Nodes))
}
// Cycle detection: led_to is acyclic — b→a should fail
err = eng.Graph().AddEdge(context.Background(), &storage.Edge{
ID: "e2", FromID: b.ID, ToID: a.ID, Type: "led_to", Weight: 1.0,
})
if err == nil {
t.Error("cycle detection failed: should have rejected b→a led_to edge")
}
// relates_to allows cycles
err = eng.Graph().AddEdge(context.Background(), &storage.Edge{
ID: "e3", FromID: b.ID, ToID: a.ID, Type: "relates_to", Weight: 1.0,
})
if err != nil {
t.Errorf("relates_to cycle should be allowed: %v", err)
}
}
func TestPhase3EmbeddingsAndHybridSearch(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// Store some nodes
nodes := []engine.RememberInput{
{Type: "convention", Content: "Use jose not jsonwebtoken", Scope: "project"},
{Type: "decision", Content: "Chose RS256 for JWT compliance", Scope: "project"},
{Type: "bug", Content: "Token refresh race in auth middleware", Scope: "project"},
}
for _, in := range nodes {
eng.Remember(context.Background(), in)
}
// Embed all nodes with local stub provider
provider := embeddings.NewLocal()
allNodes, _ := eng.Store().ListNodes(context.Background(), storage.NodeFilter{})
for _, n := range allNodes {
vec, err := provider.Embed(context.Background(), n.Content)
if err != nil {
t.Fatalf("Embed: %v", err)
}
if err := eng.Store().SaveEmbedding(context.Background(), n.ID, provider.Name(), vec); err != nil {
t.Fatalf("SaveEmbedding: %v", err)
}
}
// Verify embeddings stored
embs, err := eng.Store().AllEmbeddings(context.Background())
if err != nil {
t.Fatal(err)
}
if len(embs) == 0 {
t.Error("no embeddings stored")
}
// Hybrid search
hs := engine.NewHybridSearch(eng.Store(), eng.Graph(), provider)
scored, err := hs.Search(context.Background(), "auth JWT", engine.RecallOpts{Depth: 2, Limit: 10})
if err != nil {
t.Fatal(err)
}
if len(scored) == 0 {
t.Error("hybrid search returned no results")
}
// Re-rank
reranked := engine.Rerank(context.Background(), scored, eng.Store())
if len(reranked) == 0 {
t.Error("rerank returned no results")
}
}
func TestPhase3DecayAndGC(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
n, _ := eng.Remember(context.Background(), engine.RememberInput{Type: "decision", Content: "Old decision", Scope: "project"})
// Manually set low confidence
node, _ := eng.Store().GetNode(context.Background(), n.ID)
node.Confidence = 0.05
eng.Store().UpdateNode(context.Background(), node)
// GC should remove it
removed, err := engine.GarbageCollect(context.Background(), eng.Store(), engine.DefaultDecayConfig)
if err != nil {
t.Fatal(err)
}
if removed == 0 {
t.Error("GC should have removed low-confidence node")
}
}
func TestPhase3ProactiveContext(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
eng.Remember(context.Background(), engine.RememberInput{Type: "convention", Content: "Use TypeScript strict mode", Scope: "project"})
eng.Remember(context.Background(), engine.RememberInput{Type: "task", Content: "Add rate limiting", Scope: "project"})
eng.Remember(context.Background(), engine.RememberInput{Type: "decision", Content: "Use NATS for events", Scope: "project"})
hs := engine.NewHybridSearch(eng.Store(), eng.Graph(), nil)
pc := engine.NewProactiveContext(eng, hs)
nodes, err := pc.Predict(context.Background(), "", 2000)
if err != nil {
t.Fatal(err)
}
if len(nodes) == 0 {
t.Error("proactive context returned no nodes")
}
// FormatContext should produce markdown
ctx := engine.FormatContext(nodes)
if ctx == "" {
t.Error("FormatContext returned empty string")
}
}
func TestPhase4HooksAndReplay(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
dir := t.TempDir()
runner := hooks.New(eng, dir)
// SessionStart
in := &hooks.HookInput{Agent: "test-agent", Project: dir}
if err := runner.SessionStart(context.Background(), in); err != nil {
t.Fatalf("SessionStart: %v", err)
}
// PostToolUse — should create a memory node
toolIn := &hooks.HookInput{
ToolName: "Write",
ToolInput: "src/auth.ts",
ToolOutput: "wrote JWT middleware",
Agent: "test-agent",
}
if err := runner.PostToolUse(context.Background(), toolIn); err != nil {
t.Fatalf("PostToolUse: %v", err)
}
// Store replay event
if err := runner.StoreToolEvent(context.Background(), toolIn, eng.Store()); err != nil {
t.Fatalf("StoreToolEvent: %v", err)
}
// Verify node was created
result, err := eng.Recall(context.Background(), engine.RecallOpts{Query: "JWT middleware", Limit: 5})
if err != nil {
t.Fatal(err)
}
if len(result.Nodes) == 0 {
t.Error("PostToolUse should have created a memory node")
}
// SessionEnd
endIn := &hooks.HookInput{Summary: "Implemented JWT auth middleware", Agent: "test-agent"}
if err := runner.SessionEnd(context.Background(), endIn); err != nil {
t.Fatalf("SessionEnd: %v", err)
}
}
func TestPhase5ExportImport(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
eng.Remember(context.Background(), engine.RememberInput{Type: "convention", Content: "Use jose not jsonwebtoken", Scope: "project"})
eng.Remember(context.Background(), engine.RememberInput{Type: "decision", Content: "Chose RS256 for JWT", Scope: "project"})
// JSON export
data, err := exportimport.ExportJSON(context.Background(), eng.Store(), "")
if err != nil {
t.Fatal(err)
}
if len(data) == 0 {
t.Error("JSON export is empty")
}
// JSON import into fresh store
eng2, cleanup2 := setup(t)
defer cleanup2()
nodes, edges, err := exportimport.ImportJSON(context.Background(), eng2.Store(), data)
if err != nil {
t.Fatal(err)
}
if nodes == 0 {
t.Error("import: expected nodes > 0")
}
_ = edges
// Markdown export
md, err := exportimport.ExportMarkdown(context.Background(), eng.Store(), "")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(md, "jose") {
t.Error("markdown export missing content")
}
// Obsidian export
vaultDir := t.TempDir()
n, err := exportimport.ExportObsidian(context.Background(), eng.Store(), "", vaultDir)
if err != nil {
t.Fatal(err)
}
if n == 0 {
t.Error("obsidian export: expected files > 0")
}
}
func TestPhase5Skills(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
sk := &skill.Skill{
Name: "deploy",
Description: "Deploy the application",
Steps: []skill.Step{
{Order: 1, Description: "Run tests", Command: "pnpm test"},
{Order: 2, Description: "Build", Command: "pnpm build"},
{Order: 3, Description: "Deploy", Command: "fly deploy"},
},
}
node, err := skill.Store(context.Background(), eng, sk, "")
if err != nil {
t.Fatal(err)
}
if node.ID == "" {
t.Error("skill store: empty node ID")
}
// List skills
skills, err := skill.ListSkills(context.Background(), eng.Store(), "")
if err != nil {
t.Fatal(err)
}
if len(skills) == 0 {
t.Error("skill list: expected skills > 0")
}
// Replay
replay := skill.Replay(sk)
if !strings.Contains(replay, "deploy") {
t.Error("skill replay missing content")
}
}
func TestPhase5Benchmark(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// Seed some memories
eng.Remember(context.Background(), engine.RememberInput{Type: "convention", Content: "Use jose not jsonwebtoken for Edge compatibility", Scope: "project"})
eng.Remember(context.Background(), engine.RememberInput{Type: "decision", Content: "Chose NATS over Redis Streams for event bus", Scope: "project"})
eng.Remember(context.Background(), engine.RememberInput{Type: "bug", Content: "Token refresh race condition in auth middleware", Scope: "project"})
result := bench.Run(context.Background(), eng, bench.DefaultQAs(), 2, 10)
if result.Total == 0 {
t.Error("benchmark: no questions evaluated")
}
// R@5 should be > 0 with seeded data
if result.HitAtK[5] == 0 {
t.Log("benchmark: R@5=0 (may be ok with small dataset)")
}
t.Logf("Benchmark:\n%s", result.String())
}
func TestUserProfile(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
eng.Remember(context.Background(), engine.RememberInput{Type: "convention", Content: "Use jose for JWT auth", Scope: "project"})
eng.Remember(context.Background(), engine.RememberInput{Type: "decision", Content: "Chose NATS for event bus", Scope: "project"})
eng.Remember(context.Background(), engine.RememberInput{Type: "task", Content: "Add rate limiting", Scope: "project"})
eng.Remember(context.Background(), engine.RememberInput{Type: "preference", Content: "Prefers functional style", Scope: "project"})
p, err := eng.Profile(context.Background(), "")
if err != nil {
t.Fatal(err)
}
if len(p.Static) == 0 {
t.Error("profile: no static facts")
}
if p.Summary == "" {
t.Error("profile: empty summary")
}
formatted := p.Format()
if !strings.Contains(formatted, "User Profile") {
t.Error("profile: formatted output missing header")
}
t.Logf("Profile:\n%s", formatted)
}
func TestConflictResolver(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// Store original convention
old, _ := eng.Remember(context.Background(), engine.RememberInput{
Type: "convention", Content: "Use jsonwebtoken library for JWT", Scope: "project",
})
// Store contradicting convention (should supersede)
newNode, _ := eng.Remember(context.Background(), engine.RememberInput{
Type: "convention", Content: "Use jose instead of jsonwebtoken for Edge compatibility", Scope: "project",
})
// Verify old node confidence was lowered
oldUpdated, _ := eng.Store().GetNode(context.Background(), old.ID)
if oldUpdated.Confidence >= 1.0 {
t.Errorf("conflict: old node confidence should be lowered, got %.2f", oldUpdated.Confidence)
}
// Verify supersedes edge exists
edges, _ := eng.Store().GetEdgesFrom(context.Background(), newNode.ID)
hasSupersedes := false
for _, e := range edges {
if e.Type == "supersedes" && e.ToID == old.ID {
hasSupersedes = true
}
}
if !hasSupersedes {
t.Error("conflict: supersedes edge not created")
}
}
func TestTemporalBackbone(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
n1, _ := eng.Remember(context.Background(), engine.RememberInput{Type: "convention", Content: "First convention", Scope: "project", Project: "test"})
n2, _ := eng.Remember(context.Background(), engine.RememberInput{Type: "decision", Content: "Second decision", Scope: "project", Project: "test"})
n3, _ := eng.Remember(context.Background(), engine.RememberInput{Type: "bug", Content: "Third bug report", Scope: "project", Project: "test"})
// Verify temporal chain: n1 → n2 → n3
edges1, _ := eng.Store().GetEdgesFrom(context.Background(), n1.ID)
hasLink12 := false
for _, e := range edges1 {
if e.Type == "learned_in" && e.ToID == n2.ID {
hasLink12 = true
}
}
edges2, _ := eng.Store().GetEdgesFrom(context.Background(), n2.ID)
hasLink23 := false
for _, e := range edges2 {
if e.Type == "learned_in" && e.ToID == n3.ID {
hasLink23 = true
}
}
if !hasLink12 {
t.Error("temporal: n1→n2 learned_in edge missing")
}
if !hasLink23 {
t.Error("temporal: n2→n3 learned_in edge missing")
}
}
func TestDedupRollingWindow(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
n1, _ := eng.Remember(context.Background(), engine.RememberInput{
Type: "convention", Content: "Use jose for JWT auth", Scope: "project",
})
// Same content again — should return same node (dedup)
n2, _ := eng.Remember(context.Background(), engine.RememberInput{
Type: "convention", Content: "Use jose for JWT auth", Scope: "project",
})
if n1.ID != n2.ID {
t.Errorf("dedup: expected same node ID, got %s and %s", n1.ID[:8], n2.ID[:8])
}
}
func TestCompaction(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// Store 5 low-confidence nodes
for i := 0; i < 5; i++ {
n, _ := eng.Remember(context.Background(), engine.RememberInput{
Type: "decision", Content: fmt.Sprintf("Old decision %d about something", i), Scope: "project",
})
node, _ := eng.Store().GetNode(context.Background(), n.ID)
node.Confidence = 0.2
node.AccessCount = 0
eng.Store().UpdateNode(context.Background(), node)
}
// Run compaction
compacted, err := eng.Compact(context.Background(), "")
if err != nil {
t.Fatal(err)
}
if compacted == 0 {
t.Error("compaction: expected nodes to be compacted")
}
t.Logf("Compacted %d nodes", compacted)
}
func TestMentalModel(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
eng.Remember(context.Background(), engine.RememberInput{Type: "convention", Content: "Use jose for JWT", Scope: "project"})
eng.Remember(context.Background(), engine.RememberInput{Type: "decision", Content: "Chose NATS for events", Scope: "project"})
eng.Remember(context.Background(), engine.RememberInput{Type: "task", Content: "Add rate limiting", Scope: "project"})
model, err := eng.MentalModel(context.Background(), "")
if err != nil {
t.Fatal(err)
}
if model.Summary == "" {
t.Error("mental model: empty summary")
}
if len(model.Conventions) == 0 {
t.Error("mental model: no conventions")
}
formatted := model.Format()
if formatted == "" {
t.Error("mental model: empty formatted output")
}
t.Logf("Mental model:\n%s", formatted)
}
func TestPhase6IntentClassifier(t *testing.T) {
cases := []struct {
query string
expected intentpkg.Intent
}{
{"why did we choose NATS over Redis?", intentpkg.IntentWhy},
{"when did we fix the auth bug?", intentpkg.IntentWhen},
{"how to deploy the application?", intentpkg.IntentHow},
{"what is the auth subsystem?", intentpkg.IntentWhat},
{"which library should I use for JWT?", intentpkg.IntentWho},
{"recall auth middleware", intentpkg.IntentGeneral},
}
for _, c := range cases {
got := intentpkg.Classify(c.query)
if got != c.expected {
t.Errorf("Classify(%q) = %s, want %s", c.query, got, c.expected)
}
}
}
func TestPhase6IntentAwareRetrieval(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// Seed memories
decision, _ := eng.Remember(context.Background(), engine.RememberInput{Type: "decision", Content: "Chose NATS over Redis Streams for event bus", Scope: "project"})
convention, _ := eng.Remember(context.Background(), engine.RememberInput{Type: "convention", Content: "Use NATS client v2 for all event publishing", Scope: "project"})
// Link: decision led_to convention
eng.Graph().AddEdge(context.Background(), &storage.Edge{
ID: "e-test", FromID: decision.ID, ToID: convention.ID, Type: "led_to", Weight: 1.0,
})
// Why query should find the decision via causal traversal
result, err := eng.Recall(context.Background(), engine.RecallOpts{Query: "why NATS", Depth: 2, Limit: 10})
if err != nil {
t.Fatal(err)
}
if len(result.Nodes) == 0 {
t.Error("intent-aware recall returned no nodes")
}
// Should find both decision and convention via causal chain
found := map[string]bool{}
for _, n := range result.Nodes {
found[n.Type] = true
}
t.Logf("Why query found types: %v", found)
}
func TestPhase6DualStream(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
ds := ingest.New(eng)
defer ds.Stop()
// Fast path should return immediately
node, err := ds.Remember(context.Background(), engine.RememberInput{
Type: "convention", Content: "Use jose not jsonwebtoken", Scope: "project",
})
if err != nil {
t.Fatal(err)
}
if node.ID == "" {
t.Error("dual stream: empty node ID")
}
// Second remember should create temporal backbone edge
node2, err := ds.Remember(context.Background(), engine.RememberInput{
Type: "decision", Content: "Chose RS256 for JWT", Scope: "project",
})
if err != nil {
t.Fatal(err)
}
if node2.ID == "" {
t.Error("dual stream: second node empty ID")
}
// Give slow path time to run and release DB lock
// Retry up to 500ms (slow path runs async)
var hasTemporalEdge bool
for i := 0; i < 10; i++ {
time.Sleep(50 * time.Millisecond)
edges, _ := eng.Store().GetEdgesFrom(context.Background(), node.ID)
for _, e := range edges {
if e.ToID == node2.ID && e.Type == "learned_in" {
hasTemporalEdge = true
}
}
if hasTemporalEdge {
break
}
}
if !hasTemporalEdge {
t.Error("dual stream: temporal backbone edge not created within 500ms")
}
}
func TestPrivacyFilter(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// Store content with secrets — should be stripped
node, _ := eng.Remember(context.Background(), engine.RememberInput{
Type: "convention",
Content: "Use API key sk-1234567890abcdefghijklmnop for auth and AKIA1234567890ABCDEF for AWS",
Scope: "project",
})
if strings.Contains(node.Content, "sk-1234567890") {
t.Error("privacy: API key not stripped")
}
if strings.Contains(node.Content, "AKIA1234567890") {
t.Error("privacy: AWS key not stripped")
}
if !strings.Contains(node.Content, "[REDACTED]") {
t.Error("privacy: expected [REDACTED] placeholder")
}
}
func TestUtilsShortID(t *testing.T) {
cases := []struct{ input, expected string }{
{"abcdefghijklmnop", "abcdefgh"},
{"short", "short"},
{"12345678", "12345678"},
{"", ""},
{"ab", "ab"},
}
for _, c := range cases {
got := utils.ShortID(c.input)
if got != c.expected {
t.Errorf("ShortID(%q) = %q, want %q", c.input, got, c.expected)
}
}
}
func TestEdgeCaseEmptyRecall(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// Recall on empty DB should return empty, not error
result, err := eng.Recall(context.Background(), engine.RecallOpts{Query: "nonexistent", Limit: 5})
if err != nil {
t.Fatal(err)
}
if len(result.Nodes) != 0 {
t.Errorf("empty recall: expected 0 nodes, got %d", len(result.Nodes))
}
}
func TestEdgeCaseContextEmpty(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// Context on empty DB should return empty, not error
result, err := eng.Context(context.Background(), "")
if err != nil {
t.Fatal(err)
}
if result == nil {
t.Error("context: should return empty result, not nil")
}
}
func TestEdgeCaseForgetNonexistent(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// Forget nonexistent node should error gracefully
err := eng.Forget(context.Background(), "nonexistent-id-12345678")
if err == nil {
t.Error("forget: should error on nonexistent node")
}
}
func TestProfileMerge(t *testing.T) {
a := &profile.Profile{
Project: "test",
Static: []string{"Use jose", "Use NATS"},
Dynamic: []string{"[task] rate limiting"},
Stack: []string{"TypeScript", "NATS"},
}
b := &profile.Profile{
Static: []string{"Prefer tabs", "Use jose"}, // "Use jose" is duplicate
Dynamic: []string{"[bug] auth race"},
Stack: []string{"PostgreSQL", "NATS"}, // "NATS" is duplicate
}
merged := profile.Merge(a, b)
// Static should be deduped
if len(merged.Static) != 3 { // jose, NATS, tabs
t.Errorf("merge: expected 3 static, got %d: %v", len(merged.Static), merged.Static)
}
// Stack should be deduped
if len(merged.Stack) != 3 { // TypeScript, NATS, PostgreSQL
t.Errorf("merge: expected 3 stack, got %d: %v", len(merged.Stack), merged.Stack)
}
// Dynamic should be combined (not deduped)
if len(merged.Dynamic) != 2 {
t.Errorf("merge: expected 2 dynamic, got %d", len(merged.Dynamic))
}
}
func TestMultipleRememberAndRecall(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
// Store 20 memories of different types
types := []string{"convention", "decision", "bug", "spec", "task"}
for i := 0; i < 20; i++ {
eng.Remember(context.Background(), engine.RememberInput{
Type: types[i%len(types)],
Content: fmt.Sprintf("Memory item %d about topic %d", i, i%5),
Scope: "project",
})
}
// Recall should find results
result, err := eng.Recall(context.Background(), engine.RecallOpts{Query: "topic", Limit: 10})
if err != nil {
t.Fatal(err)
}
if len(result.Nodes) == 0 {
t.Error("bulk recall: expected nodes")
}
t.Logf("Stored 20, recalled %d nodes", len(result.Nodes))
// Status should show correct counts
st, _ := eng.Status(context.Background(), "")
if st.Nodes < 20 {
t.Errorf("status: expected ≥20 nodes, got %d", st.Nodes)
}
}
func TestTLSCertGeneration(t *testing.T) {
dir := t.TempDir()
cfg := yaadtls.Config{Enabled: true}
tlsCfg, err := yaadtls.TLSConfig(cfg, dir)
if err != nil {
t.Fatal(err)
}
if tlsCfg == nil {
t.Error("tls: nil config returned")
}
if len(tlsCfg.Certificates) == 0 {
t.Error("tls: no certificates generated")
}
// Verify cert files were created
if _, err := os.Stat(filepath.Join(dir, "cert.pem")); err != nil {
t.Error("tls: cert.pem not created")
}
if _, err := os.Stat(filepath.Join(dir, "key.pem")); err != nil {
t.Error("tls: key.pem not created")
}
}
func TestConfigDefaults(t *testing.T) {
cfg := config.Default()
if cfg.Server.Port != 3456 {
t.Errorf("config: expected port 3456, got %d", cfg.Server.Port)
}
if cfg.Decay.HalfLifeDays != 30 {
t.Errorf("config: expected half_life 30, got %d", cfg.Decay.HalfLifeDays)
}
}
func TestStorageCreateAndQuery(t *testing.T) {
dir := t.TempDir()
store, err := storage.NewStore(filepath.Join(dir, "test.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
ctx := context.Background()
// Create node
node := &storage.Node{
ID: "test-node-1", Type: "convention", Content: "Test content",
ContentHash: "hash1", Scope: "project", Tier: 1, Confidence: 1.0, Version: 1,
}
if err := store.CreateNode(ctx, node); err != nil {
t.Fatal(err)
}
// Get node
got, err := store.GetNode(ctx, "test-node-1")
if err != nil {
t.Fatal(err)
}
if got.Content != "Test content" {
t.Errorf("storage: expected 'Test content', got '%s'", got.Content)
}
// Create edge
edge := &storage.Edge{
ID: "test-edge-1", FromID: "test-node-1", ToID: "test-node-1",
Type: "relates_to", Acyclic: false, Weight: 1.0,
}
if err := store.CreateEdge(ctx, edge); err != nil {
t.Fatal(err)
}
// Get neighbors
neighbors, err := store.GetNeighbors(ctx, "test-node-1")
if err != nil {
t.Fatal(err)
}
if len(neighbors) == 0 {
t.Error("storage: expected neighbors")
}
// Version history
if err := store.SaveVersion(ctx, "test-node-1", "old content", "test", "test update"); err != nil {
t.Fatal(err)
}
versions, err := store.GetVersions(ctx, "test-node-1")
if err != nil {
t.Fatal(err)
}
if len(versions) == 0 {
t.Error("storage: expected version history")
}
}
func TestRESTAPI(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
mux := http.NewServeMux()
rest := server.NewRESTServer(eng, "")
rest.RegisterRoutes(mux)
ts := httptest.NewServer(mux)
defer ts.Close()
// POST /yaad/remember
body, _ := json.Marshal(engine.RememberInput{
Type: "convention", Content: "Always use TypeScript strict mode", Scope: "project",
})
resp, err := http.Post(ts.URL+"/yaad/remember", "application/json", bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 201 {
t.Errorf("remember: expected 201, got %d", resp.StatusCode)
}
var node storage.Node
json.NewDecoder(resp.Body).Decode(&node)
resp.Body.Close()
if node.ID == "" {
t.Error("remember: empty node ID")
}
// POST /yaad/recall
body, _ = json.Marshal(engine.RecallOpts{Query: "TypeScript", Limit: 5})
resp, err = http.Post(ts.URL+"/yaad/recall", "application/json", bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Errorf("recall: expected 200, got %d", resp.StatusCode)
}
var result engine.RecallResult
json.NewDecoder(resp.Body).Decode(&result)
resp.Body.Close()
if len(result.Nodes) == 0 {
t.Error("recall: expected nodes, got none")
}
// GET /yaad/health
resp, _ = http.Get(ts.URL + "/yaad/health")
if resp.StatusCode != 200 {
t.Errorf("health: expected 200, got %d", resp.StatusCode)
}
resp.Body.Close()
// GET /yaad/context
resp, _ = http.Get(ts.URL + "/yaad/context")
if resp.StatusCode != 200 {
t.Errorf("context: expected 200, got %d", resp.StatusCode)
}
resp.Body.Close()
}
// TestConcurrentSQLiteAccess verifies that concurrent Remember and Recall operations
// against the real SQLite backend do not race or corrupt data.
func TestConcurrentSQLiteAccess(t *testing.T) {
eng, cleanup := setup(t)
defer cleanup()
var wg sync.WaitGroup
numWriters := 5
numReaders := 5
opsPerGoroutine := 10
// Writers
for i := 0; i < numWriters; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
for j := 0; j < opsPerGoroutine; j++ {
_, err := eng.Remember(context.Background(), engine.RememberInput{
Type: "convention",
Content: fmt.Sprintf("writer-%d-op-%d", idx, j),
Scope: "project",
Project: "concurrent-test",
})
if err != nil {
t.Errorf("writer %d op %d failed: %v", idx, j, err)
}
}
}(i)
}
// Readers
for i := 0; i < numReaders; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
for j := 0; j < opsPerGoroutine; j++ {
_, err := eng.Recall(context.Background(), engine.RecallOpts{
Query: "writer",
Project: "concurrent-test",
Limit: 10,
})