package server import ( "context" "os" "path/filepath" "strings" "sync" "testing" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" ) // uiReviewerSource builds a two-stage implement -> ui_reviewer pipeline. When // withSkip is set, ui_reviewer declares an `approved` and a `skipped` route to // success; otherwise it only declares the template's `success` route, proving a // skip is never invented for a stage without that route. func uiReviewerSource(withSkip bool) string { source := pipeline.Template([]string{"implement", "ui_reviewer"}, true) if withSkip { source = strings.Replace(source, "success -> #success", "approved -> #success\nskipped -> #success", 1) } return source } // runUISkip submits source, runs it once with an execute stub that records every // visited stage key and writes the file named for that stage into the workspace, // then returns the persisted detail, the visited keys and the number of verdict // (model) calls the runner made. func runUISkip(t *testing.T, source string, files map[string]string) (*protocol.RunDetail, []string, int) { t.Helper() ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "ui.db")) if err != nil { t.Fatal(err) } defer store.Close() if _, err := store.Submit(ctx, "ui", source); err != nil { t.Fatal(err) } items, err := store.List(ctx) if err != nil || len(items) != 1 { t.Fatalf("submit/list: %+v %v", items, err) } r, err := newRunner(ctx, store) if err != nil { t.Fatal(err) } defer r.wg.Wait() dir := t.TempDir() var mu sync.Mutex var visited []string verdictCalls := 0 r.verdict = func(context.Context, map[string]any) string { mu.Lock() verdictCalls++ mu.Unlock() return "stub verdict" } r.execute = func(_ context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) { mu.Lock() visited = append(visited, def.Key) mu.Unlock() if name := files[def.Key]; name != "" { full := filepath.Join(dir, filepath.FromSlash(name)) if err := os.MkdirAll(filepath.Dir(full), 0o700); err != nil { return stage.Result{}, err } if err := os.WriteFile(full, []byte(def.Key+"\n"), 0o600); err != nil { return stage.Result{}, err } } outcome := "success" if def.Key == "ui_reviewer" { if _, ok := def.Edges["approved"]; ok { outcome = "approved" } } return stage.Result{Outcome: outcome, Output: stage.Values{"summary": def.Key + " done"}}, nil } run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "ui-skip", Workspace: dir, Input: map[string]any{"prompt": "task"}}) if err != nil { t.Fatal(err) } r.wg.Wait() detail, err := store.GetRun(ctx, run.ID) if err != nil { t.Fatal(err) } mu.Lock() got := append([]string(nil), visited...) calls := verdictCalls mu.Unlock() return detail, got, calls } func executed(visited []string, key string) bool { for _, visitedKey := range visited { if visitedKey == key { return true } } return false } // TestUISkipShortCircuitsWhenNoSurfaceChanged proves a non-UI change skips the // ui_reviewer stage with no model call and routes along its declared skip edge. func TestUISkipShortCircuitsWhenNoSurfaceChanged(t *testing.T) { detail, visited, verdictCalls := runUISkip(t, uiReviewerSource(true), map[string]string{"implement": "internal/server/fake.go"}) if executed(visited, "ui_reviewer") { t.Fatalf("ui_reviewer executed a model call; visited=%v", visited) } if verdictCalls != 1 { t.Fatalf("verdict (model) calls = %d, want only implement's 1", verdictCalls) } if detail.Status != "succeeded" { t.Fatalf("run status = %q, want succeeded: %+v", detail.Status, detail.RunSummary) } if len(detail.Steps) != 2 { t.Fatalf("steps = %d, want 2: %+v", len(detail.Steps), detail.Steps) } step := detail.Steps[1] if step.Stage != "ui_reviewer" || step.Status != "completed" || step.Outcome != "skipped" || step.Target != protocol.SuccessEnd { t.Fatalf("skip step wrong: %+v", step) } if step.FileChanges != nil && len(step.FileChanges.Files) != 0 { t.Fatalf("skip step recorded file changes: %+v", step.FileChanges) } reason, _ := step.Output["summary"].(string) if !strings.Contains(reason, "internal/server/fake.go") { t.Fatalf("skip reason does not name the checked path: %q", reason) } } // TestUISkipZeroChangesStillSkips proves the guard also short-circuits when the // run recorded no file changes at all, with a reason that says so. func TestUISkipZeroChangesStillSkips(t *testing.T) { detail, visited, verdictCalls := runUISkip(t, uiReviewerSource(true), nil) if executed(visited, "ui_reviewer") { t.Fatalf("ui_reviewer executed with no recorded changes; visited=%v", visited) } if verdictCalls != 1 { t.Fatalf("verdict (model) calls = %d, want only implement's 1", verdictCalls) } if detail.Status != "succeeded" || len(detail.Steps) != 2 { t.Fatalf("run = %q steps=%d, want succeeded/2", detail.Status, len(detail.Steps)) } step := detail.Steps[1] if step.Outcome != "skipped" { t.Fatalf("step outcome = %q, want skipped: %+v", step.Outcome, step) } reason, _ := step.Output["summary"].(string) if !strings.Contains(reason, "no recorded file changes") { t.Fatalf("zero-change reason does not state that: %q", reason) } } // TestUISkipRunsStageOnSurfaceChange proves a run that touched a UI surface // still executes the stage and routes through its normal outcome. func TestUISkipRunsStageOnSurfaceChange(t *testing.T) { cases := map[string]string{ "android": "android/app/src/main/kotlin/Main.kt", "internal tui": "internal/tui/view.go", "presentation": "web/app.tsx", } for name, path := range cases { t.Run(name, func(t *testing.T) { detail, visited, verdictCalls := runUISkip(t, uiReviewerSource(true), map[string]string{"implement": path}) if !executed(visited, "ui_reviewer") { t.Fatalf("ui_reviewer did not execute on %s; visited=%v", path, visited) } if verdictCalls != 2 { t.Fatalf("verdict (model) calls = %d, want 2 for both executed stages", verdictCalls) } if detail.Status != "succeeded" { t.Fatalf("run status = %q, want succeeded: %+v", detail.Status, detail.RunSummary) } step := detail.Steps[len(detail.Steps)-1] if step.Stage != "ui_reviewer" || step.Outcome != "approved" || step.Target != protocol.SuccessEnd { t.Fatalf("executed step did not route through approved: %+v", step) } }) } } // TestUISkipNeedsDeclaredEdge proves the guard never invents a skip for a stage // whose definition declares no `skipped` route: it always executes. func TestUISkipNeedsDeclaredEdge(t *testing.T) { detail, visited, verdictCalls := runUISkip(t, uiReviewerSource(false), map[string]string{"implement": "internal/server/fake.go"}) if !executed(visited, "ui_reviewer") { t.Fatalf("ui_reviewer without a skipped edge did not execute; visited=%v", visited) } if verdictCalls != 2 { t.Fatalf("verdict (model) calls = %d, want 2 for both executed stages", verdictCalls) } if detail.Status != "succeeded" || len(detail.Steps) != 2 { t.Fatalf("run = %q steps=%d, want succeeded/2", detail.Status, len(detail.Steps)) } step := detail.Steps[1] if step.Stage != "ui_reviewer" || step.Outcome != "success" { t.Fatalf("edge-less stage step wrong: %+v", step) } } func TestUISurfacePathClassification(t *testing.T) { cases := []struct { path string want bool }{ {"android/app/src/main/kotlin/Main.kt", true}, {"android/README.md", true}, {"internal/tui/view.go", true}, {"internal/tui", true}, {"web/index.html", true}, {"web/app.css", true}, {"web/app.js", true}, {"web/app.ts", true}, {"web/app.jsx", true}, {"web/app.tsx", true}, {"web/Index.HTML", true}, {"internal/server/fake.go", false}, {"androidx/foo.go", false}, {"cmd/pipelines/main.go", false}, {"docs/readme.md", false}, {"README.md", false}, } for _, tc := range cases { if got := uiSurfacePath(tc.path); got != tc.want { t.Errorf("uiSurfacePath(%q) = %v, want %v", tc.path, got, tc.want) } } } func TestUISurfaceArtifactClassification(t *testing.T) { cases := []struct { name string a protocol.StageArtifact want bool }{ {"image content type", protocol.StageArtifact{Name: "shot.bin", ContentType: "image/png"}, true}, {"image extension only", protocol.StageArtifact{Name: "shot.webp"}, true}, {"non-image artifact", protocol.StageArtifact{Name: "report.pdf", ContentType: "application/pdf"}, false}, {"binary artifact", protocol.StageArtifact{Name: "data.bin", ContentType: "application/octet-stream"}, false}, {"no type no image extension", protocol.StageArtifact{Name: "notes.txt"}, false}, } for _, tc := range cases { if got := uiSurfaceArtifact(tc.a); got != tc.want { t.Errorf("%s: uiSurfaceArtifact(%+v) = %v, want %v", tc.name, tc.a, got, tc.want) } } } // TestUISurfaceRecordChanges proves the accumulator latches a UI surface from an // image artifact but not from a non-image one, and retains only a bounded sample. func TestUISurfaceRecordChanges(t *testing.T) { image := &runState{} image.recordChanges(&protocol.StageFileChanges{Files: []protocol.FileChange{{Path: "internal/server/fake.go"}}}, []protocol.StageArtifact{{Name: "shot.png", ContentType: "image/png"}}) if !image.uiSurface { t.Fatal("an image artifact did not mark a UI surface") } plain := &runState{} plain.recordChanges(&protocol.StageFileChanges{Files: []protocol.FileChange{{Path: "internal/server/fake.go"}}}, []protocol.StageArtifact{{Name: "report.pdf", ContentType: "application/pdf"}}) if plain.uiSurface { t.Fatal("a non-image artifact marked a UI surface") } bounded := &runState{} files := make([]protocol.FileChange, maxRetainedChangedPaths+5) for i := range files { files[i] = protocol.FileChange{Path: "f"} } bounded.recordChanges(&protocol.StageFileChanges{Files: files}, nil) if len(bounded.changedPaths) != maxRetainedChangedPaths || bounded.changedCount != len(files) { t.Fatalf("bounded accumulator = %d paths / count %d, want %d / %d", len(bounded.changedPaths), bounded.changedCount, maxRetainedChangedPaths, len(files)) } }