| 37 | } |
| 38 | |
| 39 | func StartContractServer(ctx context.Context, dbURL string) (*ContractServer, error) { |
| 40 | pool, err := OpenPreparedTestDB(ctx, dbURL) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | |
| 45 | store := identity.NewStore(pool) |
| 46 | signer := headers.NewSigner(TestHMACSecret) |
| 47 | smtpRelay := outbound.NewSMTPRelay(&config.OutboundSMTPConfig{}) |
| 48 | sender := outbound.NewSender(smtpRelay, "test.e2a.dev") |
| 49 | noopUsage := usage.NewNoopUsageTracker() |
| 50 | |
| 51 | // Limits/usage/webhook components the /v1 Deps bind to. Caps are set |
| 52 | // generously so contract scenarios exercise contract shape, not quota |
| 53 | // enforcement (the 402 limit paths have dedicated httpapi unit tests). |
| 54 | usageStore := usage.NewStore(pool) |
| 55 | enforcer := limits.NewEnforcer(limits.NewStore(pool), usageStore, limits.Defaults{ |
| 56 | PlanCode: "contract_test", MaxAgents: 100000, MaxDomains: 100000, |
| 57 | MaxMessagesMonth: 100000, MaxStorageBytes: 1 << 40, |
| 58 | }, time.Minute) |
| 59 | subscriberStore := webhook.NewSubscriberStore(pool) |
| 60 | idempotencyStore := idempotency.NewStore(pool) |
| 61 | |
| 62 | router := mux.NewRouter() |
| 63 | api := agent.NewAPI(store, sender, smtpRelay, nil, noopUsage, "e2a.dev", "test.e2a.dev", "agents.e2a.dev", "", false) |
| 64 | api.SetIdempotencyStore(idempotencyStore) |
| 65 | api.SetEnforcer(enforcer) |
| 66 | api.SetUsageStore(usageStore) |
| 67 | api.SetSubscriberStore(subscriberStore) |
| 68 | api.RegisterRoutes(router) |
| 69 | |
| 70 | wsHub := ws.NewHub() |
| 71 | wsHandler := ws.NewHandler(wsHub, store) |
| 72 | |
| 73 | // Wrap the legacy mux with the typed /v1 surface using the SAME builder |
| 74 | // the production binary uses, so contract scenarios hit the real /v1 |
| 75 | // handler (and a dep prod wires but the harness forgets fails loudly here). |
| 76 | v1 := apiserver.New(apiserver.Params{ |
| 77 | API: api, Store: store, Enforcer: enforcer, UsageStore: usageStore, |
| 78 | SubscriberStore: subscriberStore, Idempotency: idempotencyStore, Pool: pool, |
| 79 | SMTPDomain: "test.e2a.dev", SharedDomain: "agents.e2a.dev", |
| 80 | PublicURL: "http://127.0.0.1", Production: false, |
| 81 | EventsEnabled: true, |
| 82 | Legacy: router, WSHandle: wsHandler.ServeWithEmail, |
| 83 | }) |
| 84 | |
| 85 | httpLn, err := net.Listen("tcp", "127.0.0.1:0") |
| 86 | if err != nil { |
| 87 | pool.Close() |
| 88 | wsHub.Close() |
| 89 | return nil, err |
| 90 | } |
| 91 | |
| 92 | httpServer := &http.Server{ |
| 93 | Handler: v1, |
| 94 | ReadHeaderTimeout: 5 * time.Second, |
| 95 | } |
| 96 | |