package server import ( "context" "fmt" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) func dismissRequest(kind, id string, runIDs ...string) protocol.Message { return protocol.Message{Type: kind, ID: id, ChatShellRunRequest: &protocol.ChatShellRunRequest{SessionID: "ses_1", RunIDs: runIDs}} } func TestChatShellDismissKeepsTheRunForEveryWatcherAndRestores(t *testing.T) { ctx, conns, _ := shellChat(t) done := runShell(t, ctx, conns[0], "r1", "echo kept-output; exit 2") for _, conn := range conns { finishedShell(t, ctx, conn, done.ID) } live := runShell(t, ctx, conns[0], "r2", "sleep 30") // A running command alone is refused; with a finished one it is skipped. writeChat(t, ctx, conns[0], dismissRequest(protocol.ChatShellDismiss, "running", live.ID)) if reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "running" }); reply.Type != protocol.Error { t.Fatalf("dismissing a running command = %+v", reply) } writeChat(t, ctx, conns[0], dismissRequest(protocol.ChatShellDismiss, "empty")) if reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "empty" }); reply.Type != protocol.Error { t.Fatalf("dismissing nothing = %+v", reply) } writeChat(t, ctx, conns[0], dismissRequest(protocol.ChatShellDismiss, "d", done.ID, live.ID)) reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "d" }) if reply.Type != protocol.ChatShellDismiss || fmt.Sprint(reply.ChatShellDismissed) != fmt.Sprint([]string{done.ID}) { t.Fatalf("dismiss reply = %+v", reply) } isDismissal := func(m protocol.Message) bool { return m.Type == protocol.ChatShellRunEvent && m.ChatShellRunEvent != nil && m.ChatShellRunEvent.Dismissed } for i, conn := range conns { if frame := readShellFrame(t, ctx, conn, isDismissal).ChatShellRunEvent; frame.RunID != done.ID || frame.SessionID != "ses_1" { t.Fatalf("dismissal frame %d = %+v", i, frame) } } // The readback lists it apart, flagged, with its command, status and output. writeChat(t, ctx, conns[1], protocol.Message{Type: protocol.ChatShellRuns, ID: "list", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}) list := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "list" }) if len(list.ChatShellRuns) != 1 || list.ChatShellRuns[0].ID != live.ID || fmt.Sprint(list.ChatShellDismissed) != fmt.Sprint([]string{done.ID}) { t.Fatalf("readback = %+v", list) } if len(list.ChatShellDismissedRuns) != 1 { t.Fatalf("dismissed runs = %+v", list.ChatShellDismissedRuns) } if run := list.ChatShellDismissedRuns[0]; run.ID != done.ID || !run.Dismissed || run.DismissedAt == 0 || run.Purged || run.Command != "echo kept-output; exit 2" || run.ExitCode == nil || *run.ExitCode != 2 || run.Output != "kept-output\r\n" || run.Text != "" { t.Fatalf("dismissed run = %+v", run) } // Its whole output stays readable (the history's output view, Copy output). writeChat(t, ctx, conns[1], shellRequest(protocol.ChatShellRunOutput, "out", done.ID, "")) if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "out" }); got.Type != protocol.ChatShellRunOutput || got.ChatShellRun == nil || !got.ChatShellRun.Dismissed || got.ChatShellRun.Text != "kept-output\n" { t.Fatalf("output of a dismissed run = %+v", got) } // Restore brings the run back, output included, for every watcher. writeChat(t, ctx, conns[1], dismissRequest(protocol.ChatShellRestore, "undo", done.ID)) restored := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "undo" }) if restored.Type != protocol.ChatShellRestore || len(restored.ChatShellRuns) != 1 || restored.ChatShellRuns[0].Output != "kept-output\r\n" || restored.ChatShellRuns[0].Dismissed { t.Fatalf("restore reply = %+v", restored) } for i, conn := range conns { frame := readShellFrame(t, ctx, conn, func(m protocol.Message) bool { return m.Type == protocol.ChatShellRunEvent && m.ChatShellRunEvent != nil && m.ChatShellRunEvent.RunID == done.ID && m.ChatShellRunEvent.Run != nil }).ChatShellRunEvent if frame.Run.Status != protocol.ChatShellRunExited { t.Fatalf("restore frame %d = %+v", i, frame) } } writeChat(t, ctx, conns[0], dismissRequest(protocol.ChatShellRestore, "undo-again", done.ID)) if got := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "undo-again" }); got.Type != protocol.Error { t.Fatalf("restoring an undismissed run = %+v", got) } writeChat(t, ctx, conns[0], shellRequest(protocol.ChatShellStop, "stop", live.ID, "")) finishedShell(t, ctx, conns[0], live.ID) } func TestChatShellDismissedRunIsKeptAcrossRestartsAndRestoresLater(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() runner, frames := testShellRunner(t, ctx) now := time.UnixMilli(1_000_000) runner.now = func() time.Time { return now } run, err := runner.Start(ctx, "ses_1", t.TempDir(), "echo kept", 0, 0) if err != nil { t.Fatal(err) } runner.Wait() if frames.final(run.ID) == nil { t.Fatal("the run never finished") } if _, err := runner.Dismiss(ctx, "ses_1", []string{run.ID}); err != nil { t.Fatal(err) } // A day later, after a restart, the run is still whole and restorable. now = now.Add(24 * time.Hour) runner.recover(ctx) held, ids, err := runner.Dismissed(ctx, "ses_1") if err != nil || len(held) != 1 || fmt.Sprint(ids) != fmt.Sprint([]string{run.ID}) || held[0].Command != "echo kept" || held[0].Output == "" || held[0].Purged { t.Fatalf("dismissed = %+v, %v, %v", held, ids, err) } if read, err := runner.Read(ctx, "ses_1", run.ID); err != nil || read.Text != "kept\n" || !read.Dismissed { t.Fatalf("read = %+v, %v", read, err) } if restored, err := runner.Restore(ctx, "ses_1", []string{run.ID}); err != nil || len(restored) != 1 || restored[0].Output == "" { t.Fatalf("restore a day later = %+v, %v", restored, err) } if held, _, _ := runner.Dismissed(ctx, "ses_1"); len(held) != 0 { t.Fatalf("still dismissed: %+v", held) } }