//go:build unix // Model and extension selection cross the composer, keyboard, and daemon // session. Stubbed command replies cannot catch a dropped request or reload. package e2e import ( "encoding/json" "fmt" "os" "path/filepath" "slices" "strings" "testing" "unicode/utf8" "albedo/cli/internal/daemon" "albedo/cli/internal/tui" tea "charm.land/bubbletea/v2" "github.com/charmbracelet/x/ansi" ) func TestTUISuggestsAndInvokesASkillBeforeTheFirstMessage(t *testing.T) { profile := providerRoute(t, echoReply) workspace := t.TempDir() skill := filepath.Join(workspace, ".agents", "skills", "first-turn-skill") if err := os.MkdirAll(skill, 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(skill, "SKILL.md"), []byte("---\nname: first-turn-skill\ndescription: A skill available before chatting.\n---\nFIRST_TURN_SKILL_ACTIVATED\n"), 0600); err != nil { t.Fatal(err) } session := daemonSession(t, newSession(t, workspace)) d := driveTUI(t, &session) defer d.App.Chat.Close() d.Dispatch(tui.CapabilityPageChangedMsg{}) d.Type("/first-turn") if view := d.View(); !strings.Contains(view, "/first-turn-skill") || !strings.Contains(view, "A skill available before chatting.") { t.Fatalf("fresh session has no skill suggestion:\n%s", view) } if requests := suite.provider.requests(profile); len(requests) != 0 { t.Fatal("opening autocomplete sent a model request") } d.Key(tea.KeyTab) if got := d.App.Chat.TextArea.Value(); got != "/first-turn-skill" { t.Fatalf("Tab completed %q", got) } d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEnter}) waitIdle(t, session.ID, profile, 1) requests := suite.provider.requests(profile) encoded, err := json.Marshal(requests) if err != nil { t.Fatal(err) } if !strings.Contains(string(encoded), "FIRST_TURN_SKILL_ACTIVATED") { t.Fatalf("the first message did not activate the selected skill: %s", encoded) } } func TestTUICompletesAnInlineSkillWithoutReplacingTheMessage(t *testing.T) { profile := providerRoute(t, echoReply) workspace := t.TempDir() skill := filepath.Join(workspace, ".agents", "skills", "review-patch") if err := os.MkdirAll(skill, 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(skill, "SKILL.md"), []byte("---\nname: review-patch\ndescription: Review the patch.\n---\nINLINE_SKILL_INSTRUCTIONS\n"), 0600); err != nil { t.Fatal(err) } session := daemonSession(t, newSession(t, workspace)) d := driveTUI(t, &session) defer d.App.Chat.Close() d.Dispatch(tui.CapabilityPageChangedMsg{}) d.connected() moveLeft := func(text string) { for range utf8.RuneCountInString(text) { d.Key(tea.KeyLeft) } } suggested := func() bool { view := d.View() return strings.Contains(view, "$review-patch") && strings.Contains(view, "Review the patch.") } draft, later, after := "Check 界 with $rev", " and $rev", " on this patch." d.Type(draft + later + after) moveLeft(after) if !suggested() { t.Fatalf("missing inline suggestion:\n%s", d.View()) } // Escape closes only this mention's menu; the other mention still offers one. d.Key(tea.KeyEscape) if suggested() || d.App.Chat.TextArea.Value() != draft+later+after { t.Fatalf("escape left the menu open or changed the draft %q:\n%s", d.App.Chat.TextArea.Value(), d.View()) } moveLeft(later) if !suggested() { t.Fatalf("escape on one mention hid another's suggestion:\n%s", d.View()) } d.Key(tea.KeyTab) want := "Check 界 with $review-patch" + later + after if got := d.App.Chat.TextArea.Value(); got != want { t.Fatalf("completion replaced surrounding text: %q", got) } d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEnter}) waitIdle(t, session.ID, profile, 1) request := suite.provider.requests(profile)[0]["body"].(map[string]any) if !strings.Contains(lastUserText(request), "INLINE_SKILL_INSTRUCTIONS") { t.Fatal("inline skill did not reach the model") } } func TestFreshSkillCatalogPaginatesLargeDescriptions(t *testing.T) { providerRoute(t, echoReply) workspace := t.TempDir() // Enough valid metadata to exceed one catalog response's byte budget. for index := range 96 { name := fmt.Sprintf("large-skill-%03d", index) skill := filepath.Join(workspace, ".agents", "skills", name) if err := os.MkdirAll(skill, 0700); err != nil { t.Fatal(err) } metadata := "---\nname: " + name + "\ndescription: " + strings.Repeat("界", 1024) + "\n---\nInstructions\n" if err := os.WriteFile(filepath.Join(skill, "SKILL.md"), []byte(metadata), 0600); err != nil { t.Fatal(err) } } session := newSession(t, workspace) commands, err := daemon.ListSessionCommands(t.Context(), conn(t), session) if err != nil { t.Fatal(err) } for index := range 96 { name := fmt.Sprintf("/large-skill-%03d", index) if !slices.ContainsFunc(commands, func(command daemon.SessionCommand) bool { return command.Name == name }) { t.Fatalf("fresh catalog lost %s across pages", name) } } } func TestTUIEffortSelectorCommitsThroughTheDaemon(t *testing.T) { providerRoute(t, echoReply) session := daemonSession(t, newSession(t, t.TempDir())) if _, err := daemon.SelectModel(t.Context(), conn(t), session.ID, daemon.ModelSelectionRequest{Model: "o3", Effort: "high", ETag: session.ETag}); err != nil { t.Fatal(err) } session = daemonSession(t, session.ID) d := driveTUI(t, &session) // Selecting a model goes through the real composer: enter dispatches the // command, the app asks the daemon, and the answer lands in the model. d.App.Chat.TextArea.SetValue("/model o3-mini") dispatched, ok := d.Key(tea.KeyEnter).(tui.ChatExecuteCommandMsg) if !ok || dispatched.Name != "/model" || dispatched.Args != "o3-mini" { t.Fatalf("enter did not dispatch /model: %#v", dispatched) } d.Dispatch(dispatched) if d.App.ActiveSession == nil || d.App.ActiveSession.Model != "o3-mini" || d.App.ActiveSession.Effort != "high" { t.Fatalf("the daemon's model switch did not land:\n%s", d.View()) } // /effort arrives as the message the command menu submits for it; the // daemon, not a stub, decides which tiers the model offers and which one // is current. d.Dispatch(tui.ChatExecuteCommandMsg{Name: "/effort"}) opened := d.View() for _, tier := range []string{"low", "medium", "high"} { if !strings.Contains(opened, tier) { t.Fatalf("the selector is missing tier %q:\n%s", tier, opened) } } if !strings.Contains(opened, "Reasoning effort") || !strings.Contains(opened, "[high]") { t.Fatalf("the daemon's current tier is not selected:\n%s", opened) } if strings.Contains(opened, "› ") { t.Fatal("the composer must be hidden while choosing effort") } // Even a narrow selector stays inside the terminal and Escape restores the // composer without changing the daemon. d.Update(tea.WindowSizeMsg{Width: 28, Height: 14}) for line := range strings.SplitSeq(d.View(), "\n") { if width := ansi.StringWidth(line); width > 28 { t.Fatalf("effort selector rendered a %d-cell line at width 28: %q", width, line) } } d.Key(tea.KeyEscape) if view := d.View(); strings.Contains(view, "Reasoning effort") || !strings.Contains(view, "› ") { t.Fatalf("Escape did not restore the composer:\n%s", view) } d.Update(tea.WindowSizeMsg{Width: 80, Height: 22}) d.Dispatch(tui.ChatExecuteCommandMsg{Name: "/effort"}) // The keyboard alone moves the selection and commits it. d.Key(tea.KeyLeft) if !strings.Contains(d.View(), "[medium]") { t.Fatalf("left did not select medium:\n%s", d.View()) } committed, ok := d.Key(tea.KeyEnter).(tui.ChatExecuteCommandMsg) if !ok || committed.Name != "/effort" || committed.Args != "medium" { t.Fatalf("enter did not commit the selected tier: %#v", committed) } d.Dispatch(committed) settled := d.View() if strings.Contains(settled, "Reasoning effort") { t.Fatalf("the selector stayed open after committing:\n%s", settled) } if !strings.Contains(settled, "Effort saved.") || !strings.Contains(settled, "› ") { t.Fatalf("the daemon's answer did not reach the chat:\n%s", settled) } if d.App.ActiveSession.Effort != "medium" { t.Fatalf("the chat kept effort %q", d.App.ActiveSession.Effort) } // The truth at the end is the daemon's session record. session = daemonSession(t, d.App.ActiveSession.ID) if session.Model != "o3-mini" || session.Effort != "medium" { t.Fatalf("session %s kept model %q effort %q, want o3-mini at medium", session.ID, session.Model, session.Effort) } } // A model-chosen title must not invalidate the configuration held by the chat. func TestTUIEffortAfterModelNamingKeepsConfigurationValidator(t *testing.T) { profile := providerRoute(t, func(request map[string]any) string { messages := request["messages"].([]any) last := messages[len(messages)-1].(map[string]any) if last["role"] == "tool" { return "done" } return pythonCall("await commands.rename('Upload retries')") }) session := daemonSession(t, newSession(t, t.TempDir())) if _, err := daemon.SelectModel(t.Context(), conn(t), session.ID, daemon.ModelSelectionRequest{Model: "o3", Effort: "low", ETag: session.ETag}); err != nil { t.Fatal(err) } session = daemonSession(t, session.ID) d := driveTUI(t, &session) defer d.App.Chat.Close() if _, err := daemon.NewChatClient(conn(t), session.ID).Send(t.Context(), "a prompt that names the session", nil); err != nil { t.Fatal(err) } waitIdle(t, session.ID, profile, 2) if retitled := daemonSession(t, session.ID); retitled.Title == session.Title { t.Fatalf("the prompt kept the title %q", retitled.Title) } d.Dispatch(tui.ChatExecuteCommandMsg{Name: "/effort", Args: "high"}) if view := d.View(); !strings.Contains(view, "Effort saved.") { t.Fatalf("/effort after a prompt failed:\n%s", view) } if session := daemonSession(t, session.ID); session.Effort != "high" { t.Fatalf("session kept effort %q, want high", session.Effort) } } // Text after a command that declares no arguments is a prompt that starts with // the command's name; the command would only reject it. func TestTUISendsTextAfterAnArgumentlessCommandAsAPrompt(t *testing.T) { profile := providerRoute(t, echoReply) t.Parallel() session := daemonSession(t, newSession(t, t.TempDir())) // A session lists its commands once its first turn has loaded them. if _, err := daemon.NewChatClient(conn(t), session.ID).Send(t.Context(), "hello", nil); err != nil { t.Fatal(err) } waitIdle(t, session.ID, profile, 1) d := driveTUI(t, &session) defer d.App.Chat.Close() d.Dispatch(tui.CapabilityPageChangedMsg{}) jobs := slices.IndexFunc(d.App.Chat.CommandMenu.Catalog, func(command daemon.SessionCommand) bool { return command.Name == "/jobs" }) if jobs < 0 || len(d.App.Chat.CommandMenu.Catalog[jobs].Arguments) != 0 { t.Fatalf("the daemon's catalog has no argumentless /jobs: %+v", d.App.Chat.CommandMenu.Catalog) } d.connected() d.App.Chat.TextArea.SetValue("/jobs is broken") var admitted tui.ChatTurnSentMsg for _, message := range d.results(d.Update(tea.KeyPressMsg{Code: tea.KeyEnter})) { if sent, ok := message.(tui.ChatTurnSentMsg); ok { admitted = sent d.Update(sent) } } if admitted.Err != nil || !admitted.OK { t.Fatalf("enter did not send the prompt: %+v\n%s", admitted, d.View()) } waitIdle(t, session.ID, profile, 2) if reply := strings.Join(eventText(durableHistorySnapshot(t, session.ID), "message"), "\n"); !strings.Contains(reply, "echo: /jobs is broken") { t.Fatalf("the model did not receive the whole prompt; got:\n%s", reply) } } func TestTUIWebhooksShortcutOpensSettingsWithoutACatalog(t *testing.T) { providerRoute(t, echoReply) session := daemonSession(t, newSession(t, t.TempDir())) if _, err := daemon.SelectExtension(t.Context(), conn(t), session.ID, daemon.ExtensionSelectionRequest{Name: "webhooks", Scope: "session", Enabled: new(false), ETag: session.ETag}); err != nil { t.Fatal(err) } session = daemonSession(t, session.ID) d := driveTUI(t, &session) defer d.App.Chat.Close() d.connected() d.Type("/web") if view := d.View(); !strings.Contains(view, "/webhooks") { t.Fatalf("the command menu has no webhooks settings shortcut:\n%s", view) } d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEnter}) if d.App.State != tui.AppStateWebhooksPage || !strings.Contains(d.App.WebhooksPage.Error, "webhooks are off") { t.Fatalf("the disabled extension did not open its settings guidance:\n%s", d.View()) } d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEscape}) if d.App.State != tui.AppStateChat { t.Fatalf("Escape did not return to chat:\n%s", d.View()) } enableExtension(t, conn(t), session.ID, "webhooks") d.Type("/webhooks") // Dismiss completion so Enter exercises local command recognition too. d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEscape}) d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEnter}) if d.App.State != tui.AppStateWebhooksPage || d.App.WebhooksPage.Error != "" || d.App.WebhooksPage.Loading { t.Fatalf("the typed shortcut did not load webhooks settings:\n%s", d.View()) } } func TestTUIExtensionPickerSwitchesCompactionStrategies(t *testing.T) { profile := providerRoute(t, echoReply) t.Parallel() session := daemonSession(t, newSession(t, t.TempDir())) d := driveTUI(t, &session) defer d.App.Chat.Close() for turn, name := range []string{"coding-lcm", "rolling", "coding-lcm"} { d.Dispatch(tui.ChatExecuteCommandMsg{Name: "/extensions"}) picker := d.App.ExtensionPicker index := slices.IndexFunc(picker.Extensions, func(item tui.ExtensionItem) bool { return item.Name == name }) if index < 0 { t.Fatalf("strategy %s is missing from the actual extension picker", name) } if picker.Extensions[index].Enabled { t.Fatalf("strategy %s is already enabled before its switch", name) } d.Type(name) d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEnter, Mod: tea.ModShift}) d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEnter}) if picker := d.App.ExtensionPicker; picker.Error != "" || picker.Confirming() || !strings.Contains(picker.Notice, "Reload completed.") { t.Fatalf("strategy %s did not apply through the picker:\n%s", name, d.View()) } configuration, err := daemon.GetSessionConfiguration(t.Context(), conn(t), session.ID) if err != nil { t.Fatal(err) } for _, strategy := range []string{"coding-lcm", "rolling"} { chosen := configuration.Value.Selection.Extensions[strategy] if enabled := chosen != nil && *chosen; enabled != (strategy == name) { t.Fatalf("single %s toggle left competing selection %+v", name, configuration.Value.Selection.Extensions) } } client := daemon.NewChatClient(conn(t), session.ID) if _, err := client.Send(t.Context(), "use the selected strategy", nil); err != nil { t.Fatal(err) } waitIdle(t, session.ID, profile, turn+1) prepared, err := daemon.GetContextSnapshot(t.Context(), conn(t), session.ID) if err != nil || prepared.State != "ready" || prepared.Compaction.Strategy != name { t.Fatalf("turn did not use selected strategy %s: %+v, %v", name, prepared, err) } if turn == 0 { current := daemonSession(t, session.ID) if _, err := daemon.RenameSession(t.Context(), conn(t), session.ID, "strategy review", current.ETag); err != nil { t.Fatal(err) } // Another client edited this session while the picker was open. // A stale confirmation refreshes the choices without replaying it. d.Dispatch(tea.KeyPressMsg{Code: 'u', Mod: tea.ModCtrl}) d.Type("rolling") d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEnter, Mod: tea.ModShift}) d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEnter}) picker := d.App.ExtensionPicker if picker.Loading || picker.Saving || picker.Confirming() || picker.Error != "" || !strings.Contains(picker.Notice, "Review the refreshed choices") { t.Fatalf("stale selection did not return to review:\n%s", d.View()) } configuration, err := daemon.GetSessionConfiguration(t.Context(), conn(t), session.ID) if err != nil || configuration.Value.Selection.Extensions[name] == nil || !*configuration.Value.Selection.Extensions[name] { t.Fatalf("stale confirmation changed the saved strategy: %+v, %v", configuration, err) } } d.Dispatch(tea.KeyPressMsg{Code: tea.KeyEscape}) } }