package server import ( "context" "path/filepath" "sync" "testing" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) // titledChat records the titles the server hands to a harness that names its // own sessions (Claude Code's --name). type titledChat struct { *fakeChat titleMu sync.Mutex titles []string } func (c *titledChat) SetTitle(_ context.Context, _, title string) { c.titleMu.Lock() c.titles = append(c.titles, title) c.titleMu.Unlock() } func (c *titledChat) lastTitle() string { c.titleMu.Lock() defer c.titleMu.Unlock() if len(c.titles) == 0 { return "" } return c.titles[len(c.titles)-1] } func TestChatRenameReachesTheHarnessAndARestartLearnsTheRegistryTitle(t *testing.T) { ctx := context.Background() store, err := pipeline.Open(filepath.Join(t.TempDir(), "chat.db")) if err != nil { t.Fatal(err) } id := "01900000-0000-7000-8000-000000000003" if err := store.RecordOperatorSession(ctx, id, "/fixture", "Original", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } if err := store.SetOperatorSessionBackend(ctx, id, "claude"); err != nil { t.Fatal(err) } // First routing after a restart hands the registry title to the harness. fake := &titledChat{fakeChat: scriptedChat()} manager := &chatManager{claude: fake, registry: store, cache: map[string]string{}} if _, err := manager.brokerFor(ctx, id); err != nil { t.Fatal(err) } if got := fake.lastTitle(); got != "Original" { t.Fatalf("routed session title = %q, want the registry title", got) } var reply protocol.Message capture := func(msg protocol.Message) error { reply = msg; return nil } handleChatRename(ctx, protocol.Message{ID: "rename", ChatRename: &protocol.ChatRenameRequest{SessionID: id, Title: " Renamed "}}, manager, store, nil, "client", capture) if reply.Type != protocol.ChatRename || reply.ChatSession == nil || reply.ChatSession.Title != "Renamed" { t.Fatalf("rename reply = %+v", reply) } if got := fake.lastTitle(); got != "Renamed" { t.Fatalf("harness title after rename = %q", got) } // A restarted server learns the renamed title from the registry. restarted := &titledChat{fakeChat: scriptedChat()} manager = &chatManager{claude: restarted, registry: store, cache: map[string]string{}} if _, err := manager.brokerFor(ctx, id); err != nil { t.Fatal(err) } if got := restarted.lastTitle(); got != "Renamed" { t.Fatalf("title after restart = %q", got) } // A refused rename never reaches the harness. handleChatRename(ctx, protocol.Message{ID: "blank", ChatRename: &protocol.ChatRenameRequest{SessionID: id, Title: " "}}, manager, store, nil, "client", capture) if reply.Type != protocol.Error || restarted.lastTitle() != "Renamed" { t.Fatalf("blank rename = %+v, harness title %q", reply, restarted.lastTitle()) } }