|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "text/tabwriter" |
| 10 | + |
| 11 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/render" |
| 12 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/testkit" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +func newTestCmd() *cobra.Command { |
| 17 | + return &cobra.Command{ |
| 18 | + Use: "test [workflow/<name>]", |
| 19 | + Short: "Run YAML fixture tests under tests/", |
| 20 | + Long: `Discover YAML files under <project>/tests/ (recursive), parse workflow test cases, |
| 21 | +and execute each case with the same project load, normalization, and environment overlay as |
| 22 | +agentctl run (issue #73, design doc §10.2, §17.4). |
| 23 | +
|
| 24 | +Use mock/native providers in project YAML for deterministic runs. Assertions: expect.outputContains |
| 25 | +(substrings on the workflow output JSON) and expectError. |
| 26 | +
|
| 27 | +Optional argument filters to one workflow by metadata name: workflow/demo or demo (both accepted). |
| 28 | +
|
| 29 | +Exit codes (§11.2): 0 all passed, 1 failures or I/O errors, 2 validation (bad project, bad suite, unknown workflow filter).`, |
| 30 | + Example: ` agentctl test |
| 31 | + agentctl test workflow/demo |
| 32 | + agentctl test demo -o json`, |
| 33 | + SilenceUsage: true, |
| 34 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 35 | + if len(args) > 1 { |
| 36 | + return NewExitError(ExitValidationError, fmt.Errorf("test: at most one workflow filter argument")) |
| 37 | + } |
| 38 | + return runTest(cmd, args) |
| 39 | + }, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func parseTestWorkflowFilter(arg string) (string, error) { |
| 44 | + arg = strings.TrimSpace(arg) |
| 45 | + if arg == "" { |
| 46 | + return "", nil |
| 47 | + } |
| 48 | + low := strings.ToLower(arg) |
| 49 | + if strings.HasPrefix(low, "workflow/") { |
| 50 | + return parseWorkflowTarget(arg) |
| 51 | + } |
| 52 | + return arg, nil |
| 53 | +} |
| 54 | + |
| 55 | +func runTest(cmd *cobra.Command, args []string) error { |
| 56 | + ctx := context.Background() |
| 57 | + g := Globals() |
| 58 | + |
| 59 | + graph, root, err := prepareProjectGraph(g.ProjectRoot, g) |
| 60 | + if err != nil { |
| 61 | + return NewExitError(ExitValidationError, err) |
| 62 | + } |
| 63 | + |
| 64 | + var wfFilter string |
| 65 | + if len(args) == 1 { |
| 66 | + var perr error |
| 67 | + wfFilter, perr = parseTestWorkflowFilter(args[0]) |
| 68 | + if perr != nil { |
| 69 | + return NewExitError(ExitValidationError, fmt.Errorf("test: %w", perr)) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + rootAbs := root |
| 74 | + testsDir := filepath.Join(rootAbs, "tests") |
| 75 | + if _, err := os.Stat(testsDir); os.IsNotExist(err) { |
| 76 | + return writeTestNoTests(cmd, g, rootAbs) |
| 77 | + } |
| 78 | + |
| 79 | + envName := strings.TrimSpace(g.Env) |
| 80 | + envLabel := planEnvironment(g) |
| 81 | + opts := testkit.RunOptions{ |
| 82 | + EnvironmentName: envName, |
| 83 | + EnvLabel: envLabel, |
| 84 | + } |
| 85 | + |
| 86 | + outcomes, err := testkit.LoadAndRunAll(ctx, rootAbs, opts, wfFilter) |
| 87 | + if err != nil { |
| 88 | + return NewExitError(ExitValidationError, err) |
| 89 | + } |
| 90 | + if len(outcomes) == 0 { |
| 91 | + return writeTestNoTests(cmd, g, rootAbs) |
| 92 | + } |
| 93 | + |
| 94 | + if werr := writeTestResults(cmd, rootAbs, graph.Meta.Name, envLabel, outcomes, g); werr != nil { |
| 95 | + return werr |
| 96 | + } |
| 97 | + |
| 98 | + failed := 0 |
| 99 | + for _, o := range outcomes { |
| 100 | + if !o.Passed { |
| 101 | + failed++ |
| 102 | + } |
| 103 | + } |
| 104 | + if failed > 0 { |
| 105 | + return NewExitError(ExitGenericFailure, fmt.Errorf("test: %d case(s) failed", failed)) |
| 106 | + } |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func writeTestNoTests(cmd *cobra.Command, g *Global, root string) error { |
| 111 | + out := cmd.OutOrStdout() |
| 112 | + switch g.Output { |
| 113 | + case render.FormatJSON: |
| 114 | + return render.WriteJSON(out, map[string]any{ |
| 115 | + "projectRoot": root, |
| 116 | + "message": "no tests found under tests/", |
| 117 | + "cases": []any{}, |
| 118 | + }) |
| 119 | + case render.FormatYAML: |
| 120 | + return render.WriteYAML(out, map[string]any{ |
| 121 | + "projectRoot": root, |
| 122 | + "message": "no tests found under tests/", |
| 123 | + "cases": []any{}, |
| 124 | + }) |
| 125 | + default: |
| 126 | + _, err := fmt.Fprintf(out, "No tests found under %s/tests\n", root) |
| 127 | + return err |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func writeTestResults(cmd *cobra.Command, projectRoot, projectName, env string, outcomes []testkit.CaseOutcome, g *Global) error { |
| 132 | + out := cmd.OutOrStdout() |
| 133 | + switch g.Output { |
| 134 | + case render.FormatJSON: |
| 135 | + rows := make([]map[string]any, len(outcomes)) |
| 136 | + passed := 0 |
| 137 | + for i, o := range outcomes { |
| 138 | + rows[i] = map[string]any{ |
| 139 | + "file": relPath(projectRoot, o.File), "workflow": o.Workflow, "case": o.Case, |
| 140 | + "passed": o.Passed, "detail": o.Detail, |
| 141 | + } |
| 142 | + if o.Passed { |
| 143 | + passed++ |
| 144 | + } |
| 145 | + } |
| 146 | + return render.WriteJSON(out, map[string]any{ |
| 147 | + "projectRoot": projectRoot, |
| 148 | + "project": projectName, |
| 149 | + "environment": env, |
| 150 | + "passed": passed, |
| 151 | + "failed": len(outcomes) - passed, |
| 152 | + "cases": rows, |
| 153 | + }) |
| 154 | + case render.FormatYAML: |
| 155 | + rows := make([]map[string]any, len(outcomes)) |
| 156 | + passed := 0 |
| 157 | + for i, o := range outcomes { |
| 158 | + rows[i] = map[string]any{ |
| 159 | + "file": relPath(projectRoot, o.File), "workflow": o.Workflow, "case": o.Case, |
| 160 | + "passed": o.Passed, "detail": o.Detail, |
| 161 | + } |
| 162 | + if o.Passed { |
| 163 | + passed++ |
| 164 | + } |
| 165 | + } |
| 166 | + return render.WriteYAML(out, map[string]any{ |
| 167 | + "projectRoot": projectRoot, |
| 168 | + "project": projectName, |
| 169 | + "environment": env, |
| 170 | + "passed": passed, |
| 171 | + "failed": len(outcomes) - passed, |
| 172 | + "cases": rows, |
| 173 | + }) |
| 174 | + default: |
| 175 | + passed := 0 |
| 176 | + for _, o := range outcomes { |
| 177 | + if o.Passed { |
| 178 | + passed++ |
| 179 | + } |
| 180 | + } |
| 181 | + if _, err := fmt.Fprintf(out, "Project: %s (%s)\nEnvironment: %s\n\n", projectName, projectRoot, env); err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + tw := tabwriter.NewWriter(out, 0, 0, 2, ' ', 0) |
| 185 | + _, _ = fmt.Fprintln(tw, "FILE\tWORKFLOW\tCASE\tRESULT\tDETAIL") |
| 186 | + for _, o := range outcomes { |
| 187 | + res := "pass" |
| 188 | + if !o.Passed { |
| 189 | + res = "fail" |
| 190 | + } |
| 191 | + d := o.Detail |
| 192 | + if d == "" { |
| 193 | + d = "-" |
| 194 | + } |
| 195 | + _, _ = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\n", relPath(projectRoot, o.File), o.Workflow, o.Case, res, d) |
| 196 | + } |
| 197 | + if err := tw.Flush(); err != nil { |
| 198 | + return err |
| 199 | + } |
| 200 | + _, err := fmt.Fprintf(out, "\n%d passed, %d failed\n", passed, len(outcomes)-passed) |
| 201 | + return err |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +func relPath(root, p string) string { |
| 206 | + r, err := filepath.Rel(root, p) |
| 207 | + if err != nil { |
| 208 | + return p |
| 209 | + } |
| 210 | + return r |
| 211 | +} |
0 commit comments