| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/provider" |
| 9 | "reasonix/internal/store" |
| 10 | ) |
| 11 | |
| 12 | func removeHistorySliceNativeState(t *testing.T, path string) { |
| 13 | t.Helper() |
| 14 | for _, sidecar := range []string{store.SessionEventLog(path), store.SessionEventIndex(path), store.SessionMeta(path)} { |
| 15 | if err := os.Remove(sidecar); err != nil && !os.IsNotExist(err) { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func TestHistorySliceColdLegacyDetectsSameSizeAnchorRewrite(t *testing.T) { |
| 22 | app := historySliceTestApp(t) |
| 23 | tab := newColdHistoryTab(t, app) |
| 24 | dir := tabSessionDir(tab) |
| 25 | _, path := saveHistorySliceSession(t, dir, "cold-same-size.jsonl", []provider.Message{ |
| 26 | historySliceUser(0, "old question"), historySliceAssistant(0, "old answer"), |
| 27 | }) |
| 28 | tab.SessionPath = path |
| 29 | // Model a pre-WAL checkpoint. Native sessions must recover from the event log |
| 30 | // rather than accepting an out-of-band rewrite of the compatibility anchor. |
| 31 | removeHistorySliceNativeState(t, path) |
| 32 | before, err := os.ReadFile(path) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | // Replace only equal-length content. A size-only guard would keep serving |
| 37 | // the old offsets/index; digest validation must force a scan. |
| 38 | rewritten := strings.Replace(string(before), "old answer", "new answer", 1) |
| 39 | if len(rewritten) != len(before) { |
| 40 | t.Fatalf("test fixture rewrite changed size: %d -> %d", len(before), len(rewritten)) |
| 41 | } |
| 42 | if err := os.WriteFile(path, []byte(rewritten), 0o600); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | page := app.HistorySliceForTab("cold", HistorySliceRequest{Turns: 12}) |
| 46 | if page.Source != "scan" { |
| 47 | t.Fatalf("Source = %q, want scan after same-size rewrite", page.Source) |
| 48 | } |
| 49 | if len(page.Entries) == 0 || page.Entries[len(page.Entries)-1].Message.Content != "new answer" { |
| 50 | t.Fatalf("latest entry = %+v, want rewritten content", page.Entries) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestHistorySliceColdNativeRejectsSameSizeAnchorRewrite(t *testing.T) { |
| 55 | app := historySliceTestApp(t) |
| 56 | tab := newColdHistoryTab(t, app) |
| 57 | dir := tabSessionDir(tab) |
| 58 | _, path := saveHistorySliceSession(t, dir, "cold-native-same-size.jsonl", []provider.Message{ |
| 59 | historySliceUser(0, "old question"), historySliceAssistant(0, "old answer"), |
| 60 | }) |
| 61 | tab.SessionPath = path |
| 62 | before, err := os.ReadFile(path) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | rewritten := strings.Replace(string(before), "old answer", "bad answer", 1) |
| 67 | if len(rewritten) != len(before) { |
| 68 | t.Fatalf("test fixture rewrite changed size: %d -> %d", len(before), len(rewritten)) |
| 69 | } |
| 70 | if err := os.WriteFile(path, []byte(rewritten), 0o600); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | |
| 74 | page := app.HistorySliceForTab("cold", HistorySliceRequest{Turns: 12}) |
| 75 | if page.Source != "event-log" { |
| 76 | t.Fatalf("Source = %q, want authoritative event-log recovery", page.Source) |
| 77 | } |
| 78 | if len(page.Entries) == 0 || page.Entries[len(page.Entries)-1].Message.Content != "old answer" { |
| 79 | t.Fatalf("latest entry = %+v, want canonical WAL content", page.Entries) |
| 80 | } |
| 81 | } |
| 82 |