package workflow import ( "context" "errors" "fmt" "log/slog" "maps" "strings" "sync" "testing" "gopkg.in/yaml.v3" "github.com/samcharles93/archie-core/internal/domain/usage" "github.com/samcharles93/archie-core/internal/domain/workflow/task" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/taskstate" ) // fakeStepStore records the step writes a branch makes. type fakeStepStore struct { started []task.StepStart finished []task.StepFinish events []events.Event nextID int64 } func (f *fakeStepStore) Update(context.Context, *task.Task) error { return nil } func (f *fakeStepStore) Transition(context.Context, int64, string, string, string) error { return nil } func (f *fakeStepStore) InsertEvent(_ context.Context, e events.Event) (int64, error) { f.events = append(f.events, e) return 0, nil } func (f *fakeStepStore) StartStep(_ context.Context, s task.StepStart) (int64, events.Event, error) { f.nextID++ f.started = append(f.started, s) return f.nextID, events.Event{ID: f.nextID}, nil } func (f *fakeStepStore) FinishStep(_ context.Context, s task.StepFinish) (events.Event, error) { f.finished = append(f.finished, s) return events.Event{ID: s.StepID}, nil } func (f *fakeStepStore) RecordUsage(context.Context, usage.Record) error { return nil } // TestRunBranchRecordsChildSteps pins the step contract a branch owes the // canvas: every branch step is a stage row parented to the parallel step and // named for its branch, and its outcome is recorded. func TestRunBranchRecordsChildSteps(t *testing.T) { boom := errors.New("boom") tests := []struct { name string stage Stage wantErr error wantStatus taskstate.StepStatus wantDetail string }{ { name: "a passing branch step", stage: Stage{Name: "agent.run", Run: func(context.Context, *TaskContext) error { return nil }}, wantStatus: taskstate.StepSucceeded, }, { name: "a failing branch step", stage: Stage{Name: "command.run", Run: func(context.Context, *TaskContext) error { return boom }}, wantErr: boom, wantStatus: taskstate.StepFailed, wantDetail: "boom", }, { name: "a branch step whose when was false", stage: wrapStep(StepRecord{ID: "agent.run", When: "task.title"}, func(context.Context, *TaskContext) error { return nil }), wantStatus: taskstate.StepSkipped, wantDetail: "task.title", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { store := &fakeStepStore{} tc := &TaskContext{Task: &task.Task{ID: 1, Attempt: 1}, Store: store, StepID: 7, workflowBranch: "docs", Log: slog.Default()} err := runBranch(context.Background(), tc, []Stage{tt.stage}) if !errors.Is(err, tt.wantErr) { t.Fatalf("err = %v, want %v", err, tt.wantErr) } if len(store.started) != 1 { t.Fatalf("started %d steps, want 1", len(store.started)) } start := store.started[0] if start.Kind != task.StepKindStage || start.ParentID != 7 || start.Name != "docs/"+tt.stage.Name { t.Fatalf("start = %#v, want a stage named %q under step 7", start, "docs/"+tt.stage.Name) } if len(store.finished) != 1 { t.Fatalf("finished %d steps, want 1", len(store.finished)) } finish := store.finished[0] if finish.To != tt.wantStatus || finish.Detail != tt.wantDetail { t.Fatalf("finish = %#v, want %s %q", finish, tt.wantStatus, tt.wantDetail) } if tc.StepID != 7 { t.Fatalf("branch step leaked its step id: %d", tc.StepID) } }) } } // TestSwitchRunsTheMatchingCase pins the switch contract: the case keyed by // the referenced value runs, else the default case, else nothing, and its steps // record under the case name. func TestSwitchRunsTheMatchingCase(t *testing.T) { src := `id: route steps: - switch: on: task.title cases: bug: [{id: fix, type: mark}] "true": [{id: yes, type: mark}] %s` tests := []struct { name, title, fallback, want string }{ {name: "a matching case", title: "bug", want: "bug/fix"}, {name: "a boolean-looking key", title: "true", want: "true/yes"}, {name: "no match falls to the default", title: "feature", fallback: " default: [{id: other, type: mark}]", want: "default/other"}, {name: "no match and no default runs nothing", title: "feature"}, } registry := StepRegistry{"mark": func(yaml.Node) (Stage, error) { return Stage{Run: func(context.Context, *TaskContext) error { return nil }}, nil }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { wf, err := ParseAndCompile(fmt.Sprintf(src, tt.fallback), registry) if err != nil { t.Fatal(err) } store := &fakeStepStore{} tc := &TaskContext{Task: &task.Task{ID: 1, Attempt: 1, Title: tt.title}, Store: store, StepID: 7, Log: slog.Default()} if err := wf.Stages[0].Run(context.Background(), tc); err != nil { t.Fatal(err) } var got string if len(store.started) > 0 { got = store.started[0].Name } if got != tt.want { t.Fatalf("ran %q, want %q", got, tt.want) } }) } } // lockedStepStore serialises the step writes parallel branches make. type lockedStepStore struct { mu sync.Mutex fakeStepStore } func (f *lockedStepStore) StartStep(ctx context.Context, s task.StepStart) (int64, events.Event, error) { f.mu.Lock() defer f.mu.Unlock() return f.fakeStepStore.StartStep(ctx, s) } func (f *lockedStepStore) FinishStep(ctx context.Context, s task.StepFinish) (events.Event, error) { f.mu.Lock() defer f.mu.Unlock() return f.fakeStepStore.FinishStep(ctx, s) } // TestParallelBranchesWriteOutputs pins how parallel branches write declared // outputs: each branch writes its own, and two branches writing one output is // refused as a second write would be, whatever order the branches finish in. func TestParallelBranchesWriteOutputs(t *testing.T) { src := `id: fan repository: none outputs: a: {type: string} b: {type: string} c: {type: string} z: {type: string} steps: - parallel: one: [{type: agent.run, settings: {read_only: true, names: [a, c]}}] two: [{type: agent.run, settings: {read_only: true, names: [%s]}}] ` tests := []struct { name, two, wantErr string before, want map[string]any }{ {name: "distinct outputs", two: "b", want: map[string]any{"a": "one", "b": "two", "c": "one"}}, { name: "distinct outputs beside an earlier one", two: "b", before: map[string]any{"z": "before"}, want: map[string]any{"a": "one", "b": "two", "c": "one", "z": "before"}, }, {name: "one output from two branches", two: "b, c", wantErr: `output "c" has already been written`}, {name: "a branch rewriting an earlier output", two: "z", before: map[string]any{"z": "before"}, wantErr: `output "z" has already been written`}, } // A fake read-only agent.run, the step type a branch may run. registry := StepRegistry{AgentRunStepName: func(node yaml.Node) (Stage, error) { var s struct{ Names []string } if err := node.Decode(&s); err != nil { return Stage{}, err } return Stage{Run: func(_ context.Context, tc *TaskContext) error { for _, name := range s.Names { for range 100 { tc.workflowInterface() } if err := tc.WriteOutput(name, tc.workflowBranch); err != nil { return err } } return nil }}, nil }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { wf, err := ParseAndCompile(fmt.Sprintf(src, tt.two), registry) if err != nil { t.Fatal(err) } tc := &TaskContext{Task: &task.Task{ID: 1, Attempt: 1, Outputs: maps.Clone(tt.before)}, Store: &lockedStepStore{}, Log: slog.Default()} tc.runInterface, tc.runInterfaceSet = wf.Interface, true err = wf.Stages[0].Run(context.Background(), tc) if tt.wantErr != "" { if err == nil || !strings.Contains(err.Error(), tt.wantErr) { t.Fatalf("err = %v, want %q", err, tt.wantErr) } return } if err != nil { t.Fatal(err) } if !maps.Equal(tc.Task.Outputs, tt.want) { t.Fatalf("outputs = %v, want %v", tc.Task.Outputs, tt.want) } }) } }