Files
pipelines/internal/server/chat_shell_dismiss_test.go
T
alan 03123c23fd Dismiss and restore operator shell commands per chat
chat_shell_dismiss hides finished runs from every readback and pushes a
dismissal frame to every watcher; chat_shell_restore undoes it within a
30 s window, after which the command, output and error are purged and only
a tombstone remains, reported by chat_shell_runs for clients that missed
the event.
2026-11-09 21:22:07 +00:00

115 lines
5.2 KiB
Go

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 TestChatShellDismissRemovesForEveryWatcherAndRestores(t *testing.T) {
ctx, conns, _ := shellChat(t)
done := runShell(t, ctx, conns[0], "r1", "echo kept-output")
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 leaves it out and reports its tombstone; its output is gone.
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)
}
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.Error {
t.Fatalf("output of a dismissed run = %+v", got)
}
// Undo 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" {
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 TestChatShellDismissPurgesAfterTheUndoWindow(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 gone", 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)
}
now = now.Add(runner.undo - time.Millisecond)
runner.purgeDismissed(ctx)
if restored, err := runner.Restore(ctx, "ses_1", []string{run.ID}); err != nil || len(restored) != 1 || restored[0].Output == "" {
t.Fatalf("restore inside the window = %+v, %v", restored, err)
}
if _, err := runner.Dismiss(ctx, "ses_1", []string{run.ID}); err != nil {
t.Fatal(err)
}
now = now.Add(runner.undo + time.Millisecond)
// A restart purges what expired while no server ran.
runner.recover(ctx)
if _, err := runner.Restore(ctx, "ses_1", []string{run.ID}); err == nil {
t.Fatal("a run was restored after the undo window")
}
if tombs, err := runner.Dismissed(ctx, "ses_1"); err != nil || fmt.Sprint(tombs) != fmt.Sprint([]string{run.ID}) {
t.Fatalf("tombstones = %v, %v", tombs, err)
}
}