package equipotest
import (
"encoding/json"
"net/http/httptest"
"net/url"
"strings"
"testing"
"amadan.net/oficina/equipo/internal/equipo"
"amadan.net/oficina/equipo/internal/runnerproto"
)
func nativeQuestionFixture(t *testing.T) (runnerFixture, runnerproto.Question) {
f, q := questionFixture(t)
q.ID = "native"
q.PermissionID = "7"
q.Kind = "tool"
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"}}}`)
r := f.request()
r.RequestID = "native"
r.Question = &q
_, err := f.x.Mutate(connectionContext, f.principal, "questions", r)
executionMust(t, err)
return f, q
}
func TestApprovedBeyondProfileHTTP(t *testing.T) {
f, q := nativeQuestionFixture(t)
mail := &capturingMailer{}
router, err := equipo.RouterWithStore(f.d, testConfig(testOrigin), mail, nil, f.s)
executionMust(t, err)
server := httptest.NewServer(router)
t.Cleanup(server.Close)
client := signinClient(t, server.URL)
signinEmail(t, client, server.URL, testOrigin, mail, ownerAddress)
path := "/tasks/" + f.a.TaskID + "/questions/" + q.ID
body := taskGet(t, client, server.URL, path, 200)
if strings.Count(body, `name="behavior"`) != 2 || !strings.Contains(body, ">Allow once") || !strings.Contains(body, ">Deny") {
t.Fatal("native approval must have exactly Allow once and Deny")
}
for _, extra := range []string{"actor", "answered_by", "grantRoot", "decision"} {
res := clientPost(t, client, server.URL+path+"/answer", testOrigin, url.Values{"revision": {"1"}, "behavior": {"allow"}, extra: {"forged"}})
res.Body.Close()
if res.StatusCode != 422 {
t.Fatal("authority or amendment accepted", extra, res.StatusCode)
}
}
res := clientPost(t, client, server.URL+path+"/answer", testOrigin, url.Values{"revision": {"1"}, "behavior": {"allow"}})
res.Body.Close()
if res.StatusCode != 303 {
t.Fatal("allow once refused", res.StatusCode)
}
var who, raw, iso string
executionMust(t, f.d.Writer().QueryRow("SELECT answered_by,raw_request FROM questions WHERE id=?", q.ID).Scan(&who, &raw))
executionMust(t, f.d.Writer().QueryRow("SELECT isolation FROM runner_assignments WHERE id=?", f.a.ID).Scan(&iso))
if who != ownerAddress || !strings.Contains(raw, "printf hello") || !strings.Contains(raw, "provider-turn") {
t.Fatal("instance approval lost actor or exact request", who, raw)
}
if !strings.Contains(iso, "approved beyond profile") || !strings.Contains(iso, ownerAddress) {
t.Fatal("assignment missing approval evidence", iso)
}
for _, route := range []string{"/journey", "/tasks/" + f.a.TaskID, "/specialists/engineering"} {
body = taskGet(t, client, server.URL, route, 200)
part := func(start, end string) string {
_, rest, ok := strings.Cut(body, start)
if !ok {
t.Fatalf("missing surface %s on %s", start, route)
}
value, _, ok := strings.Cut(rest, end)
if !ok {
t.Fatalf("unclosed surface %s on %s", start, route)
}
return value
}
if !strings.Contains(part("
", "
"), "approved beyond profile") {
t.Fatal("task card header lost label", route)
}
if !strings.Contains(part(`data-step="`+f.a.StepKey+`"`, ""), "approved beyond profile") {
t.Fatal("Journey step chip lost label", route)
}
if route == "/tasks/"+f.a.TaskID {
_, detail, _ := strings.Cut(body, "")
if !strings.Contains(detail, "approved beyond profile") {
t.Fatal("task detail lost label")
}
}
}
}
func TestDirectStaleAnswerNativeRevocation(t *testing.T) {
f, q := nativeQuestionFixture(t)
executionMust(t, f.s.AnswerQuestion(connectionContext, ownerActor, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "allow"}))
mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", ownerAddress)
r := f.request()
r.RequestID = "poll-native"
result, err := f.x.Mutate(connectionContext, f.principal, "answers", r)
if err == nil && len(result.([]runnerproto.Question)) != 0 {
t.Fatal("revoked native allow polled")
}
}
func TestApprovedBeyondProfileFileRecord(t *testing.T) {
f, q := nativeQuestionFixture(t)
q.ID = "files"
q.PermissionID = "8"
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"}}]}}}}`)
r := f.request()
r.RequestID = "files"
r.Question = &q
_, err := f.x.Mutate(connectionContext, f.principal, "questions", r)
executionMust(t, err)
executionMust(t, f.s.AnswerQuestion(connectionContext, ownerActor, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "allow"}))
var iso string
executionMust(t, f.d.Writer().QueryRow("SELECT isolation FROM runner_assignments WHERE id=?", f.a.ID).Scan(&iso))
for _, want := range []string{"approved beyond profile", ownerAddress, "/workspace/a.txt", "/workspace/b.txt", "provider-turn", `"request_id":"8"`, `"item_id":"files"`} {
if !strings.Contains(iso, want) {
t.Fatal("file approval evidence lost", want, iso)
}
}
}
func TestApprovedBeyondProfileLegacyArgv(t *testing.T) {
f, q := nativeQuestionFixture(t)
q.ID = "argv"
q.PermissionID = "9"
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"}}}`)
r := f.request()
r.RequestID = "argv"
r.Question = &q
_, err := f.x.Mutate(connectionContext, f.principal, "questions", r)
executionMust(t, err)
executionMust(t, f.s.AnswerQuestion(connectionContext, ownerActor, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "allow"}))
var iso string
executionMust(t, f.d.Writer().QueryRow("SELECT isolation FROM runner_assignments WHERE id=?", f.a.ID).Scan(&iso))
for _, want := range []string{`"command":["/bin/sh","-c","printf '%s' 'hello world'"]`, `"cwd":"/workspace"`, `"turn_id":"provider-turn"`, ownerAddress} {
if !strings.Contains(iso, want) {
t.Fatal("argv or person changed", want, iso)
}
}
}
func TestReopenedQuestionDropsProfileApproval(t *testing.T) {
f, q := nativeQuestionFixture(t)
person := "second-owner@example.com"
executionMust(t, f.d.G.Create(&equipo.Member{Address: person, Role: equipo.RoleOwner, Active: true, Revision: 1}).Error)
executionMust(t, f.s.AnswerQuestion(connectionContext, equipo.Actor{Address: person}, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "allow"}))
mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", person)
r := f.request()
r.RequestID = "reopen"
_, err := f.x.Mutate(connectionContext, f.principal, "answers", r)
executionMust(t, err)
var iso string
executionMust(t, f.d.Writer().QueryRow("SELECT isolation FROM runner_assignments WHERE id=?", f.a.ID).Scan(&iso))
if strings.Contains(iso, "approved beyond profile") || strings.Contains(iso, person) {
t.Fatal("revoked approval label retained", iso)
}
}
func TestRunnerTrialHistoryLabel(t *testing.T) {
f := runnerFixtureNew(t, false)
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"})
mail := &capturingMailer{}
router, err := equipo.RouterWithStore(f.d, testConfig(testOrigin), mail, nil, f.s)
executionMust(t, err)
server := httptest.NewServer(router)
defer server.Close()
client := signinClient(t, server.URL)
signinEmail(t, client, server.URL, testOrigin, mail, ownerAddress)
body := taskGet(t, client, server.URL, "/runners/enrol", 200)
if !strings.Contains(body, "Historical supervised trial") || !strings.Contains(body, "historical versions") {
t.Fatal("old trial displayed without historical label")
}
}