f2520a09b8
A dismissed run keeps its command, status, times and output within the usual history bounds. chat_shell_runs lists dismissed runs apart (chat_shell_dismissed_runs), chat_shell_run_output still reads them, and the purge timer and startup purge are gone. Rows an older server purged are listed with purged set and cannot be restored.
146 lines
5.3 KiB
Go
146 lines
5.3 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func chatShellIDs(t *testing.T, runs []protocol.ChatShellRunRecord, err error) string {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out []string
|
|
for _, run := range runs {
|
|
out = append(out, run.ID)
|
|
}
|
|
return fmt.Sprint(out)
|
|
}
|
|
|
|
func TestChatShellRunsDismissKeepsOutputAndRestores(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()
|
|
code := 3
|
|
run := protocol.ChatShellRunRecord{ID: id, SessionID: session, Command: "echo " + id, Directory: "/w", Status: status,
|
|
ExitCode: &code, StartedAt: started, EndedAt: started + 5, DurationMS: 5, 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)
|
|
listed := func(session string) string {
|
|
t.Helper()
|
|
runs, err := store.ChatShellRuns(ctx, session)
|
|
return chatShellIDs(t, runs, err)
|
|
}
|
|
|
|
// 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 := listed("ses_1"); got != "[run_b run_live]" {
|
|
t.Fatalf("readback after dismiss = %s", got)
|
|
}
|
|
if again, _ := store.DismissChatShellRuns(ctx, "ses_1", []string{"run_a"}, 1001); len(again) != 0 {
|
|
t.Fatalf("dismissed twice: %v", again)
|
|
}
|
|
|
|
// The dismissed run keeps its command, status, times and output.
|
|
held, err := store.DismissedChatShellRuns(ctx, "ses_1")
|
|
if err != nil || len(held) != 1 {
|
|
t.Fatalf("dismissed runs = %+v, %v", held, err)
|
|
}
|
|
if run := held[0]; run.ID != "run_a" || !run.Dismissed || run.DismissedAt != 1000 || run.Purged ||
|
|
run.Command != "echo run_a" || run.Output != "out run_a" || run.Text != "out run_a" ||
|
|
run.ExitCode == nil || *run.ExitCode != 3 || run.DurationMS != 5 || run.EndedAt != 6 {
|
|
t.Fatalf("dismissed run = %+v", run)
|
|
}
|
|
if run, found, err := store.ChatShellRun(ctx, "ses_1", "run_a"); err != nil || !found || !run.Dismissed || run.Output != "out run_a" {
|
|
t.Fatalf("reading a dismissed run = %+v, %v, %v", run, found, err)
|
|
}
|
|
|
|
// Restore has no time limit and brings the whole record back.
|
|
restored, err := store.RestoreChatShellRuns(ctx, "ses_1", []string{"run_a", "run_b"})
|
|
if err != nil || len(restored) != 1 || restored[0].ID != "run_a" || restored[0].Dismissed || restored[0].Output != "out run_a" {
|
|
t.Fatalf("restored = %+v, %v", restored, err)
|
|
}
|
|
if got := listed("ses_1"); got != "[run_a run_b run_live]" {
|
|
t.Fatalf("readback after restore = %s", got)
|
|
}
|
|
if runs, err := store.DismissedChatShellRuns(ctx, "ses_1"); chatShellIDs(t, runs, err) != "[]" {
|
|
t.Fatalf("dismissed after restore = %+v", runs)
|
|
}
|
|
if got := listed("ses_2"); got != "[run_other]" {
|
|
t.Fatalf("another session changed: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestChatShellRunsListOlderPurgedTombstones(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "shell.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
run := protocol.ChatShellRunRecord{ID: "run_old", SessionID: "ses_1", Directory: "/w",
|
|
Status: protocol.ChatShellRunExited, StartedAt: 10, EndedAt: 20, DurationMS: 10}
|
|
if err := store.SaveChatShellRun(ctx, run, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// An older server purged this dismissed row after its undo window.
|
|
if _, err := store.db.ExecContext(ctx, `UPDATE chat_shell_runs SET dismissed_at = 15, purged = 1 WHERE id = 'run_old'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
held, err := store.DismissedChatShellRuns(ctx, "ses_1")
|
|
if err != nil || len(held) != 1 || !held[0].Purged || !held[0].Dismissed || held[0].Command != "" || held[0].Output != "" {
|
|
t.Fatalf("tombstone = %+v, %v", held, err)
|
|
}
|
|
if restored, err := store.RestoreChatShellRuns(ctx, "ses_1", []string{"run_old"}); err != nil || len(restored) != 0 {
|
|
t.Fatalf("a purged run was restored: %+v, %v", restored, err)
|
|
}
|
|
}
|
|
|
|
func TestChatShellRunsDismissedCountTowardHistory(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "shell.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
for i := range 4 {
|
|
run := protocol.ChatShellRunRecord{ID: fmt.Sprintf("run_%d", i), SessionID: "ses_1", Command: "true", Directory: "/w",
|
|
Status: protocol.ChatShellRunExited, StartedAt: int64(i + 1)}
|
|
if err := store.SaveChatShellRun(ctx, run, 3); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if i == 1 {
|
|
if _, err := store.DismissChatShellRuns(ctx, "ses_1", []string{"run_0", "run_1"}, 100); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
dismissed, err := store.DismissedChatShellRuns(ctx, "ses_1")
|
|
if got := chatShellIDs(t, dismissed, err); got != "[run_1]" {
|
|
t.Fatalf("dismissed kept = %s", got)
|
|
}
|
|
kept, err := store.ChatShellRuns(ctx, "ses_1")
|
|
if got := chatShellIDs(t, kept, err); got != "[run_2 run_3]" {
|
|
t.Fatalf("runs kept = %s", got)
|
|
}
|
|
}
|