Files
archie/internal/domain/workflow/triage_definition_test.go
T

127 lines
3.9 KiB
Go

package workflow
import (
"encoding/json"
"slices"
"strings"
"testing"
"github.com/google/jsonschema-go/jsonschema"
)
// triageRegistry is the built-in step vocabulary the shipped definitions use.
func triageRegistry() StepRegistry {
registry := StepRegistry{}
add := func(types ...StepType) {
for _, stepType := range types {
registry[stepType.Name] = stepType.Factory
}
}
add(RepoStepTypes()...)
add(ControlStepTypes()...)
add(ReviewStepTypes()...)
add(AgentRunStepType(), FinishStepType(), CommandRunStepType(), WorkflowCallStepType(), DiffRulesStepType())
return registry
}
// triageDefinition returns the shipped triage definition's steps.
func triageDefinition(t *testing.T) []StepRecord {
t.Helper()
var source string
for _, entry := range ShippedDefinitions().Definitions {
if entry.ID == "triage" {
source = entry.YAML
}
}
if source == "" {
t.Fatal("the triage definition is not shipped")
}
definition, err := ParseDefinition(source, triageRegistry())
if err != nil {
t.Fatalf("parse the triage definition: %v", err)
}
return definition.Steps
}
// triageStep returns the first step whose type or id is want, or fails.
func triageStep(t *testing.T, steps []StepRecord, want string) StepRecord {
t.Helper()
for _, step := range steps {
if step.Type == want || step.ID == want {
return step
}
}
t.Fatalf("no %q step", want)
return StepRecord{}
}
// The triage result's workflow names where a code change goes. A task that
// needs no change has nowhere to go, so requiring it makes the agent assert
// something it does not know -- and the capture tool rejects an empty required
// string on the agent's single allowed call, which fails the classification
// rather than closing the task.
func TestTriageDoesNotRequireAWorkflowForANonCodeTask(t *testing.T) {
t.Parallel()
classify := triageStep(t, triageDefinition(t), "classify")
var settings struct {
Result map[string]any `yaml:"result"`
}
if err := classify.Settings.Decode(&settings); err != nil {
t.Fatalf("decode the classify settings: %v", err)
}
tool, err := agentResultTool(settings.Result)
if err != nil {
t.Fatalf("build the result tool: %v", err)
}
if tool == nil {
t.Fatal("classify declares no result schema")
}
if slices.Contains(tool.RequiredFields, "workflow") {
t.Errorf("required = %v, want no workflow: a task that needs no code change has none to name", tool.RequiredFields)
}
if !slices.Contains(tool.RequiredFields, "needs_code_change") {
t.Errorf("required = %v, want needs_code_change", tool.RequiredFields)
}
// A code change with no workflow would hand off to nothing, so the schema
// rejects it and the agent can correct the call.
var schema jsonschema.Schema
if err := json.Unmarshal(tool.Parameters, &schema); err != nil {
t.Fatalf("decode the result schema: %v", err)
}
resolved, err := schema.Resolve(nil)
if err != nil {
t.Fatalf("resolve the result schema: %v", err)
}
for _, tt := range []struct {
result string
valid bool
}{
{`{"needs_code_change": false, "reasons": "r"}`, true},
{`{"needs_code_change": true, "workflow": "tdd", "reasons": "r"}`, true},
{`{"needs_code_change": true, "reasons": "r"}`, false},
} {
var instance any
if err := json.Unmarshal([]byte(tt.result), &instance); err != nil {
t.Fatal(err)
}
if err := resolved.Validate(instance); (err == nil) != tt.valid {
t.Errorf("%s: validate = %v, want valid=%v", tt.result, err, tt.valid)
}
}
}
// Handoff reads steps.classify.result.workflow. On the non-code path that field
// is absent, so the step must be skipped by its condition rather than relying on
// workflow.finish having ended the run first.
func TestTriageOnlyHandsOffWhenACodeChangeIsNeeded(t *testing.T) {
t.Parallel()
handoff := triageStep(t, triageDefinition(t), HandoffStepName)
if !strings.Contains(handoff.When, "needs_code_change") {
t.Errorf("%s when = %q, want a needs_code_change condition", HandoffStepName, handoff.When)
}
}