package cli_test import ( "context" "encoding/json" "strconv" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" ) const cliHumanSource = "# stage gate\n\n```output\nDecision evidence.\n```\n\n```prompt\nApprove the release.\n```\n\n```edges\ngo -> #success\nhalt -> #failure\nerror -> #failure\n```\n\n```configs\ntype=human\n```\n\n" func TestCLIDecideWaitingHumanRun(t *testing.T) { run, store, _ := setup(t) ctx := context.Background() if _, err := store.Submit(ctx, "human-ws", cliHumanSource); err != nil { t.Fatal(err) } output, err := run("run", "human-ws", "ship it", "--json", "--yes") if err != nil { t.Fatal(err) } var started protocol.Message if err := json.Unmarshal([]byte(output), &started); err != nil || started.RunDetail == nil { t.Fatalf("run did not return detail: %v %s", err, output) } runID := started.RunDetail.ID waitFor := func(status string) protocol.RunDetail { t.Helper() for i := 0; i < 100; i++ { text, err := run("inspect", strconv.FormatInt(runID, 10), "--json") if err != nil { t.Fatal(err) } var detail protocol.RunDetail if err := json.Unmarshal([]byte(text), &detail); err != nil { t.Fatal(err) } if detail.Status == status { return detail } } t.Fatalf("run never reached %s", status) return protocol.RunDetail{} } waiting := waitFor(protocol.RunStatusWaiting) if len(waiting.Steps) != 1 || waiting.Steps[0].Status != protocol.StepStatusWaiting { t.Fatalf("waiting step not visible: %+v", waiting.Steps) } if prompt := protocol.WaitingPrompt(waiting.Steps[0].Output); !strings.Contains(prompt, "Approve the release") { t.Fatalf("waiting prompt not visible: %+v", waiting.Steps[0].Output) } if _, err := run("decide", strconv.FormatInt(runID, 10)); err == nil || !strings.Contains(err.Error(), "--option") { t.Fatalf("missing --option was accepted: %v", err) } if _, err := run("decide", strconv.FormatInt(runID, 10), "--option", "launch"); err == nil || !strings.Contains(err.Error(), "valid options") { t.Fatalf("unknown option was accepted: %v", err) } output, err = run("decide", strconv.FormatInt(runID, 10), "gate", "--option", "go", "--feedback", "approved", "--json") if err != nil { t.Fatal(err) } var decided protocol.RunDetail if err := json.Unmarshal([]byte(output), &decided); err != nil { t.Fatal(err) } final := waitFor("succeeded") gate := final.Steps[0] if gate.Outcome != "go" || gate.Target != protocol.SuccessEnd || gate.Output["feedback"] != "approved" || gate.Diagnostic != "" { t.Fatalf("decision evidence not recorded: %+v", gate) } if gate.Verdict == nil || gate.Verdict.Option != "go → success" || gate.Verdict.Reason != "approved" { t.Fatalf("derived verdict missing the decision: %+v", gate.Verdict) } // A repeated decision must not double-apply and must report the recorded // evidence, not the ignored request text. repeat, err := run("decide", strconv.FormatInt(runID, 10), "gate", "--option", "go", "--feedback", "again") if err != nil { t.Fatal(err) } if strings.Contains(repeat, "again") || !strings.Contains(repeat, "approved") { t.Fatalf("repeated decision reported the request, not the recorded evidence: %q", repeat) } after := waitFor("succeeded") if len(after.Steps) != 1 || after.Steps[0].Output["feedback"] != "approved" { t.Fatalf("repeated decision changed the recorded evidence: %+v", after.Steps) } if _, err := run("help", "decide"); err != nil { t.Fatal(err) } }