The prd step returns the PRD as structured fields and human.approve takes them as its document. The plan text every surface reads and the approval card's layout are both rendered from that one document, so what the human approves is what the implement workflow is handed. Replaces the separately generated OpenUI view, which could drift from the plan.
290 lines
7.7 KiB
Go
290 lines
7.7 KiB
Go
package workflow
|
|
|
|
import (
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
)
|
|
|
|
func TestValidateControlTargets(t *testing.T) {
|
|
registry := StepRegistry{
|
|
AgentRunStepName: newAgentRunStage,
|
|
FinishStepName: newFinishStage,
|
|
HandoffStepName: newHandoffStage,
|
|
ApproveStepName: newApproveStage,
|
|
WorkflowCallStepName: newWorkflowCallStage,
|
|
}
|
|
defined := `id: defined
|
|
repository: none
|
|
steps:
|
|
- type: workflow.finish
|
|
`
|
|
tests := []struct {
|
|
name string
|
|
defs map[string]string
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "a literal handoff to a defined workflow is accepted",
|
|
defs: map[string]string{
|
|
"source": `id: source
|
|
repository: none
|
|
steps:
|
|
- type: workflow.handoff
|
|
settings: {workflow: defined}
|
|
`,
|
|
"defined": defined,
|
|
},
|
|
},
|
|
{
|
|
name: "an approve target to a defined workflow is accepted",
|
|
defs: map[string]string{
|
|
"source": `id: source
|
|
repository: none
|
|
steps:
|
|
- type: human.approve
|
|
settings: {plan: p, then: defined}
|
|
`,
|
|
"defined": defined,
|
|
},
|
|
},
|
|
{
|
|
name: "an unknown literal handoff is refused naming the step",
|
|
defs: map[string]string{
|
|
"source": `id: source
|
|
repository: none
|
|
steps:
|
|
- type: workflow.finish
|
|
- type: workflow.handoff
|
|
settings: {workflow: missing}
|
|
`,
|
|
},
|
|
wantErr: `workflow "source" step 2 targets "missing", which is not defined`,
|
|
},
|
|
{
|
|
name: "an unknown approve target is refused naming the step",
|
|
defs: map[string]string{
|
|
"source": `id: source
|
|
repository: none
|
|
steps:
|
|
- type: human.approve
|
|
settings: {plan: p, then: missing}
|
|
`,
|
|
},
|
|
wantErr: `workflow "source" step 1 targets "missing", which is not defined`,
|
|
},
|
|
{
|
|
name: "a target needing a repository the task does not have is refused",
|
|
defs: map[string]string{
|
|
"source": `id: source
|
|
repository: none
|
|
steps:
|
|
- type: workflow.handoff
|
|
settings: {workflow: defined}
|
|
`,
|
|
"defined": `id: defined
|
|
steps:
|
|
- type: workflow.finish
|
|
`,
|
|
},
|
|
wantErr: `which needs a repository the task does not have`,
|
|
},
|
|
{
|
|
name: "a target requiring an input the task may not carry is refused",
|
|
defs: map[string]string{
|
|
"source": `id: source
|
|
repository: none
|
|
steps:
|
|
- type: workflow.handoff
|
|
settings: {workflow: defined}
|
|
`,
|
|
"defined": `id: defined
|
|
repository: none
|
|
inputs:
|
|
pr_number: {type: number, required: true}
|
|
steps:
|
|
- type: workflow.finish
|
|
`,
|
|
},
|
|
wantErr: `which requires input "pr_number" the task may not carry`,
|
|
},
|
|
{
|
|
name: "a target that drops an input the task carries is refused",
|
|
defs: map[string]string{
|
|
"source": `id: source
|
|
repository: none
|
|
inputs:
|
|
plan: {type: string, required: true}
|
|
steps:
|
|
- type: workflow.handoff
|
|
settings: {workflow: defined}
|
|
`,
|
|
"defined": defined,
|
|
},
|
|
wantErr: `which does not declare input "plan" the task carries`,
|
|
},
|
|
{
|
|
name: "an enum target whose values are all defined is accepted",
|
|
defs: map[string]string{
|
|
"source": `id: source
|
|
repository: none
|
|
steps:
|
|
- id: classify
|
|
type: agent.run
|
|
settings:
|
|
mission: m
|
|
read_only: true
|
|
result:
|
|
type: object
|
|
properties:
|
|
workflow: {type: string, enum: [alpha, beta]}
|
|
- type: workflow.handoff
|
|
settings: {workflow: "{{ steps.classify.result.workflow }}"}
|
|
`,
|
|
"alpha": strings.Replace(defined, "id: defined", "id: alpha", 1),
|
|
"beta": strings.Replace(defined, "id: defined", "id: beta", 1),
|
|
},
|
|
},
|
|
{
|
|
name: "an enum target with an undefined value is refused",
|
|
defs: map[string]string{
|
|
"source": `id: source
|
|
repository: none
|
|
steps:
|
|
- id: classify
|
|
type: agent.run
|
|
settings:
|
|
mission: m
|
|
read_only: true
|
|
result:
|
|
type: object
|
|
properties:
|
|
workflow: {type: string, enum: [alpha, missing]}
|
|
- type: workflow.handoff
|
|
settings: {workflow: "{{ steps.classify.result.workflow }}"}
|
|
`,
|
|
"alpha": strings.Replace(defined, "id: defined", "id: alpha", 1),
|
|
},
|
|
wantErr: `targets "missing", which is not defined`,
|
|
},
|
|
{
|
|
name: "a templated target without an enum is not checked",
|
|
defs: map[string]string{
|
|
"source": `id: source
|
|
repository: none
|
|
steps:
|
|
- id: classify
|
|
type: agent.run
|
|
settings:
|
|
mission: m
|
|
read_only: true
|
|
result:
|
|
type: object
|
|
properties:
|
|
workflow: {type: string}
|
|
- type: workflow.handoff
|
|
settings: {workflow: "{{ steps.classify.result.workflow }}"}
|
|
`,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
collection := task.WorkflowDefinitionCollection{}
|
|
for _, id := range slices.Sorted(maps.Keys(tt.defs)) {
|
|
collection.Definitions = append(collection.Definitions, task.WorkflowDefinitionEntry{ID: id, YAML: tt.defs[id]})
|
|
}
|
|
err := ValidateDefinitionCollection(collection, registry)
|
|
if tt.wantErr == "" {
|
|
if err != nil {
|
|
t.Fatalf("err = %v, want nil", err)
|
|
}
|
|
return
|
|
}
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Fatalf("err = %v, want %q", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The text every surface reads is rendered from the document the approval card
|
|
// lays out, so the human approves what the next workflow is handed.
|
|
func TestApprovePlanDocument(t *testing.T) {
|
|
prd := map[string]any{
|
|
"title": "Percent helper", "problem": "No helper.", "solution": "Add `Percent`.",
|
|
"files": []any{map[string]any{"path": "calc/calc.go", "change": "modified", "note": "adds Percent"}},
|
|
"acceptance": []any{"Percent(1, 4) is 25"}, "non_goals": []any{"Floats"},
|
|
"diff_estimate": "~40 lines",
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
settings string
|
|
result map[string]any
|
|
wantPlan []string
|
|
wantDoc bool
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "a document becomes the plan text and is kept",
|
|
settings: `{document: "{{ steps.prd.result }}", then: implement}`,
|
|
result: prd,
|
|
wantPlan: []string{"# Percent helper", "## Problem\n\nNo helper.", "- `calc/calc.go` (modified): adds Percent", "## Acceptance criteria\n\n- Percent(1, 4) is 25", "## Non-goals\n\n- Floats", "## Estimated diff\n\n~40 lines"},
|
|
wantDoc: true,
|
|
},
|
|
{
|
|
name: "a plain plan is kept as written",
|
|
settings: `{plan: "ship it", then: implement}`,
|
|
wantPlan: []string{"ship it"},
|
|
},
|
|
{
|
|
name: "a document missing its solution is refused",
|
|
settings: `{document: "{{ steps.prd.result }}", then: implement}`,
|
|
result: map[string]any{"problem": "No helper."},
|
|
wantErr: "document needs a problem and a solution",
|
|
},
|
|
{
|
|
name: "an absent document leaves no plan to approve",
|
|
settings: `{document: "{{ steps.prd.result }}", then: implement}`,
|
|
wantErr: "the plan is empty",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
definition, err := ParseDefinition("id: w\nrepository: none\nsteps:\n - id: prd\n type: agent.run\n settings: {mission: m}\n - type: human.approve\n settings: "+tt.settings+"\n",
|
|
StepRegistry{AgentRunStepName: newAgentRunStage, ApproveStepName: newApproveStage})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tc := &TaskContext{Task: &Task{Source: task.SourceChat}, stepResults: map[string]StepResult{"prd": {Result: tt.result}}}
|
|
err = func() error {
|
|
stage, err := newApproveStage(renderSettings(definition.Steps[1].Settings, tc))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return stage.Run(t.Context(), 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)
|
|
}
|
|
for _, want := range tt.wantPlan {
|
|
if !strings.Contains(tc.Task.Plan, want) {
|
|
t.Errorf("plan lacks %q:\n%s", want, tc.Task.Plan)
|
|
}
|
|
}
|
|
if got := tc.Task.PlanDocument != ""; got != tt.wantDoc {
|
|
t.Errorf("document kept = %v, want %v", got, tt.wantDoc)
|
|
}
|
|
})
|
|
}
|
|
}
|