| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/provider" |
| 9 | "reasonix/internal/store" |
| 10 | ) |
| 11 | |
| 12 | func TestScanSessionDisplayIndexListingMatchesBuilderAfterEmptyUserTurn(t *testing.T) { |
| 13 | msgs := []provider.Message{ |
| 14 | {Role: provider.RoleUser, Images: []string{"data:image/png;base64,iVBORw0KGgo="}}, |
| 15 | {Role: provider.RoleAssistant, Content: "image response"}, |
| 16 | {Role: provider.RoleUser, Content: "later question"}, |
| 17 | } |
| 18 | path := filepath.Join(t.TempDir(), "session.jsonl") |
| 19 | if err := writeSessionMessages(path, msgs); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | digest, err := digestSessionMessages(msgs) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | built := BuildSessionDisplayIndex(msgs, 1, true, digest) |
| 27 | scanned, err := ScanSessionDisplayIndex(path) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | if !scanned.ListingPreviewKnown || scanned.ListingPreview != built.ListingPreview || |
| 32 | scanned.AuthoredTurns != built.AuthoredTurns { |
| 33 | t.Fatalf("scan listing = %q/%d known=%v, build = %q/%d", scanned.ListingPreview, |
| 34 | scanned.AuthoredTurns, scanned.ListingPreviewKnown, built.ListingPreview, built.AuthoredTurns) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestIndexedSessionListingRejectsEqualDisplayIndexMtime(t *testing.T) { |
| 39 | path := filepath.Join(t.TempDir(), "session.jsonl") |
| 40 | writeSessionFile(t, path, []provider.Message{{Role: provider.RoleUser, Content: "question"}}) |
| 41 | if _, err := RepairSessionListingProjection(t.Context(), path); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | meta, ok, err := LoadBranchMeta(path) |
| 45 | if err != nil || !ok { |
| 46 | t.Fatalf("LoadBranchMeta ok=%v err=%v", ok, err) |
| 47 | } |
| 48 | contentTime := SessionContentModTime(path) |
| 49 | indexPath := store.SessionDisplayIndex(path) |
| 50 | if err := os.Chtimes(indexPath, contentTime, contentTime); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if _, _, fast, _, err := indexedSessionListing(path, meta); err != nil || fast { |
| 54 | t.Fatalf("equal-mtime display index fast=%v err=%v, want fallback", fast, err) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestIndexedSessionListingRejectsEqualEventIndexMtime(t *testing.T) { |
| 59 | path := schemaOneSessionPath(t, "session.jsonl") |
| 60 | session := NewSession("system") |
| 61 | session.Add(provider.Message{Role: provider.RoleUser, Content: "question"}) |
| 62 | if err := session.SaveSnapshot(path); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | meta, ok, err := LoadBranchMeta(path) |
| 66 | if err != nil || !ok { |
| 67 | t.Fatalf("LoadBranchMeta ok=%v err=%v", ok, err) |
| 68 | } |
| 69 | displayInfo, err := os.Stat(store.SessionDisplayIndex(path)) |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | contentTime := SessionContentModTime(path) |
| 74 | if !displayInfo.ModTime().After(contentTime) { |
| 75 | newer := contentTime.AddDate(0, 0, 1) |
| 76 | if err := os.Chtimes(store.SessionDisplayIndex(path), newer, newer); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | } |
| 80 | logInfo, err := os.Stat(store.SessionEventLog(path)) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | if err := os.Chtimes(store.SessionEventIndex(path), logInfo.ModTime(), logInfo.ModTime()); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | if _, _, fast, _, err := indexedSessionListing(path, meta); err != nil || fast { |
| 88 | t.Fatalf("equal-mtime event index fast=%v err=%v, want fallback", fast, err) |
| 89 | } |
| 90 | } |
| 91 |