package pipeline import ( "context" "fmt" "path/filepath" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" ) func TestChatTurnDiffsSurviveReopenAndPrunePerSession(t *testing.T) { path := filepath.Join(t.TempDir(), "diffs.db") s, err := Open(path) if err != nil { t.Fatal(err) } ctx := context.Background() changes := protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "a.go", Kind: "modified", Patch: "@@ -1 +1 @@\n-a\n+b\n"}}} for i := 0; i < protocol.ChatTurnDiffLimit+5; i++ { summary := protocol.ChatTurnDiffSummary{TurnID: fmt.Sprintf("turn_%03d", i), Files: 1, Additions: 1, Deletions: 1, Status: "complete"} if err := s.RecordChatTurnDiff(ctx, "ses_a", summary, changes); err != nil { t.Fatal(err) } } if err := s.RecordChatTurnDiff(ctx, "ses_b", protocol.ChatTurnDiffSummary{TurnID: "turn_b", Files: 1}, changes); err != nil { t.Fatal(err) } s.Close() s, err = Open(path) if err != nil { t.Fatal(err) } defer s.Close() got, err := s.ChatTurnDiffs(ctx, "ses_a") if err != nil || len(got) != protocol.ChatTurnDiffLimit || got[0].TurnID != "turn_005" || got[len(got)-1].TurnID != fmt.Sprintf("turn_%03d", protocol.ChatTurnDiffLimit+4) { t.Fatalf("ses_a = %d rows (first %+v) err %v", len(got), got[0], err) } if other, _ := s.ChatTurnDiffs(ctx, "ses_b"); len(other) != 1 { t.Fatalf("pruning one session touched another: %+v", other) } summary, stored, found, err := s.ChatTurnDiff(ctx, "ses_a", "turn_050") if err != nil || !found || summary.Additions != 1 || len(stored.Files) != 1 || stored.Files[0].Patch != changes.Files[0].Patch { t.Fatalf("turn_050 = %+v %+v found=%v err=%v", summary, stored, found, err) } if _, _, found, _ := s.ChatTurnDiff(ctx, "ses_a", "turn_000"); found { t.Fatal("a pruned turn is still readable") } }