| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "path/filepath" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | |
| 11 | "reasonix/internal/agent" |
| 12 | ) |
| 13 | |
| 14 | func TestReadLeaseAllowsWritesAndKeepsFlatPageSnapshot(t *testing.T) { |
| 15 | c, err := Open(t.Context(), Options{Path: filepath.Join(t.TempDir(), "catalog.sqlite"), MetadataOnly: true}) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | t.Cleanup(func() { c.Close(context.Background()) }) |
| 20 | r := SessionRecord{Scope: "global", Directory: t.TempDir(), TopicID: "same", TopicTitle: "Old", OrdinaryVisible: true, Health: HealthOK, TurnsState: TurnsUnknown, LastActivityAt: 1} |
| 21 | r.Path = filepath.Join(r.Directory, "old.jsonl") |
| 22 | if err := c.UpsertSession(t.Context(), r); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | lease, err := c.OpenReadLease(t.Context()) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | defer lease.Close() |
| 30 | r.LastActivityAt = 2 |
| 31 | if err := c.UpsertSession(t.Context(), r); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | page, err := c.ListOrdinarySessions(lease.Context(t.Context()), OrdinaryPageRequest{Scope: "global", Limit: 1}) |
| 35 | if err != nil || len(page) != 1 || page[0].LastActivityAt != 1 { |
| 36 | t.Fatalf("captured view: %+v %v", page, err) |
| 37 | } |
| 38 | page, err = c.ListOrdinarySessions(t.Context(), OrdinaryPageRequest{Scope: "global", Limit: 1}) |
| 39 | if err != nil || len(page) != 1 || page[0].LastActivityAt != 2 { |
| 40 | t.Fatalf("current view: %+v %v", page, err) |
| 41 | } |
| 42 | lease.Close() |
| 43 | lease.Close() |
| 44 | } |
| 45 | |
| 46 | func TestOrdinaryGroupFiltersPhysicalSourcesAcrossCursorRanges(t *testing.T) { |
| 47 | c, err := Open(t.Context(), Options{Path: filepath.Join(t.TempDir(), "catalog.sqlite"), MetadataOnly: true}) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | defer c.Close(context.Background()) |
| 52 | dir := t.TempDir() |
| 53 | keys := []string{} |
| 54 | for i := range 8 { |
| 55 | path := filepath.Join(dir, fmt.Sprintf("%d.jsonl", i)) |
| 56 | topic := "shared" |
| 57 | if i < 2 { |
| 58 | topic = "pinned" |
| 59 | } |
| 60 | if err := c.UpsertSession(t.Context(), SessionRecord{Path: path, Directory: dir, Scope: "global", TopicID: topic, |
| 61 | LastActivityAt: int64(i), CreatedAt: int64(i), OrdinaryVisible: true, Health: HealthOK}); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | if i%2 == 0 { |
| 65 | keys = append(keys, agent.SessionSourceKeyFromIdentity(PathIdentityKey(path), "")) |
| 66 | } |
| 67 | } |
| 68 | if err := c.SyncMetadata(t.Context(), nil, []TopicMetadata{{Scope: "global", TopicID: "pinned", Pinned: true}}); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | encoded, _ := json.Marshal(keys) |
| 72 | lease, err := c.OpenReadLease(t.Context()) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | defer lease.Close() |
| 77 | for _, size := range []int{1, 2, 3} { |
| 78 | for _, include := range []bool{true, false} { |
| 79 | req := OrdinaryPageRequest{Scope: "global", Limit: size} |
| 80 | want := []string{"0.jsonl", "6.jsonl", "4.jsonl", "2.jsonl"} |
| 81 | if include { |
| 82 | req.IncludeSourceKeysJSON = string(encoded) |
| 83 | } else { |
| 84 | req.ExcludeSourceKeysJSON = string(encoded) |
| 85 | want = []string{"1.jsonl", "7.jsonl", "5.jsonl", "3.jsonl"} |
| 86 | } |
| 87 | got := []string{} |
| 88 | for len(got) <= 8 { |
| 89 | page, err := c.ListOrdinarySessions(lease.Context(t.Context()), req) |
| 90 | if err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | if len(page) == 0 { |
| 94 | break |
| 95 | } |
| 96 | for _, row := range page { |
| 97 | got = append(got, filepath.Base(row.Path)) |
| 98 | } |
| 99 | req.Cursor = page[len(page)-1].Cursor |
| 100 | } |
| 101 | if !reflect.DeepEqual(got, want) { |
| 102 | t.Fatalf("size=%d include=%v got %v, want %v", size, include, got, want) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | page, err := c.ListOrdinarySessions(lease.Context(t.Context()), OrdinaryPageRequest{Scope: "global", IncludeSourceKeysJSON: "[]"}) |
| 107 | if err != nil || len(page) != 0 { |
| 108 | t.Fatalf("empty group admitted rows: %+v %v", page, err) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestOrdinaryCursorCrossesPinnedActivityAndIdentityRangesExactlyOnce(t *testing.T) { |
| 113 | c, err := Open(t.Context(), Options{InMemory: true, MetadataOnly: true}) |
| 114 | if err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | defer c.Close(context.Background()) |
| 118 | dir := t.TempDir() |
| 119 | for i, topic := range []string{"p", "p", "p", "a", "a", "b", "c"} { |
| 120 | activity := int64(30 - i/2) |
| 121 | if i >= 3 { |
| 122 | activity = 100 |
| 123 | } |
| 124 | r := SessionRecord{Path: filepath.Join(dir, fmt.Sprintf("%d.jsonl", i)), Directory: dir, Scope: "global", TopicID: topic, CreatedAt: activity, LastActivityAt: activity, Health: HealthOK, TurnsState: TurnsUnknown, OrdinaryVisible: true} |
| 125 | if err := c.UpsertSession(t.Context(), r); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | } |
| 129 | if err := c.SyncMetadata(t.Context(), nil, []TopicMetadata{{Scope: "global", TopicID: "p", Title: "Pinned", Pinned: true}}); err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | for _, mode := range []string{"activity", "created"} { |
| 133 | for _, size := range []int{1, 2, 3} { |
| 134 | req := OrdinaryPageRequest{Scope: "global", SortMode: mode, Limit: size} |
| 135 | var paths []string |
| 136 | for len(paths) <= 7 { |
| 137 | page, err := c.ListOrdinarySessions(t.Context(), req) |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | if len(page) == 0 { |
| 142 | break |
| 143 | } |
| 144 | for _, row := range page { |
| 145 | paths = append(paths, filepath.Base(row.Path)) |
| 146 | } |
| 147 | req.Cursor = page[len(page)-1].Cursor |
| 148 | } |
| 149 | want := []string{"0.jsonl", "1.jsonl", "2.jsonl", "3.jsonl", "4.jsonl", "5.jsonl", "6.jsonl"} |
| 150 | if !reflect.DeepEqual(paths, want) { |
| 151 | t.Fatalf("%s/%d: %v", mode, size, paths) |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func TestOrdinaryPagePinIndexTracksMetadataAndSourceMoves(t *testing.T) { |
| 158 | c, err := Open(t.Context(), Options{InMemory: true, MetadataOnly: true}) |
| 159 | if err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | defer c.Close(context.Background()) |
| 163 | dir := t.TempDir() |
| 164 | a := SessionRecord{Path: filepath.Join(dir, "a.jsonl"), Directory: dir, Scope: "global", TopicID: "a", LastActivityAt: 20, Health: HealthOK, TurnsState: TurnsUnknown, OrdinaryVisible: true} |
| 165 | b := a |
| 166 | b.Path = filepath.Join(dir, "b.jsonl") |
| 167 | b.TopicID = "b" |
| 168 | b.LastActivityAt = 10 |
| 169 | for _, row := range []SessionRecord{a, b} { |
| 170 | if err := c.UpsertSession(t.Context(), row); err != nil { |
| 171 | t.Fatal(err) |
| 172 | } |
| 173 | } |
| 174 | assertFirst := func(path string) { |
| 175 | t.Helper() |
| 176 | page, err := c.ListOrdinarySessions(t.Context(), OrdinaryPageRequest{Scope: "global", Limit: 1}) |
| 177 | if err != nil || len(page) != 1 || page[0].Path != path { |
| 178 | t.Fatalf("first=%+v err=%v want %s", page, err, path) |
| 179 | } |
| 180 | } |
| 181 | assertFirst(a.Path) |
| 182 | if err := c.SyncMetadata(t.Context(), nil, []TopicMetadata{{Scope: "global", TopicID: "b", Title: "Pinned", Pinned: true}}); err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | assertFirst(b.Path) |
| 186 | b.TopicID = "moved" |
| 187 | if err := c.UpsertSession(t.Context(), b); err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | assertFirst(a.Path) |
| 191 | if err := c.SyncMetadata(t.Context(), nil, []TopicMetadata{{Scope: "global", TopicID: "new", Title: "New pinned", Pinned: true}}); err != nil { |
| 192 | t.Fatal(err) |
| 193 | } |
| 194 | b.TopicID = "new" |
| 195 | b.Path = filepath.Join(dir, "new.jsonl") |
| 196 | if err := c.UpsertSession(t.Context(), b); err != nil { |
| 197 | t.Fatal(err) |
| 198 | } |
| 199 | assertFirst(b.Path) |
| 200 | } |
| 201 | |
| 202 | func TestOrdinaryPageTimeFilterUsesFrozenInclusiveBoundary(t *testing.T) { |
| 203 | c, err := Open(t.Context(), Options{InMemory: true, MetadataOnly: true}) |
| 204 | if err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | defer c.Close(context.Background()) |
| 208 | dir := t.TempDir() |
| 209 | for i, times := range [][2]int64{{99, 99}, {100, 90}, {80, 100}, {90, 120}} { |
| 210 | r := SessionRecord{Path: filepath.Join(dir, fmt.Sprintf("%d.jsonl", i)), Directory: dir, Scope: "global", TopicID: "shared", |
| 211 | CreatedAt: times[0], LastActivityAt: times[1], Health: HealthOK, TurnsState: TurnsUnknown, OrdinaryVisible: true} |
| 212 | if err := c.UpsertSession(t.Context(), r); err != nil { |
| 213 | t.Fatal(err) |
| 214 | } |
| 215 | } |
| 216 | for _, mode := range []string{"activity", "created"} { |
| 217 | req := OrdinaryPageRequest{Scope: "global", SortMode: mode, Limit: 1, MinActivity: 100} |
| 218 | seen := map[string]bool{} |
| 219 | for { |
| 220 | page, err := c.ListOrdinarySessions(t.Context(), req) |
| 221 | if err != nil { |
| 222 | t.Fatal(err) |
| 223 | } |
| 224 | if len(page) == 0 { |
| 225 | break |
| 226 | } |
| 227 | name := filepath.Base(page[0].Path) |
| 228 | if seen[name] || name == "0.jsonl" || len(seen) > 3 { |
| 229 | t.Fatalf("%s repeated or admitted a pre-boundary record: %s", mode, name) |
| 230 | } |
| 231 | seen[name] = true |
| 232 | req.Cursor = page[0].Cursor |
| 233 | } |
| 234 | if len(seen) != 3 { |
| 235 | t.Fatalf("%s omitted inclusive created/activity boundary: %v", mode, seen) |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 |