// The /cd checklist's keys and the finish it drives against a fake daemon. // The e2e suite has no second host to move to; these pin that the checklist // is one choice, all or later, that later leaves the move resumable from the // chat and from /cd, that a partial failure keeps the box with each // binding's status to retry, and that a long checklist fits the screen. package tui import ( "context" "errors" "fmt" "io" "net/http" "slices" "strings" "sync" "testing" "albedo/cli/internal/attach" "albedo/cli/internal/daemon" tea "charm.land/bubbletea/v2" "github.com/charmbracelet/x/ansi" ) func TestCDChecklistIsOneChoiceAllOrLater(t *testing.T) { q := newCDQuestion("a", cdShotTarget, nil) checklistShot(q, cdShotBindings()) for _, key := range []tea.KeyPressMsg{typed("y"), {Code: tea.KeySpace}} { if q.key(key) != cdNothing { t.Fatalf("%q answered the checklist", key.String()) } } if q.key(keyEnter) != cdFinish { t.Fatal("enter on the first line must finish") } if q.key(keyDown); q.key(keyEnter) != cdLater { t.Fatal("↓ then enter must be later") } if q.key(keyUp); q.key(keyEsc) != cdLater { t.Fatal("esc must be later") } } func TestCDChecklistFitsAShortScreen(t *testing.T) { q := newCDQuestion("a", cdShotTarget, nil) var bindings []daemon.MoveBinding for i := range 30 { bindings = append(bindings, daemon.MoveBinding{Kind: "schedule", ID: fmt.Sprint(i), Label: fmt.Sprintf("job %d", i), Detail: "every 1h · a prompt"}) } checklistShot(q, bindings) box := q.view(80, 24) text := ansi.Strip(strings.Join(box, "\n")) if len(box) > 24 || !strings.Contains(text, "more") || !strings.Contains(text, "later") { t.Fatalf("%d rows:\n%s", len(box), text) } } // checklistDaemon is a fake source daemon whose move reaches its checklist; // finishes answers each finish in turn. type checklistDaemon struct { finishes []string reads int mu sync.Mutex } func checklistMove(state string, statuses ...string) string { var bindings []string for i, status := range statuses { bindings = append(bindings, fmt.Sprintf(`{"kind":"schedule","id":"s%d","label":"job %d","detail":"every 1h","old_url":null,"new_url":null,"status":%q,"error":null}`, i, i, status)) } errText := "null" if slices.Contains(statuses, "failed") { errText = `{"code":"disarm_failed","detail":"1 could not be disarmed"}` } return `{"id":"t1","session_id":"a","target":"` + cdShotTarget + `","state":"` + state + `","progress":{"bytes_done":1,"bytes_total":1},"error":` + errText + `,"reason":null,"target_session_id":"a","target_home":"/home/ana/.albedo","lost":[],"bindings":[` + strings.Join(bindings, ",") + `]}` } func (d *checklistDaemon) serve(t *testing.T) *daemon.Connection { return commandTestConnection(t, func(w http.ResponseWriter, r *http.Request) { d.mu.Lock() defer d.mu.Unlock() w.Header().Set("Content-Type", "application/json") switch { case r.Method == http.MethodPost && r.URL.Path == "/transfers/t1/finish": if len(d.finishes) == 0 { t.Error("finished more often than scripted") w.WriteHeader(http.StatusConflict) return } _, _ = io.WriteString(w, d.finishes[0]) d.finishes = d.finishes[1:] case r.Method == http.MethodGet && r.URL.Path == "/transfers/t1": d.reads++ _, _ = io.WriteString(w, checklistMove("checklist", "pending")) default: t.Errorf("unexpected %s %s", r.Method, r.URL.Path) w.WriteHeader(http.StatusNotFound) } }) } // pendingApp is a chat on session a, its move to vassago waiting in its // checklist. func pendingApp(t *testing.T, conn *daemon.Connection) *AppModel { session := cdShotSession() session.MovePending = &daemon.SessionMovePending{TransferID: "t1", Host: "vassago"} app := NewAppModel(conn, Bootstrap{Sessions: []daemon.Session{session}}, &session, shotHere, false, nil) t.Cleanup(func() { app.Chat.Close() }) app.Update(tea.WindowSizeMsg{Width: 120, Height: 30}) return app } func TestCDChecklistLaterLeavesTheMoveToResumeFromTheChatAndCD(t *testing.T) { fake := &checklistDaemon{} app := pendingApp(t, fake.serve(t)) if !strings.Contains(strings.Join(app.Chat.movedRows(120), "\n"), "moving to vassago · checklist pending") { t.Fatal("the chat does not say its move waits") } _, cmd := app.Update(keyEnter) settleCD(t, app, cmd) if app.cd == nil || app.cd.phase != cdChecklist || fake.reads != 1 { t.Fatal("enter on an empty composer must reopen the checklist, read afresh") } if app.cd.target != cdShotTarget { t.Fatalf("the reopened box names %q", app.cd.target) } app.Update(keyEsc) if app.cd != nil || app.ActiveSession.MovePending == nil || app.Chat.movePending == nil { t.Fatal("later must close the box and leave the move pending") } _, cmd = app.Update(ChatExecuteCommandMsg{Name: "/cd", Args: "elsewhere"}) settleCD(t, app, cmd) if app.cd == nil || app.cd.phase != cdChecklist || fake.reads != 2 { t.Fatal("/cd on a session whose move waits must reopen its checklist") } } func TestCDChecklistRetriesAPartialFinishThenOpensTheSessionThere(t *testing.T) { fake := &checklistDaemon{finishes: []string{ checklistMove("checklist", "disarmed", "failed"), checklistMove("active", "disarmed", "disarmed"), }} app := pendingApp(t, fake.serve(t)) var opened []string app.openAttachment = func(_ context.Context, host, home string) (*attach.Attachment, error) { opened = append(opened, host+" "+home) return nil, errors.New("connection refused") } _, cmd := app.Update(keyEnter) settleCD(t, app, cmd) _, cmd = app.Update(keyEnter) settleCD(t, app, cmd) if app.cd == nil || app.cd.phase != cdChecklist { t.Fatal("a partial finish must keep the checklist") } box := ansi.Strip(strings.Join(app.cd.view(120, 30), "\n")) for _, want := range []string{"✓ schedule", "✗ schedule", "1 could not be disarmed", "retry"} { if !strings.Contains(box, want) { t.Fatalf("box lacks %q:\n%s", want, box) } } _, cmd = app.Update(keyEnter) settleCD(t, app, cmd) if len(fake.finishes) != 0 || len(opened) != 1 || opened[0] != "vassago /home/ana/.albedo" { t.Fatalf("finishes left %d, attached to %q", len(fake.finishes), opened) } if app.cd != nil || app.ActiveSession.Moved == nil || app.ActiveSession.MovePending != nil { t.Fatal("a finished move marks the session moved and no longer pending") } }