refactor: testing from the beginning. a journey in 150k lines of tests to none

This commit is contained in:
sam committed 2026-10-03 21:16:25 +10:00
1 parent ba37e37e53
commit ff424b70ac
651 files changed
-146443

No files matched your search

@@ -1,29 +0,0 @@
package task
import "testing"
// TestChangeStatusesAreSpelledAsPersisted pins the five status strings as
// literals. They are the on-disk vocabulary of a changes_captured payload and
// the dashboard mirrors them as its own key set
// (ui/src/tasks/changed-files.jsx FILE_STATUS), so renaming one here without
// the matching dashboard change renders raw status codes instead of words --
// with both suites still green unless each side pins the literal.
func TestChangeStatusesAreSpelledAsPersisted(t *testing.T) {
statuses := []struct {
name string
got string
want string
}{
{name: "ChangeAdded", got: ChangeAdded, want: "added"},
{name: "ChangeModified", got: ChangeModified, want: "modified"},
{name: "ChangeDeleted", got: ChangeDeleted, want: "deleted"},
{name: "ChangeRenamed", got: ChangeRenamed, want: "renamed"},
{name: "ChangeTypeChanged", got: ChangeTypeChanged, want: "typechange"},
}
for _, s := range statuses {
if s.got != s.want {
t.Errorf("%s = %q, want %q", s.name, s.got, s.want)
}
}
}
@@ -1,58 +0,0 @@
package task
import (
"testing"
"github.com/samcharles93/archie-core/internal/domain/org"
)
func TestWorkflowEnablementEnabled(t *testing.T) {
enablement := WorkflowEnablement{Orgs: map[org.OrgID]OrgWorkflows{
"acme": {Disabled: []string{"triage"}},
}}
cases := []struct {
org org.OrgID
workflow string
want bool
}{
{"acme", "triage", false},
{"acme", "implement", true},
{"other", "triage", true},
}
for _, c := range cases {
if got := enablement.Enabled(c.org, c.workflow); got != c.want {
t.Errorf("Enabled(%q, %q) = %v, want %v", c.org, c.workflow, got, c.want)
}
}
}
func TestWorkflowEnablementSetEnabledRoundTrips(t *testing.T) {
var enablement WorkflowEnablement
enablement = enablement.SetEnabled("acme", "triage", false)
enablement = enablement.SetEnabled("acme", "triage", false)
if enablement.Enabled("acme", "triage") || len(enablement.Orgs["acme"].Disabled) != 1 {
t.Fatalf("disable twice: %+v", enablement)
}
enablement = enablement.SetEnabled("acme", "triage", true)
if !enablement.Enabled("acme", "triage") {
t.Fatalf("re-enable: %+v", enablement)
}
}
func TestWorkflowEnablementValidate(t *testing.T) {
cases := map[string]struct {
enablement WorkflowEnablement
ok bool
}{
"empty": {WorkflowEnablement{}, true},
"valid": {WorkflowEnablement{Orgs: map[org.OrgID]OrgWorkflows{"acme": {Disabled: []string{"triage"}}}}, true},
"blank org": {WorkflowEnablement{Orgs: map[org.OrgID]OrgWorkflows{" ": {Disabled: []string{"triage"}}}}, false},
"blank workflow": {WorkflowEnablement{Orgs: map[org.OrgID]OrgWorkflows{"acme": {Disabled: []string{""}}}}, false},
"duplicate entry": {WorkflowEnablement{Orgs: map[org.OrgID]OrgWorkflows{"acme": {Disabled: []string{"triage", "triage"}}}}, false},
}
for name, c := range cases {
if err := c.enablement.Validate(); (err == nil) != c.ok {
t.Errorf("%s: Validate() = %v, want ok=%v", name, err, c.ok)
}
}
}
@@ -1,211 +0,0 @@
package task
import (
"encoding/json"
"strings"
"testing"
)
func TestParseWorkflowInterface(t *testing.T) {
tests := []struct {
name string
src string
wantErr string
wantMode RepositoryMode
}{
{name: "undeclared repository defaults to required", src: "id: a\nsteps: []\n", wantMode: RepositoryRequired},
{name: "none", src: "id: a\nrepository: none\n", wantMode: RepositoryNone},
{name: "unknown mode", src: "id: a\nrepository: sometimes\n", wantErr: "must be none, optional or required"},
{name: "unknown input type", src: "id: a\ninputs:\n ip: {type: ipv4}\n", wantErr: `type "ipv4"`},
{name: "malformed input name", src: "id: a\ninputs:\n src-ip: {type: string}\n", wantErr: `input "src-ip"`},
{name: "negative gate_retries", src: "id: a\nneeds: {gate_retries: -1}\n", wantErr: "gate_retries must not be negative"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w, err := ParseWorkflowInterface(tt.src)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("err = %v, want containing %q", err, tt.wantErr)
}
return
}
if err != nil {
t.Fatal(err)
}
if got := w.RepositoryMode(); got != tt.wantMode {
t.Errorf("RepositoryMode() = %q, want %q", got, tt.wantMode)
}
})
}
}
// TestParseWorkflowInterfaceNeeds pins the declared-needs vocabulary
// (docs/prds/external-agent-harness.md, "Contract"): a stage's requirements
// of its harness are static, readable metadata, not derived at run time.
func TestParseWorkflowInterfaceNeeds(t *testing.T) {
w, err := ParseWorkflowInterface("id: a\nprofile: contain\nneeds: {captures: true, gate_retries: 3}\n")
if err != nil {
t.Fatal(err)
}
if needs := w.Needs(); !needs.Captures || needs.GateRetries != 3 {
t.Fatalf("Needs() = %+v, want {Captures:true GateRetries:3}", needs)
}
}
// TestParseWorkflowInterfaceOutputsForceCaptures: an output is written
// through a capture tool, so declaring one forces the captures need whether
// or not the author also wrote needs.captures
// (docs/prds/workflow-call-outputs.md, "How a run writes one").
func TestParseWorkflowInterfaceOutputsForceCaptures(t *testing.T) {
w, err := ParseWorkflowInterface("id: a\noutputs:\n summary: {type: string}\n")
if err != nil {
t.Fatal(err)
}
if needs := w.Needs(); !needs.Captures {
t.Fatalf("Needs() = %+v, want Captures for a workflow that declares an output", needs)
}
}
func TestCheckInputs(t *testing.T) {
w := WorkflowInterface{Inputs: map[string]InputSpec{
"src_ip": {Type: "string", Required: true},
"severity": {Type: "number"},
"extra": {Type: "any"},
}}
tests := []struct {
name string
values map[string]any
wantErr string
}{
{name: "required and typed", values: map[string]any{"src_ip": "10.0.0.1", "severity": json.Number("3")}},
{name: "optional null is absent", values: map[string]any{"src_ip": "10.0.0.1", "severity": nil}},
{name: "any takes an object", values: map[string]any{"src_ip": "x", "extra": map[string]any{"a": 1.0}}},
{name: "missing required", values: map[string]any{"severity": 1.0}, wantErr: `"src_ip" is required`},
{name: "null required", values: map[string]any{"src_ip": nil}, wantErr: `"src_ip" is required`},
{name: "undeclared", values: map[string]any{"src_ip": "x", "port": 22.0}, wantErr: `"port" is not declared`},
{name: "wrong type", values: map[string]any{"src_ip": "x", "severity": "high"}, wantErr: `"severity" is string, want number`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := w.CheckInputs(tt.values)
if tt.wantErr == "" {
if err != nil {
t.Fatalf("CheckInputs() = %v", err)
}
return
}
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("CheckInputs() = %v, want containing %q", err, tt.wantErr)
}
})
}
}
// TestParseWorkflowInterfaceOutputs pins the declared-output grammar beside
// the declared-input grammar (docs/prds/workflow-call-outputs.md, "Where
// outputs are declared"): the same identifier shape, the same type
// vocabulary, the same Validate both ParseDefinition and
// ParseWorkflowInterface call.
func TestParseWorkflowInterfaceOutputs(t *testing.T) {
tests := []struct {
name string
src string
wantErr string
}{
{
name: "outputs parse beside inputs",
src: "id: a\ninputs:\n src_ip: {type: string}\noutputs:\n contained: {type: bool, required: true}\n summary: {type: string}\n",
},
{name: "unknown output type", src: "id: a\noutputs:\n contained: {type: boolean}\n", wantErr: `output "contained" type "boolean" must be one of`},
{name: "malformed output name", src: "id: a\noutputs:\n con-tained: {type: bool}\n", wantErr: `output "con-tained" must be a letter or underscore`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w, err := ParseWorkflowInterface(tt.src)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("err = %v, want containing %q", err, tt.wantErr)
}
return
}
if err != nil {
t.Fatal(err)
}
contained := w.Outputs["contained"]
if contained.Type != "bool" || !contained.Required {
t.Errorf("outputs.contained = %+v, want {bool required}", contained)
}
// required defaults to false.
if summary := w.Outputs["summary"]; summary.Type != "string" || summary.Required {
t.Errorf("outputs.summary = %+v, want {string not required}", summary)
}
})
}
}
// TestCheckOutputs pins the run-time and finish-time refusal the engine
// applies to a written output set: required, undeclared, wrong type -- with
// a written null counting as not written, exactly as CheckInputs judges a
// null input (docs/prds/workflow-call-outputs.md, "Failure rules").
func TestCheckOutputs(t *testing.T) {
w := WorkflowInterface{Outputs: map[string]OutputSpec{
"contained": {Type: "bool", Required: true},
"summary": {Type: "string"},
"extra": {Type: "any"},
}}
tests := []struct {
name string
values map[string]any
wantErr string
}{
{name: "required and typed", values: map[string]any{"contained": true, "summary": "done"}},
{name: "optional null is absent", values: map[string]any{"contained": true, "summary": nil}},
{name: "any takes an object", values: map[string]any{"contained": true, "extra": map[string]any{"a": 1.0}}},
{name: "missing required", values: map[string]any{"summary": "done"}, wantErr: `"contained" is required`},
{name: "null required", values: map[string]any{"contained": nil}, wantErr: `"contained" is required`},
{name: "undeclared", values: map[string]any{"contained": true, "port": 22}, wantErr: `"port" is not declared`},
{name: "wrong type", values: map[string]any{"contained": true, "summary": json.Number("3")}, wantErr: `"summary" is number, want string`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := w.CheckOutputs(tt.values)
if tt.wantErr == "" {
if err != nil {
t.Fatalf("CheckOutputs() = %v", err)
}
return
}
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("CheckOutputs() = %v, want containing %q", err, tt.wantErr)
}
})
}
}
// TestEncodeDecodeOutputs pins outputs' stored form: the shape EncodeInputs
// already has, with numbers exact across the round trip and empty for none.
func TestEncodeDecodeOutputs(t *testing.T) {
if s, err := EncodeOutputs(nil); err != nil || s != "" {
t.Fatalf("EncodeOutputs(nil) = (%q, %v), want (\"\", nil)", s, err)
}
encoded, err := EncodeOutputs(map[string]any{"contained": true, "n": json.Number("3")})
if err != nil {
t.Fatal(err)
}
decoded, err := DecodeOutputs(encoded)
if err != nil {
t.Fatal(err)
}
if decoded["contained"] != true {
t.Errorf("contained = %v, want true", decoded["contained"])
}
if _, ok := decoded["n"].(json.Number); !ok {
t.Errorf("n = %T, want json.Number", decoded["n"])
}
if outputs, err := DecodeOutputs(""); err != nil || outputs != nil {
t.Fatalf("DecodeOutputs(\"\") = (%+v, %v), want (nil, nil)", outputs, err)
}
if _, err := DecodeOutputs("{"); err == nil {
t.Fatal("DecodeOutputs({) = nil, want an error")
}
}
@@ -1,29 +0,0 @@
package task
import (
"encoding/json"
"testing"
)
func TestEffectivePRNumber(t *testing.T) {
for _, test := range []struct {
name string
task Task
want int
}{
{"PRNumber set directly wins", Task{PRNumber: 7, Inputs: map[string]any{"pr_number": json.Number("99")}}, 7},
{"json.Number from a store round trip", Task{Inputs: map[string]any{"pr_number": json.Number("42")}}, 42},
{"float64 from a hand-built map", Task{Inputs: map[string]any{"pr_number": float64(13)}}, 13},
{"int from a hand-built map", Task{Inputs: map[string]any{"pr_number": 5}}, 5},
{"missing input", Task{Inputs: map[string]any{}}, 0},
{"nil Inputs", Task{}, 0},
{"wrong type", Task{Inputs: map[string]any{"pr_number": "not a number"}}, 0},
{"malformed json.Number", Task{Inputs: map[string]any{"pr_number": json.Number("not-a-number")}}, 0},
} {
t.Run(test.name, func(t *testing.T) {
if got := test.task.EffectivePRNumber(); got != test.want {
t.Errorf("EffectivePRNumber() = %d, want %d", got, test.want)
}
})
}
}