package claude import ( "context" "os" "slices" "testing" ) func TestCLIArgsNameTheSessionWithItsTitle(t *testing.T) { chat := &Chat{sessions: map[string]*session{}} s := chat.sessionFor("id-1", "/fixture") args, _ := s.cliArgs(false) if slices.Contains(args, "--name") { t.Fatalf("an untitled session must not pass --name: %q", args) } s.title = " Fix the\nlogin page " args, _ = s.cliArgs(false) if got := argValue(args, "--name"); got != "Fix the login page" { t.Fatalf("--name = %q, want the title on one line: %q", got, args) } if argValue(args, "--session-id") != "id-1" { t.Fatalf("a new session must pin its id: %q", args) } args, _ = s.cliArgs(true) if argValue(args, "--resume") != "id-1" || argValue(args, "--name") != "Fix the login page" { t.Fatalf("a resumed session must keep its id and name: %q", args) } } func TestSetTitleReachesExistingAndRebuiltSessions(t *testing.T) { ctx := context.Background() chat := &Chat{sessions: map[string]*session{}} // A title recorded before the session is rebuilt (a server restart) is // applied when sessionFor creates it. chat.SetTitle(ctx, "id-1", "From the registry") if got := chat.sessionFor("id-1", "/fixture").title; got != "From the registry" { t.Fatalf("rebuilt session title = %q", got) } // A rename updates a live session, and survives an idle release. chat.SetTitle(ctx, "id-1", "Renamed") if got := chat.session("id-1").title; got != "Renamed" { t.Fatalf("live session title = %q", got) } chat.forget("id-1") if got := chat.sessionFor("id-1", "/fixture").title; got != "Renamed" { t.Fatalf("title after release = %q", got) } // A blank title never erases the name. chat.SetTitle(ctx, "id-1", " ") if got := chat.session("id-1").title; got != "Renamed" { t.Fatalf("blank rename changed the title to %q", got) } } func TestSpawnPassesTheTitleAndARenameReachesTheNextSpawn(t *testing.T) { ctx := context.Background() chat := testChat(t) session := newCreatedChat(t, chat) frames := subscribe(t, chat, session.ID) if err := chat.Prompt(ctx, session.ID, "hello"); err != nil { t.Fatal(err) } waitStepEnded(t, frames) logPath := os.Getenv("FAKE_CLAUDE_LOG") entries := readArgs(t, logPath) if got := argValue(entries[len(entries)-1].Args, "--name"); got != "Fixture" { t.Fatalf("first spawn --name = %q: %+v", got, entries) } chat.SetTitle(ctx, session.ID, "Renamed chat") chat.session(session.ID).closeProcess() if err := chat.Prompt(ctx, session.ID, "hello again"); err != nil { t.Fatal(err) } waitStepEnded(t, frames) entries = readArgs(t, logPath) last := entries[len(entries)-1].Args if argValue(last, "--resume") != session.ID || argValue(last, "--name") != "Renamed chat" { t.Fatalf("respawn after rename = %q", last) } }