| 1 | package equipotest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http/httptest" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | ) |
| 13 | |
| 14 | func nativeQuestionFixture(t *testing.T) (runnerFixture, runnerproto.Question) { |
| 15 | f, q := questionFixture(t) |
| 16 | q.ID = "native" |
| 17 | q.PermissionID = "7" |
| 18 | q.Kind = "tool" |
| 19 | q.RawRequest = json.RawMessage(`{"id":"7","kind":"tool","direct":true,"provider":"codex","name":"item/commandExecution/requestApproval","turn_id":"local-turn","input":{"threadId":"session","turnId":"provider-turn","itemId":"item","command":"printf hello","cwd":"/workspace"},"request":{"id":7,"method":"item/commandExecution/requestApproval","params":{"threadId":"session","turnId":"provider-turn","itemId":"item","command":"printf hello","cwd":"/workspace"}}}`) |
| 20 | r := f.request() |
| 21 | r.RequestID = "native" |
| 22 | r.Question = &q |
| 23 | _, err := f.x.Mutate(connectionContext, f.principal, "questions", r) |
| 24 | executionMust(t, err) |
| 25 | return f, q |
| 26 | } |
| 27 | |
| 28 | func TestApprovedBeyondProfileHTTP(t *testing.T) { |
| 29 | f, q := nativeQuestionFixture(t) |
| 30 | mail := &capturingMailer{} |
| 31 | router, err := equipo.RouterWithStore(f.d, testConfig(testOrigin), mail, nil, f.s) |
| 32 | executionMust(t, err) |
| 33 | server := httptest.NewServer(router) |
| 34 | t.Cleanup(server.Close) |
| 35 | client := signinClient(t, server.URL) |
| 36 | signinEmail(t, client, server.URL, testOrigin, mail, ownerAddress) |
| 37 | path := "/tasks/" + f.a.TaskID + "/questions/" + q.ID |
| 38 | body := taskGet(t, client, server.URL, path, 200) |
| 39 | if strings.Count(body, `name="behavior"`) != 2 || !strings.Contains(body, ">Allow once</button>") || !strings.Contains(body, ">Deny</button>") { |
| 40 | t.Fatal("native approval must have exactly Allow once and Deny") |
| 41 | } |
| 42 | for _, extra := range []string{"actor", "answered_by", "grantRoot", "decision"} { |
| 43 | res := clientPost(t, client, server.URL+path+"/answer", testOrigin, url.Values{"revision": {"1"}, "behavior": {"allow"}, extra: {"forged"}}) |
| 44 | res.Body.Close() |
| 45 | if res.StatusCode != 422 { |
| 46 | t.Fatal("authority or amendment accepted", extra, res.StatusCode) |
| 47 | } |
| 48 | } |
| 49 | res := clientPost(t, client, server.URL+path+"/answer", testOrigin, url.Values{"revision": {"1"}, "behavior": {"allow"}}) |
| 50 | res.Body.Close() |
| 51 | if res.StatusCode != 303 { |
| 52 | t.Fatal("allow once refused", res.StatusCode) |
| 53 | } |
| 54 | var who, raw, iso string |
| 55 | executionMust(t, f.d.Writer().QueryRow("SELECT answered_by,raw_request FROM questions WHERE id=?", q.ID).Scan(&who, &raw)) |
| 56 | executionMust(t, f.d.Writer().QueryRow("SELECT isolation FROM runner_assignments WHERE id=?", f.a.ID).Scan(&iso)) |
| 57 | if who != ownerAddress || !strings.Contains(raw, "printf hello") || !strings.Contains(raw, "provider-turn") { |
| 58 | t.Fatal("instance approval lost actor or exact request", who, raw) |
| 59 | } |
| 60 | if !strings.Contains(iso, "approved beyond profile") || !strings.Contains(iso, ownerAddress) { |
| 61 | t.Fatal("assignment missing approval evidence", iso) |
| 62 | } |
| 63 | for _, route := range []string{"/journey", "/tasks/" + f.a.TaskID, "/specialists/engineering"} { |
| 64 | body = taskGet(t, client, server.URL, route, 200) |
| 65 | part := func(start, end string) string { |
| 66 | _, rest, ok := strings.Cut(body, start) |
| 67 | if !ok { |
| 68 | t.Fatalf("missing surface %s on %s", start, route) |
| 69 | } |
| 70 | value, _, ok := strings.Cut(rest, end) |
| 71 | if !ok { |
| 72 | t.Fatalf("unclosed surface %s on %s", start, route) |
| 73 | } |
| 74 | return value |
| 75 | } |
| 76 | if !strings.Contains(part("<div equipo-card>", "</div>"), "approved beyond profile") { |
| 77 | t.Fatal("task card header lost label", route) |
| 78 | } |
| 79 | if !strings.Contains(part(`data-step="`+f.a.StepKey+`"`, "</li>"), "approved beyond profile") { |
| 80 | t.Fatal("Journey step chip lost label", route) |
| 81 | } |
| 82 | if route == "/tasks/"+f.a.TaskID { |
| 83 | _, detail, _ := strings.Cut(body, "</article>") |
| 84 | if !strings.Contains(detail, "approved beyond profile") { |
| 85 | t.Fatal("task detail lost label") |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestDirectStaleAnswerNativeRevocation(t *testing.T) { |
| 92 | f, q := nativeQuestionFixture(t) |
| 93 | executionMust(t, f.s.AnswerQuestion(connectionContext, ownerActor, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "allow"})) |
| 94 | mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", ownerAddress) |
| 95 | r := f.request() |
| 96 | r.RequestID = "poll-native" |
| 97 | result, err := f.x.Mutate(connectionContext, f.principal, "answers", r) |
| 98 | if err == nil && len(result.([]runnerproto.Question)) != 0 { |
| 99 | t.Fatal("revoked native allow polled") |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestApprovedBeyondProfileFileRecord(t *testing.T) { |
| 104 | f, q := nativeQuestionFixture(t) |
| 105 | q.ID = "files" |
| 106 | q.PermissionID = "8" |
| 107 | q.RawRequest = json.RawMessage(`{"id":"8","kind":"tool","direct":true,"provider":"codex","name":"item/fileChange/requestApproval","turn_id":"local-turn","input":{"threadId":"session","turnId":"provider-turn","itemId":"files"},"request":{"id":8,"method":"item/fileChange/requestApproval","params":{"threadId":"session","turnId":"provider-turn","itemId":"files"}},"tool_call":{"method":"item/started","params":{"threadId":"session","turnId":"provider-turn","item":{"id":"files","type":"fileChange","status":"inProgress","changes":[{"path":"/workspace/a.txt","diff":"-old\n+new","kind":{"type":"update","move_path":"/workspace/b.txt"}}]}}}}`) |
| 108 | r := f.request() |
| 109 | r.RequestID = "files" |
| 110 | r.Question = &q |
| 111 | _, err := f.x.Mutate(connectionContext, f.principal, "questions", r) |
| 112 | executionMust(t, err) |
| 113 | executionMust(t, f.s.AnswerQuestion(connectionContext, ownerActor, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "allow"})) |
| 114 | var iso string |
| 115 | executionMust(t, f.d.Writer().QueryRow("SELECT isolation FROM runner_assignments WHERE id=?", f.a.ID).Scan(&iso)) |
| 116 | for _, want := range []string{"approved beyond profile", ownerAddress, "/workspace/a.txt", "/workspace/b.txt", "provider-turn", `"request_id":"8"`, `"item_id":"files"`} { |
| 117 | if !strings.Contains(iso, want) { |
| 118 | t.Fatal("file approval evidence lost", want, iso) |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestApprovedBeyondProfileLegacyArgv(t *testing.T) { |
| 124 | f, q := nativeQuestionFixture(t) |
| 125 | q.ID = "argv" |
| 126 | q.PermissionID = "9" |
| 127 | q.RawRequest = json.RawMessage(`{"id":"9","kind":"tool","direct":true,"provider":"codex","name":"execCommandApproval","turn_id":"local-turn","provider_turn_id":"provider-turn","input":{"conversationId":"session","callId":"command","command":["/bin/sh","-c","printf '%s' 'hello world'"],"cwd":"/workspace"},"request":{"id":9,"method":"execCommandApproval","params":{"conversationId":"session","callId":"command","command":["/bin/sh","-c","printf '%s' 'hello world'"],"cwd":"/workspace"}}}`) |
| 128 | r := f.request() |
| 129 | r.RequestID = "argv" |
| 130 | r.Question = &q |
| 131 | _, err := f.x.Mutate(connectionContext, f.principal, "questions", r) |
| 132 | executionMust(t, err) |
| 133 | executionMust(t, f.s.AnswerQuestion(connectionContext, ownerActor, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "allow"})) |
| 134 | var iso string |
| 135 | executionMust(t, f.d.Writer().QueryRow("SELECT isolation FROM runner_assignments WHERE id=?", f.a.ID).Scan(&iso)) |
| 136 | for _, want := range []string{`"command":["/bin/sh","-c","printf '%s' 'hello world'"]`, `"cwd":"/workspace"`, `"turn_id":"provider-turn"`, ownerAddress} { |
| 137 | if !strings.Contains(iso, want) { |
| 138 | t.Fatal("argv or person changed", want, iso) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestReopenedQuestionDropsProfileApproval(t *testing.T) { |
| 144 | f, q := nativeQuestionFixture(t) |
| 145 | person := "second-owner@example.com" |
| 146 | executionMust(t, f.d.G.Create(&equipo.Member{Address: person, Role: equipo.RoleOwner, Active: true, Revision: 1}).Error) |
| 147 | executionMust(t, f.s.AnswerQuestion(connectionContext, equipo.Actor{Address: person}, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "allow"})) |
| 148 | mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", person) |
| 149 | r := f.request() |
| 150 | r.RequestID = "reopen" |
| 151 | _, err := f.x.Mutate(connectionContext, f.principal, "answers", r) |
| 152 | executionMust(t, err) |
| 153 | var iso string |
| 154 | executionMust(t, f.d.Writer().QueryRow("SELECT isolation FROM runner_assignments WHERE id=?", f.a.ID).Scan(&iso)) |
| 155 | if strings.Contains(iso, "approved beyond profile") || strings.Contains(iso, person) { |
| 156 | t.Fatal("revoked approval label retained", iso) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func TestRunnerTrialHistoryLabel(t *testing.T) { |
| 161 | f := runnerFixtureNew(t, false) |
| 162 | cutoverRow(t, f.d, "supervised_trials", map[string]any{"id": "history", "runner_id": f.principal.RunnerID, "owner": ownerAddress, "acknowledged_at": "2026-11-01T00:00:00Z", "revoked_at": "2026-11-02T00:00:00Z", "versions": "historical versions"}) |
| 163 | mail := &capturingMailer{} |
| 164 | router, err := equipo.RouterWithStore(f.d, testConfig(testOrigin), mail, nil, f.s) |
| 165 | executionMust(t, err) |
| 166 | server := httptest.NewServer(router) |
| 167 | defer server.Close() |
| 168 | client := signinClient(t, server.URL) |
| 169 | signinEmail(t, client, server.URL, testOrigin, mail, ownerAddress) |
| 170 | body := taskGet(t, client, server.URL, "/runners/enrol", 200) |
| 171 | if !strings.Contains(body, "Historical supervised trial") || !strings.Contains(body, "historical versions") { |
| 172 | t.Fatal("old trial displayed without historical label") |
| 173 | } |
| 174 | } |
| 175 | |