//go:build darwin || linux package server import ( "context" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // outputFrames is runID's output frames so far, in order. func (f *shellFrames) outputFrames(runID string) []protocol.ChatShellRunEventFrame { f.mu.Lock() defer f.mu.Unlock() var out []protocol.ChatShellRunEventFrame for _, frame := range f.frames { if frame.RunID == runID && frame.Run == nil && !frame.Dismissed { out = append(out, frame) } } return out } // joined is the frames' text and the bytes skipped between them. func joined(t *testing.T, frames []protocol.ChatShellRunEventFrame) (string, int64) { t.Helper() var text strings.Builder var end, skipped int64 for _, frame := range frames { if frame.Offset < end { t.Fatalf("frame [%d, %d) overlaps %d", frame.Offset, frame.End, end) } skipped += frame.Offset - end text.WriteString(frame.Output) end = frame.End } return text.String(), skipped } // A command writing more than the retained tail to watchers that keep up // is streamed whole, oldest first, without a gap. func TestChatShellFlowStreamsEveryByte(t *testing.T) { runner, frames := testShellRunner(t, context.Background()) runner.flush = time.Hour // output must not wait for a tick run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "seq 1 300000", 0, 0) if err != nil { t.Fatal(err) } runner.Wait() text, skipped := joined(t, frames.outputFrames(run.ID)) if skipped != 0 { t.Fatalf("%d bytes skipped", skipped) } sep := "\n" if run.Terminal { sep = "\r\n" } var want strings.Builder for i := 1; i <= 300000; i++ { want.WriteString(itoa(i) + sep) } if text != want.String() { t.Fatalf("streamed %d bytes, want %d", len(text), want.Len()) } } // Output is pushed when it arrives, not on the flush tick. func TestChatShellFlowPushesAtOnce(t *testing.T) { runner, frames := testShellRunner(t, context.Background()) runner.flush = time.Hour run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "echo early; sleep 5", 0, 0) if err != nil { t.Fatal(err) } t.Cleanup(func() { _, _ = runner.Stop("ses_1", run.ID); runner.Wait() }) for deadline := time.Now().Add(3 * time.Second); ; time.Sleep(5 * time.Millisecond) { if text, _ := joined(t, frames.outputFrames(run.ID)); strings.Contains(text, "early") { return } if time.Now().After(deadline) { t.Fatal("no output frame while the command runs") } } } // Watchers that stop draining hold the command back only for // chatShellStall; then the output skips ahead and the command finishes. func TestChatShellFlowStalledWatchersDoNotHoldForever(t *testing.T) { runner, frames := testShellRunner(t, context.Background()) runner.backlog = func(string) int { return chatShellBacklog } start := time.Now() run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "seq 1 400000", 0, 0) if err != nil { t.Fatal(err) } runner.Wait() if elapsed := time.Since(start); elapsed < chatShellStall || elapsed > 6*chatShellStall { t.Fatalf("finished after %s", elapsed) } if _, skipped := joined(t, frames.outputFrames(run.ID)); skipped == 0 { t.Fatal("stalled watchers were sent everything") } if final := frames.final(run.ID); final == nil || final.Status != protocol.ChatShellRunExited { t.Fatalf("final = %+v", final) } } func itoa(i int) string { var b [20]byte n := len(b) for { n-- b[n] = byte('0' + i%10) i /= 10 if i == 0 { return string(b[n:]) } } }