| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | "amadan.net/oficina/equipo/internal/taskplan" |
| 13 | ) |
| 14 | |
| 15 | func nativeConversationFixture(t *testing.T) (*fixture, *equipo.Execution, equipo.RunnerPrincipal, equipo.RunnerRequest, runnerproto.Question) { |
| 16 | t.Helper() |
| 17 | f, x, principal, _ := coordinatorFixture(t) |
| 18 | var original equipo.Task |
| 19 | must(t, f.DB.G.First(&original).Error) |
| 20 | p, err := f.Store.ProposePlan(ctx, owner, taskplan.Plan{Kind: taskplan.Recommendation, ProjectID: original.ProjectID, Steps: []taskplan.Step{{Key: "research", Specialist: "product", Action: "recommend", Instructions: "Read project material"}, {Key: "merge", Action: "person"}}}, equipo.Mutation{RequestID: "native-plan"}) |
| 21 | must(t, err) |
| 22 | // Retain just the read task for deterministic offline assignment. |
| 23 | f.exec(t, "UPDATE tasks SET state='cancelled' WHERE id=?", original.ID) |
| 24 | must(t, x.Sync(ctx)) |
| 25 | var id string |
| 26 | var revision int64 |
| 27 | must(t, f.DB.Writer().QueryRow("SELECT id,revision FROM runner_assignments WHERE task_id=? AND state='pending'", p.TaskID).Scan(&id, &revision)) |
| 28 | claim, err := x.Claim(ctx, principal, equipo.ClaimRequest{Version: 2, AssignmentID: id, Revision: revision, RequestID: "native-claim"}) |
| 29 | must(t, err) |
| 30 | x.SetRunnerValidators(equipo.RunnerValidators{Question: func(context.Context, equipo.Assignment, equipo.RunnerRequest) error { return nil }}) |
| 31 | q := runnerproto.Question{ID: "native-question", TaskID: p.TaskID, ServerID: "server", AgentID: "agent", SessionID: "session", PermissionID: "permission", Kind: "question", Revision: 1, RawRequest: json.RawMessage(`{"id":"permission","kind":"question","provider":"claude","name":"AskUserQuestion","input":{"questions":[{"question":"Which?","header":"Which?"}]}}`)} |
| 32 | request := equipo.RunnerRequest{Version: 2, AssignmentID: id, Scope: claim.Scope, Handle: claim.Handle, RequestID: "native-question", Question: &q} |
| 33 | _, err = x.Mutate(ctx, principal, "questions", request) |
| 34 | must(t, err) |
| 35 | request.Question = nil |
| 36 | liveFixture(t, f) |
| 37 | return f, x, principal, request, q |
| 38 | } |
| 39 | func TestConversationQuestionRaceNative(t *testing.T) { |
| 40 | for _, state := range []string{"current", "stale", "expired", "cancelled"} { |
| 41 | t.Run(state, func(t *testing.T) { |
| 42 | f, x, principal, request, q := nativeConversationFixture(t) |
| 43 | path := "/tasks/" + q.TaskID + "/questions/" + q.ID + "/answer" |
| 44 | v := url.Values{"revision": {"1"}, "behavior": {"allow"}, "answer_0": {"Blue"}} |
| 45 | switch state { |
| 46 | case "stale": |
| 47 | v.Set("revision", "2") |
| 48 | case "expired": |
| 49 | f.exec(t, "UPDATE runner_assignments SET lease_deadline='2000-01-01T00:00:00Z' WHERE id=?", request.AssignmentID) |
| 50 | case "cancelled": |
| 51 | _, err := x.Core.Cancel(ctx, request.Scope, "fixture", "cancel") |
| 52 | must(t, err) |
| 53 | } |
| 54 | for i := 0; i < 2; i++ { |
| 55 | w := formPost(t, f, owner, path, v) |
| 56 | if state == "current" { |
| 57 | expectHTTP(t, w, 303) |
| 58 | } else if w.Code < 400 { |
| 59 | t.Fatal("inactive native answer accepted", state, w.Code) |
| 60 | } |
| 61 | } |
| 62 | if state != "current" { |
| 63 | if f.count(t, "SELECT count(*) FROM questions WHERE state='answered'") != 0 { |
| 64 | t.Fatal("inactive native answer persisted") |
| 65 | } |
| 66 | return |
| 67 | } |
| 68 | if f.count(t, "SELECT count(*) FROM audit_events WHERE kind='question.answer'") != 1 { |
| 69 | t.Fatal("duplicate native answer effect") |
| 70 | } |
| 71 | request.RequestID = "poll-native" |
| 72 | result, err := x.Mutate(ctx, principal, "answers", request) |
| 73 | must(t, err) |
| 74 | answers := result.([]runnerproto.Question) |
| 75 | if len(answers) != 1 || answers[0].PermissionID != q.PermissionID || answers[0].SessionID != q.SessionID || answers[0].Revision != q.Revision { |
| 76 | t.Fatal("answer lost original provider request", answers) |
| 77 | } |
| 78 | if !strings.Contains(string(answers[0].Answer.UpdatedInput), "Blue") { |
| 79 | t.Fatal("native answer shape lost") |
| 80 | } |
| 81 | expectHTTP(t, formPost(t, f, owner, path, url.Values{"revision": {"1"}, "text": {"allow"}, "option_id": {"one"}}), 422) |
| 82 | }) |
| 83 | } |
| 84 | } |
| 85 | func TestConversationScriptlessSharedPlan(t *testing.T) { |
| 86 | f := newFixture(t) |
| 87 | p := proposed(t, f) |
| 88 | p.Kind = taskplan.Recommendation |
| 89 | p.Steps = []taskplan.Step{{Key: "research", Specialist: "product", Action: "recommend", Instructions: "Investigate the project"}, {Key: "merge", Action: "person"}} |
| 90 | p, err := f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "read-plan", Revision: p.Revision}) |
| 91 | must(t, err) |
| 92 | f.exec(t, "UPDATE members SET role='member' WHERE address=?", peer.Address) |
| 93 | for _, path := range []string{"/tasks/" + p.TaskID, "/tasks/" + p.TaskID + "/plan", "/tasks/" + p.TaskID + "/thread", "/journey", "/specialists/product"} { |
| 94 | w := f.get(t, peer, path) |
| 95 | expectHTTP(t, w, 200) |
| 96 | if path != "/tasks/"+p.TaskID+"/thread" && (!strings.Contains(w.Body.String(), "research") || !strings.Contains(w.Body.String(), "/tasks/"+p.TaskID+"/thread")) { |
| 97 | t.Fatal("shared page lacks real planned steps or thread link", path) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestConversationScriptlessReadCompletion(t *testing.T) { |
| 103 | f, _, _, request, q := nativeConversationFixture(t) |
| 104 | f.exec(t, "UPDATE jobs SET state='succeeded' WHERE id=?", request.Scope.Job) |
| 105 | f.exec(t, "UPDATE tasks SET state='succeeded' WHERE id=?", q.TaskID) |
| 106 | w := f.get(t, owner, "/tasks/"+q.TaskID) |
| 107 | expectHTTP(t, w, 200) |
| 108 | if strings.Contains(w.Body.String(), `href="/tasks/`+q.TaskID+`/merge"`) || !strings.Contains(w.Body.String(), "Answer delivered") { |
| 109 | t.Fatal("read-only completion presented as publication") |
| 110 | } |
| 111 | } |
| 112 | |