| 1 | package goal |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestWorkDurationPersistsWithoutChangingAuthority(t *testing.T) { |
| 10 | machine := newTestMachine(t) |
| 11 | created, err := machine.Create(CreateRequest{Objective: "finish"}) |
| 12 | if err != nil { |
| 13 | t.Fatal(err) |
| 14 | } |
| 15 | measured, err := machine.AddWorkDuration(created.Ref(), 1234) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | if measured.Ref() != created.Ref() || measured.WorkDurationMs != 1234 { |
| 20 | t.Fatalf("measured = %+v", measured) |
| 21 | } |
| 22 | cloned := machine.Clone() |
| 23 | if _, err := cloned.AddWorkDuration(created.Ref(), 10); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | if machine.Get().WorkDurationMs != 1234 { |
| 27 | t.Fatal("candidate mutated published goal") |
| 28 | } |
| 29 | data, err := cloned.Encode() |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | restored := newTestMachine(t) |
| 34 | view, err := restored.Restore(data) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if view.WorkDurationMs != 1244 || view.Activation != ActivationDisarmed { |
| 39 | t.Fatalf("restored = %+v", view) |
| 40 | } |
| 41 | invalid := strings.Replace(string(data), `"workDurationMs":1244`, `"workDurationMs":-1`, 1) |
| 42 | if _, err := restored.Restore([]byte(invalid)); err == nil { |
| 43 | t.Fatal("negative persisted work accepted") |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestWorkDurationRejectsStaleRefAndSaturates(t *testing.T) { |
| 48 | machine := newTestMachine(t) |
| 49 | created, err := machine.Create(CreateRequest{Objective: "finish"}) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if _, err := machine.AddWorkDuration(Ref{ID: "old", Revision: created.Revision}, 10); ErrorCodeOf(err) != ErrStaleRevision { |
| 54 | t.Fatalf("stale error = %v", err) |
| 55 | } |
| 56 | for _, duration := range []int64{0, -1, math.MaxInt64, 10} { |
| 57 | if _, err := machine.AddWorkDuration(created.Ref(), duration); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | } |
| 61 | if got := machine.Get().WorkDurationMs; got != math.MaxInt64 { |
| 62 | t.Fatalf("duration overflow = %d", got) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestWorkDurationDoesNotChangeGoalPrompts(t *testing.T) { |
| 67 | for _, tc := range []struct { |
| 68 | name string |
| 69 | activation Activation |
| 70 | prompt func(View) (string, error) |
| 71 | }{ |
| 72 | {"continuation", ActivationArmed, ContinuationPrompt}, {"recovery", ActivationDisarmed, RecoveryPrompt}, |
| 73 | } { |
| 74 | t.Run(tc.name, func(t *testing.T) { |
| 75 | view := View{Snapshot: Snapshot{ID: "goal", Revision: 1, Objective: "ship", Phase: PhaseActive}, Activation: tc.activation} |
| 76 | before, err := tc.prompt(view) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | view.WorkDurationMs = 123456 |
| 81 | after, err := tc.prompt(view) |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | if before != after || strings.Contains(after, "workDurationMs") { |
| 86 | t.Fatalf("duration changed %s prompt", tc.name) |
| 87 | } |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 |