@testsprite/testsprite-cli 0.16.0 → 0.17.0
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.
- package/README.md +51 -147
- package/dist/commands/agent.d.ts +12 -4
- package/dist/commands/agent.js +48 -63
- package/dist/commands/auth.d.ts +6 -10
- package/dist/commands/auth.js +38 -54
- package/dist/commands/ci.d.ts +23 -100
- package/dist/commands/ci.js +293 -487
- package/dist/commands/conversation.d.ts +27 -0
- package/dist/commands/conversation.js +702 -0
- package/dist/commands/doctor.d.ts +5 -1
- package/dist/commands/doctor.js +129 -34
- package/dist/commands/init.d.ts +4 -45
- package/dist/commands/init.js +108 -66
- package/dist/commands/project-env.d.ts +62 -2
- package/dist/commands/project-env.js +388 -37
- package/dist/commands/project-sign-in.d.ts +0 -48
- package/dist/commands/project-sign-in.js +1 -229
- package/dist/commands/project.d.ts +15 -186
- package/dist/commands/project.js +89 -924
- package/dist/commands/result.d.ts +10 -0
- package/dist/commands/result.js +215 -0
- package/dist/commands/schedule.d.ts +0 -29
- package/dist/commands/schedule.js +28 -87
- package/dist/commands/test.d.ts +301 -1816
- package/dist/commands/test.js +1951 -13902
- package/dist/commands/testlist.d.ts +128 -5
- package/dist/commands/testlist.js +269 -339
- package/dist/commands/tunnel.d.ts +2 -2
- package/dist/commands/tunnel.js +23 -19
- package/dist/commands/usage.d.ts +12 -45
- package/dist/commands/usage.js +50 -131
- package/dist/index.js +182 -104
- package/dist/lib/agent-detect.js +9 -5
- package/dist/lib/agent-targets.d.ts +21 -2
- package/dist/lib/agent-targets.js +88 -11
- package/dist/lib/args.d.ts +13 -0
- package/dist/lib/args.js +49 -0
- package/dist/lib/basic-auth.d.ts +3 -0
- package/dist/lib/basic-auth.js +15 -0
- package/dist/lib/billing-refusal.js +4 -3
- package/dist/lib/ci-linkage.d.ts +4 -82
- package/dist/lib/ci-linkage.js +4 -157
- package/dist/lib/ci-run.js +1 -1
- package/dist/lib/client-factory.d.ts +10 -2
- package/dist/lib/client-factory.js +38 -26
- package/dist/lib/commit-signal.d.ts +65 -0
- package/dist/lib/commit-signal.js +305 -0
- package/dist/lib/concurrency-limiter.d.ts +6 -0
- package/dist/lib/concurrency-limiter.js +29 -0
- package/dist/lib/config.js +10 -5
- package/dist/lib/conflict-reason.d.ts +9 -3
- package/dist/lib/conflict-reason.js +6 -3
- package/dist/lib/conversation/answers.d.ts +9 -0
- package/dist/lib/conversation/answers.js +49 -0
- package/dist/lib/conversation/api.d.ts +71 -0
- package/dist/lib/conversation/api.js +150 -0
- package/dist/lib/conversation/change.d.ts +31 -0
- package/dist/lib/conversation/change.js +124 -0
- package/dist/lib/conversation/context.d.ts +19 -0
- package/dist/lib/conversation/context.js +70 -0
- package/dist/lib/conversation/environment.d.ts +15 -0
- package/dist/lib/conversation/environment.js +33 -0
- package/dist/lib/conversation/follow.d.ts +24 -0
- package/dist/lib/conversation/follow.js +42 -0
- package/dist/lib/conversation/render.d.ts +33 -0
- package/dist/lib/conversation/render.js +315 -0
- package/dist/lib/conversation/title.d.ts +2 -0
- package/dist/lib/conversation/title.js +18 -0
- package/dist/lib/conversation/types.d.ts +156 -0
- package/dist/lib/conversation/types.js +6 -0
- package/dist/lib/credentials.js +67 -11
- package/dist/lib/cron.d.ts +1 -1
- package/dist/lib/cron.js +2 -2
- package/dist/lib/dispatch-stop.d.ts +20 -0
- package/dist/lib/dispatch-stop.js +30 -0
- package/dist/lib/download.d.ts +71 -0
- package/dist/lib/download.js +274 -0
- package/dist/lib/dry-run/fetch.d.ts +10 -0
- package/dist/lib/dry-run/fetch.js +20 -0
- package/dist/lib/dry-run/samples.d.ts +6 -34
- package/dist/lib/dry-run/samples.js +375 -747
- package/dist/lib/environment-tunnel.d.ts +16 -27
- package/dist/lib/environment-tunnel.js +161 -154
- package/dist/lib/errors.d.ts +20 -4
- package/dist/lib/errors.js +36 -16
- package/dist/lib/facade.d.ts +7 -29
- package/dist/lib/facade.js +7 -36
- package/dist/lib/failing-fe-resolver.d.ts +13 -58
- package/dist/lib/failing-fe-resolver.js +47 -132
- package/dist/lib/first-suite-hint.d.ts +16 -22
- package/dist/lib/first-suite-hint.js +38 -47
- package/dist/lib/gh-output.d.ts +76 -21
- package/dist/lib/gh-output.js +171 -90
- package/dist/lib/git-utils.d.ts +56 -0
- package/dist/lib/git-utils.js +212 -0
- package/dist/lib/github-context.d.ts +2 -0
- package/dist/lib/github-context.js +40 -8
- package/dist/lib/history-retention.d.ts +6 -2
- package/dist/lib/history-retention.js +10 -7
- package/dist/lib/http.d.ts +58 -134
- package/dist/lib/http.js +352 -289
- package/dist/lib/interrupt.d.ts +14 -8
- package/dist/lib/interrupt.js +28 -10
- package/dist/lib/junit-report.d.ts +57 -4
- package/dist/lib/junit-report.js +202 -32
- package/dist/lib/local-target.d.ts +3 -3
- package/dist/lib/local-target.js +3 -3
- package/dist/lib/output.d.ts +1 -40
- package/dist/lib/output.js +11 -49
- package/dist/lib/pagination.d.ts +2 -5
- package/dist/lib/pagination.js +2 -5
- package/dist/lib/presigned-url.d.ts +22 -0
- package/dist/lib/presigned-url.js +90 -0
- package/dist/lib/prompt.js +36 -2
- package/dist/lib/proxy.js +2 -1
- package/dist/lib/redact.d.ts +2 -2
- package/dist/lib/redact.js +3 -4
- package/dist/lib/removed-commands.d.ts +18 -0
- package/dist/lib/removed-commands.js +154 -0
- package/dist/lib/render-error.d.ts +31 -6
- package/dist/lib/render-error.js +88 -25
- package/dist/lib/response-schemas.d.ts +38 -30
- package/dist/lib/response-schemas.js +207 -207
- package/dist/lib/result/api.d.ts +24 -0
- package/dist/lib/result/api.js +28 -0
- package/dist/lib/result/evidence.d.ts +71 -0
- package/dist/lib/result/evidence.js +420 -0
- package/dist/lib/result/render.d.ts +20 -0
- package/dist/lib/result/render.js +194 -0
- package/dist/lib/result/types.d.ts +122 -0
- package/dist/lib/result/types.js +19 -0
- package/dist/lib/run-output.d.ts +275 -0
- package/dist/lib/run-output.js +227 -0
- package/dist/lib/run-render.d.ts +101 -0
- package/dist/lib/run-render.js +183 -0
- package/dist/lib/run-wait.d.ts +192 -0
- package/dist/lib/run-wait.js +452 -0
- package/dist/lib/runs.types.d.ts +118 -367
- package/dist/lib/runs.types.js +0 -14
- package/dist/lib/secret-file.d.ts +1 -1
- package/dist/lib/secret-file.js +11 -2
- package/dist/lib/skill-nudge.d.ts +7 -27
- package/dist/lib/skill-nudge.js +9 -37
- package/dist/lib/target-path.d.ts +15 -0
- package/dist/lib/target-path.js +35 -0
- package/dist/lib/target-url-advisory.d.ts +19 -0
- package/dist/lib/target-url-advisory.js +31 -0
- package/dist/lib/target-url-preflight.d.ts +9 -0
- package/dist/lib/target-url-preflight.js +214 -106
- package/dist/lib/target-url.d.ts +1 -1
- package/dist/lib/target-url.js +6 -16
- package/dist/lib/telemetry.d.ts +31 -11
- package/dist/lib/telemetry.js +37 -8
- package/dist/lib/terminal-text.d.ts +41 -0
- package/dist/lib/terminal-text.js +127 -0
- package/dist/lib/testlist.types.d.ts +16 -8
- package/dist/lib/testlist.types.js +1 -1
- package/dist/lib/text-equality.d.ts +2 -0
- package/dist/lib/text-equality.js +5 -0
- package/dist/lib/text-file.d.ts +16 -0
- package/dist/lib/text-file.js +41 -0
- package/dist/lib/text-table.d.ts +46 -0
- package/dist/lib/text-table.js +72 -4
- package/dist/lib/ticker.d.ts +9 -2
- package/dist/lib/ticker.js +16 -3
- package/dist/lib/transport-failure.d.ts +22 -0
- package/dist/lib/transport-failure.js +126 -0
- package/dist/lib/tunnel-session.d.ts +7 -1
- package/dist/lib/tunnel-session.js +22 -9
- package/dist/lib/update-check.js +3 -2
- package/dist/lib/url-template.d.ts +43 -0
- package/dist/lib/url-template.js +151 -0
- package/dist/lib/validate.d.ts +0 -64
- package/dist/lib/validate.js +2 -131
- package/dist/lib/version-notice.js +4 -3
- package/dist/lib/wait-exit.d.ts +37 -2
- package/dist/lib/wait-exit.js +88 -30
- package/dist/lib/workspace-status.js +10 -2
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -3
- package/skills/testsprite-ci.skill.md +48 -18
- package/skills/testsprite-onboard.skill.md +66 -151
- package/skills/testsprite-verify.codex.md +50 -52
- package/skills/testsprite-verify.skill.md +184 -351
- package/dist/lib/browser.d.ts +0 -6
- package/dist/lib/browser.js +0 -54
- package/dist/lib/bundle.d.ts +0 -234
- package/dist/lib/bundle.js +0 -781
- package/dist/lib/deprecate.d.ts +0 -32
- package/dist/lib/deprecate.js +0 -39
- package/dist/lib/flaky.d.ts +0 -91
- package/dist/lib/flaky.js +0 -71
- package/dist/lib/plan-poll.d.ts +0 -171
- package/dist/lib/plan-poll.js +0 -441
- package/dist/lib/plans.types.d.ts +0 -130
- package/dist/lib/plans.types.js +0 -31
- package/dist/lib/poll.d.ts +0 -100
- package/dist/lib/poll.js +0 -290
- package/dist/lib/rate-throttle.d.ts +0 -49
- package/dist/lib/rate-throttle.js +0 -75
- package/dist/lib/run-progress.d.ts +0 -41
- package/dist/lib/run-progress.js +0 -56
- package/dist/lib/v3-advisory.d.ts +0 -55
- package/dist/lib/v3-advisory.js +0 -74
- package/schemas/plan.schema.json +0 -61
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A result's evidence on disk, as `result get --out <dir>` writes it:
|
|
3
|
+
*
|
|
4
|
+
* result.json the result read, each signed link replaced by
|
|
5
|
+
* the file it was written to, or null
|
|
6
|
+
* video.<ext> the run's recording
|
|
7
|
+
* steps/<NNN>.png each step's screenshot; NNN is its position in steps[], from 001
|
|
8
|
+
* steps/<NNN>.html each step's page
|
|
9
|
+
* sign-in/video.<ext> for a result blocked at sign-in: the sign-in run's recording,
|
|
10
|
+
* sign-in/steps/<NNN>.png and each of its steps' files, by position in signIn.steps[]
|
|
11
|
+
* sign-in/steps/<NNN>.html
|
|
12
|
+
*
|
|
13
|
+
* File names come from positions and the known video containers, never from a
|
|
14
|
+
* server value. The links are signed and expire (the video's after 15
|
|
15
|
+
* minutes), so the download runs right after the read, the videos first.
|
|
16
|
+
*/
|
|
17
|
+
import type { FetchImpl } from '../http.js';
|
|
18
|
+
import type { ResultDetail } from './types.js';
|
|
19
|
+
/** Written last: a directory without it holds a download that did not finish. */
|
|
20
|
+
export declare const RESULT_FILE = "result.json";
|
|
21
|
+
/** One file to download: where it goes, relative to the directory, and its signed link. */
|
|
22
|
+
export interface EvidenceFile {
|
|
23
|
+
file: string;
|
|
24
|
+
url: string;
|
|
25
|
+
}
|
|
26
|
+
export interface WrittenEvidence {
|
|
27
|
+
dir: string;
|
|
28
|
+
/** Relative to `dir`, in the order planned; `result.json` last. */
|
|
29
|
+
written: string[];
|
|
30
|
+
/**
|
|
31
|
+
* The files that could not be downloaded, and why. `kept` when `dir` held
|
|
32
|
+
* an earlier download of the same result with that file: its copy stays,
|
|
33
|
+
* and result.json names it.
|
|
34
|
+
*/
|
|
35
|
+
failed: Array<{
|
|
36
|
+
file: string;
|
|
37
|
+
reason: string;
|
|
38
|
+
kept?: true;
|
|
39
|
+
}>;
|
|
40
|
+
}
|
|
41
|
+
/** Whether any step of the run or of its sign-in failed. */
|
|
42
|
+
export declare function hasFailedStep(result: ResultDetail): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* What a download of `result` fetches, in order. Pure: nothing is fetched or
|
|
45
|
+
* written. `failedOnly` keeps the failed steps and their neighbours; with no
|
|
46
|
+
* failed step at all it keeps every step, as `--failed-only` always has.
|
|
47
|
+
*/
|
|
48
|
+
export declare function planEvidence(result: ResultDetail, failedOnly?: boolean): EvidenceFile[];
|
|
49
|
+
/**
|
|
50
|
+
* Download a result's evidence into `dir` and write its `result.json`.
|
|
51
|
+
*
|
|
52
|
+
* `dir` is created with its parents. The files download, at most four at a
|
|
53
|
+
* time, into a staging directory inside `dir`; only once every download has
|
|
54
|
+
* ended does the new set replace what an earlier download wrote (`result.json`,
|
|
55
|
+
* `video.<mp4|webm|mov|mkv|avi>`, `steps/`, `sign-in/`), and every other file
|
|
56
|
+
* in `dir` is left alone. A file that cannot be downloaded is listed in
|
|
57
|
+
* `failed` and the rest are still written.
|
|
58
|
+
*
|
|
59
|
+
* An interrupt before the swap starts, an error before it, or a swap that
|
|
60
|
+
* fails part-way leaves the earlier download as it was, and the staging
|
|
61
|
+
* directory is removed. Only when the earlier files cannot all be put back is
|
|
62
|
+
* the staging directory kept, and the error says where they are.
|
|
63
|
+
*/
|
|
64
|
+
export declare function writeEvidence(result: ResultDetail, opts: {
|
|
65
|
+
dir: string;
|
|
66
|
+
warn?: (message: string) => void;
|
|
67
|
+
failedOnly?: boolean;
|
|
68
|
+
fetchImpl?: FetchImpl;
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
sleep?: (ms: number) => Promise<void>;
|
|
71
|
+
}): Promise<WrittenEvidence>;
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A result's evidence on disk, as `result get --out <dir>` writes it:
|
|
3
|
+
*
|
|
4
|
+
* result.json the result read, each signed link replaced by
|
|
5
|
+
* the file it was written to, or null
|
|
6
|
+
* video.<ext> the run's recording
|
|
7
|
+
* steps/<NNN>.png each step's screenshot; NNN is its position in steps[], from 001
|
|
8
|
+
* steps/<NNN>.html each step's page
|
|
9
|
+
* sign-in/video.<ext> for a result blocked at sign-in: the sign-in run's recording,
|
|
10
|
+
* sign-in/steps/<NNN>.png and each of its steps' files, by position in signIn.steps[]
|
|
11
|
+
* sign-in/steps/<NNN>.html
|
|
12
|
+
*
|
|
13
|
+
* File names come from positions and the known video containers, never from a
|
|
14
|
+
* server value. The links are signed and expire (the video's after 15
|
|
15
|
+
* minutes), so the download runs right after the read, the videos first.
|
|
16
|
+
*/
|
|
17
|
+
import { copyFile, lstat, mkdir, mkdtemp, readFile, readdir, realpath, rename, rm, rmdir, writeFile, } from 'node:fs/promises';
|
|
18
|
+
import { basename, dirname, join, relative, sep } from 'node:path';
|
|
19
|
+
import { concurrencyLimiter } from '../concurrency-limiter.js';
|
|
20
|
+
import { assertNoEscape, KNOWN_VIDEO_EXTENSIONS, pickVideoExtension, streamUrlToFile, } from '../download.js';
|
|
21
|
+
import { ApiError, CLIError, InterruptError } from '../errors.js';
|
|
22
|
+
import { ensureStateGitignore, findGitRoot } from '../git-utils.js';
|
|
23
|
+
import { globalShutdown } from '../interrupt.js';
|
|
24
|
+
/** How many steps on each side of a failed one `--failed-only` keeps. */
|
|
25
|
+
const FAILED_ONLY_RADIUS = 1;
|
|
26
|
+
/** Written last: a directory without it holds a download that did not finish. */
|
|
27
|
+
export const RESULT_FILE = 'result.json';
|
|
28
|
+
/** How many files download at once. */
|
|
29
|
+
const PARALLEL_DOWNLOADS = 4;
|
|
30
|
+
/** The download is staged in a directory of this name inside `dir`, then swapped in. */
|
|
31
|
+
const STAGING_PREFIX = '.testsprite-partial-';
|
|
32
|
+
/** Inside the staging directory: where the earlier download waits while the new one moves in. */
|
|
33
|
+
const ASIDE = '.earlier';
|
|
34
|
+
const SIGN_IN = 'sign-in/';
|
|
35
|
+
const videoFile = (url, under = '') => `${under}video.${pickVideoExtension(url)}`;
|
|
36
|
+
const stepFile = (under, position, ext) => `${under}steps/${String(position + 1).padStart(3, '0')}.${ext}`;
|
|
37
|
+
/** Whether any step of the run or of its sign-in failed. */
|
|
38
|
+
export function hasFailedStep(result) {
|
|
39
|
+
return [...result.steps, ...(result.signIn?.steps ?? [])].some(step => step.status === 'failed');
|
|
40
|
+
}
|
|
41
|
+
/** The files of `steps` under `under`; when `narrow`, only those of a failed step and its neighbours. */
|
|
42
|
+
function stepFiles(steps, under, narrow) {
|
|
43
|
+
const failed = steps.flatMap((step, position) => (step.status === 'failed' ? [position] : []));
|
|
44
|
+
const kept = (position) => !narrow || failed.some(at => Math.abs(position - at) <= FAILED_ONLY_RADIUS);
|
|
45
|
+
return steps.flatMap((step, position) => {
|
|
46
|
+
if (!kept(position))
|
|
47
|
+
return [];
|
|
48
|
+
const files = [];
|
|
49
|
+
if (step.screenshotSignedUrl) {
|
|
50
|
+
files.push({ file: stepFile(under, position, 'png'), url: step.screenshotSignedUrl });
|
|
51
|
+
}
|
|
52
|
+
if (step.htmlSignedUrl) {
|
|
53
|
+
files.push({ file: stepFile(under, position, 'html'), url: step.htmlSignedUrl });
|
|
54
|
+
}
|
|
55
|
+
return files;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* What a download of `result` fetches, in order. Pure: nothing is fetched or
|
|
60
|
+
* written. `failedOnly` keeps the failed steps and their neighbours; with no
|
|
61
|
+
* failed step at all it keeps every step, as `--failed-only` always has.
|
|
62
|
+
*/
|
|
63
|
+
export function planEvidence(result, failedOnly = false) {
|
|
64
|
+
const narrow = failedOnly && hasFailedStep(result);
|
|
65
|
+
const files = [];
|
|
66
|
+
if (result.videoUrl)
|
|
67
|
+
files.push({ file: videoFile(result.videoUrl), url: result.videoUrl });
|
|
68
|
+
const signInVideo = result.signIn?.videoUrl;
|
|
69
|
+
if (signInVideo)
|
|
70
|
+
files.push({ file: videoFile(signInVideo, SIGN_IN), url: signInVideo });
|
|
71
|
+
return [
|
|
72
|
+
...files,
|
|
73
|
+
...stepFiles(result.steps, '', narrow),
|
|
74
|
+
...stepFiles(result.signIn?.steps ?? [], SIGN_IN, narrow),
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Download a result's evidence into `dir` and write its `result.json`.
|
|
79
|
+
*
|
|
80
|
+
* `dir` is created with its parents. The files download, at most four at a
|
|
81
|
+
* time, into a staging directory inside `dir`; only once every download has
|
|
82
|
+
* ended does the new set replace what an earlier download wrote (`result.json`,
|
|
83
|
+
* `video.<mp4|webm|mov|mkv|avi>`, `steps/`, `sign-in/`), and every other file
|
|
84
|
+
* in `dir` is left alone. A file that cannot be downloaded is listed in
|
|
85
|
+
* `failed` and the rest are still written.
|
|
86
|
+
*
|
|
87
|
+
* An interrupt before the swap starts, an error before it, or a swap that
|
|
88
|
+
* fails part-way leaves the earlier download as it was, and the staging
|
|
89
|
+
* directory is removed. Only when the earlier files cannot all be put back is
|
|
90
|
+
* the staging directory kept, and the error says where they are.
|
|
91
|
+
*/
|
|
92
|
+
export async function writeEvidence(result, opts) {
|
|
93
|
+
const { dir } = opts;
|
|
94
|
+
const plan = planEvidence(result, opts.failedOnly);
|
|
95
|
+
const deps = {
|
|
96
|
+
fetchImpl: opts.fetchImpl ?? globalThis.fetch.bind(globalThis),
|
|
97
|
+
signal: opts.signal ?? globalShutdown.signal,
|
|
98
|
+
sleep: opts.sleep,
|
|
99
|
+
};
|
|
100
|
+
// Critical work: a first Ctrl-C aborts the downloads and waits for the
|
|
101
|
+
// staging directory to go, rather than exiting with it half written.
|
|
102
|
+
return globalShutdown.runCriticalOperation(async () => {
|
|
103
|
+
if (deps.signal.aborted)
|
|
104
|
+
throw deps.signal.reason;
|
|
105
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- the --out directory the user chose.
|
|
106
|
+
await mkdir(dir, { recursive: true });
|
|
107
|
+
// Use the physical destination so a symlink or a nested checkout cannot
|
|
108
|
+
// make an export appear protected by another directory's ignore rules.
|
|
109
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- the output directory just created.
|
|
110
|
+
const destination = await realpath(dir);
|
|
111
|
+
const repo = findGitRoot(destination);
|
|
112
|
+
for (let at = destination; at !== repo && at !== dirname(at); at = dirname(at)) {
|
|
113
|
+
if (basename(at) === '.testsprite') {
|
|
114
|
+
ensureStateGitignore(at);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (repo) {
|
|
119
|
+
opts.warn?.('Result evidence can contain credentials. Check that Git ignores the exported files before staging changes.');
|
|
120
|
+
}
|
|
121
|
+
// The top-level names this download installs are known from the plan, so
|
|
122
|
+
// a name the filesystem takes for one of them is refused before anything
|
|
123
|
+
// is downloaded; the swap checks again at the end.
|
|
124
|
+
const installs = [...new Set([...plan.map(({ file }) => file.split('/')[0]), RESULT_FILE])];
|
|
125
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- the --out directory the user chose.
|
|
126
|
+
await refuseNamesTheUserHolds(dir, await readdir(dir), installs);
|
|
127
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- a staging directory under --out, its name checked by assertNoEscape.
|
|
128
|
+
const staging = await mkdtemp(assertNoEscape(dir, STAGING_PREFIX));
|
|
129
|
+
let keepStaging = false;
|
|
130
|
+
try {
|
|
131
|
+
const reasons = await downloadAll(plan, staging, deps);
|
|
132
|
+
const written = plan.filter((_, i) => reasons[i] === null).map(({ file }) => file);
|
|
133
|
+
const failed = plan.flatMap(({ file }, i) => {
|
|
134
|
+
const reason = reasons[i];
|
|
135
|
+
return reason === null || reason === undefined ? [] : [{ file, reason }];
|
|
136
|
+
});
|
|
137
|
+
const kept = await keepEarlierCopies(dir, staging, result.resultId, failed.map(({ file }) => file));
|
|
138
|
+
const local = localCopy(result, new Set([...written, ...kept]));
|
|
139
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- result.json in the staging directory, its name checked by assertNoEscape.
|
|
140
|
+
await writeFile(assertNoEscape(staging, RESULT_FILE), `${JSON.stringify(local, null, 2)}\n`);
|
|
141
|
+
// A Ctrl-C up to here leaves the earlier download as it was; once the
|
|
142
|
+
// swap starts, it runs to the end.
|
|
143
|
+
if (deps.signal.aborted)
|
|
144
|
+
throw deps.signal.reason;
|
|
145
|
+
await swapIn(staging, dir);
|
|
146
|
+
return {
|
|
147
|
+
dir,
|
|
148
|
+
written: [...written, RESULT_FILE],
|
|
149
|
+
failed: failed.map(entry => (kept.has(entry.file) ? { ...entry, kept: true } : entry)),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
keepStaging = err instanceof EarlierDownloadKept;
|
|
154
|
+
throw err;
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
if (!keepStaging) {
|
|
158
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- the staging directory this download made under --out.
|
|
159
|
+
await rm(staging, { recursive: true, force: true }).catch(() => undefined);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Whether `path`, below `dir`, is a regular file reached through real
|
|
166
|
+
* directories: each directory between `dir` and it is an lstat'ed directory,
|
|
167
|
+
* not a link, and the file is an lstat'ed regular file. `dir` itself, the
|
|
168
|
+
* --out the user chose, is not checked.
|
|
169
|
+
*/
|
|
170
|
+
async function isRegularFileBelow(dir, path) {
|
|
171
|
+
const names = relative(dir, path).split(sep);
|
|
172
|
+
let at = dir;
|
|
173
|
+
for (const [i, name] of names.entries()) {
|
|
174
|
+
at = join(at, name);
|
|
175
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- a directory or file under the --out directory the user chose, its name checked by assertNoEscape.
|
|
176
|
+
const stats = await lstat(at).catch(() => undefined);
|
|
177
|
+
if (!(i === names.length - 1 ? stats?.isFile() : stats?.isDirectory()))
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Stage the earlier copy of each file that failed, when `dir` holds an earlier
|
|
184
|
+
* download of the same result: a rerun after a link stopped working (the
|
|
185
|
+
* video aged out of storage, a permission change) never costs the copy
|
|
186
|
+
* already made. Another result's files are never mixed in. An earlier copy is
|
|
187
|
+
* staged only when, at the moment it is checked, it is a regular file and each
|
|
188
|
+
* directory between `dir` and it is a real directory, not a symlink. The check
|
|
189
|
+
* and the copy are separate calls, so it is not atomic; and result.json, read
|
|
190
|
+
* only to compare its resultId, is read through a link. Returns the files
|
|
191
|
+
* staged.
|
|
192
|
+
*/
|
|
193
|
+
async function keepEarlierCopies(dir, staging, resultId, files) {
|
|
194
|
+
const kept = new Set();
|
|
195
|
+
if (files.length === 0)
|
|
196
|
+
return kept;
|
|
197
|
+
let earlier;
|
|
198
|
+
try {
|
|
199
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- result.json under the --out directory the user chose, its name checked by assertNoEscape.
|
|
200
|
+
earlier = JSON.parse(await readFile(assertNoEscape(dir, RESULT_FILE), 'utf8'));
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return kept;
|
|
204
|
+
}
|
|
205
|
+
const earlierId = typeof earlier === 'object' && earlier !== null
|
|
206
|
+
? earlier.resultId
|
|
207
|
+
: undefined;
|
|
208
|
+
if (earlierId !== resultId)
|
|
209
|
+
return kept;
|
|
210
|
+
for (const file of files) {
|
|
211
|
+
const from = assertNoEscape(dir, file);
|
|
212
|
+
if (!(await isRegularFileBelow(dir, from)))
|
|
213
|
+
continue;
|
|
214
|
+
const to = assertNoEscape(staging, file);
|
|
215
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- a directory in the staging directory, its name checked by assertNoEscape.
|
|
216
|
+
await mkdir(dirname(to), { recursive: true });
|
|
217
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- from --out into its staging directory, both names checked by assertNoEscape.
|
|
218
|
+
await copyFile(from, to);
|
|
219
|
+
kept.add(file);
|
|
220
|
+
}
|
|
221
|
+
return kept;
|
|
222
|
+
}
|
|
223
|
+
/** The directories below `root` that the planned files go in, parents before children. */
|
|
224
|
+
function directoriesFor(root, plan) {
|
|
225
|
+
const dirs = new Set();
|
|
226
|
+
for (const { file } of plan) {
|
|
227
|
+
for (let at = dirname(assertNoEscape(root, file)); at.length > root.length; at = dirname(at)) {
|
|
228
|
+
dirs.add(at);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return [...dirs].sort((a, b) => a.length - b.length);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Download every planned file into `staging`, at most four at a time, in plan
|
|
235
|
+
* order. Returns, per file, null when it was written or the reason it was not.
|
|
236
|
+
* An interrupt rethrows, but only once every download has ended, so none
|
|
237
|
+
* writes into the staging directory after it is removed.
|
|
238
|
+
*/
|
|
239
|
+
async function downloadAll(plan, staging, deps) {
|
|
240
|
+
const dirs = directoriesFor(staging, plan);
|
|
241
|
+
// The directories first, so each download reaches its fetch at once.
|
|
242
|
+
for (const at of dirs) {
|
|
243
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- a directory in the staging directory, its name checked by assertNoEscape.
|
|
244
|
+
await mkdir(at, { recursive: true });
|
|
245
|
+
}
|
|
246
|
+
const slot = concurrencyLimiter(PARALLEL_DOWNLOADS);
|
|
247
|
+
const outcomes = await Promise.allSettled(plan.map(({ file, url }) => slot(() => downloadOne(url, assertNoEscape(staging, file), deps))));
|
|
248
|
+
const stopped = outcomes.find(outcome => outcome.status === 'rejected');
|
|
249
|
+
if (stopped !== undefined)
|
|
250
|
+
throw stopped.reason;
|
|
251
|
+
// A directory none of whose files came down is not installed. Children go
|
|
252
|
+
// first; removing one that holds a file fails, and it stays.
|
|
253
|
+
for (const at of dirs.reverse()) {
|
|
254
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- a directory in the staging directory, its name checked by assertNoEscape.
|
|
255
|
+
await rmdir(at).catch(() => undefined);
|
|
256
|
+
}
|
|
257
|
+
return outcomes.map(outcome => (outcome.status === 'fulfilled' ? outcome.value : null));
|
|
258
|
+
}
|
|
259
|
+
/** Download one file: null when written, or why it was not. An interrupt rethrows. */
|
|
260
|
+
async function downloadOne(url, path, deps) {
|
|
261
|
+
try {
|
|
262
|
+
await streamUrlToFile(url, path, deps.fetchImpl, {
|
|
263
|
+
sleep: deps.sleep,
|
|
264
|
+
shutdownSignal: deps.signal,
|
|
265
|
+
});
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
catch (err) {
|
|
269
|
+
if (err instanceof InterruptError || deps.signal.aborted)
|
|
270
|
+
throw err;
|
|
271
|
+
// A download that broke off leaves what it got, which is not the file.
|
|
272
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- a file in the staging directory, its name checked by assertNoEscape.
|
|
273
|
+
await rm(path, { force: true }).catch(() => undefined);
|
|
274
|
+
return messageOf(err);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const messageOf = (err) => (err instanceof Error ? err.message : String(err));
|
|
278
|
+
/** What an earlier download wrote in `dir`; every other entry is the user's. */
|
|
279
|
+
function isEvidenceEntry(entry) {
|
|
280
|
+
if (entry === RESULT_FILE || entry === 'steps' || entry === 'sign-in')
|
|
281
|
+
return true;
|
|
282
|
+
return entry.startsWith('video.') && KNOWN_VIDEO_EXTENSIONS.has(entry.slice('video.'.length));
|
|
283
|
+
}
|
|
284
|
+
/** A swap that failed and could not put the whole earlier download back. */
|
|
285
|
+
class EarlierDownloadKept extends CLIError {
|
|
286
|
+
constructor(aside, cause) {
|
|
287
|
+
super(`The new download could not be moved into place (${messageOf(cause)}), and the earlier ` +
|
|
288
|
+
`one could not all be put back: the files missing from the directory are in ${aside}.`);
|
|
289
|
+
this.name = 'EarlierDownloadKept';
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Replace the earlier download in `dir` with the staged one. The earlier
|
|
294
|
+
* entries move aside into the staging directory (which `writeEvidence`
|
|
295
|
+
* removes), `result.json` first; the staged ones move in, `result.json` last,
|
|
296
|
+
* so while the sets change places the directory reads as a download in
|
|
297
|
+
* progress. If a move fails, what moved in comes out and what moved aside goes
|
|
298
|
+
* back, newest first, so `dir` holds the whole earlier set. A restore that
|
|
299
|
+
* fails stops the rest: `result.json` stays out, so the directory reads as a
|
|
300
|
+
* download that did not finish, and the error names where the files are.
|
|
301
|
+
*/
|
|
302
|
+
async function swapIn(staging, dir) {
|
|
303
|
+
const aside = assertNoEscape(staging, ASIDE);
|
|
304
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- a directory in the staging directory, its name checked by assertNoEscape.
|
|
305
|
+
await mkdir(aside);
|
|
306
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- the --out directory the user chose.
|
|
307
|
+
const names = await readdir(dir);
|
|
308
|
+
const earlier = names.filter(isEvidenceEntry).sort(byResultFile(-1));
|
|
309
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- the staging directory this download made under --out.
|
|
310
|
+
const staged = (await readdir(staging)).filter(entry => entry !== ASIDE).sort(byResultFile(1));
|
|
311
|
+
await refuseNamesTheUserHolds(dir, names, staged);
|
|
312
|
+
const movedAside = [];
|
|
313
|
+
const movedIn = [];
|
|
314
|
+
try {
|
|
315
|
+
for (const entry of earlier) {
|
|
316
|
+
await move(dir, aside, entry);
|
|
317
|
+
movedAside.push(entry);
|
|
318
|
+
}
|
|
319
|
+
for (const entry of staged) {
|
|
320
|
+
await move(staging, dir, entry);
|
|
321
|
+
movedIn.push(entry);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
catch (err) {
|
|
325
|
+
for (const entry of movedIn.reverse()) {
|
|
326
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- an entry this download moved into --out, its name checked by assertNoEscape.
|
|
327
|
+
await rm(assertNoEscape(dir, entry), { recursive: true, force: true }).catch(() => undefined);
|
|
328
|
+
}
|
|
329
|
+
for (const entry of movedAside.reverse()) {
|
|
330
|
+
try {
|
|
331
|
+
await move(aside, dir, entry);
|
|
332
|
+
}
|
|
333
|
+
catch {
|
|
334
|
+
throw new EarlierDownloadKept(aside, err);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
throw err;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Refuse, before anything moves, an entry this download installs that `dir`
|
|
342
|
+
* already holds under another name: a case-insensitive filesystem (macOS and
|
|
343
|
+
* Windows by default) takes a user's RESULT.JSON for result.json, or their
|
|
344
|
+
* Steps/ for steps/. The names this download owns are not lower-cased
|
|
345
|
+
* instead: where case matters, a user's Steps/ is a directory of its own.
|
|
346
|
+
*/
|
|
347
|
+
async function refuseNamesTheUserHolds(dir, names, staged) {
|
|
348
|
+
for (const entry of staged) {
|
|
349
|
+
if (names.includes(entry) || !(await exists(assertNoEscape(dir, entry))))
|
|
350
|
+
continue;
|
|
351
|
+
const theirs = names.find(name => name.toLowerCase() === entry.toLowerCase()) ?? entry;
|
|
352
|
+
throw ApiError.fromEnvelope({
|
|
353
|
+
error: {
|
|
354
|
+
code: 'VALIDATION_ERROR',
|
|
355
|
+
message: `--out holds ${theirs}, which this filesystem takes for the ${entry} that result get writes.`,
|
|
356
|
+
nextAction: 'Pick another --out directory; nothing in this one was changed.',
|
|
357
|
+
requestId: 'local',
|
|
358
|
+
details: { field: 'out', entry, existing: theirs },
|
|
359
|
+
},
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
async function exists(path) {
|
|
364
|
+
try {
|
|
365
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- a path its caller placed under --out with assertNoEscape.
|
|
366
|
+
await lstat(path);
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
catch (err) {
|
|
370
|
+
if (err.code === 'ENOENT')
|
|
371
|
+
return false;
|
|
372
|
+
throw err;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/** Orders `result.json` first (-1) or last (1) among the other entries. */
|
|
376
|
+
const byResultFile = (place) => (a, b) => place * (Number(a === RESULT_FILE) - Number(b === RESULT_FILE));
|
|
377
|
+
/** Move `entry` from `fromBase` to `toBase`; both paths are placed here by assertNoEscape. */
|
|
378
|
+
async function move(fromBase, toBase, entry) {
|
|
379
|
+
const from = assertNoEscape(fromBase, entry);
|
|
380
|
+
const to = assertNoEscape(toBase, entry);
|
|
381
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- both paths were placed under their base by assertNoEscape just above.
|
|
382
|
+
await rename(from, to);
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* The result as `result.json` holds it: each signed link replaced by the file
|
|
386
|
+
* it was written to, or null when that file was not downloaded.
|
|
387
|
+
*/
|
|
388
|
+
function localCopy(result, written) {
|
|
389
|
+
const local = (file) => (written.has(file) ? file : null);
|
|
390
|
+
const localSteps = (steps, under) => steps.map((step, position) => ({
|
|
391
|
+
...step,
|
|
392
|
+
screenshotSignedUrl: local(stepFile(under, position, 'png')),
|
|
393
|
+
htmlSignedUrl: local(stepFile(under, position, 'html')),
|
|
394
|
+
}));
|
|
395
|
+
const steps = localSteps(result.steps, '');
|
|
396
|
+
// The failed step is a copy of the first failed one in `steps`.
|
|
397
|
+
const failedAt = steps[result.steps.findIndex(step => step.status === 'failed')];
|
|
398
|
+
return {
|
|
399
|
+
...result,
|
|
400
|
+
videoUrl: result.videoUrl ? local(videoFile(result.videoUrl)) : null,
|
|
401
|
+
failedStep: result.failedStep === null
|
|
402
|
+
? null
|
|
403
|
+
: {
|
|
404
|
+
...result.failedStep,
|
|
405
|
+
screenshotSignedUrl: failedAt?.screenshotSignedUrl ?? null,
|
|
406
|
+
htmlSignedUrl: failedAt?.htmlSignedUrl ?? null,
|
|
407
|
+
},
|
|
408
|
+
steps,
|
|
409
|
+
signIn: result.signIn === null
|
|
410
|
+
? null
|
|
411
|
+
: {
|
|
412
|
+
...result.signIn,
|
|
413
|
+
videoUrl: result.signIn.videoUrl
|
|
414
|
+
? local(videoFile(result.signIn.videoUrl, SIGN_IN))
|
|
415
|
+
: null,
|
|
416
|
+
steps: localSteps(result.signIn.steps, SIGN_IN),
|
|
417
|
+
},
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
//# sourceMappingURL=evidence.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type WrittenEvidence } from './evidence.js';
|
|
2
|
+
import { type ResultDetail, type ResultList } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* One line per result, newest first, under a header: both ids are opaque, so
|
|
5
|
+
* the header is what tells the result id from the run id.
|
|
6
|
+
*/
|
|
7
|
+
export declare function renderResultList(list: ResultList): string;
|
|
8
|
+
/**
|
|
9
|
+
* One screen about one result: what happened, why, and the failed step; for a
|
|
10
|
+
* backend test, the end of its traceback and the start of its output. Each
|
|
11
|
+
* step, and both of those in full, are in the JSON (and the evidence
|
|
12
|
+
* download), not here.
|
|
13
|
+
*/
|
|
14
|
+
export declare function renderResultDetail(detail: ResultDetail): string;
|
|
15
|
+
/**
|
|
16
|
+
* What a download wrote to its directory, one file a line, then each file it
|
|
17
|
+
* could not download with the reason. `preview` is a dry run's plan. It counts
|
|
18
|
+
* evidence files, result.json apart, as the error for a partial download does.
|
|
19
|
+
*/
|
|
20
|
+
export declare function renderEvidence(evidence: WrittenEvidence, preview?: boolean): string;
|