@cnc-tools/storm 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/bin/storm.js +3 -14
  2. package/dist/application.d.ts +18 -10
  3. package/dist/application.js +10 -11
  4. package/dist/bin/storm.js +97 -41
  5. package/dist/build-info.js +1 -1
  6. package/dist/chunks/chunk-5CQCRX2L.js +5768 -0
  7. package/dist/chunks/chunk-5U5Q6VZR.js +556 -0
  8. package/dist/chunks/chunk-7O6UN63H.js +63 -0
  9. package/dist/chunks/chunk-I5GZLOLQ.js +38 -0
  10. package/dist/chunks/chunk-NX55IJ5S.js +1842 -0
  11. package/dist/chunks/chunk-OCZJE5XV.js +910 -0
  12. package/dist/chunks/chunk-RRQ2ZGSE.js +1512 -0
  13. package/dist/chunks/chunk-S5NAIZYS.js +13 -0
  14. package/dist/chunks/chunk-UHO5OCEX.js +360 -0
  15. package/dist/chunks/chunk-WBIHJM7D.js +188 -0
  16. package/dist/chunks/chunk-WV6WQFC6.js +436 -0
  17. package/dist/chunks/live-app-X3PW2W42.js +89 -0
  18. package/dist/chunks/version-X7RMSKZI.js +12 -0
  19. package/dist/commands/exec/command.js +35 -21
  20. package/dist/commands/exec/metadata.js +66 -58
  21. package/dist/commands/exec/redaction.js +1 -1
  22. package/dist/commands/exec/run-collector.d.ts +1 -1
  23. package/dist/commands/exec/run-collector.js +1 -1
  24. package/dist/commands/exec/run-record.d.ts +1 -1
  25. package/dist/commands/exec/tests-summary.d.ts +1 -1
  26. package/dist/commands/exec/worker-chart.d.ts +1 -1
  27. package/dist/commands/runs/list/command.js +136 -33
  28. package/dist/commands/runs/list/metadata.js +22 -18
  29. package/dist/commands/runs/logs/command.js +131 -48
  30. package/dist/commands/runs/logs/metadata.js +24 -20
  31. package/dist/commands/runs/metadata.js +6 -2
  32. package/dist/commands/runs/past-run.d.ts +1 -1
  33. package/dist/commands/runs/past-run.js +1 -1
  34. package/dist/commands/runs/show/command.js +396 -74
  35. package/dist/commands/runs/show/metadata.js +24 -20
  36. package/dist/commands/vitest/command.js +195 -128
  37. package/dist/commands/vitest/metadata.js +45 -37
  38. package/dist/context.d.ts +1 -1
  39. package/dist/core/events.d.ts +1 -1
  40. package/dist/core/payload.d.ts +1 -1
  41. package/dist/core/payload.js +1 -1
  42. package/dist/core/provider.d.ts +12 -2
  43. package/dist/core/run.js +1 -1
  44. package/dist/live-app.d.ts +9 -0
  45. package/dist/live-app.js +21 -0
  46. package/dist/resource-commands.d.ts +13 -0
  47. package/dist/resource-commands.js +22 -0
  48. package/dist/resources/github/provider.d.ts +1 -1
  49. package/dist/resources/testserver/command-tree.d.ts +4 -0
  50. package/dist/resources/testserver/command-tree.js +12 -0
  51. package/dist/resources/testserver/commands/create/command.js +373 -194
  52. package/dist/resources/testserver/commands/create/metadata.js +52 -48
  53. package/dist/resources/testserver/commands/list/command.js +195 -50
  54. package/dist/resources/testserver/commands/list/metadata.js +12 -8
  55. package/dist/resources/testserver/provider.d.ts +1 -1
  56. package/dist/resources/testserver/resource-provider.js +2 -8
  57. package/dist/resources.d.ts +0 -3
  58. package/dist/resources.js +5 -10
  59. package/dist/runner/recipe.js +1 -1
  60. package/dist/runner/remote-command.js +1 -1
  61. package/dist/sdk/host.d.ts +2 -0
  62. package/dist/sdk/host.js +7 -0
  63. package/dist/version.d.ts +2 -2
  64. package/dist/version.js +10 -11
  65. package/dist/vitest/config.d.ts +4 -8
  66. package/dist/vitest/config.js +5 -9
  67. package/dist/vitest/container-scope.d.ts +12 -5
  68. package/dist/vitest/container-scope.js +27 -4
  69. package/dist/vitest/diagnostics.d.ts +8 -2
  70. package/dist/vitest/diagnostics.js +7 -4
  71. package/dist/vitest/fixtures.d.ts +4 -1
  72. package/dist/vitest/fixtures.js +17 -6
  73. package/dist/vitest/plan.d.ts +1 -1
  74. package/dist/vitest/plan.js +2 -2
  75. package/dist/vitest/sandbox.d.ts +6 -0
  76. package/dist/vitest/sandbox.js +6 -0
  77. package/package.json +4 -3
  78. package/bin/node-version.js +0 -24
  79. package/dist/closed-terminal.d.ts +0 -7
  80. package/dist/closed-terminal.js +0 -18
