package tui // These unit tests read Bubble Tea's title metadata, which the daemon E2E // tests cannot see: terminal capture holds only the rendered body. import ( "albedo/cli/internal/daemon" "strconv" "strings" "testing" ) func TestWindowTitleTracksActiveSession(t *testing.T) { t.Setenv("ALBEDO_WINDOW_TITLE", "id") conn := daemon.NewConnection(daemon.ConnectionSnapshot{Port: 1}, nil) first := daemon.Session{ID: "01901234-1234-5678-9012-123456789012", Title: "First"} second := daemon.Session{ID: "01901234-1234-5678-9012-123456789013", Title: "Second"} app := NewAppModel(conn, Bootstrap{Sessions: []daemon.Session{first, second}}, &first, "", false, nil) t.Cleanup(func() { app.Chat.Close() }) assertTitle := func(name, id string) { t.Helper() if got, want := app.View().WindowTitle, name+" — albedo ["+id+"]"; got != want { t.Fatalf("title = %q, want %q", got, want) } } assertTitle("First", first.ID) first.Title = "Renamed" app.Update(sessionRenamedMsg{Session: first}) assertTitle("Renamed", first.ID) // Renaming another session must not retitle this window. second.Title = "Other renamed" app.Update(sessionRenamedMsg{Session: second}) assertTitle("Renamed", first.ID) app.State = AppStateSessionPicker app.Update(PickerSelectMsg{ID: second.ID}) assertTitle("Other renamed", second.ID) snapshot := second snapshot.Title = "Snapshot name" app.Update(ChatStreamEventMsg{SessionID: second.ID, Generation: app.Chat.Generation, Event: daemon.StreamEvent{Type: daemon.EventReset, Snapshot: &snapshot}}) assertTitle("Snapshot name", second.ID) // A reset from an old stream must not rename the newly selected session. app.Update(ChatStreamEventMsg{SessionID: first.ID, Generation: app.Chat.Generation, Event: daemon.StreamEvent{Type: daemon.EventReset, Snapshot: &first}}) assertTitle("Snapshot name", second.ID) snapshot.Title = "Listing name" app.Update(sessionsLoadedMsg{Gen: app.SessionGen, Sessions: []daemon.Session{snapshot}}) assertTitle("Listing name", second.ID) } func TestWindowTitleSanitizesUntrustedNames(t *testing.T) { for _, tc := range []struct{ name, want string }{ {"", "albedo [full-session-id]"}, {" \t\n\r\x00\x07\x1b\u009c\u202e ", "albedo [full-session-id]"}, {"\x1b[31mRose\x1b[0m\x07\r\n\t\u202e café", "Rose café — albedo [full-session-id]"}, {"Name\x1b]2;injected title\x07", "Name — albedo [full-session-id]"}, {"a\u009b31mb\u2028c\u2066d\u200be", "a31mbcde — albedo [full-session-id]"}, {strings.Repeat("界", 200), strings.Repeat("界", 120) + " — albedo [full-session-id]"}, } { t.Run(strconv.Quote(tc.name), func(t *testing.T) { got := windowTitle(titleWithID, &daemon.Session{ID: "full-session-id", Title: tc.name}) if got != tc.want { t.Fatalf("title = %q, want %q", got, tc.want) } }) } }