package server import ( "context" "errors" "path/filepath" "strings" "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" ) // TestFailureRouteRunDiagnostic proves A9: a declared outcome routed to #failure // with an empty step diagnostic still yields a meaningful run diagnostic, while // an existing step diagnostic is preferred verbatim. func TestFailureRouteRunDiagnostic(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "runs.db")) if err != nil { t.Fatal(err) } defer store.Close() source := strings.Replace(pipeline.Template([]string{"worker"}, true), "success -> #success", "success -> #success\nblocked -> #failure", 1) if _, err := store.Submit(ctx, "diagnostic", source); err != nil { t.Fatal(err) } items, _ := store.List(ctx) r, err := newRunner(ctx, store) if err != nil { t.Fatal(err) } r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) { return stage.Result{Outcome: "blocked", Output: stage.Values{}}, nil } run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "blocked", Workspace: t.TempDir(), 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) } if detail.Status != "failed" || detail.Outcome != "error" || !strings.Contains(detail.Diagnostic, `stage worker returned outcome "blocked"`) { t.Fatalf("empty run diagnostic not replaced: %+v", detail) } r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) { return stage.Result{}, errors.New("fixture execution failed") } run, err = r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "execution", Workspace: t.TempDir(), 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) } if detail.Status != "failed" || detail.Diagnostic != "fixture execution failed" { t.Fatalf("existing step diagnostic not preferred: %+v", detail) } }