@@ -1,208 +1,387 @@
1
+ import {
2
+ REQUESTING_TOKEN_LINE,
3
+ configErrors,
4
+ credentialOriginRows,
5
+ fileOptions,
6
+ fileSettings,
7
+ invalidResourceOptions,
8
+ loadSettingsFile,
9
+ mergeResourceOptions,
10
+ prBuildRefusalErrors,
11
+ prBuildRefusalFacts,
12
+ rejectedCredentialFacts,
13
+ rejectedCredentialShape,
14
+ rejectedCredentialText,
15
+ testserverPoolConfig,
16
+ tokenRequestFailureFacts,
17
+ tokenRequestFailureShape
18
+ } from "../../../../chunks/chunk-OCZJE5XV.js";
19
+ import {
20
+ CREATE_ATTEMPTS,
21
+ DEFAULT_TTL_MINUTES,
22
+ TESTSERVER_REPO,
23
+ createPooledSandbox,
24
+ credentialErrors,
25
+ credentialSourceFor,
26
+ describeMintFailure,
27
+ describeTtl,
28
+ invalidTtlMessage,
29
+ mayLeaveSandbox,
30
+ mintTargetText,
31
+ parseBuildReference,
32
+ parseTtl,
33
+ resolvePrBuild,
34
+ settleCredential,
35
+ shortSha
36
+ } from "../../../../chunks/chunk-5CQCRX2L.js";
37
+
38
+ // src/resources/testserver/commands/create/command.ts
39
+ import { z as z2 } from "zod";
40
+
41
+ // src/resources/testserver/commands/create/outcomes.ts
42
+ import { defineOutcomes, renderOutcome } from "@vercel/clinkr";
1
43
  import { z } from "zod";
