File
Blob: tests/worker/webhooks/public-helpers.ts
| 1 | import { |
| 2 | CommitSha, |
| 3 | UnixTimestampMs, |
| 4 | type BranchName as BranchNameValue, |
| 5 | type ProjectId, |
| 6 | RunId, |
| 7 | type UserId, |
| 8 | } from "@/contracts"; |
| 9 | import { expectTrusted } from "@/worker/contracts"; |
| 10 | import { fillManualRunQueueWithoutAlarmOrRunInitialization } from "../../helpers/project-do"; |
| 11 | import { getProjectStub } from "../../helpers/runtime"; |
| 12 | |
| 13 | export const BEFORE_SHA = "1111111111111111111111111111111111111111"; |
| 14 | export const AFTER_SHA = "2222222222222222222222222222222222222222"; |
| 15 | export const THIRD_SHA = "3333333333333333333333333333333333333333"; |
| 16 | |
| 17 | export const buildVerifiedPushDeliveryInput = (input: { |
| 18 | projectId: ProjectId; |
| 19 | repoUrl: string; |
| 20 | deliveryId: string; |
| 21 | branch: BranchNameValue; |
| 22 | verifiedWebhookUpdatedAt: number; |
| 23 | }) => ({ |
| 24 | projectId: input.projectId, |
| 25 | payload: { |
| 26 | provider: "github" as const, |
| 27 | deliveryId: input.deliveryId, |
| 28 | eventKind: "push" as const, |
| 29 | eventName: "push", |
| 30 | repoUrl: input.repoUrl, |
| 31 | ref: "refs/heads/main", |
| 32 | branch: input.branch, |
| 33 | commitSha: expectTrusted(CommitSha, AFTER_SHA, "CommitSha"), |
| 34 | beforeSha: expectTrusted(CommitSha, BEFORE_SHA, "CommitSha"), |
| 35 | }, |
| 36 | outcome: "accepted" as const, |
| 37 | verifiedWebhookUpdatedAt: expectTrusted(UnixTimestampMs, input.verifiedWebhookUpdatedAt, "UnixTimestampMs"), |
| 38 | }); |
| 39 | |
| 40 | export const fillProjectQueueToCapacity = async (input: { |
| 41 | projectId: ProjectId; |
| 42 | userId: UserId; |
| 43 | branch: BranchNameValue; |
| 44 | }): Promise<void> => |
| 45 | await fillManualRunQueueWithoutAlarmOrRunInitialization( |
| 46 | getProjectStub(input.projectId), |
| 47 | { |
| 48 | projectId: input.projectId, |
| 49 | triggeredByUserId: input.userId, |
| 50 | branch: input.branch, |
| 51 | }, |
| 52 | 20, |
| 53 | ); |
| 54 | |
| 55 | export const freeOneQueuedSlot = async (projectId: ProjectId, runId: string): Promise<void> => { |
| 56 | const claim = await getProjectStub(projectId).claimRunWork({ |
| 57 | projectId, |
| 58 | runId: expectTrusted(RunId, runId, "RunId"), |
| 59 | }); |
| 60 | if (claim.kind !== "execute") { |
| 61 | throw new Error(`Expected queued slot release claim to execute, got ${claim.kind}.`); |
| 62 | } |
| 63 | |
| 64 | await getProjectStub(projectId).finalizeRunExecution({ |
| 65 | projectId, |
| 66 | runId: expectTrusted(RunId, runId, "RunId"), |
| 67 | terminalStatus: "failed", |
| 68 | lastError: "capacity_freed_for_webhook_resend", |
| 69 | sandboxDestroyed: true, |
| 70 | }); |
| 71 | }; |