| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestSyncMetadataKeepsTopicTitleIndependentFromSessionNote(t *testing.T) { |
| 10 | t.Parallel() |
| 11 | ctx := context.Background() |
| 12 | catalog, err := Open(ctx, Options{Path: filepath.Join(t.TempDir(), "catalog.sqlite"), DisableRepair: true}) |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | t.Cleanup(func() { _ = catalog.Close(context.Background()) }) |
| 17 | record := SessionRecord{ |
| 18 | Path: "/sessions/titled.jsonl", Directory: "/sessions", Scope: "global", |
| 19 | TopicID: "titled", TopicTitle: "Original topic", CustomTitle: "Explicit session title", |
| 20 | LastActivityAt: 2, Turns: 1, TurnsState: TurnsValid, Health: HealthOK, |
| 21 | } |
| 22 | if err := catalog.UpsertSession(ctx, record); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | if err := catalog.SyncMetadata(ctx, nil, []TopicMetadata{{ |
| 26 | Scope: "global", TopicID: "titled", Title: "Changed topic", TitleSource: "auto", |
| 27 | }}); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | topic, ok, err := catalog.GetTopic(ctx, TopicKey{Scope: "global", TopicID: "titled"}) |
| 31 | if err != nil || !ok || topic.Title != "Changed topic" || topic.TitleSource != "auto" { |
| 32 | t.Fatalf("topic title after metadata sync = %+v, ok=%v, err=%v", topic, ok, err) |
| 33 | } |
| 34 | page, err := catalog.ListTopics(ctx, TopicPageRequest{Scope: "global", Limit: 10}) |
| 35 | if err != nil || len(page.Items) != 1 || page.Items[0].Title != "Changed topic" || page.Items[0].TitleSource != "auto" || |
| 36 | len(page.Items[0].Sessions) != 1 || page.Items[0].Sessions[0].CustomTitle != "Explicit session title" { |
| 37 | t.Fatalf("listed topic/session titles = %+v, err=%v", page.Items, err) |
| 38 | } |
| 39 | record.CustomTitle = "" |
| 40 | record.TopicTitle = "Session-side topic title" |
| 41 | if err := catalog.UpsertSession(ctx, record); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if err := catalog.SyncMetadata(ctx, nil, []TopicMetadata{{ |
| 45 | Scope: "global", TopicID: "titled", Title: "Renamed topic", TitleSource: "manual", |
| 46 | }}); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | topic, ok, err = catalog.GetTopic(ctx, TopicKey{Scope: "global", TopicID: "titled"}) |
| 50 | if err != nil || !ok || topic.Title != "Renamed topic" || topic.TitleSource != "manual" || |
| 51 | len(topic.Sessions) != 1 || topic.Sessions[0].CustomTitle != "" { |
| 52 | t.Fatalf("independent cleared session note = %+v, ok=%v, err=%v", topic, ok, err) |
| 53 | } |
| 54 | } |
| 55 |