| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestMatchingOrdinaryPagesKeepSnapshotAndCancelBetweenCandidates(t *testing.T) { |
| 13 | c, err := Open(t.Context(), Options{Path: filepath.Join(t.TempDir(), "catalog.sqlite"), MetadataOnly: true, StartPaused: true}) |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | t.Cleanup(func() { _ = c.Close(context.Background()) }) |
| 18 | root := t.TempDir() |
| 19 | for i := range 137 { |
| 20 | title := "irrelevant" |
| 21 | if i == 1 || i == 80 || i == 135 { |
| 22 | title = "ÜBER 历史" |
| 23 | } |
| 24 | err := c.UpsertSession(t.Context(), SessionRecord{Path: filepath.Join(root, fmt.Sprintf("%03d.jsonl", i)), Directory: root, |
| 25 | Scope: "global", TopicID: fmt.Sprint(i), TopicTitle: title, LastActivityAt: int64(i + 1), OrdinaryVisible: true, Health: HealthOK}) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | } |
| 30 | lease, err := c.OpenReadLease(t.Context()) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | defer lease.Close() |
| 35 | ctx := lease.Context(t.Context()) |
| 36 | match := func(record OrdinaryRecord) bool { return strings.Contains(strings.ToLower(record.Title), "über") } |
| 37 | first, err := c.ListMatchingOrdinarySessions(ctx, OrdinaryPageRequest{Scope: "global", Limit: 2}, match) |
| 38 | if err != nil || len(first) != 2 || first[0].TopicID != "135" || first[1].TopicID != "80" { |
| 39 | t.Fatalf("sparse first page: %+v %v", first, err) |
| 40 | } |
| 41 | // A new match belongs only to a new database view. |
| 42 | err = c.UpsertSession(t.Context(), SessionRecord{Path: filepath.Join(root, "079.jsonl"), Directory: root, Scope: "global", |
| 43 | TopicID: "79", TopicTitle: "ÜBER new", LastActivityAt: 80, OrdinaryVisible: true, Health: HealthOK}) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | last, err := c.ListMatchingOrdinarySessions(ctx, OrdinaryPageRequest{Scope: "global", Limit: 2, Cursor: first[1].Cursor}, match) |
| 48 | if err != nil || len(last) != 1 || last[0].TopicID != "1" { |
| 49 | t.Fatalf("sparse continuation lost its snapshot: %+v %v", last, err) |
| 50 | } |
| 51 | readCtx, cancel := context.WithCancel(ctx) |
| 52 | defer cancel() |
| 53 | calls := 0 |
| 54 | _, err = c.ListMatchingOrdinarySessions(readCtx, OrdinaryPageRequest{Scope: "global", Limit: 2}, func(OrdinaryRecord) bool { |
| 55 | calls++ |
| 56 | cancel() |
| 57 | return false |
| 58 | }) |
| 59 | if !errors.Is(err, context.Canceled) || calls != 1 { |
| 60 | t.Fatalf("canceled metadata filter continued: calls=%d err=%v", calls, err) |
| 61 | } |
| 62 | } |
| 63 |