| 1 | package transporttest |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/coordinatorcontract" |
| 16 | "amadan.net/oficina/equipo/internal/runner" |
| 17 | "amadan.net/oficina/equipo/internal/runner/heartbeat" |
| 18 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 19 | ) |
| 20 | |
| 21 | func TestCoordinatorRecordedRunnerContract(t *testing.T) { |
| 22 | for _, name := range []string{"heartbeat", "poll", "claim", "delivery", "text", "reply"} { |
| 23 | t.Run(name, func(t *testing.T) { |
| 24 | p, err := coordinatorcontract.Load(name) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | called := false |
| 29 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 30 | called = true |
| 31 | if r.URL.Path != p.Path || r.Method != "POST" || r.Header.Get("Authorization") != "Bearer fixture" { |
| 32 | t.Errorf("request routing drift: %s %s", r.Method, r.URL.Path) |
| 33 | } |
| 34 | body, err := io.ReadAll(r.Body) |
| 35 | if err != nil { |
| 36 | t.Error(err) |
| 37 | } |
| 38 | want := p.Request |
| 39 | if name == "heartbeat" { |
| 40 | var got, expected map[string]any |
| 41 | json.Unmarshal(body, &got) |
| 42 | json.Unmarshal(want, &expected) |
| 43 | if got["request_id"] == "" { |
| 44 | t.Error("heartbeat has no request ID") |
| 45 | } |
| 46 | got["request_id"] = expected["request_id"] |
| 47 | expected["repositories"] = []string{} |
| 48 | want, _ = json.Marshal(expected) |
| 49 | body, _ = json.Marshal(got) |
| 50 | } |
| 51 | if coordinatorcontract.Canonical(body) != coordinatorcontract.Canonical(want) { |
| 52 | t.Error("runner request differs from recorded instance contract") |
| 53 | } |
| 54 | w.Header().Set("Content-Type", "application/json") |
| 55 | w.Write(p.Response) |
| 56 | })) |
| 57 | defer server.Close() |
| 58 | j, err := runner.OpenJournal(context.Background(), filepath.Join(t.TempDir(), "runner", "journal.db")) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | defer j.Close() |
| 63 | api, err := runner.NewHTTPInstance(server.URL+"/api/runner/v2", j, runnerproto.RunnerCredential{Token: "fixture", ExpiresAt: time.Now().Add(time.Hour)}) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | var out any |
| 68 | if name == "heartbeat" { |
| 69 | var q runnerproto.HeartbeatRequest |
| 70 | if err = runnerproto.DecodeLimit(bytes.NewReader(p.Request), &q, 3<<20); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | var reports []runnerproto.ConnectionReport |
| 74 | if err = runnerproto.DecodeLimit(bytes.NewReader(q.Reports), &reports, 3<<20); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | built, buildErr := heartbeat.Request(reports, func() ([]string, error) { return q.Repositories, nil }) |
| 78 | if buildErr != nil { |
| 79 | t.Fatal(buildErr) |
| 80 | } |
| 81 | built.RequestID = q.RequestID |
| 82 | raw, _ := runnerproto.Marshal(built) |
| 83 | if coordinatorcontract.Canonical(raw) != coordinatorcontract.Canonical(p.Request) { |
| 84 | t.Fatal("heartbeat builder drift") |
| 85 | } |
| 86 | err = api.Heartbeat(context.Background(), reports) |
| 87 | } else { |
| 88 | var q runnerproto.CoordinatorRequest |
| 89 | if err = runnerproto.DecodeLimit(bytes.NewReader(p.Request), &q, 3<<20); err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | switch name { |
| 93 | case "poll": |
| 94 | out = &[]runnerproto.CoordinatorTurn{} |
| 95 | case "claim": |
| 96 | out = &runnerproto.CoordinatorClaim{} |
| 97 | case "reply": |
| 98 | out = &runnerproto.CoordinatorToolResult{} |
| 99 | } |
| 100 | op := strings.TrimPrefix(p.Path, "/api/runner/v2/coordinator/") |
| 101 | err = api.CoordinatorCall(context.Background(), op, q, out) |
| 102 | } |
| 103 | if err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | if !called { |
| 107 | t.Fatal("no HTTP request") |
| 108 | } |
| 109 | if out != nil { |
| 110 | var envelope struct { |
| 111 | Result json.RawMessage `json:"result"` |
| 112 | } |
| 113 | json.Unmarshal(p.Response, &envelope) |
| 114 | raw, err := runnerproto.Marshal(out) |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | if coordinatorcontract.Canonical(raw) != coordinatorcontract.Canonical(envelope.Result) { |
| 119 | t.Fatal("runner decoded response drift") |
| 120 | } |
| 121 | } |
| 122 | }) |
| 123 | } |
| 124 | } |
| 125 | |