package taskplan import ( "fmt" "testing" ) func publication() Plan { return Plan{Kind: Bug, ProjectID: 1, Steps: []Step{ {Key: "reproduce", Specialist: "engineering", Action: "reproduce"}, {Key: "implement", Specialist: "engineering", Action: "implement"}, {Key: "review", Specialist: "review", Action: "review"}, {Key: "verify", Action: "checks"}, {Key: "publish", Action: "publish"}, {Key: "merge", Action: "person"}, }} } func TestPlanRequiredSteps(t *testing.T) { if err := Validate(publication()); err != nil { t.Fatal(err) } for _, key := range []string{"review", "verify", "merge"} { for _, op := range []string{"delete", "rename", "substitute"} { t.Run(key+"/"+op, func(t *testing.T) { p := publication() for i := range p.Steps { if p.Steps[i].Key == key { switch op { case "delete": p.Steps = append(p.Steps[:i], p.Steps[i+1:]...) case "rename": p.Steps[i].Key = "other" case "substitute": p.Steps[i].Action = "implement" } break } } if Validate(p) == nil { t.Fatal("mandatory step bypassed") } }) } } for _, tc := range []string{"duplicate", "cycle", "early_publish", "late_builder", "model_checks", "limit", "fake_review"} { t.Run(tc, func(t *testing.T) { p := publication() switch tc { case "duplicate": p.Steps[1].Key = p.Steps[0].Key case "cycle": p.Steps[0].DependsOn = []string{"implement"} case "early_publish": p.Steps[2], p.Steps[4] = p.Steps[4], p.Steps[2] case "late_builder": p.Steps[1], p.Steps[3] = p.Steps[3], p.Steps[1] case "model_checks": p.Steps[3].Model = "model" case "limit": for len(p.Steps) <= 16 { p.Steps = append(p.Steps, Step{Key: "extra", Action: "scope"}) } case "fake_review": p.Steps[2].Specialist = "engineering" } if Validate(p) == nil { t.Fatal("invalid plan accepted") } }) } for _, kind := range []Kind{Recommendation, Investigation} { p := Plan{Kind: kind, ProjectID: 1, Steps: []Step{{Key: "read", Specialist: "product", Action: "recommend"}, {Key: "merge", Action: "person"}}} if kind == Investigation { p.Steps[0].Action = "investigate" } if err := Validate(p); err != nil { t.Fatal(err) } p.Steps[0].Action = "implement" if Validate(p) == nil { t.Fatal("read-only plan permits write") } } } func TestPlanStartedImmutable(t *testing.T) { before := publication() before.TaskID = "task" before.Revision = 1 for _, change := range []string{"order", "instructions", "model", "connection", "dependencies", "delete"} { t.Run(change, func(t *testing.T) { after := publication() after.TaskID = "task" after.Revision = 1 switch change { case "order": after.Steps[0], after.Steps[1] = after.Steps[1], after.Steps[0] case "instructions": after.Steps[1].Instructions = "changed" case "model": after.Steps[1].Model = "new" case "connection": after.Steps[1].ConnectionID = "new" case "dependencies": after.Steps[1].DependsOn = []string{"reproduce"} case "delete": after.Steps = append(after.Steps[:1], after.Steps[2:]...) } if ValidateEdit(before, after, map[string]bool{"implement": true}) == nil { t.Fatal("started step changed") } }) } after := publication() after.TaskID = "task" after.Revision = 1 after.Steps[1].Instructions = "new" if err := ValidateEdit(before, after, nil); err != nil { t.Fatal(err) } } func TestPlanExecutionLimit(t *testing.T) { for _, hidden := range []bool{false, true} { p := publication() if hidden { p.Steps = append(p.Steps[:4], p.Steps[5:]...) } for i := 0; len(p.Steps) < 16; i++ { p.Steps = append([]Step{{Key: fmt.Sprint("scope-", i), Action: "scope"}}, p.Steps...) } if hidden { if Validate(p) == nil { t.Fatal("hidden publication exceeds limit") } } else { if err := Validate(p); err != nil { t.Fatal(err) } p.Steps = append([]Step{{Key: "extra", Action: "scope"}}, p.Steps...) if Validate(p) == nil { t.Fatal("plan exceeds 16 steps") } } } }