@testsprite/testsprite-cli 0.7.0 → 0.9.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 +62 -45
- package/dist/commands/agent.d.ts +3 -0
- package/dist/commands/agent.js +79 -10
- package/dist/commands/ci.d.ts +88 -0
- package/dist/commands/ci.js +432 -0
- package/dist/commands/doctor.js +100 -1
- package/dist/commands/init.d.ts +45 -2
- package/dist/commands/init.js +198 -29
- package/dist/commands/project.d.ts +78 -0
- package/dist/commands/project.js +439 -5
- package/dist/commands/test.d.ts +193 -4
- package/dist/commands/test.js +1948 -398
- package/dist/commands/testlist.js +15 -9
- package/dist/commands/tunnel.d.ts +72 -0
- package/dist/commands/tunnel.js +367 -0
- package/dist/index.js +107 -38
- package/dist/lib/agent-detect.d.ts +53 -0
- package/dist/lib/agent-detect.js +198 -0
- package/dist/lib/dry-run/samples.d.ts +9 -0
- package/dist/lib/dry-run/samples.js +116 -0
- package/dist/lib/gh-output.d.ts +7 -0
- package/dist/lib/gh-output.js +50 -7
- package/dist/lib/http.d.ts +104 -0
- package/dist/lib/http.js +126 -7
- package/dist/lib/interrupt.d.ts +38 -13
- package/dist/lib/interrupt.js +130 -15
- package/dist/lib/local-target.d.ts +95 -0
- package/dist/lib/local-target.js +184 -0
- package/dist/lib/plan-poll.d.ts +171 -0
- package/dist/lib/plan-poll.js +442 -0
- package/dist/lib/plans.types.d.ts +130 -0
- package/dist/lib/plans.types.js +31 -0
- package/dist/lib/poll-support.d.ts +26 -0
- package/dist/lib/poll-support.js +65 -0
- package/dist/lib/poll.d.ts +1 -0
- package/dist/lib/poll.js +2 -40
- package/dist/lib/response-schemas.d.ts +13 -0
- package/dist/lib/response-schemas.js +46 -0
- package/dist/lib/run-progress.d.ts +41 -0
- package/dist/lib/run-progress.js +56 -0
- package/dist/lib/runs.types.d.ts +50 -0
- package/dist/lib/skill-nudge.d.ts +13 -5
- package/dist/lib/skill-nudge.js +40 -14
- package/dist/lib/target-url-preflight.js +3 -3
- package/dist/lib/target-url.d.ts +54 -8
- package/dist/lib/target-url.js +274 -68
- package/dist/lib/telemetry.d.ts +11 -0
- package/dist/lib/telemetry.js +1 -0
- package/dist/lib/ticker.d.ts +8 -0
- package/dist/lib/ticker.js +3 -0
- package/dist/lib/tunnel-session.d.ts +138 -0
- package/dist/lib/tunnel-session.js +249 -0
- package/dist/lib/tunnel.types.d.ts +54 -0
- package/dist/lib/tunnel.types.js +8 -0
- package/dist/vendor/tunnel-client/client.d.ts +82 -0
- package/dist/vendor/tunnel-client/client.js +924 -0
- package/dist/vendor/tunnel-client/config.d.ts +49 -0
- package/dist/vendor/tunnel-client/config.js +50 -0
- package/dist/vendor/tunnel-client/index.d.ts +9 -0
- package/dist/vendor/tunnel-client/index.js +9 -0
- package/dist/vendor/tunnel-client/lodash-lite.d.ts +14 -0
- package/dist/vendor/tunnel-client/lodash-lite.js +24 -0
- package/dist/vendor/tunnel-client/protocol.d.ts +60 -0
- package/dist/vendor/tunnel-client/protocol.js +143 -0
- package/dist/vendor/tunnel-client/types.d.ts +101 -0
- package/dist/vendor/tunnel-client/types.js +10 -0
- package/dist/vendor/tunnel-client/ws-compat.d.ts +206 -0
- package/dist/vendor/tunnel-client/ws-compat.js +316 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -1
- package/skills/testsprite-onboard.skill.md +60 -4
- package/skills/testsprite-verify.codex.md +22 -16
- package/skills/testsprite-verify.skill.md +27 -18
|
@@ -7,6 +7,7 @@ import { GLOBAL_OPTS_HINT, Output, resolveOutputMode } from '../lib/output.js';
|
|
|
7
7
|
import { pollRunUntilTerminal, TimeoutError } from '../lib/poll.js';
|
|
8
8
|
import { emitCiArtifacts, summarizeAcceptedPayload } from '../lib/gh-output.js';
|
|
9
9
|
import { describeConflict, summarizeConflicts } from '../lib/conflict-reason.js';
|
|
10
|
+
import { formatRunProgressLine } from '../lib/run-progress.js';
|
|
10
11
|
import { createTicker } from '../lib/ticker.js';
|
|
11
12
|
import { buildJUnitReport, parseJUnitReportFormat, writeJUnitReportFile, } from '../lib/junit-report.js';
|
|
12
13
|
import { renderTextTable, resolveTextColumns } from '../lib/text-table.js';
|
|
@@ -409,24 +410,29 @@ export async function runTestlistRun(opts, deps = {}) {
|
|
|
409
410
|
timeoutSeconds: Math.ceil(remainingMs / 1000),
|
|
410
411
|
sleep: deps.sleep,
|
|
411
412
|
onTransition: opts.verbose ? (msg) => stderrFn(`[verbose] ${msg}`) : undefined,
|
|
412
|
-
onTick: (run, elapsedMs) => {
|
|
413
|
-
const s = run.stepSummary ?? { total: 0, completed: 0, passedCount: 0, failedCount: 0 };
|
|
414
|
-
ticker.update(`Run ${run.runId} (${entry.testId}) — ${run.status} (${s.completed}/${s.total} steps elapsed=${Math.round(elapsedMs / 1000)}s)`);
|
|
415
|
-
},
|
|
413
|
+
onTick: (run, elapsedMs) => ticker.update(formatRunProgressLine(run, elapsedMs, `(${entry.testId})`)),
|
|
416
414
|
});
|
|
417
415
|
return {
|
|
418
416
|
testId: entry.testId,
|
|
417
|
+
testTitle: finalRun.testTitle ?? null,
|
|
418
|
+
// Give the JUnit `<testcase name>` the human title too (testlist has no
|
|
419
|
+
// separate name sweep like `test run --all`, so this is its only source;
|
|
420
|
+
// renderTestcase falls back to the id when name is absent).
|
|
421
|
+
...(finalRun.testTitle ? { name: finalRun.testTitle } : {}),
|
|
419
422
|
runId: entry.runId,
|
|
420
423
|
projectId: finalRun.projectId,
|
|
421
424
|
status: finalRun.status,
|
|
422
|
-
// Carry
|
|
423
|
-
// job-summary
|
|
424
|
-
//
|
|
425
|
-
//
|
|
426
|
-
//
|
|
425
|
+
// Carry both SERVER-built links so the ::error:: annotations and the
|
|
426
|
+
// job-summary are clickable (parity with `test run --all`): the test-case
|
|
427
|
+
// page anchors the Test column title, the execution-result page anchors
|
|
428
|
+
// the Run column. Never a client-built resolvePortalUrl — testlist run is
|
|
429
|
+
// V3-only and the client helper emits the V2 route shape which 404s.
|
|
427
430
|
...(typeof finalRun.dashboardUrl === 'string'
|
|
428
431
|
? { dashboardUrl: finalRun.dashboardUrl }
|
|
429
432
|
: {}),
|
|
433
|
+
...(typeof finalRun.executionUrl === 'string'
|
|
434
|
+
? { executionUrl: finalRun.executionUrl }
|
|
435
|
+
: {}),
|
|
430
436
|
};
|
|
431
437
|
}
|
|
432
438
|
catch (err) {
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `testsprite tunnel start | status | stop` — DEV-747 piece 3.
|
|
3
|
+
*
|
|
4
|
+
* The out-of-band primitive under `test run --local`, which is sugar over it.
|
|
5
|
+
* Two things it buys that the sugar cannot:
|
|
6
|
+
*
|
|
7
|
+
* - **One tunnel, many runs.** `tunnel start` in one terminal, then
|
|
8
|
+
* `test run <id> --local <port> --tunnel-client <id>` as many times as you
|
|
9
|
+
* like in another. Each run would otherwise mint and tear down its own
|
|
10
|
+
* credential, and the per-principal live-binding cap is small.
|
|
11
|
+
* - **A place to look when a run says the tunnel is down.** `tunnel status`
|
|
12
|
+
* answers "is my client connected" without spending a run to find out.
|
|
13
|
+
*
|
|
14
|
+
* There is deliberately no daemon (design ledger D1 (b), deferred): `tunnel
|
|
15
|
+
* start` holds the tunnel in the FOREGROUND and closes it when you stop the
|
|
16
|
+
* command. That is not a limitation to work around with `&` — the secret lives
|
|
17
|
+
* only in this process's memory, and detaching it would mean persisting a
|
|
18
|
+
* credential that opens an inbound path into this machine. `config.json`
|
|
19
|
+
* credentials are a settled no in this codebase.
|
|
20
|
+
*/
|
|
21
|
+
import { Command } from 'commander';
|
|
22
|
+
import type { CommonOptions } from '../lib/client-factory.js';
|
|
23
|
+
import { type ShutdownHandle } from '../lib/interrupt.js';
|
|
24
|
+
import { type TunnelClientHandle } from '../lib/tunnel-session.js';
|
|
25
|
+
import type { TunnelStatusResponse } from '../lib/tunnel.types.js';
|
|
26
|
+
import { type TunnelClientOptions } from '../vendor/tunnel-client/index.js';
|
|
27
|
+
export interface TunnelDeps {
|
|
28
|
+
env?: NodeJS.ProcessEnv;
|
|
29
|
+
credentialsPath?: string;
|
|
30
|
+
fetchImpl?: typeof globalThis.fetch;
|
|
31
|
+
stdout?: (line: string) => void;
|
|
32
|
+
stderr?: (line: string) => void;
|
|
33
|
+
shutdown?: ShutdownHandle;
|
|
34
|
+
/** Injectable tunnel-client factory (tests). Defaults to the vendored client. */
|
|
35
|
+
createTunnelClient?: (options: TunnelClientOptions) => TunnelClientHandle;
|
|
36
|
+
}
|
|
37
|
+
export interface TunnelStartOptions extends CommonOptions {
|
|
38
|
+
/** Requested binding lifetime in seconds. Server clamps to [60, 28800]. */
|
|
39
|
+
ttlSeconds?: number;
|
|
40
|
+
}
|
|
41
|
+
export interface TunnelClientIdOptions extends CommonOptions {
|
|
42
|
+
clientId: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Open a tunnel and hold it until the command is stopped.
|
|
46
|
+
*
|
|
47
|
+
* Ctrl-C is the documented way to end this command, so it exits 0 — the
|
|
48
|
+
* interrupt is the user getting what they asked for, not a failure. A tunnel
|
|
49
|
+
* the SERVICE disconnects is different: that is exit 10 (`UNAVAILABLE`), and
|
|
50
|
+
* anything attached to it has already stopped working.
|
|
51
|
+
*/
|
|
52
|
+
export declare function runTunnelStart(opts: TunnelStartOptions, deps?: TunnelDeps): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Is a client of mine connected?
|
|
55
|
+
*
|
|
56
|
+
* The reason this reads the way it does: `offline` is reported ONLY when the
|
|
57
|
+
* API answered and said so. An unreachable API, a 5xx, or any transport
|
|
58
|
+
* failure propagates as its own error — never as `offline`. Collapsing the two
|
|
59
|
+
* sends someone to restart a tunnel that was never the problem, which is the
|
|
60
|
+
* defect backend-v2.0 #1068 fixed on the server side of this same question.
|
|
61
|
+
*/
|
|
62
|
+
export declare function runTunnelStatus(opts: TunnelClientIdOptions, deps?: TunnelDeps): Promise<TunnelStatusResponse>;
|
|
63
|
+
/**
|
|
64
|
+
* Destroy a binding. Idempotent by contract — deleting an unknown or
|
|
65
|
+
* already-deleted binding is a success, because the requested end state holds.
|
|
66
|
+
*
|
|
67
|
+
* Worth knowing: this removes the ability to ATTACH a run to that client, and
|
|
68
|
+
* removes the client from the tunnel server. It does not reach into a
|
|
69
|
+
* `tunnel start` process still running elsewhere; stop that one with Ctrl-C.
|
|
70
|
+
*/
|
|
71
|
+
export declare function runTunnelStop(opts: TunnelClientIdOptions, deps?: TunnelDeps): Promise<void>;
|
|
72
|
+
export declare function createTunnelCommand(deps?: TunnelDeps): Command;
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `testsprite tunnel start | status | stop` — DEV-747 piece 3.
|
|
3
|
+
*
|
|
4
|
+
* The out-of-band primitive under `test run --local`, which is sugar over it.
|
|
5
|
+
* Two things it buys that the sugar cannot:
|
|
6
|
+
*
|
|
7
|
+
* - **One tunnel, many runs.** `tunnel start` in one terminal, then
|
|
8
|
+
* `test run <id> --local <port> --tunnel-client <id>` as many times as you
|
|
9
|
+
* like in another. Each run would otherwise mint and tear down its own
|
|
10
|
+
* credential, and the per-principal live-binding cap is small.
|
|
11
|
+
* - **A place to look when a run says the tunnel is down.** `tunnel status`
|
|
12
|
+
* answers "is my client connected" without spending a run to find out.
|
|
13
|
+
*
|
|
14
|
+
* There is deliberately no daemon (design ledger D1 (b), deferred): `tunnel
|
|
15
|
+
* start` holds the tunnel in the FOREGROUND and closes it when you stop the
|
|
16
|
+
* command. That is not a limitation to work around with `&` — the secret lives
|
|
17
|
+
* only in this process's memory, and detaching it would mean persisting a
|
|
18
|
+
* credential that opens an inbound path into this machine. `config.json`
|
|
19
|
+
* credentials are a settled no in this codebase.
|
|
20
|
+
*/
|
|
21
|
+
import { Command } from 'commander';
|
|
22
|
+
import { emitDryRunBanner, makeHttpClient, parseRequestTimeoutFlag, resolveRequestTimeoutMs, } from '../lib/client-factory.js';
|
|
23
|
+
import { ApiError, RequestTimeoutError } from '../lib/errors.js';
|
|
24
|
+
import { globalShutdown } from '../lib/interrupt.js';
|
|
25
|
+
import { GLOBAL_OPTS_HINT, Output, resolveOutputMode } from '../lib/output.js';
|
|
26
|
+
import { openTunnelSession } from '../lib/tunnel-session.js';
|
|
27
|
+
import { TunnelClient } from '../vendor/tunnel-client/index.js';
|
|
28
|
+
function stdoutOf(deps) {
|
|
29
|
+
return deps.stdout ?? ((line) => process.stdout.write(`${line}\n`));
|
|
30
|
+
}
|
|
31
|
+
function stderrOf(deps) {
|
|
32
|
+
return deps.stderr ?? ((line) => process.stderr.write(`${line}\n`));
|
|
33
|
+
}
|
|
34
|
+
function makeClient(opts, deps, shutdownSignal = (deps.shutdown ?? globalShutdown).signal) {
|
|
35
|
+
return makeHttpClient(opts, {
|
|
36
|
+
env: deps.env,
|
|
37
|
+
credentialsPath: deps.credentialsPath,
|
|
38
|
+
fetchImpl: deps.fetchImpl,
|
|
39
|
+
stderr: deps.stderr,
|
|
40
|
+
shutdownSignal,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async function withUninterruptibleRequest(opts, deps, timeoutMs, operation) {
|
|
44
|
+
const deadline = new AbortController();
|
|
45
|
+
const timer = setTimeout(() => {
|
|
46
|
+
deadline.abort(new RequestTimeoutError(timeoutMs));
|
|
47
|
+
}, timeoutMs);
|
|
48
|
+
timer.unref?.();
|
|
49
|
+
try {
|
|
50
|
+
return await operation(makeClient({ ...opts, requestTimeoutMs: timeoutMs }, deps, deadline.signal));
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function shutdownAwareTunnelClientFactory(createClient, signal) {
|
|
57
|
+
return options => {
|
|
58
|
+
const client = createClient(options);
|
|
59
|
+
return {
|
|
60
|
+
start: async () => {
|
|
61
|
+
if (signal.aborted)
|
|
62
|
+
throw signal.reason;
|
|
63
|
+
await new Promise((resolve, reject) => {
|
|
64
|
+
const onAbort = () => reject(signal.reason);
|
|
65
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
66
|
+
client.start().then(() => {
|
|
67
|
+
signal.removeEventListener('abort', onAbort);
|
|
68
|
+
resolve();
|
|
69
|
+
}, err => {
|
|
70
|
+
signal.removeEventListener('abort', onAbort);
|
|
71
|
+
reject(err);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
stop: () => {
|
|
76
|
+
const stopping = client.stop();
|
|
77
|
+
if (!signal.aborted)
|
|
78
|
+
return stopping;
|
|
79
|
+
void stopping.catch(() => { });
|
|
80
|
+
return Promise.resolve();
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A client for teardown, whose requests are NOT composed with the shutdown
|
|
87
|
+
* signal. `tunnel start` ends because of a Ctrl-C essentially every time, and
|
|
88
|
+
* the delete it issues at that moment must actually leave the machine — see
|
|
89
|
+
* the same helper in `commands/test.ts`.
|
|
90
|
+
*/
|
|
91
|
+
function makeDetachedClient(opts, deps, operationSignal) {
|
|
92
|
+
return makeHttpClient({ ...opts, requestTimeoutMs: TEARDOWN_OPERATION_TIMEOUT_MS }, {
|
|
93
|
+
env: deps.env,
|
|
94
|
+
credentialsPath: deps.credentialsPath,
|
|
95
|
+
fetchImpl: deps.fetchImpl,
|
|
96
|
+
stderr: deps.stderr,
|
|
97
|
+
shutdownSignal: operationSignal,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
async function withTeardownDeadline(opts, deps, operation) {
|
|
101
|
+
const deadline = new AbortController();
|
|
102
|
+
const timer = setTimeout(() => {
|
|
103
|
+
deadline.abort(new RequestTimeoutError(TEARDOWN_OPERATION_TIMEOUT_MS));
|
|
104
|
+
}, TEARDOWN_OPERATION_TIMEOUT_MS);
|
|
105
|
+
timer.unref?.();
|
|
106
|
+
const shutdown = deps.shutdown ?? globalShutdown;
|
|
107
|
+
try {
|
|
108
|
+
return await shutdown.runCriticalOperation(() => operation(makeDetachedClient(opts, deps, deadline.signal)));
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async function deleteTunnelForCleanup(opts, deps, clientId) {
|
|
115
|
+
await withTeardownDeadline(opts, deps, client => client.delete(`/tunnel/${encodeURIComponent(clientId)}`, {
|
|
116
|
+
allowNoContent: true,
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
const TEARDOWN_OPERATION_TIMEOUT_MS = 10_000;
|
|
120
|
+
function makeOutput(mode, deps) {
|
|
121
|
+
return new Output(mode, { stdout: deps.stdout, stderr: deps.stderr });
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Open a tunnel and hold it until the command is stopped.
|
|
125
|
+
*
|
|
126
|
+
* Ctrl-C is the documented way to end this command, so it exits 0 — the
|
|
127
|
+
* interrupt is the user getting what they asked for, not a failure. A tunnel
|
|
128
|
+
* the SERVICE disconnects is different: that is exit 10 (`UNAVAILABLE`), and
|
|
129
|
+
* anything attached to it has already stopped working.
|
|
130
|
+
*/
|
|
131
|
+
export async function runTunnelStart(opts, deps = {}) {
|
|
132
|
+
const stdout = stdoutOf(deps);
|
|
133
|
+
const stderr = stderrOf(deps);
|
|
134
|
+
const out = makeOutput(opts.output, deps);
|
|
135
|
+
if (opts.dryRun) {
|
|
136
|
+
emitDryRunBanner(stderr);
|
|
137
|
+
out.print({
|
|
138
|
+
method: 'POST',
|
|
139
|
+
path: '/api/cli/v1/tunnel',
|
|
140
|
+
...(opts.ttlSeconds !== undefined ? { body: { ttlSeconds: opts.ttlSeconds } } : {}),
|
|
141
|
+
thenHold: 'until interrupted',
|
|
142
|
+
thenDelete: '/api/cli/v1/tunnel/<client-id>',
|
|
143
|
+
}, () => [
|
|
144
|
+
'POST /api/cli/v1/tunnel',
|
|
145
|
+
'hold until interrupted (Ctrl-C)',
|
|
146
|
+
'DELETE /api/cli/v1/tunnel/<client-id>',
|
|
147
|
+
].join('\n'));
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const requestTimeoutMs = resolveRequestTimeoutMs(opts, deps.env ?? process.env);
|
|
151
|
+
const clientOpts = { ...opts, requestTimeoutMs };
|
|
152
|
+
const shutdown = deps.shutdown ?? globalShutdown;
|
|
153
|
+
let fatal = false;
|
|
154
|
+
let resolveWait;
|
|
155
|
+
let session;
|
|
156
|
+
const disarm = shutdown.arm();
|
|
157
|
+
try {
|
|
158
|
+
try {
|
|
159
|
+
session = await openTunnelSession({
|
|
160
|
+
log: stderr,
|
|
161
|
+
logLevel: opts.debug ? 'debug' : opts.verbose ? 'info' : 'error',
|
|
162
|
+
...(opts.ttlSeconds !== undefined ? { ttlSeconds: opts.ttlSeconds } : {}),
|
|
163
|
+
onFatal: () => {
|
|
164
|
+
fatal = true;
|
|
165
|
+
resolveWait?.();
|
|
166
|
+
},
|
|
167
|
+
}, {
|
|
168
|
+
mint: async (ttlSeconds) => withUninterruptibleRequest(clientOpts, deps, requestTimeoutMs, client => client.mintTunnel({ ...(ttlSeconds ? { ttlSeconds } : {}) })),
|
|
169
|
+
destroy: async (clientId) => deleteTunnelForCleanup(opts, deps, clientId),
|
|
170
|
+
createClient: shutdownAwareTunnelClientFactory(deps.createTunnelClient ?? (options => new TunnelClient(options)), shutdown.signal),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
catch (err) {
|
|
174
|
+
// The session helper has already stopped the client and deleted a minted
|
|
175
|
+
// binding. A first Ctrl-C during mint/connect is the command's documented
|
|
176
|
+
// normal stop, so retain exit 0 instead of exposing its wrapped error.
|
|
177
|
+
if (shutdown.signal.aborted)
|
|
178
|
+
return;
|
|
179
|
+
throw err;
|
|
180
|
+
}
|
|
181
|
+
out.print({ clientId: session.clientId, expiresAt: session.expiresAt, status: 'online' }, data => {
|
|
182
|
+
const d = data;
|
|
183
|
+
return [
|
|
184
|
+
`clientId ${d.clientId}`,
|
|
185
|
+
`expiresAt ${d.expiresAt}`,
|
|
186
|
+
`status online`,
|
|
187
|
+
`hint Attach a run: testsprite test run <test-id> --local <port> --tunnel-client ${d.clientId}`,
|
|
188
|
+
`hint Stop it: Ctrl-C here (or: testsprite tunnel stop ${d.clientId})`,
|
|
189
|
+
].join('\n');
|
|
190
|
+
});
|
|
191
|
+
void stdout;
|
|
192
|
+
await new Promise(resolve => {
|
|
193
|
+
resolveWait = resolve;
|
|
194
|
+
if (fatal || shutdown.signal.aborted) {
|
|
195
|
+
resolve();
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
shutdown.signal.addEventListener('abort', () => resolve(), { once: true });
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
try {
|
|
203
|
+
await session?.close();
|
|
204
|
+
}
|
|
205
|
+
finally {
|
|
206
|
+
disarm();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (fatal && !shutdown.signal.aborted) {
|
|
210
|
+
throw ApiError.fromEnvelope({
|
|
211
|
+
error: {
|
|
212
|
+
code: 'UNAVAILABLE',
|
|
213
|
+
message: 'The tunnel service disconnected this client and it cannot be restored.',
|
|
214
|
+
nextAction: 'Start it again — the retry mints a fresh client. Any run attached to the old client ' +
|
|
215
|
+
'has already stopped being able to reach this machine.',
|
|
216
|
+
requestId: 'local',
|
|
217
|
+
details: { reason: 'auth-failed', clientId: session.clientId },
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
stderr(`Tunnel ${session.clientId} closed.`);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Is a client of mine connected?
|
|
225
|
+
*
|
|
226
|
+
* The reason this reads the way it does: `offline` is reported ONLY when the
|
|
227
|
+
* API answered and said so. An unreachable API, a 5xx, or any transport
|
|
228
|
+
* failure propagates as its own error — never as `offline`. Collapsing the two
|
|
229
|
+
* sends someone to restart a tunnel that was never the problem, which is the
|
|
230
|
+
* defect backend-v2.0 #1068 fixed on the server side of this same question.
|
|
231
|
+
*/
|
|
232
|
+
export async function runTunnelStatus(opts, deps = {}) {
|
|
233
|
+
const out = makeOutput(opts.output, deps);
|
|
234
|
+
if (opts.dryRun) {
|
|
235
|
+
emitDryRunBanner(stderrOf(deps));
|
|
236
|
+
const sample = {
|
|
237
|
+
clientId: opts.clientId,
|
|
238
|
+
status: 'online',
|
|
239
|
+
expiresAt: '2026-01-01T00:00:00.000Z',
|
|
240
|
+
};
|
|
241
|
+
out.print(sample, () => renderStatus(sample));
|
|
242
|
+
return sample;
|
|
243
|
+
}
|
|
244
|
+
const status = await makeClient(opts, deps).getTunnelStatus(opts.clientId);
|
|
245
|
+
out.print(status, data => renderStatus(data));
|
|
246
|
+
return status;
|
|
247
|
+
}
|
|
248
|
+
function renderStatus(status) {
|
|
249
|
+
const lines = [
|
|
250
|
+
`clientId ${status.clientId}`,
|
|
251
|
+
`status ${status.status}`,
|
|
252
|
+
`expiresAt ${status.expiresAt}`,
|
|
253
|
+
];
|
|
254
|
+
if (status.status !== 'online') {
|
|
255
|
+
lines.push('hint Nothing is connected with this id. Start one with: testsprite tunnel start');
|
|
256
|
+
}
|
|
257
|
+
return lines.join('\n');
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Destroy a binding. Idempotent by contract — deleting an unknown or
|
|
261
|
+
* already-deleted binding is a success, because the requested end state holds.
|
|
262
|
+
*
|
|
263
|
+
* Worth knowing: this removes the ability to ATTACH a run to that client, and
|
|
264
|
+
* removes the client from the tunnel server. It does not reach into a
|
|
265
|
+
* `tunnel start` process still running elsewhere; stop that one with Ctrl-C.
|
|
266
|
+
*/
|
|
267
|
+
export async function runTunnelStop(opts, deps = {}) {
|
|
268
|
+
const out = makeOutput(opts.output, deps);
|
|
269
|
+
if (opts.dryRun) {
|
|
270
|
+
emitDryRunBanner(stderrOf(deps));
|
|
271
|
+
out.print({ method: 'DELETE', path: `/api/cli/v1/tunnel/${opts.clientId}` }, () => `DELETE /api/cli/v1/tunnel/${opts.clientId}`);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
await makeClient(opts, deps).deleteTunnel(opts.clientId);
|
|
275
|
+
out.print({ clientId: opts.clientId, deleted: true }, () => `Tunnel ${opts.clientId} deleted.`);
|
|
276
|
+
}
|
|
277
|
+
export function createTunnelCommand(deps = {}) {
|
|
278
|
+
const tunnel = new Command('tunnel')
|
|
279
|
+
.description('Open a tunnel so TestSprite can reach an app on this machine')
|
|
280
|
+
.addHelpText('after', '\n`testsprite test run <id> --local <port>` opens and closes a tunnel for you for a single\n' +
|
|
281
|
+
'run. Use these commands when you want one tunnel to serve several runs, or to check on\n' +
|
|
282
|
+
'one that a run reported as down.\n' +
|
|
283
|
+
'\nA tunnel lives only as long as `tunnel start` is running — there is no background\n' +
|
|
284
|
+
'daemon, because the credential that authorises an inbound network path into this machine\n' +
|
|
285
|
+
'is never written to disk.\n' +
|
|
286
|
+
'\nExamples:\n' +
|
|
287
|
+
' testsprite tunnel start # hold a tunnel open (Ctrl-C to stop)\n' +
|
|
288
|
+
' testsprite test run <id> --local 5173 --tunnel-client <id>\n' +
|
|
289
|
+
' testsprite tunnel status <id>\n' +
|
|
290
|
+
' testsprite tunnel stop <id>\n');
|
|
291
|
+
tunnel
|
|
292
|
+
.command('start')
|
|
293
|
+
// One-line description: Commander prints it in the parent's command list,
|
|
294
|
+
// where an embedded newline breaks out of the indented column. The detail
|
|
295
|
+
// belongs in this command's own help, below.
|
|
296
|
+
.description('Open a tunnel and hold it until you stop the command (Ctrl-C)')
|
|
297
|
+
.option('--ttl <seconds>', 'requested lifetime of the tunnel credential (60–28800). The server clamps this, and the ' +
|
|
298
|
+
'credential is deleted when the command exits regardless — the lifetime only matters if ' +
|
|
299
|
+
'this process is killed without cleaning up.')
|
|
300
|
+
.addHelpText('after', '\nPrints the client id to pass to `test run --local <port> --tunnel-client <id>`.\n' +
|
|
301
|
+
'\nExit codes:\n' +
|
|
302
|
+
' 0 you stopped it (Ctrl-C is the normal way to end this command)\n' +
|
|
303
|
+
' 3 auth error — the key needs the `run:tunnel` scope; mint a new key\n' +
|
|
304
|
+
' 10 the tunnel service disconnected the client, or it never connected\n' +
|
|
305
|
+
' 11 rate limited, or too many tunnels are already open for this account\n')
|
|
306
|
+
.addHelpText('after', GLOBAL_OPTS_HINT)
|
|
307
|
+
.action(async (cmdOpts, command) => {
|
|
308
|
+
const ttlSeconds = parseTtl(cmdOpts.ttl);
|
|
309
|
+
await runTunnelStart({
|
|
310
|
+
...resolveCommonOptions(command),
|
|
311
|
+
...(ttlSeconds !== undefined ? { ttlSeconds } : {}),
|
|
312
|
+
}, deps);
|
|
313
|
+
});
|
|
314
|
+
tunnel
|
|
315
|
+
.command('status <client-id>')
|
|
316
|
+
.description('Report whether a tunnel client of yours is connected')
|
|
317
|
+
.addHelpText('after', '\n`offline` means the API answered and said nothing is connected with that id. A failure\n' +
|
|
318
|
+
'to reach TestSprite is reported as its own error, never as `offline`.\n' +
|
|
319
|
+
'\nExit codes:\n' +
|
|
320
|
+
' 0 answered (the answer may be `offline`)\n' +
|
|
321
|
+
' 4 no such tunnel for this account\n' +
|
|
322
|
+
' 10 could not reach TestSprite to ask\n')
|
|
323
|
+
.addHelpText('after', GLOBAL_OPTS_HINT)
|
|
324
|
+
.action(async (clientId, _cmdOpts, command) => {
|
|
325
|
+
await runTunnelStatus({ ...resolveCommonOptions(command), clientId }, deps);
|
|
326
|
+
});
|
|
327
|
+
tunnel
|
|
328
|
+
.command('stop <client-id>')
|
|
329
|
+
.description('Destroy a tunnel credential (idempotent)')
|
|
330
|
+
.addHelpText('after', '\nStopping one that is already gone succeeds. This revokes the credential; it does not\n' +
|
|
331
|
+
'stop a `tunnel start` still running in another terminal (press Ctrl-C there).\n')
|
|
332
|
+
.addHelpText('after', GLOBAL_OPTS_HINT)
|
|
333
|
+
.action(async (clientId, _cmdOpts, command) => {
|
|
334
|
+
await runTunnelStop({ ...resolveCommonOptions(command), clientId }, deps);
|
|
335
|
+
});
|
|
336
|
+
return tunnel;
|
|
337
|
+
}
|
|
338
|
+
function resolveCommonOptions(command) {
|
|
339
|
+
const globals = command.optsWithGlobals();
|
|
340
|
+
return {
|
|
341
|
+
profile: globals.profile ?? 'default',
|
|
342
|
+
output: resolveOutputMode(globals.output),
|
|
343
|
+
endpointUrl: globals.endpointUrl,
|
|
344
|
+
debug: globals.debug ?? false,
|
|
345
|
+
verbose: globals.verbose ?? false,
|
|
346
|
+
dryRun: globals.dryRun ?? false,
|
|
347
|
+
requestTimeoutMs: parseRequestTimeoutFlag(globals.requestTimeout),
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function parseTtl(raw) {
|
|
351
|
+
if (raw === undefined)
|
|
352
|
+
return undefined;
|
|
353
|
+
const n = Number(raw);
|
|
354
|
+
if (!Number.isInteger(n) || n <= 0) {
|
|
355
|
+
throw ApiError.fromEnvelope({
|
|
356
|
+
error: {
|
|
357
|
+
code: 'VALIDATION_ERROR',
|
|
358
|
+
message: 'Invalid request.',
|
|
359
|
+
nextAction: 'Flag `--ttl` is invalid: must be a positive whole number of seconds.',
|
|
360
|
+
requestId: 'local',
|
|
361
|
+
details: { field: 'ttl', reason: 'must be a positive whole number of seconds' },
|
|
362
|
+
},
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
return n;
|
|
366
|
+
}
|
|
367
|
+
//# sourceMappingURL=tunnel.js.map
|