| 1 | package transporttest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "strings" |
| 11 | "sync/atomic" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/runner" |
| 16 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 17 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 18 | ) |
| 19 | |
| 20 | func TestRunnerHeartbeatWireReports(t *testing.T) { |
| 21 | reports := []runnerproto.ConnectionReport{ |
| 22 | {LocalID: "claude-a", Provider: "claude", Family: "claude", Model: "claude-opus-5-5", Available: true, Routing: runnerproto.ConnectionRouting{BindingID: "claude-a", AccountID: "account-a", AccountPinned: true}}, |
| 23 | {LocalID: "claude-b", Provider: "claude", Family: "claude", Model: "claude-opus-5-5", UnavailableReason: "capability_unverified", Routing: runnerproto.ConnectionRouting{BindingID: "claude-b", AccountID: "account-b"}}, |
| 24 | } |
| 25 | for _, tc := range []struct { |
| 26 | name string |
| 27 | reports []runnerproto.ConnectionReport |
| 28 | }{{"two accounts", reports}, {"empty", []runnerproto.ConnectionReport{}}, {"nil", nil}} { |
| 29 | t.Run(tc.name, func(t *testing.T) { |
| 30 | reports := tc.reports |
| 31 | var called atomic.Bool |
| 32 | handler := (&runnerproto.Handler{ |
| 33 | AuthenticateRunner: func(context.Context, string) (runnerproto.RunnerPrincipal, error) { |
| 34 | return runnerproto.RunnerPrincipal{}, nil |
| 35 | }, |
| 36 | ReportConnections: func(_ context.Context, _ runnerproto.RunnerPrincipal, got []runnerproto.ConnectionReport) error { |
| 37 | called.Store(true) |
| 38 | if !reflect.DeepEqual(got, reports) { |
| 39 | t.Errorf("reports changed: %#v", got) |
| 40 | } |
| 41 | return nil |
| 42 | }, |
| 43 | Invalid: runnerproto.ErrInvalid, |
| 44 | Failure: func(w http.ResponseWriter, _ error) { w.WriteHeader(422) }, |
| 45 | JSON: func(w http.ResponseWriter, status int, v any) { |
| 46 | raw, err := runnerproto.Marshal(v) |
| 47 | if err != nil { |
| 48 | t.Error(err) |
| 49 | w.WriteHeader(http.StatusInternalServerError) |
| 50 | return |
| 51 | } |
| 52 | w.WriteHeader(status) |
| 53 | _, _ = w.Write(raw) |
| 54 | }, |
| 55 | }).Router() |
| 56 | client := httpClient(t, handler) |
| 57 | if err := client.Heartbeat(context.Background(), reports); err != nil { |
| 58 | t.Fatalf("heartbeat rejected: %v", err) |
| 59 | } |
| 60 | if !called.Load() { |
| 61 | t.Fatal("heartbeat did not report accounts") |
| 62 | } |
| 63 | }) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestRunnerHTTPContractDiagnostics(t *testing.T) { |
| 68 | for _, tc := range []struct { |
| 69 | name string |
| 70 | status int |
| 71 | body, check string |
| 72 | }{ |
| 73 | {"refusal", 422, `{"version":2,"error":"form.invalid"}`, "http.response.status_422.form.invalid"}, |
| 74 | {"untrusted refusal", 409, `{"version":2,"error":"secret-response"}`, "http.response.status_409.form.invalid"}, |
| 75 | {"redirect", 302, "secret-response", "http.response.status_302"}, |
| 76 | {"malformed envelope", 200, "secret-response", "http.response.envelope"}, |
| 77 | {"wrong version", 200, `{"version":1,"result":null}`, "http.response.version"}, |
| 78 | {"wrong result", 200, `{"version":2,"result":{"secret-response":true}}`, "http.response.result"}, |
| 79 | } { |
| 80 | t.Run(tc.name, func(t *testing.T) { |
| 81 | c := httpClient(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 82 | w.WriteHeader(tc.status) |
| 83 | _, _ = w.Write([]byte(tc.body)) |
| 84 | })) |
| 85 | _, err := c.Poll(context.Background()) |
| 86 | code, check := runner.Diagnostic(err) |
| 87 | if code != "runner.contract" || check != tc.check { |
| 88 | t.Fatalf("diagnostic = %s at %s; want runner.contract at %s", code, check, tc.check) |
| 89 | } |
| 90 | if !errors.Is(err, provider.ErrContract) { |
| 91 | t.Fatalf("contract classification lost: %v", err) |
| 92 | } |
| 93 | if strings.Contains(err.Error()+code+check, "secret-") { |
| 94 | t.Fatal("secret in diagnostic") |
| 95 | } |
| 96 | if tc.status == 422 || tc.status == 409 { |
| 97 | var refusal *runner.RefusalError |
| 98 | if !errors.As(err, &refusal) || refusal.Status != tc.status { |
| 99 | t.Fatal("refusal details lost") |
| 100 | } |
| 101 | } |
| 102 | }) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func httpClient(t *testing.T, handler http.Handler) *runner.HTTPInstance { |
| 107 | t.Helper() |
| 108 | server := httptest.NewServer(handler) |
| 109 | t.Cleanup(server.Close) |
| 110 | journal, err := runner.OpenJournal(context.Background(), filepath.Join(t.TempDir(), "state", "journal.db")) |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | t.Cleanup(func() { |
| 115 | if err := journal.Close(); err != nil { |
| 116 | t.Error(err) |
| 117 | } |
| 118 | }) |
| 119 | client, err := runner.NewHTTPInstance(server.URL, journal, runnerproto.RunnerCredential{Token: "secret-token", ExpiresAt: time.Now().Add(time.Hour)}) |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | return client |
| 124 | } |
| 125 | |