|
| 1 | +import { WORKLOAD_HEADERS } from "@trigger.dev/core/v3/workers"; |
| 2 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +vi.mock("std-env", () => ({ |
| 5 | + env: { |
| 6 | + TRIGGER_API_URL: "http://localhost:3030", |
| 7 | + TRIGGER_WORKER_TOKEN: "test-token", |
| 8 | + MANAGED_WORKER_SECRET: "test-secret", |
| 9 | + OTEL_EXPORTER_OTLP_ENDPOINT: "http://localhost:4318", |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +const { WorkloadServer } = await import("./index.js"); |
| 14 | + |
| 15 | +const PORT = 18734; |
| 16 | +const BASE = `http://127.0.0.1:${PORT}`; |
| 17 | + |
| 18 | +const startRunAttempt = vi.fn(async () => ({ |
| 19 | + success: true as const, |
| 20 | + data: { envVars: {} }, |
| 21 | +})); |
| 22 | + |
| 23 | +const workerClient = { startRunAttempt } as any; |
| 24 | + |
| 25 | +let server: InstanceType<typeof WorkloadServer>; |
| 26 | + |
| 27 | +beforeAll(async () => { |
| 28 | + server = new WorkloadServer({ |
| 29 | + port: PORT, |
| 30 | + workerClient, |
| 31 | + wideEventOpts: { service: "supervisor", env: { nodeId: "test" }, enabled: false }, |
| 32 | + wideEventsNoisyRoutes: false, |
| 33 | + workerStamp: { workerCell: "cell-a", workerLane: "microvm" }, |
| 34 | + }); |
| 35 | + await server.start(); |
| 36 | +}); |
| 37 | + |
| 38 | +afterAll(async () => { |
| 39 | + await server.stop(); |
| 40 | +}); |
| 41 | + |
| 42 | +function startAttempt(body: Record<string, unknown>) { |
| 43 | + return fetch(`${BASE}/api/v1/workload-actions/runs/run_1/snapshots/snap_1/attempts/start`, { |
| 44 | + method: "POST", |
| 45 | + headers: { |
| 46 | + "content-type": "application/json", |
| 47 | + [WORKLOAD_HEADERS.RUNNER_ID]: "runner_1", |
| 48 | + }, |
| 49 | + body: JSON.stringify(body), |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +describe("WorkloadServer attempt start", () => { |
| 54 | + it("forwards the runner body with the supervisor's cell and lane stamped on", async () => { |
| 55 | + const res = await startAttempt({ isWarmStart: true }); |
| 56 | + |
| 57 | + expect(res.status).toBe(200); |
| 58 | + expect(startRunAttempt).toHaveBeenLastCalledWith( |
| 59 | + "run_1", |
| 60 | + "snap_1", |
| 61 | + { isWarmStart: true, workerCell: "cell-a", workerLane: "microvm" }, |
| 62 | + "runner_1", |
| 63 | + undefined |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it("does not let the runner choose its own cell or lane", async () => { |
| 68 | + const res = await startAttempt({ workerCell: "spoofed", workerLane: "container" }); |
| 69 | + |
| 70 | + expect(res.status).toBe(200); |
| 71 | + expect(startRunAttempt).toHaveBeenLastCalledWith( |
| 72 | + "run_1", |
| 73 | + "snap_1", |
| 74 | + { workerCell: "cell-a", workerLane: "microvm" }, |
| 75 | + "runner_1", |
| 76 | + undefined |
| 77 | + ); |
| 78 | + }); |
| 79 | +}); |
0 commit comments