//go:build darwin || linux package server import ( "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/termstate" ) // tuiCommand enters the alternate screen, sets mouse, bracketed paste and // application cursor/keypad modes, writes more than the retained tail // there, draws a last frame and waits for a line; then it restores the // modes, leaves the alternate screen and prints a marker. The markers are // computed, so the command's own echo does not contain them. const tuiCommand = `echo before-$((1))tui; ` + `printf '\033[?1049h\033[?1000h\033[?1006h\033[?2004h\033[?1h\033=\033[H\033[2J'; ` + `head -c 1300000 /dev/zero | tr '\0' x; ` + `printf '\033[H\033[2JFRAME-%s\033[24;1Hstatus-%s' $((7)) $((8)); read x; ` + `printf '\033[?2004l\033[?1006l\033[?1000l\033[?1l\033>\033[?1049l'; echo after-$((2))tui` + "\r" // A client that attaches (a reload, or one that fell behind the retained // tail) while a full-screen program runs gets the terminal's state, not the // tail: the program's screen on the alternate screen with its modes, and // the shell's screen underneath, which the program's exit brings back. func TestTerminalSnapshotAfterReload(t *testing.T) { terminalHome(t) ctx, conns := dialTerminalConns(t, 2, 60*time.Second) term := openLoggedIn(t, ctx, conns[0], true) reader := &terminalReader{t: t, ctx: ctx, conn: conns[0], id: term.ID, end: term.OutputEnd, ack: true} typeInto(t, ctx, conns[0], term.ID, tuiCommand) reader.until("status-8", nil) // The reload: no After at all, and one far before the tail. var reply *protocol.TerminalRecord for _, after := range []int64{0, 5} { reply = terminalReply(t, ctx, conns[1], terminalRequest(protocol.TerminalOutput, "reload", protocol.TerminalRequest{TerminalID: term.ID, After: after, Flow: true})).Terminal if !reply.OutputSnapshot || reply.OutputStart != reply.OutputEnd || reply.OutputEnd != reader.end || reply.Window != protocol.TerminalFlowWindow { t.Fatalf("after %d: reply start %d end %d (reader at %d) snapshot %v window %d", after, reply.OutputStart, reply.OutputEnd, reader.end, reply.OutputSnapshot, reply.Window) } } for _, want := range []string{"\x1b[?1049h", "\x1b[?1000h", "\x1b[?1006h", "\x1b[?2004h", "\x1b[?1h", "\x1b=", "FRAME-7", "status-8", "before-1tui"} { if !strings.Contains(reply.Output, want) { t.Fatalf("snapshot lacks %q", want) } } if len(reply.Output) > 64<<10 { t.Fatalf("snapshot is %d bytes", len(reply.Output)) } // A resume inside the tail is raw output, as before. if resume := terminalReply(t, ctx, conns[1], terminalRequest(protocol.TerminalOutput, "resume", protocol.TerminalRequest{TerminalID: term.ID, After: reply.OutputEnd, Flow: true})).Terminal; resume.OutputSnapshot || resume.Output != "" { t.Fatalf("resume = %+v", resume) } screen := termstate.New(term.Cols, term.Rows, termstate.DefaultScrollback) screen.Write([]byte(reply.Output)) if !screen.AltScreen() || !strings.HasPrefix(screen.Text(), "FRAME-7") || !strings.Contains(screen.Text(), "status-8") { t.Fatalf("snapshot screen (alt %v):\n%s", screen.AltScreen(), screen.Text()) } // The program exits; the frames continue exactly after the snapshot. typeInto(t, ctx, conns[0], term.ID, "\r") late := &terminalReader{t: t, ctx: ctx, conn: conns[1], id: term.ID, end: reply.OutputEnd, ack: true} late.until("after-2tui", nil) if late.gaps != 0 { t.Fatalf("frames after the snapshot skipped %d bytes", late.gaps) } screen.Write([]byte(late.text.String())) text := screen.Text() if screen.AltScreen() || !strings.Contains(text, "before-1tui") || !strings.Contains(text, "after-2tui") || strings.Contains(text, "FRAME-7") || strings.Contains(text, "xxxx") { t.Fatalf("main screen after the exit (alt %v):\n%s", screen.AltScreen(), text) } }