| 1 | package builtin |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | goaldomain "reasonix/internal/goal" |
| 11 | "reasonix/internal/tool" |
| 12 | ) |
| 13 | |
| 14 | func TestUpdateGoalLifecycleActionsAcceptInactiveFields(t *testing.T) { |
| 15 | for _, action := range []tool.GoalAction{tool.GoalActionResume, tool.GoalActionPause, tool.GoalActionComplete} { |
| 16 | for _, fields := range []string{ |
| 17 | ``, |
| 18 | `,"objective":"ship","max_goal_rounds":null,"blocked_reason":""`, |
| 19 | `,"objective":"","max_goal_rounds":null,"blocked_reason":""`, |
| 20 | `,"objective":null,"max_goal_rounds":null,"blocked_reason":null`, |
| 21 | `,"objective":"different objective","max_goal_rounds":20,"blocked_reason":"old blocker"`, |
| 22 | } { |
| 23 | t.Run(string(action)+fields, func(t *testing.T) { |
| 24 | args := json.RawMessage(fmt.Sprintf(`{"goal_id":"goal-1","revision":3,"action":%q%s}`, action, fields)) |
| 25 | validation := tool.ValidateArguments(updateGoal{}, args) |
| 26 | if validation.CompileErr != nil || len(validation.Violations) != 0 || validation.Skipped { |
| 27 | t.Fatalf("host argument validation = %+v", validation) |
| 28 | } |
| 29 | stub := &goalLifecycleStub{view: goalView()} |
| 30 | ctx := goalLifecycleContext(stub, tool.GoalSourceDirectHuman) |
| 31 | if _, err := (updateGoal{}).Execute(ctx, args); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | want := tool.GoalUpdateRequest{Ref: goaldomain.Ref{ID: "goal-1", Revision: 3}, Action: action} |
| 35 | if stub.updateRequest != want { |
| 36 | t.Fatalf("inactive fields leaked into mutation: %+v", stub.updateRequest) |
| 37 | } |
| 38 | binding, _ := tool.GoalLifecycleFromContext(ctx) |
| 39 | if stub.authority != binding.Authority { |
| 40 | t.Fatalf("authority changed: %+v", stub.authority) |
| 41 | } |
| 42 | }) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestUpdateGoalEditAndBlockedConsumeOnlyActiveFields(t *testing.T) { |
| 48 | for _, tc := range []struct { |
| 49 | name, args string |
| 50 | check func(*testing.T, tool.GoalUpdateRequest) |
| 51 | }{ |
| 52 | {"edit", `{"goal_id":"goal-1","revision":3,"action":"edit","objective":" ship safely ","blocked_reason":"old blocker"}`, func(t *testing.T, got tool.GoalUpdateRequest) { |
| 53 | if got.Objective == nil || *got.Objective != "ship safely" || got.MaxGoalRounds.Set || got.BlockedReason != nil { |
| 54 | t.Fatalf("edit request = %+v", got) |
| 55 | } |
| 56 | }}, |
| 57 | {"blocked", `{"goal_id":"goal-1","revision":3,"action":"blocked","objective":"old objective","max_goal_rounds":null,"blocked_reason":" dependency unavailable "}`, func(t *testing.T, got tool.GoalUpdateRequest) { |
| 58 | if got.Objective != nil || got.MaxGoalRounds.Set || got.BlockedReason == nil || got.BlockedReason.Message != "dependency unavailable" { |
| 59 | t.Fatalf("blocked request = %+v", got) |
| 60 | } |
| 61 | }}, |
| 62 | } { |
| 63 | t.Run(tc.name, func(t *testing.T) { |
| 64 | stub := &goalLifecycleStub{view: goalView()} |
| 65 | if _, err := (updateGoal{}).Execute(goalLifecycleContext(stub, tool.GoalSourceDirectHuman), json.RawMessage(tc.args)); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | tc.check(t, stub.updateRequest) |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestUpdateGoalInactiveFieldsPreserveOwnerRejections(t *testing.T) { |
| 74 | for _, code := range []goaldomain.ErrorCode{goaldomain.ErrStaleRevision, goaldomain.ErrUserAuthorityRequired, goaldomain.ErrInvalidTransition} { |
| 75 | t.Run(string(code), func(t *testing.T) { |
| 76 | stub := &goalLifecycleStub{view: goalView(), err: &goaldomain.Error{Code: code, Message: "rejected by owner"}} |
| 77 | result, err := (updateGoal{}).Execute(goalLifecycleContext(stub, tool.GoalSourceDirectHuman), json.RawMessage(`{"goal_id":"goal-1","revision":3,"action":"resume","objective":"ship","max_goal_rounds":null,"blocked_reason":""}`)) |
| 78 | if !errors.Is(err, stub.err) || goaldomain.ErrorCodeOf(err) != code { |
| 79 | t.Fatalf("owner error was lost: %v", err) |
| 80 | } |
| 81 | if result != "" { |
| 82 | t.Fatalf("rejected action returned a success result: %s", result) |
| 83 | } |
| 84 | }) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestUpdateGoalReportsUnusedFields(t *testing.T) { |
| 89 | for _, tc := range []struct { |
| 90 | name, action, fields, ignored string |
| 91 | }{ |
| 92 | {"resume", "resume", `,"objective":"other target","max_goal_rounds":20,"blocked_reason":"old blocker"`, "objective, max_goal_rounds, blocked_reason"}, |
| 93 | {"pause", "pause", `,"objective":"other target","max_goal_rounds":20`, "objective, max_goal_rounds"}, |
| 94 | {"complete", "complete", `,"objective":"other target","max_goal_rounds":20`, "objective, max_goal_rounds"}, |
| 95 | {"edit", "edit", `,"objective":"new target","blocked_reason":"old blocker"`, "blocked_reason"}, |
| 96 | {"blocked", "blocked", `,"objective":"old target","max_goal_rounds":null,"blocked_reason":"dependency missing"`, "objective, max_goal_rounds"}, |
| 97 | {"empty placeholders", "resume", `,"objective":"","max_goal_rounds":null,"blocked_reason":""`, "objective, max_goal_rounds, blocked_reason"}, |
| 98 | {"null placeholders", "resume", `,"objective":null,"max_goal_rounds":null,"blocked_reason":null`, "max_goal_rounds"}, |
| 99 | {"omitted", "resume", ``, ""}, |
| 100 | {"active fields only", "edit", `,"objective":"new target","max_goal_rounds":null`, ""}, |
| 101 | } { |
| 102 | t.Run(tc.name, func(t *testing.T) { |
| 103 | view := goalView() |
| 104 | switch tc.action { |
| 105 | case "complete": |
| 106 | view.Phase = goaldomain.PhaseComplete |
| 107 | case "blocked": |
| 108 | view.Phase = goaldomain.PhaseBlocked |
| 109 | } |
| 110 | stub := &goalLifecycleStub{view: view} |
| 111 | args := json.RawMessage(fmt.Sprintf(`{"goal_id":"goal-1","revision":3,"action":%q%s}`, tc.action, tc.fields)) |
| 112 | result, err := (updateGoal{}).Execute(goalLifecycleContext(stub, tool.GoalSourceDirectHuman), args) |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | var got goalToolValue |
| 117 | if err := json.Unmarshal([]byte(result), &got); err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | if tc.ignored == "" { |
| 121 | if got.Instruction != "" { |
| 122 | t.Fatalf("unexpected instruction: %q", got.Instruction) |
| 123 | } |
| 124 | } else if !strings.Contains(got.Instruction, "Fields not applied: "+tc.ignored+".") { |
| 125 | t.Fatalf("instruction = %q, want ignored fields %q", got.Instruction, tc.ignored) |
| 126 | } |
| 127 | if (tc.action == "complete" || tc.action == "blocked") && !strings.HasPrefix(got.Instruction, "Finish the current turn") { |
| 128 | t.Fatalf("terminal instruction lost: %q", got.Instruction) |
| 129 | } |
| 130 | if got.Goal == nil || got.Goal.Objective != view.Objective || got.Goal.Phase != view.Phase { |
| 131 | t.Fatalf("effective goal state lost: %+v", got.Goal) |
| 132 | } |
| 133 | }) |
| 134 | } |
| 135 | } |
| 136 |