Files
pipelines/internal/pipeline/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

103 lines
3.8 KiB
Go

package pipeline
import (
"context"
"fmt"
"path/filepath"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestChatShellRunsDismissRestoreAndPurge(t *testing.T) {
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "shell.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
save := func(id, session, status string, started int64) {
t.Helper()
run := protocol.ChatShellRunRecord{ID: id, SessionID: session, Command: "echo " + id, Directory: "/w", Status: status,
StartedAt: started, Output: "out " + id, OutputBytes: 6, Text: "out " + id, Error: "err"}
if err := store.SaveChatShellRun(ctx, run, 0); err != nil {
t.Fatal(err)
}
}
save("run_a", "ses_1", protocol.ChatShellRunExited, 1)
save("run_b", "ses_1", protocol.ChatShellRunFailed, 2)
save("run_live", "ses_1", protocol.ChatShellRunRunning, 3)
save("run_other", "ses_2", protocol.ChatShellRunExited, 4)
ids := func(session string) string {
t.Helper()
runs, err := store.ChatShellRuns(ctx, session)
if err != nil {
t.Fatal(err)
}
var out []string
for _, run := range runs {
out = append(out, run.ID)
}
return fmt.Sprint(out)
}
// Running, foreign and unknown runs are skipped.
dismissed, err := store.DismissChatShellRuns(ctx, "ses_1", []string{"run_a", "run_live", "run_other", "nope"}, 1000)
if err != nil || fmt.Sprint(dismissed) != "[run_a]" {
t.Fatalf("dismissed = %v, %v", dismissed, err)
}
if got := ids("ses_1"); got != "[run_b run_live]" {
t.Fatalf("readback after dismiss = %s", got)
}
if _, found, _ := store.ChatShellRun(ctx, "ses_1", "run_a"); found {
t.Fatal("a dismissed run is still readable")
}
if again, _ := store.DismissChatShellRuns(ctx, "ses_1", []string{"run_a"}, 1001); len(again) != 0 {
t.Fatalf("dismissed twice: %v", again)
}
if tombs, _ := store.DismissedChatShellRuns(ctx, "ses_1"); fmt.Sprint(tombs) != "[run_a]" {
t.Fatalf("tombstones = %v", tombs)
}
// Undo inside the window restores the whole record.
restored, err := store.RestoreChatShellRuns(ctx, "ses_1", []string{"run_a", "run_b"}, 900)
if err != nil || len(restored) != 1 || restored[0].ID != "run_a" || restored[0].Output != "out run_a" || restored[0].Text != "out run_a" {
t.Fatalf("restored = %+v, %v", restored, err)
}
if got := ids("ses_1"); got != "[run_a run_b run_live]" {
t.Fatalf("readback after restore = %s", got)
}
// Past the window: no restore, and the purge keeps only a tombstone.
if _, err := store.DismissChatShellRuns(ctx, "ses_1", []string{"run_a", "run_b"}, 2000); err != nil {
t.Fatal(err)
}
if late, err := store.RestoreChatShellRuns(ctx, "ses_1", []string{"run_a"}, 2001); err != nil || len(late) != 0 {
t.Fatalf("late restore = %+v, %v", late, err)
}
if n, err := store.PurgeDismissedChatShellRuns(ctx, 2000); err != nil || n != 0 {
t.Fatalf("purged inside the window: %d, %v", n, err)
}
if n, err := store.PurgeDismissedChatShellRuns(ctx, 2001); err != nil || n != 2 {
t.Fatalf("purge = %d, %v", n, err)
}
if restoredPurged, _ := store.RestoreChatShellRuns(ctx, "ses_1", []string{"run_a"}, 0); len(restoredPurged) != 0 {
t.Fatalf("a purged run was restored: %+v", restoredPurged)
}
var command, output, text, errText string
var bytes int64
if err := store.db.QueryRowContext(ctx, `SELECT command, output, output_text, error, output_bytes FROM chat_shell_runs WHERE id = 'run_a'`).
Scan(&command, &output, &text, &errText, &bytes); err != nil {
t.Fatal(err)
}
if command != "" || output != "" || text != "" || errText != "" || bytes != 0 {
t.Fatalf("purged row kept %q %q %q %q %d", command, output, text, errText, bytes)
}
if tombs, _ := store.DismissedChatShellRuns(ctx, "ses_1"); fmt.Sprint(tombs) != "[run_a run_b]" {
t.Fatalf("tombstones = %v", tombs)
}
if got := ids("ses_2"); got != "[run_other]" {
t.Fatalf("another session changed: %s", got)
}
}