diff --git a/app/notes/terminal-panel.md b/app/notes/terminal-panel.md index 05c3e9a2..44683a39 100644 --- a/app/notes/terminal-panel.md +++ b/app/notes/terminal-panel.md @@ -198,7 +198,7 @@ data?, after?}`. `internal/pipeline/terminals.go`, `store.go`; `internal/server/terminal_{manager,supervise,process,login,legacy_tmux,clients,title,cwd,handlers}.go`, `websocket.go`, `cmd/pipelines-server/main.go`; tests `terminal_ws_test.go`, - `terminal_login_test.go`, `terminal_legacy_tmux_test.go`, + `terminal_login_test.go`, `terminal_lifetime_test.go`, `terminal_legacy_tmux_test.go`, `internal/pipeline/terminals_test.go`; `docs/cli.md`. - App: `lib/features/terminal/` (`terminal_{info,stream,tab,hub,listing,machines,layout,scope,surface,stage,focus,shortcuts,panel,float,float_frame,page,body,tab_strip,menu,toggle_button,open_action}.dart`), `lib/application/app_terminals.dart`, `pipelines_app.dart`, diff --git a/internal/server/terminal_lifetime_test.go b/internal/server/terminal_lifetime_test.go new file mode 100644 index 00000000..2bd627a4 --- /dev/null +++ b/internal/server/terminal_lifetime_test.go @@ -0,0 +1,109 @@ +//go:build darwin || linux + +package server + +import ( + "context" + "strings" + "testing" + "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// A terminal ends with its server: the stop is recorded as ended, and a +// record a crashed (or tmux-era) server left running is listed as ended +// after the restart, saying why. +func TestTerminalEndsWithServer(t *testing.T) { + terminalHome(t) + store := newOperatorStore(t) + first, stop := newTestTerminals(t, store) + term, err := first.Open(context.Background(), "", "", 0, 0) + if err != nil { + t.Fatal(err) + } + if err := first.Input(term.ID, fakeSuPassword+"\r"); err != nil { + t.Fatal(err) + } + stop() + first.Wait() + crashed := protocol.TerminalRecord{ID: "term_crashed", Cwd: "/", CreatedAt: 1, Status: protocol.TerminalRunning, Cols: 80, Rows: 24} + if err := store.SaveTerminal(context.Background(), crashed, 0); err != nil { + t.Fatal(err) + } + second, _ := newTestTerminals(t, store) + list, err := second.List(context.Background()) + if err != nil { + t.Fatal(err) + } + reasons := map[string]string{} + for _, record := range list { + if record.Status != protocol.TerminalExited || record.EndedAt == 0 { + t.Fatalf("record = %+v", record) + } + reasons[record.ID] = record.Reason + } + if len(reasons) != 2 || reasons[term.ID] != terminalStopReason || reasons["term_crashed"] != terminalRestartReason { + t.Fatalf("reasons = %+v", reasons) + } + if _, err := second.Close(context.Background(), "term_crashed"); err != nil { + t.Fatal(err) + } + if list, _ := second.List(context.Background()); len(list) != 1 { + t.Fatalf("list after closing an ended terminal = %+v", list) + } +} + +// The window title the shell sets is followed across reads and recorded. +func TestTerminalTitles(t *testing.T) { + var titles terminalTitles + if _, ok := titles.feed([]byte("plain \x1b[1mtext")); ok { + t.Fatal("title from plain text") + } + if _, ok := titles.feed([]byte("\x1b]0;~/src")); ok { + t.Fatal("title before its end") + } + if title, ok := titles.feed([]byte("/app\x07more")); !ok || title != "~/src/app" { + t.Fatalf("title = %q %v", title, ok) + } + if title, ok := titles.feed([]byte("\x1b]2;vim\x01 x\x1b\\")); !ok || title != "vim x" { + t.Fatalf("ST title = %q %v", title, ok) + } + if _, ok := titles.feed([]byte("\x1b]8;;https://x\x07")); ok { + t.Fatal("a hyperlink is not a title") + } + if _, ok := titles.feed(terminalLoginMarker); ok { + t.Fatal("the login marker is not a title") + } + if title, _ := titles.feed([]byte("\x1b]0;" + strings.Repeat("é", protocol.TerminalTitleLimit) + "\x07")); len(title) > protocol.TerminalTitleLimit { + t.Fatalf("title of %d bytes", len(title)) + } + terminalHome(t) + m, _ := newTestTerminals(t, newOperatorStore(t)) + term, err := m.Open(context.Background(), "", "", 0, 0) + if err != nil { + t.Fatal(err) + } + if err := m.Input(term.ID, fakeSuPassword+"\r"); err != nil { + t.Fatal(err) + } + if err := m.Input(term.ID, "printf '\\033]0;build-%s\\007' ok\r"); err != nil { + t.Fatal(err) + } + waitTerminalTitle(t, m, "build-ok") +} + +func waitTerminalTitle(t *testing.T, m *terminalManager, want string) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for { + list, _ := m.List(context.Background()) + if len(list) == 1 && list[0].Title == want { + return + } + if time.Now().After(deadline) { + t.Fatalf("list = %+v", list) + } + time.Sleep(50 * time.Millisecond) + } +} diff --git a/internal/server/terminal_login_test.go b/internal/server/terminal_login_test.go index 55ebaaae..e4d8ff3a 100644 --- a/internal/server/terminal_login_test.go +++ b/internal/server/terminal_login_test.go @@ -234,100 +234,3 @@ func TestTerminalRefusesUnpromptedLogin(t *testing.T) { t.Fatalf("%d refused terminals still running", running) } } - -// A terminal ends with its server: the stop is recorded as ended, and a -// record a crashed (or tmux-era) server left running is listed as ended -// after the restart, saying why. -func TestTerminalEndsWithServer(t *testing.T) { - terminalHome(t) - store := newOperatorStore(t) - first, stop := newTestTerminals(t, store) - term, err := first.Open(context.Background(), "", "", 0, 0) - if err != nil { - t.Fatal(err) - } - if err := first.Input(term.ID, fakeSuPassword+"\r"); err != nil { - t.Fatal(err) - } - stop() - first.Wait() - crashed := protocol.TerminalRecord{ID: "term_crashed", Cwd: "/", CreatedAt: 1, Status: protocol.TerminalRunning, Cols: 80, Rows: 24} - if err := store.SaveTerminal(context.Background(), crashed, 0); err != nil { - t.Fatal(err) - } - second, _ := newTestTerminals(t, store) - list, err := second.List(context.Background()) - if err != nil { - t.Fatal(err) - } - reasons := map[string]string{} - for _, record := range list { - if record.Status != protocol.TerminalExited || record.EndedAt == 0 { - t.Fatalf("record = %+v", record) - } - reasons[record.ID] = record.Reason - } - if len(reasons) != 2 || reasons[term.ID] != terminalStopReason || reasons["term_crashed"] != terminalRestartReason { - t.Fatalf("reasons = %+v", reasons) - } - if _, err := second.Close(context.Background(), "term_crashed"); err != nil { - t.Fatal(err) - } - if list, _ := second.List(context.Background()); len(list) != 1 { - t.Fatalf("list after closing an ended terminal = %+v", list) - } -} - -// The window title the shell sets is followed across reads and recorded. -func TestTerminalTitles(t *testing.T) { - var titles terminalTitles - if _, ok := titles.feed([]byte("plain \x1b[1mtext")); ok { - t.Fatal("title from plain text") - } - if _, ok := titles.feed([]byte("\x1b]0;~/src")); ok { - t.Fatal("title before its end") - } - if title, ok := titles.feed([]byte("/app\x07more")); !ok || title != "~/src/app" { - t.Fatalf("title = %q %v", title, ok) - } - if title, ok := titles.feed([]byte("\x1b]2;vim\x01 x\x1b\\")); !ok || title != "vim x" { - t.Fatalf("ST title = %q %v", title, ok) - } - if _, ok := titles.feed([]byte("\x1b]8;;https://x\x07")); ok { - t.Fatal("a hyperlink is not a title") - } - if _, ok := titles.feed(terminalLoginMarker); ok { - t.Fatal("the login marker is not a title") - } - if title, _ := titles.feed([]byte("\x1b]0;" + strings.Repeat("é", protocol.TerminalTitleLimit) + "\x07")); len(title) > protocol.TerminalTitleLimit { - t.Fatalf("title of %d bytes", len(title)) - } - terminalHome(t) - m, _ := newTestTerminals(t, newOperatorStore(t)) - term, err := m.Open(context.Background(), "", "", 0, 0) - if err != nil { - t.Fatal(err) - } - if err := m.Input(term.ID, fakeSuPassword+"\r"); err != nil { - t.Fatal(err) - } - if err := m.Input(term.ID, "printf '\\033]0;build-%s\\007' ok\r"); err != nil { - t.Fatal(err) - } - waitTerminalTitle(t, m, "build-ok") -} - -func waitTerminalTitle(t *testing.T, m *terminalManager, want string) { - t.Helper() - deadline := time.Now().Add(5 * time.Second) - for { - list, _ := m.List(context.Background()) - if len(list) == 1 && list[0].Title == want { - return - } - if time.Now().After(deadline) { - t.Fatalf("list = %+v", list) - } - time.Sleep(50 * time.Millisecond) - } -}