2
- import { fileOptions, fileSettings, invalidResourceOptions, loadSettingsFile, mergeResourceOptions, } from "../../../../config/index.js";
3
- import { credentialSourceFor, describeMintFailure, mintTargetText, settleCredential, } from "../../../../credential/index.js";
4
- import { parseTtl } from "../../../../time/index.js";
5
- import { testserverPoolConfig } from "../../config.js";
6
- import { rejectedCredentialFacts, REQUESTING_TOKEN_LINE, tokenRequestFailureFacts } from "../../credential-facts.js";
7
- import { createPooledSandbox, resolvePrBuild } from "../../pool/index.js";
8
- import { prBuildRefusalFacts } from "../../pr-build-facts.js";
9
- import { CREATE_ATTEMPTS, DEFAULT_TTL_MINUTES, invalidTtlMessage, mayLeaveSandbox, parseBuildReference, shortSha, TESTSERVER_REPO, } from "../../sandbox/index.js";
10
- import { outcomes } from "./outcomes.js";
11
- /**
12
- * Makes sure the pool is usable, then gets a credential from the credential source `--credential` picks (no config
13
- * key): by default it mints one for `--scope`/`--project`; with `github-actions` it requests one GitHub Actions OIDC
14
- * token instead, and needs no `--scope` or `--project`. Then it creates one Testserver sandbox on `--build` living
15
- * `--ttl` (5 hours by default), retrying only creates that never reached the Testserver control plane, and adds it to
16
- * the pool, unleased. In text output it reports each step, and each progress message the control plane sends, indented,
17
- * as it arrives.
18
- *
19
- * A PR build reference (`pr:<N>`, or the vercel/api pull request's URL) is resolved first, as a run resolves it, with
20
- * the host's GitHub token: the pull request's head commit from GitHub, then the PR build of a pool entry created for
21
- * `pr:<N>` on that commit, or else the newest PR build vercel/api's `Testserver` workflow published from it. The
22
- * Testserver sandbox is created on that PR build, and its pool entry records `pr:<N>` as its requested build and the
23
- * head commit, so a later run on that head finds the PR build in the pool. A PR build reference that can't be resolved
24
- * refuses, and nothing is created.
25
- *
26
- * Each setting comes from its flag, else `storm.config.ts` (found from the working directory), else its built-in
27
- * default; `--scope` and `--project` have none, so one of the two must give them. `pool create` only ever makes
28
- * Testserver sandboxes, so it reads `build` and `ttl` through the Testserver pool resource's config, from the file's
29
- * `resources.testserverPool` whether or not the file has one, with `--build` and `--ttl` over them. It checks the
30
- * whole file, as every command that loads it does, but not the Testserver pool's checks across options, since
31
- * `minTimeLeft` doesn't apply to a create.
32
- */
33
- export function command(cmd) {
34
- return cmd.define({
35
- requestSchema: z.object({
36
- scope: z.string().min(1).optional(),
37
- project: z.string().min(1).optional(),
38
- build: z.string().min(1).optional(),
39
- ttl: z.string().optional(),
40
- credential: z.enum(["mint", "github-actions"]).default("mint"),
41
- }),
42
- cli: { options: { project: { short: "p" } } },
43
- response: outcomes,
44
- validate: ({ ttl, build }) => [
45
- ...(ttl !== undefined && parseTtl(ttl) === undefined ? [{ field: "ttl", message: invalidTtlMessage(ttl) }] : []),
46
- ...(build === undefined ? [] : buildProblems(build)),
44
+ function sandboxMayExist(ttlMinutes) {
45
+ return `A Testserver sandbox may exist anyway. Nothing can delete it, and it expires within ${describeTtl(ttlMinutes)}.`;
46
+ }
47
+ var createFailedKinds = ["http-error", "stream-error", "stream-ended", "dropped", "timeout", "malformed"];
48
+ var sandboxSchema = z.object({
49
+ /** The Testserver URL, with no trailing slash. */
50
+ testserverUrl: z.string(),
51
+ /** The log viewer URL, with no trailing slash. */
52
+ logViewerUrl: z.string(),
53
+ /** The build, build channel or PR build reference (pr:<N>) given with --build. */
54
+ requestedBuild: z.string(),
55
+ /** The build the Testserver sandbox runs. */
56
+ resolvedBuild: z.string(),
57
+ /** For a PR build reference, the pull request and the head commit the resolved PR build was made from; null otherwise. */
58
+ pullRequest: z.object({
59
+ /** The repository, vercel/api. */
60
+ repo: z.string(),
61
+ number: z.number(),
62
+ /** The pull request's head commit, 40 hex characters. */
63
+ headSha: z.string()
64
+ }).nullable(),
65
+ /** Its TTL, in minutes. */
66
+ ttlMinutes: z.number(),
67
+ /** The config file the settings came from (its absolute path); null when none applied. */
68
+ configFile: z.string().nullable()
69
+ });
70
+ var outcomes = defineOutcomes({
71
+ results: {
72
+ created: {
73
+ schema: sandboxSchema,
74
+ description: "Created one Testserver sandbox with the TTL --ttl gives and added it to the pool, unleased.",
75
+ render: (sandbox) => renderOutcome({
76
+ done: ["Created", sandbox.testserverUrl],
77
+ rows: [
78
+ ["Log viewer URL", sandbox.logViewerUrl],
79
+ ["Requested build", sandbox.requestedBuild],
80
+ ["Resolved build", sandbox.resolvedBuild],
81
+ ...sandbox.pullRequest === null ? [] : [["Head commit", sandbox.pullRequest.headSha]],
82
+ ["TTL", describeTtl(sandbox.ttlMinutes)]
83
+ ],
84
+ message: "Added it to the pool, unleased."
85
+ })
86
+ }
87
+ },
88
+ errors: {
89
+ ...configErrors,
90
+ ...credentialErrors,
91
+ ...prBuildRefusalErrors("No Testserver sandbox was created."),
92
+ poolUnavailable: {
93
+ exitCode: 1,
94
+ description: "storm couldn't use the pool, so it minted no credential and created no Testserver sandbox.",
95
+ schema: z.object({
96
+ /** The pool directory that couldn't be made, or where the pool would live. */
97
+ path: z.string(),
98
+ /** What the OS said, or why there is no place for the pool. */
99
+ message: z.string()
100
+ }),
101
+ render: ({ path, message }) => ({
102
+ message: `Couldn't prepare the pool: ${message}`,
103
+ rows: [["Pool path", path]],
104
+ next: [{ type: "text", text: "Fix the pool path, then retry. No Testserver sandbox was created." }]
105
+ })
106
+ },
107
+ mintFailed: {
108
+ exitCode: 1,
109
+ description: "The vercel CLI couldn't mint a credential for the team and project.",
110
+ schema: z.object({ team: z.string(), project: z.string(), cause: z.string(), fix: z.string() }),
111
+ render: ({ cause, fix }) => ({
112
+ message: `Couldn't mint a credential: ${cause}.`,
113
+ next: [{ type: "text", text: fix }]
114
+ })
115
+ },
116
+ tokenRequestFailed: {
117
+ exitCode: 1,
118
+ description: "With --credential github-actions, the GitHub Actions OIDC token request failed.",
119
+ schema: z.object(tokenRequestFailureShape),
120
+ render: ({ detail, fix }) => ({
121
+ message: `Couldn't request a GitHub Actions OIDC token: ${detail}.`,
122
+ next: [{ type: "text", text: fix }]
123
+ })
124
+ },
125
+ credentialRejected: {
126
+ exitCode: 1,
127
+ description: "The Testserver control plane refused the credential, minted or a GitHub Actions OIDC token (a redirect to SSO, a 401 or a 403).",
128
+ schema: z.object({
129
+ ...rejectedCredentialShape,
130
+ status: z.number(),
131
+ vercelId: z.string().nullable()
132
+ }),
133
+ render: ({ origin, fix, status, vercelId }) => ({
134
+ message: `The Testserver control plane rejected ${rejectedCredentialText(origin)} (HTTP ${status}, x-vercel-id ${vercelId ?? "unknown"}).`,
135
+ rows: credentialOriginRows(origin),
136
+ next: [{ type: "text", text: fix }]
137
+ })
138
+ },
139
+ testserverUnreachable: {
140
+ exitCode: 1,
141
+ description: "No create reached the Testserver control plane, after every attempt.",
142
+ schema: z.object({ attempts: z.number(), code: z.string() }),
143
+ render: ({ attempts, code }) => ({
144
+ message: `Couldn't reach the Testserver control plane after ${attempts} attempts (${code}).`,
145
+ next: [{ type: "text", text: "Check your network connection and any VPN or proxy, then retry." }]
146
+ })
147
+ },
148
+ poolWriteFailed: {
149
+ exitCode: 1,
150
+ description: "Created the Testserver sandbox, but couldn't add it to the pool.",
151
+ schema: sandboxSchema.extend({
152
+ /** The pool file or directory the write failed on. */
153
+ path: z.string(),
154
+ /** What the OS said. */
155
+ message: z.string()
156
+ }),
157
+ render: (failure) => ({
158
+ message: `Created the Testserver sandbox ${failure.testserverUrl}, but couldn't add it to the pool: ${failure.message}`,
159
+ rows: [
160
+ ["Testserver URL", failure.testserverUrl],
161
+ ["Log viewer URL", failure.logViewerUrl],
162
+ ["Requested build", failure.requestedBuild],
163
+ ["Resolved build", failure.resolvedBuild],
164
+ ["Pool path", failure.path]
47
165
  ],
48
- handler: async ({ app, ui }, request) => {
49
- const { credential: source, scope, project } = request;
50
- const file = await loadSettingsFile(app.context.config, app.cwd, app.resourceConfigs);
51
- if (!file.ok)
52
- return file.refusal;
53
- const configFile = file.path;
54
- const settings = fileSettings(file, { scope, project });
55
- const pool = mergeResourceOptions(testserverPoolConfig, fileOptions(file, testserverPoolConfig), {
56
- ...(request.build === undefined ? {} : { build: settledBuild(request.build) }),
57
- ...(request.ttl === undefined ? {} : { ttl: request.ttl }),
58
- });
59
- if (!pool.ok)
60
- return invalidResourceOptions(configFile, pool.problems);
61
- const chosen = settleCredential(source, settings.scope, settings.project);
62
- if (!chosen.ok)
63
- return chosen.refusal;
64
- const requestedBuild = pool.value.build.value;
65
- const ttlMinutes = parseTtl(pool.value.ttl.value) ?? DEFAULT_TTL_MINUTES;
66
- if (configFile !== null)
67
- await ui.progress?.(`Config: ${configFile}`);
68
- const { context } = app;
69
- const prepared = await context.pool.prepare();
70
- if (!prepared.ok)
71
- return outcomes.poolUnavailable({ ...prepared.failure });
72
- const credential = credentialSourceFor(chosen.credential, context, {
73
- onMint: async (target) => {
74
- await ui.progress?.(`Minting a credential for ${mintTargetText(target)}…`);
75
- },
76
- onRequest: async () => {
77
- await ui.progress?.(REQUESTING_TOKEN_LINE);
78
- },
79
- });
80
- let resolution;
81
- const reference = parseBuildReference(requestedBuild);
82
- if (reference.ok && reference.reference.kind === "pull-request") {
83
- const resolved = await resolvePrBuild(context, { pr: reference.reference.number });
84
- if (!resolved.ok) {
85
- const { key, data } = prBuildRefusalFacts(resolved.refusal);
86
- // Each key names one of the four errors, with that error's data.
87
- switch (key) {
88
- case "pullRequestUnreadable":
89
- return outcomes.pullRequestUnreadable(data);
90
- case "prBuildsUnlisted":
91
- return outcomes.prBuildsUnlisted(data);
92
- case "prBuildNotReady":
93
- return outcomes.prBuildNotReady(data);
94
- case "prBuildExpired":
95
- return outcomes.prBuildExpired(data);
96
- }
97
- }
98
- resolution = resolved.resolution;
99
- await ui.progress?.(`${TESTSERVER_REPO}#${resolution.pr} at ${shortSha(resolution.headSha)} → build ${resolution.build}${resolution.from === "pool" ? " (from the pool)" : ""}`);
100
- }
101
- const build = resolution?.build ?? requestedBuild;
102
- const got = await credential.get();
103
- if (!got.ok)
104
- return credentialFailed(got.failure);
105
- await ui.progress?.(`Creating a Testserver sandbox on ${build}…`);
106
- const progress = ui.progress;
107
- const created = await createPooledSandbox(app.context, {
108
- credential: got.credential,
109
- build,
110
- requestedBuild,
111
- ...(resolution === undefined ? {} : { headSha: resolution.headSha }),
112
- ttlMinutes,
113
- lease: false,
114
- }, progress === undefined ? {} : { onProgress: (message) => progress(` ${message}`) });
115
- const pullRequest = resolution === undefined ? null : { repo: TESTSERVER_REPO, number: resolution.pr, headSha: resolution.headSha };
116
- switch (created.kind) {
117
- case "pooled":
118
- return outcomes.created({ ...sandboxFacts(created.entry), pullRequest, configFile });
119
- case "pool-write-failed":
120
- return outcomes.poolWriteFailed({
121
- ...sandboxFacts(created.entry),
122
- pullRequest,
123
- configFile,
124
- path: created.failure.path,
125
- message: created.failure.message,
126
- });
127
- case "create-failed":
128
- return createFailure(created.failure, credential.origin(got.credential), build, ttlMinutes);
129
- }
166
+ next: [
167
+ {
168
+ type: "text",
169
+ text: `The Testserver sandbox exists and expires within ${describeTtl(failure.ttlMinutes)}; nothing can delete it. Fix the pool path, then retry.`
170
+ }
171
+ ]
172
+ })
173
+ },
174
+ createFailed: {
175
+ exitCode: 1,
176
+ description: "The create reached the Testserver control plane and failed.",
177
+ schema: z.object({
178
+ kind: z.enum(createFailedKinds),
179
+ build: z.string(),
180
+ /** The TTL the create asked for, in minutes: how long a Testserver sandbox it may have left lives. */
181
+ ttlMinutes: z.number(),
182
+ /** What the Testserver control plane or the connection said; null for stream-ended and timeout. */
183
+ detail: z.string().nullable(),
184
+ /** The HTTP status of an http-error; otherwise null. */
185
+ status: z.number().nullable(),
186
+ /** How long a timed-out create ran; otherwise null. */
187
+ seconds: z.number().nullable(),
188
+ /** Whether a Testserver sandbox may exist despite the failure: all kinds but http-error. */
189
+ sandboxMayExist: z.boolean()
190
+ }),
191
+ render: (failure) => ({
192
+ message: createFailedMessage(failure),
193
+ ...failure.sandboxMayExist ? { next: [{ type: "text", text: sandboxMayExist(failure.ttlMinutes) }] } : {}
194
+ })
195
+ }
196
+ }
197
+ });
198
+ function createFailedMessage(failure) {
199
+ const said = failure.detail ?? "";
200
+ switch (failure.kind) {
201
+ case "http-error":
202
+ return `The Testserver control plane failed the create with HTTP ${failure.status ?? "an error"}: ${said}`;
203
+ case "stream-error":
204
+ return `The Testserver control plane couldn't create a Testserver sandbox on ${failure.build}: ${said}`;
205
+ case "stream-ended":
206
+ return "The create stream ended without a Testserver sandbox config.";
207
+ case "dropped":
208
+ return `The connection to the Testserver control plane dropped during the create: ${said}`;
209
+ case "timeout":
210
+ return failure.seconds === null ? "The create didn't finish in time." : `The create didn't finish within ${failure.seconds}s.`;
211
+ case "malformed":
212
+ return `The Testserver control plane's sandbox config is malformed: ${said}`;
213
+ }
214
+ }
215
+
216
+ // src/resources/testserver/commands/create/command.ts
217
+ function command(cmd) {
218
+ return cmd.define({
219
+ requestSchema: z2.object({
220
+ scope: z2.string().min(1).optional(),
221
+ project: z2.string().min(1).optional(),
222
+ build: z2.string().min(1).optional(),
223
+ ttl: z2.string().optional(),
224
+ credential: z2.enum(["mint", "github-actions"]).default("mint")
225
+ }),
226
+ cli: { options: { project: { short: "p" } } },
227
+ response: outcomes,
228
+ validate: ({ ttl, build }) => [
229
+ ...ttl !== void 0 && parseTtl(ttl) === void 0 ? [{ field: "ttl", message: invalidTtlMessage(ttl) }] : [],
230
+ ...build === void 0 ? [] : buildProblems(build)
231
+ ],
232
+ handler: async ({ app, ui }, request) => {
233
+ const { credential: source, scope, project } = request;
234
+ const file = await loadSettingsFile(app.context.config, app.cwd, app.resourceConfigs);
235
+ if (!file.ok) return file.refusal;
236
+ const configFile = file.path;
237
+ const settings = fileSettings(file, { scope, project });
238
+ const pool = mergeResourceOptions(testserverPoolConfig, fileOptions(file, testserverPoolConfig), {
239
+ ...request.build === void 0 ? {} : { build: settledBuild(request.build) },
240
+ ...request.ttl === void 0 ? {} : { ttl: request.ttl }
241
+ });
242
+ if (!pool.ok) return invalidResourceOptions(configFile, pool.problems);
243
+ const chosen = settleCredential(source, settings.scope, settings.project);
244
+ if (!chosen.ok) return chosen.refusal;
245
+ const requestedBuild = pool.value.build.value;
246
+ const ttlMinutes = parseTtl(pool.value.ttl.value) ?? DEFAULT_TTL_MINUTES;
247
+ if (configFile !== null) await ui.progress?.(`Config: ${configFile}`);
248
+ const { context } = app;
249
+ const prepared = await context.pool.prepare();
250
+ if (!prepared.ok) return outcomes.poolUnavailable({ ...prepared.failure });
251
+ const credential = credentialSourceFor(chosen.credential, context, {
252
+ onMint: async (target) => {
253
+ await ui.progress?.(`Minting a credential for ${mintTargetText(target)}\u2026`);
254
+ },
255
+ onRequest: async () => {
256
+ await ui.progress?.(REQUESTING_TOKEN_LINE);
257
+ }
258
+ });
259
+ let resolution;
260
+ const reference = parseBuildReference(requestedBuild);
261
+ if (reference.ok && reference.reference.kind === "pull-request") {
262
+ const resolved = await resolvePrBuild(context, { pr: reference.reference.number });
263
+ if (!resolved.ok) {
264
+ const { key, data } = prBuildRefusalFacts(resolved.refusal);
265
+ switch (key) {
266
+ case "pullRequestUnreadable":
267
+ return outcomes.pullRequestUnreadable(data);
268
+ case "prBuildsUnlisted":
269
+ return outcomes.prBuildsUnlisted(data);
270
+ case "prBuildNotReady":
271
+ return outcomes.prBuildNotReady(data);
272
+ case "prBuildExpired":
273
+ return outcomes.prBuildExpired(data);
274
+ }
275
+ }
276
+ resolution = resolved.resolution;
277
+ await ui.progress?.(
278
+ `${TESTSERVER_REPO}#${resolution.pr} at ${shortSha(resolution.headSha)} \u2192 build ${resolution.build}${resolution.from === "pool" ? " (from the pool)" : ""}`
279
+ );
280
+ }
281
+ const build = resolution?.build ?? requestedBuild;
282
+ const got = await credential.get();
283
+ if (!got.ok) return credentialFailed(got.failure);
284
+ await ui.progress?.(`Creating a Testserver sandbox on ${build}\u2026`);
285
+ const progress = ui.progress;
286
+ const created = await createPooledSandbox(
287
+ app.context,
288
+ {
289
+ credential: got.credential,
290
+ build,
291
+ requestedBuild,
292
+ ...resolution === void 0 ? {} : { headSha: resolution.headSha },
293
+ ttlMinutes,
294
+ lease: false
130
295
  },
131
- });
296
+ progress === void 0 ? {} : { onProgress: (message) => progress(` ${message}`) }
297
+ );
298
+ const pullRequest = resolution === void 0 ? null : { repo: TESTSERVER_REPO, number: resolution.pr, headSha: resolution.headSha };
299
+ switch (created.kind) {
300
+ case "pooled":
301
+ return outcomes.created({ ...sandboxFacts(created.entry), pullRequest, configFile });
302
+ case "pool-write-failed":
303
+ return outcomes.poolWriteFailed({
304
+ ...sandboxFacts(created.entry),
305
+ pullRequest,
306
+ configFile,
307
+ path: created.failure.path,
308
+ message: created.failure.message
309
+ });
310
+ case "create-failed":
311
+ return createFailure(created.failure, credential.origin(got.credential), build, ttlMinutes);
312
+ }
313
+ }
314
+ });
132
315
  }
133
- /** The `--build` problems the Testserver pool's `build` rule finds, as `validate` reports them. */
134
316
  function buildProblems(build) {
135
- const checked = testserverPoolConfig.options.build.check(build);
136
- return checked.ok ? [] : checked.problems.map(({ problem }) => ({ field: "build", message: `--build ${problem}.` }));
317
+ const checked = testserverPoolConfig.options.build.check(build);
318
+ return checked.ok ? [] : checked.problems.map(({ problem }) => ({ field: "build", message: `--build ${problem}.` }));
137
319
  }
138
- /** `--build` as the Testserver pool's `build` rule settles it: a PR build reference as `pr:<N>`. */
139
320
  function settledBuild(build) {
140
- const checked = testserverPoolConfig.options.build.check(build);
141
- // `validate` refused every `--build` the rule refuses.
142
- if (!checked.ok)
143
- throw new Error(`--build ${build} should have been refused`);
144
- return checked.value;
321
+ const checked = testserverPoolConfig.options.build.check(build);
322
+ if (!checked.ok) throw new Error(`--build ${build} should have been refused`);
323
+ return checked.value;
145
324
  }
146
- /** The outcome of a credential source that couldn't give a credential. */
147
325
  function credentialFailed(failure) {
148
- return failure.source === "mint"
149
- ? outcomes.mintFailed({ ...failure.target, ...describeMintFailure(failure.failure, failure.target) })
150
- : outcomes.tokenRequestFailed(tokenRequestFailureFacts(failure.failure));
326
+ return failure.source === "mint" ? outcomes.mintFailed({ ...failure.target, ...describeMintFailure(failure.failure, failure.target) }) : outcomes.tokenRequestFailed(tokenRequestFailureFacts(failure.failure));
151
327
  }
152
328
  function sandboxFacts(entry) {
153
- const { testserverUrl, logViewerUrl, requestedBuild, resolvedBuild, ttlMinutes } = entry;
154
- return { testserverUrl, logViewerUrl, requestedBuild, resolvedBuild, ttlMinutes };
329
+ const { testserverUrl, logViewerUrl, requestedBuild, resolvedBuild, ttlMinutes } = entry;
330
+ return { testserverUrl, logViewerUrl, requestedBuild, resolvedBuild, ttlMinutes };
155
331
  }
156
332
  function createFailure(failure, origin, build, ttlMinutes) {
157
- const facts = {
158
- build,
159
- ttlMinutes,
160
- detail: null,
161
- status: null,
162
- seconds: null,
163
- sandboxMayExist: mayLeaveSandbox(failure),
164
- };
165
- switch (failure.kind) {
166
- case "connect-failed":
167
- return outcomes.testserverUnreachable({ attempts: CREATE_ATTEMPTS, code: failure.code });
168
- case "rejected":
169
- return outcomes.credentialRejected({
170
- ...rejectedCredentialFacts(origin),
171
- status: failure.status,
172
- vercelId: failure.vercelId ?? null,
173
- });
174
- case "http-error":
175
- return outcomes.createFailed({
176
- ...facts,
177
- kind: failure.kind,
178
- detail: failure.body,
179
- status: failure.status,
180
- });
181
- case "stream-error":
182
- return outcomes.createFailed({
183
- ...facts,
184
- kind: failure.kind,
185
- detail: failure.error,
186
- });
187
- case "stream-ended":
188
- return outcomes.createFailed({ ...facts, kind: failure.kind });
189
- case "dropped":
190
- return outcomes.createFailed({
191
- ...facts,
192
- kind: failure.kind,
193
- detail: failure.message,
194
- });
195
- case "timeout":
196
- return outcomes.createFailed({
197
- ...facts,
198
- kind: failure.kind,
199
- seconds: failure.seconds,
200
- });
201
- case "malformed":
202
- return outcomes.createFailed({
203
- ...facts,
204
- kind: failure.kind,
205
- detail: failure.detail,
206
- });
207
- }
333
+ const facts = {
334
+ build,
335
+ ttlMinutes,
336
+ detail: null,
337
+ status: null,
338
+ seconds: null,
339
+ sandboxMayExist: mayLeaveSandbox(failure)
340
+ };
341
+ switch (failure.kind) {
342
+ case "connect-failed":
343
+ return outcomes.testserverUnreachable({ attempts: CREATE_ATTEMPTS, code: failure.code });
344
+ case "rejected":
345
+ return outcomes.credentialRejected({
346
+ ...rejectedCredentialFacts(origin),
347
+ status: failure.status,
348
+ vercelId: failure.vercelId ?? null
349
+ });
350
+ case "http-error":
351
+ return outcomes.createFailed({
352
+ ...facts,
353
+ kind: failure.kind,
354
+ detail: failure.body,
355
+ status: failure.status
356
+ });
357
+ case "stream-error":
358
+ return outcomes.createFailed({
359
+ ...facts,
360
+ kind: failure.kind,
361
+ detail: failure.error
362
+ });
363
+ case "stream-ended":
364
+ return outcomes.createFailed({ ...facts, kind: failure.kind });
365
+ case "dropped":
366
+ return outcomes.createFailed({
367
+ ...facts,
368
+ kind: failure.kind,
369
+ detail: failure.message
370
+ });
371
+ case "timeout":
372
+ return outcomes.createFailed({
373
+ ...facts,
374
+ kind: failure.kind,
375
+ seconds: failure.seconds
376
+ });
377
+ case "malformed":
378
+ return outcomes.createFailed({
379
+ ...facts,
380
+ kind: failure.kind,
381
+ detail: failure.detail
382
+ });
383
+ }
208
384
  }
385
+ export {
386
+ command
387
+ };