| 1 | package taskplan |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func publication() Plan { |
| 9 | return Plan{Kind: Bug, ProjectID: 1, Steps: []Step{ |
| 10 | {Key: "reproduce", Specialist: "engineering", Action: "reproduce"}, |
| 11 | {Key: "implement", Specialist: "engineering", Action: "implement"}, |
| 12 | {Key: "review", Specialist: "review", Action: "review"}, |
| 13 | {Key: "verify", Action: "checks"}, |
| 14 | {Key: "publish", Action: "publish"}, |
| 15 | {Key: "merge", Action: "person"}, |
| 16 | }} |
| 17 | } |
| 18 | func TestPlanRequiredSteps(t *testing.T) { |
| 19 | if err := Validate(publication()); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | for _, key := range []string{"review", "verify", "merge"} { |
| 23 | for _, op := range []string{"delete", "rename", "substitute"} { |
| 24 | t.Run(key+"/"+op, func(t *testing.T) { |
| 25 | p := publication() |
| 26 | for i := range p.Steps { |
| 27 | if p.Steps[i].Key == key { |
| 28 | switch op { |
| 29 | case "delete": |
| 30 | p.Steps = append(p.Steps[:i], p.Steps[i+1:]...) |
| 31 | case "rename": |
| 32 | p.Steps[i].Key = "other" |
| 33 | case "substitute": |
| 34 | p.Steps[i].Action = "implement" |
| 35 | } |
| 36 | break |
| 37 | } |
| 38 | } |
| 39 | if Validate(p) == nil { |
| 40 | t.Fatal("mandatory step bypassed") |
| 41 | } |
| 42 | }) |
| 43 | } |
| 44 | } |
| 45 | for _, tc := range []string{"duplicate", "cycle", "early_publish", "late_builder", "model_checks", "limit", "fake_review"} { |
| 46 | t.Run(tc, func(t *testing.T) { |
| 47 | p := publication() |
| 48 | switch tc { |
| 49 | case "duplicate": |
| 50 | p.Steps[1].Key = p.Steps[0].Key |
| 51 | case "cycle": |
| 52 | p.Steps[0].DependsOn = []string{"implement"} |
| 53 | case "early_publish": |
| 54 | p.Steps[2], p.Steps[4] = p.Steps[4], p.Steps[2] |
| 55 | case "late_builder": |
| 56 | p.Steps[1], p.Steps[3] = p.Steps[3], p.Steps[1] |
| 57 | case "model_checks": |
| 58 | p.Steps[3].Model = "model" |
| 59 | case "limit": |
| 60 | for len(p.Steps) <= 16 { |
| 61 | p.Steps = append(p.Steps, Step{Key: "extra", Action: "scope"}) |
| 62 | } |
| 63 | case "fake_review": |
| 64 | p.Steps[2].Specialist = "engineering" |
| 65 | } |
| 66 | if Validate(p) == nil { |
| 67 | t.Fatal("invalid plan accepted") |
| 68 | } |
| 69 | }) |
| 70 | } |
| 71 | for _, kind := range []Kind{Recommendation, Investigation} { |
| 72 | p := Plan{Kind: kind, ProjectID: 1, Steps: []Step{{Key: "read", Specialist: "product", Action: "recommend"}, {Key: "merge", Action: "person"}}} |
| 73 | if kind == Investigation { |
| 74 | p.Steps[0].Action = "investigate" |
| 75 | } |
| 76 | if err := Validate(p); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | p.Steps[0].Action = "implement" |
| 80 | if Validate(p) == nil { |
| 81 | t.Fatal("read-only plan permits write") |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | func TestPlanStartedImmutable(t *testing.T) { |
| 86 | before := publication() |
| 87 | before.TaskID = "task" |
| 88 | before.Revision = 1 |
| 89 | for _, change := range []string{"order", "instructions", "model", "connection", "dependencies", "delete"} { |
| 90 | t.Run(change, func(t *testing.T) { |
| 91 | after := publication() |
| 92 | after.TaskID = "task" |
| 93 | after.Revision = 1 |
| 94 | switch change { |
| 95 | case "order": |
| 96 | after.Steps[0], after.Steps[1] = after.Steps[1], after.Steps[0] |
| 97 | case "instructions": |
| 98 | after.Steps[1].Instructions = "changed" |
| 99 | case "model": |
| 100 | after.Steps[1].Model = "new" |
| 101 | case "connection": |
| 102 | after.Steps[1].ConnectionID = "new" |
| 103 | case "dependencies": |
| 104 | after.Steps[1].DependsOn = []string{"reproduce"} |
| 105 | case "delete": |
| 106 | after.Steps = append(after.Steps[:1], after.Steps[2:]...) |
| 107 | } |
| 108 | if ValidateEdit(before, after, map[string]bool{"implement": true}) == nil { |
| 109 | t.Fatal("started step changed") |
| 110 | } |
| 111 | }) |
| 112 | } |
| 113 | after := publication() |
| 114 | after.TaskID = "task" |
| 115 | after.Revision = 1 |
| 116 | after.Steps[1].Instructions = "new" |
| 117 | if err := ValidateEdit(before, after, nil); err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestPlanExecutionLimit(t *testing.T) { |
| 123 | for _, hidden := range []bool{false, true} { |
| 124 | p := publication() |
| 125 | if hidden { |
| 126 | p.Steps = append(p.Steps[:4], p.Steps[5:]...) |
| 127 | } |
| 128 | for i := 0; len(p.Steps) < 16; i++ { |
| 129 | p.Steps = append([]Step{{Key: fmt.Sprint("scope-", i), Action: "scope"}}, p.Steps...) |
| 130 | } |
| 131 | if hidden { |
| 132 | if Validate(p) == nil { |
| 133 | t.Fatal("hidden publication exceeds limit") |
| 134 | } |
| 135 | } else { |
| 136 | if err := Validate(p); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | p.Steps = append([]Step{{Key: "extra", Action: "scope"}}, p.Steps...) |
| 140 | if Validate(p) == nil { |
| 141 | t.Fatal("plan exceeds 16 steps") |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |