@quiesco/core 0.1.1 → 0.3.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/dist/bundle-check.d.ts +16 -0
- package/dist/bundle-check.d.ts.map +1 -0
- package/dist/bundle-check.js +84 -0
- package/dist/bundle-check.js.map +1 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/devices/android.d.ts.map +1 -1
- package/dist/devices/android.js +1 -0
- package/dist/devices/android.js.map +1 -1
- package/dist/devices/ios-runner.d.ts +24 -0
- package/dist/devices/ios-runner.d.ts.map +1 -1
- package/dist/devices/ios-runner.js +27 -0
- package/dist/devices/ios-runner.js.map +1 -1
- package/dist/devices/ios.d.ts +7 -1
- package/dist/devices/ios.d.ts.map +1 -1
- package/dist/devices/ios.js +22 -2
- package/dist/devices/ios.js.map +1 -1
- package/dist/driver.d.ts +22 -2
- package/dist/driver.d.ts.map +1 -1
- package/dist/driver.js +35 -1
- package/dist/driver.js.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/lifecycle.d.ts +13 -0
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +34 -3
- package/dist/lifecycle.js.map +1 -1
- package/dist/repeated-failures.d.ts +43 -0
- package/dist/repeated-failures.d.ts.map +1 -0
- package/dist/repeated-failures.js +111 -0
- package/dist/repeated-failures.js.map +1 -0
- package/dist/run-events.d.ts +60 -0
- package/dist/run-events.d.ts.map +1 -0
- package/dist/run-events.js +161 -0
- package/dist/run-events.js.map +1 -0
- package/dist/run-list.d.ts +13 -0
- package/dist/run-list.d.ts.map +1 -0
- package/dist/run-list.js +47 -0
- package/dist/run-list.js.map +1 -0
- package/dist/run-state.d.ts +81 -0
- package/dist/run-state.d.ts.map +1 -0
- package/dist/run-state.js +196 -0
- package/dist/run-state.js.map +1 -0
- package/dist/session.d.ts +11 -1
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +47 -3
- package/dist/session.js.map +1 -1
- package/ios-runner/Runner/SystemUIServer.swift +17 -0
- package/package.json +6 -2
- package/src/bundle-check.ts +83 -0
- package/src/config.ts +28 -1
- package/src/devices/android.ts +1 -0
- package/src/devices/ios-runner.ts +33 -0
- package/src/devices/ios.ts +23 -3
- package/src/driver.ts +50 -2
- package/src/index.ts +9 -1
- package/src/lifecycle.ts +38 -3
- package/src/repeated-failures.ts +114 -0
- package/src/run-events.ts +185 -0
- package/src/run-list.ts +55 -0
- package/src/run-state.ts +234 -0
- package/src/session.ts +47 -4
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport of run events (`@quiesco/protocol` run-events) between a runner's Quiesco reporter and the
|
|
3
|
+
* `quiesco` process: a local socket (Unix socket file, or a named pipe on Windows) carrying NDJSON.
|
|
4
|
+
*
|
|
5
|
+
* - The CLI starts a RunEventServer and passes `server.env()` to the runner it spawns.
|
|
6
|
+
* - Reporters (and workers adding evidence) call `connectRunEvents()`: without QUIESCO_EVENTS it
|
|
7
|
+
* returns a client that sends nothing, so running the runner directly is unaffected.
|
|
8
|
+
* - A broken connection never fails a test: the client warns once and drops further events.
|
|
9
|
+
*/
|
|
10
|
+
import { randomBytes } from 'node:crypto';
|
|
11
|
+
import { chmodSync } from 'node:fs';
|
|
12
|
+
import net from 'node:net';
|
|
13
|
+
import os from 'node:os';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
|
|
16
|
+
import { RUN_EVENTS_ENV, RUN_EVENTS_PROTOCOL, createRunEventDecoder, encodeRunEvent } from '@quiesco/protocol';
|
|
17
|
+
import type { RunEvent, RunnerName } from '@quiesco/protocol';
|
|
18
|
+
|
|
19
|
+
/** Longest Unix socket path that works everywhere (sun_path is 104 bytes on macOS, 108 on Linux). */
|
|
20
|
+
const MAX_SOCKET_PATH = 100;
|
|
21
|
+
|
|
22
|
+
/** A fresh socket address for this process. */
|
|
23
|
+
export function runEventsAddress(platform: NodeJS.Platform = process.platform, tmpdir: string = os.tmpdir()): string {
|
|
24
|
+
const name = `quiesco-${process.pid}-${randomBytes(4).toString('hex')}`;
|
|
25
|
+
if (platform === 'win32') return `\\\\.\\pipe\\${name}`;
|
|
26
|
+
const file = path.join(tmpdir, `${name}.sock`);
|
|
27
|
+
// A long TMPDIR (some CI images) would make the path too long for a Unix socket.
|
|
28
|
+
return file.length <= MAX_SOCKET_PATH ? file : path.join('/tmp', `${name}.sock`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface RunEventServerOptions {
|
|
32
|
+
/** Each decoded event, with the connection it came on (1, 2, …). */
|
|
33
|
+
onEvent: (event: RunEvent, connection: number) => void;
|
|
34
|
+
/** A line that is not a known run event (skipped). */
|
|
35
|
+
onInvalid?: (line: string, reason: string) => void;
|
|
36
|
+
/** Default: runEventsAddress(). */
|
|
37
|
+
address?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class RunEventServer {
|
|
41
|
+
private readonly server: net.Server;
|
|
42
|
+
private readonly sockets = new Set<net.Socket>();
|
|
43
|
+
private readonly idleWaiters: (() => void)[] = [];
|
|
44
|
+
private nextConnection = 1;
|
|
45
|
+
private listening = false;
|
|
46
|
+
readonly address: string;
|
|
47
|
+
/** The runners that said hello (a reporter is wired in). */
|
|
48
|
+
readonly hellos: { runner: RunnerName; protocol: number; connection: number }[] = [];
|
|
49
|
+
|
|
50
|
+
constructor(private readonly options: RunEventServerOptions) {
|
|
51
|
+
this.address = options.address ?? runEventsAddress();
|
|
52
|
+
this.server = net.createServer((socket) => this.accept(socket));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async listen(): Promise<string> {
|
|
56
|
+
await new Promise<void>((resolve, reject) => {
|
|
57
|
+
this.server.once('error', reject);
|
|
58
|
+
this.server.listen(this.address, () => {
|
|
59
|
+
this.server.off('error', reject);
|
|
60
|
+
resolve();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
this.listening = true;
|
|
64
|
+
// Only this user may connect (the socket file is created with the process umask).
|
|
65
|
+
if (process.platform !== 'win32') chmodSync(this.address, 0o600);
|
|
66
|
+
return this.address;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Environment for the runner process. */
|
|
70
|
+
env(): Record<string, string> {
|
|
71
|
+
return { [RUN_EVENTS_ENV]: this.address };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Number of open connections. */
|
|
75
|
+
get connections(): number {
|
|
76
|
+
return this.sockets.size;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Resolves once no connection is open, or after `timeoutMs`. Called after the runner exits: its
|
|
81
|
+
* last events may still be in flight on a connection that is closing.
|
|
82
|
+
*/
|
|
83
|
+
async drain(timeoutMs = 2000): Promise<void> {
|
|
84
|
+
if (this.sockets.size === 0) return;
|
|
85
|
+
await new Promise<void>((resolve) => {
|
|
86
|
+
const timer = setTimeout(done, timeoutMs);
|
|
87
|
+
timer.unref();
|
|
88
|
+
function done() {
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
resolve();
|
|
91
|
+
}
|
|
92
|
+
this.idleWaiters.push(done);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Stops listening, closes the connections still open and removes the socket file. */
|
|
97
|
+
async close(): Promise<void> {
|
|
98
|
+
for (const s of this.sockets) s.destroy();
|
|
99
|
+
this.sockets.clear();
|
|
100
|
+
if (!this.listening) return;
|
|
101
|
+
this.listening = false;
|
|
102
|
+
// net.Server removes its Unix socket file on close.
|
|
103
|
+
await new Promise<void>((resolve) => this.server.close(() => resolve()));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private accept(socket: net.Socket): void {
|
|
107
|
+
const connection = this.nextConnection++;
|
|
108
|
+
this.sockets.add(socket);
|
|
109
|
+
socket.setEncoding('utf8');
|
|
110
|
+
const decoder = createRunEventDecoder((event) => {
|
|
111
|
+
if (event.type === 'hello') this.hellos.push({ runner: event.runner, protocol: event.protocol, connection });
|
|
112
|
+
this.options.onEvent(event, connection);
|
|
113
|
+
}, this.options.onInvalid);
|
|
114
|
+
socket.on('data', (chunk: string) => decoder.push(chunk));
|
|
115
|
+
socket.on('end', () => decoder.end());
|
|
116
|
+
socket.on('error', () => {});
|
|
117
|
+
socket.on('close', () => {
|
|
118
|
+
this.sockets.delete(socket);
|
|
119
|
+
if (this.sockets.size === 0) for (const w of this.idleWaiters.splice(0)) w();
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface RunEventClient {
|
|
125
|
+
/** False when QUIESCO_EVENTS is not set or the connection failed: send() does nothing. */
|
|
126
|
+
readonly enabled: boolean;
|
|
127
|
+
send(event: RunEvent): void;
|
|
128
|
+
/** Flushes and closes the connection. Safe to call more than once. */
|
|
129
|
+
close(): Promise<void>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const disabledClient: RunEventClient = {
|
|
133
|
+
enabled: false,
|
|
134
|
+
send() {},
|
|
135
|
+
close: () => Promise.resolve(),
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export interface ConnectOptions {
|
|
139
|
+
runner: RunnerName;
|
|
140
|
+
runnerVersion?: string;
|
|
141
|
+
env?: NodeJS.ProcessEnv;
|
|
142
|
+
/** Told once when the connection fails. Default: a line on stderr. */
|
|
143
|
+
warn?: (message: string) => void;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Connects to the `quiesco` process named by QUIESCO_EVENTS and sends `hello`. Events sent before the
|
|
148
|
+
* connection is up are buffered by the socket. The connection does not keep the process alive.
|
|
149
|
+
*/
|
|
150
|
+
export function connectRunEvents(options: ConnectOptions): RunEventClient {
|
|
151
|
+
const address = (options.env ?? process.env)[RUN_EVENTS_ENV];
|
|
152
|
+
if (address === undefined || address === '') return disabledClient;
|
|
153
|
+
const warn = options.warn ?? ((m: string) => process.stderr.write(`quiesco: ${m}\n`));
|
|
154
|
+
let failed = false;
|
|
155
|
+
let closing: Promise<void> | null = null;
|
|
156
|
+
const socket = net.createConnection(address);
|
|
157
|
+
socket.unref();
|
|
158
|
+
socket.on('error', (e) => {
|
|
159
|
+
if (failed) return;
|
|
160
|
+
failed = true;
|
|
161
|
+
warn(`run events are not sent (progress / UI will not update): ${e.message}`);
|
|
162
|
+
socket.destroy();
|
|
163
|
+
});
|
|
164
|
+
const client: RunEventClient = {
|
|
165
|
+
get enabled() {
|
|
166
|
+
return !failed;
|
|
167
|
+
},
|
|
168
|
+
send(event) {
|
|
169
|
+
if (failed || closing !== null || socket.destroyed) return;
|
|
170
|
+
socket.write(encodeRunEvent(event));
|
|
171
|
+
},
|
|
172
|
+
close() {
|
|
173
|
+
closing ??= new Promise<void>((resolve) => {
|
|
174
|
+
if (failed || socket.destroyed) return resolve();
|
|
175
|
+
// Unref'd so a forgotten client never holds the runner open; while flushing, keep the process alive.
|
|
176
|
+
socket.ref();
|
|
177
|
+
socket.once('close', () => resolve());
|
|
178
|
+
socket.end();
|
|
179
|
+
});
|
|
180
|
+
return closing;
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
client.send({ type: 'hello', protocol: RUN_EVENTS_PROTOCOL, runner: options.runner, pid: process.pid, ...(options.runnerVersion !== undefined ? { runnerVersion: options.runnerVersion } : {}) });
|
|
184
|
+
return client;
|
|
185
|
+
}
|
package/src/run-list.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List the tests a runner would run, without running them (`quiesco list`, the UI's test tree): runs
|
|
3
|
+
* the project's own `@quiesco/<runner>/list` in a child process (it loads the project's runner) and
|
|
4
|
+
* reads back the TestList it writes.
|
|
5
|
+
*/
|
|
6
|
+
import { spawn } from 'node:child_process';
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import { createRequire } from 'node:module';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
|
|
12
|
+
import type { RunnerName, TestList } from '@quiesco/protocol';
|
|
13
|
+
|
|
14
|
+
export interface ListTestsOptions {
|
|
15
|
+
runner: RunnerName;
|
|
16
|
+
/** The project directory. */
|
|
17
|
+
cwd: string;
|
|
18
|
+
/** Runner arguments: `--config <file>`, paths / filters (cucumber also `--profile`). */
|
|
19
|
+
args?: readonly string[];
|
|
20
|
+
env?: NodeJS.ProcessEnv;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Where the project's @quiesco/<runner>/list is; null when that adapter is not installed. */
|
|
24
|
+
export function listEntry(runner: RunnerName, cwd: string): string | null {
|
|
25
|
+
try {
|
|
26
|
+
return createRequire(path.join(cwd, 'package.json')).resolve(`@quiesco/${runner}/list`);
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function listTests(options: ListTestsOptions): Promise<TestList> {
|
|
33
|
+
const { runner, cwd } = options;
|
|
34
|
+
const entry = listEntry(runner, cwd);
|
|
35
|
+
if (entry === null) return { runner, files: [], tests: [], errors: [{ message: `@quiesco/${runner} is not installed in ${cwd} (or is older than 0.3.0)` }] };
|
|
36
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'quiesco-list-'));
|
|
37
|
+
const out = path.join(dir, 'list.json');
|
|
38
|
+
let stderr = '';
|
|
39
|
+
try {
|
|
40
|
+
const code = await new Promise<number | null>((resolve) => {
|
|
41
|
+
const child = spawn(process.execPath, [entry, '--out', out, ...(options.args ?? [])], { cwd, env: options.env ?? process.env, stdio: ['ignore', 'ignore', 'pipe'] });
|
|
42
|
+
child.stderr.setEncoding('utf8').on('data', (d: string) => (stderr += d));
|
|
43
|
+
child.on('error', () => resolve(127));
|
|
44
|
+
child.on('close', resolve);
|
|
45
|
+
});
|
|
46
|
+
try {
|
|
47
|
+
return JSON.parse(fs.readFileSync(out, 'utf8')) as TestList;
|
|
48
|
+
} catch {
|
|
49
|
+
const tail = stderr.trim().split('\n').slice(-5).join('\n');
|
|
50
|
+
return { runner, files: [], tests: [], errors: [{ message: `listing the ${runner} tests failed (exit ${code})${tail !== '' ? `:\n${tail}` : ''}` }] };
|
|
51
|
+
}
|
|
52
|
+
} finally {
|
|
53
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/run-state.ts
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state of a run, built from run events (`@quiesco/protocol` run-events): what the progress bar and
|
|
3
|
+
* `quiesco ui` show. Pure: feed it events with `apply()`, read it back.
|
|
4
|
+
*
|
|
5
|
+
* Several runs in a row (the UI runs again) start over at each `run:start`, keeping what was learned
|
|
6
|
+
* about tests not in the new run.
|
|
7
|
+
*/
|
|
8
|
+
import { emptyCounts } from '@quiesco/protocol';
|
|
9
|
+
import type { RunCounts, RunError, RunEvent, RunEventOf, RunnerName, TestRef, TestStatus } from '@quiesco/protocol';
|
|
10
|
+
|
|
11
|
+
export interface TestState {
|
|
12
|
+
test: TestRef;
|
|
13
|
+
status: TestStatus | 'running' | 'queued';
|
|
14
|
+
durationMs?: number;
|
|
15
|
+
error?: RunError;
|
|
16
|
+
logs?: readonly { stream: 'stdout' | 'stderr'; text: string }[];
|
|
17
|
+
/** Absolute paths of Quiesco's evidence files (screenshot, element tree, …). */
|
|
18
|
+
evidence?: readonly string[];
|
|
19
|
+
worker?: number;
|
|
20
|
+
attempt?: number;
|
|
21
|
+
startedAt?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface RunningTest {
|
|
25
|
+
test: TestRef;
|
|
26
|
+
worker?: number;
|
|
27
|
+
startedAt: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type RunPhase = 'idle' | 'running' | 'ended';
|
|
31
|
+
|
|
32
|
+
/** A RunState as plain JSON (`quiesco ui` sends it to a browser that connects mid-run). */
|
|
33
|
+
export interface RunStateSnapshot {
|
|
34
|
+
phase: RunPhase;
|
|
35
|
+
runner: RunnerName | null;
|
|
36
|
+
reportedTotal: number;
|
|
37
|
+
totalIsEstimate: boolean;
|
|
38
|
+
workers?: number;
|
|
39
|
+
files: string[];
|
|
40
|
+
counts: RunCounts;
|
|
41
|
+
done: number;
|
|
42
|
+
running: RunningTest[];
|
|
43
|
+
tests: TestState[];
|
|
44
|
+
fileErrors: [string, RunError][];
|
|
45
|
+
runErrors: RunError[];
|
|
46
|
+
success: boolean | null;
|
|
47
|
+
interrupted: boolean;
|
|
48
|
+
startedAt: number | null;
|
|
49
|
+
endedAt: number | null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class RunState {
|
|
53
|
+
phase: RunPhase = 'idle';
|
|
54
|
+
runner: RunnerName | null = null;
|
|
55
|
+
/** As told by the runner (exact, an estimate, or what is known so far). See `total`. */
|
|
56
|
+
reportedTotal = 0;
|
|
57
|
+
totalIsEstimate = false;
|
|
58
|
+
workers: number | undefined;
|
|
59
|
+
files: string[] = [];
|
|
60
|
+
counts: RunCounts = emptyCounts();
|
|
61
|
+
/** Tests that finished in this run (retries count once, when they stop retrying). */
|
|
62
|
+
done = 0;
|
|
63
|
+
readonly running = new Map<string, RunningTest>();
|
|
64
|
+
/** Every test seen, by id, also from earlier runs. */
|
|
65
|
+
readonly tests = new Map<string, TestState>();
|
|
66
|
+
readonly fileErrors = new Map<string, RunError>();
|
|
67
|
+
readonly runErrors: RunError[] = [];
|
|
68
|
+
success: boolean | null = null;
|
|
69
|
+
interrupted = false;
|
|
70
|
+
/** Epoch ms of `run:start` / `run:end` as seen by this process. */
|
|
71
|
+
startedAt: number | null = null;
|
|
72
|
+
endedAt: number | null = null;
|
|
73
|
+
/** Evidence that arrived before its test ended (the worker can be faster than the reporter). */
|
|
74
|
+
private readonly earlyEvidence = new Map<string, readonly string[]>();
|
|
75
|
+
|
|
76
|
+
constructor(private readonly now: () => number = Date.now) {}
|
|
77
|
+
|
|
78
|
+
/** Never below what is already done or running. */
|
|
79
|
+
get total(): number {
|
|
80
|
+
return Math.max(this.reportedTotal, this.done + this.running.size);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The total is not final yet (estimate, or Jest still finding tests). */
|
|
84
|
+
get totalIsApproximate(): boolean {
|
|
85
|
+
return this.totalIsEstimate && this.phase !== 'ended';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
get elapsedMs(): number {
|
|
89
|
+
if (this.startedAt === null) return 0;
|
|
90
|
+
return (this.endedAt ?? this.now()) - this.startedAt;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
apply(event: RunEvent): void {
|
|
94
|
+
switch (event.type) {
|
|
95
|
+
case 'hello':
|
|
96
|
+
this.runner = event.runner;
|
|
97
|
+
break;
|
|
98
|
+
case 'run:start':
|
|
99
|
+
this.start(event);
|
|
100
|
+
break;
|
|
101
|
+
case 'total:update':
|
|
102
|
+
this.reportedTotal = event.total;
|
|
103
|
+
this.totalIsEstimate = event.totalIsEstimate;
|
|
104
|
+
break;
|
|
105
|
+
case 'file:start':
|
|
106
|
+
break;
|
|
107
|
+
case 'file:end':
|
|
108
|
+
if (event.error !== undefined) this.fileErrors.set(event.file, event.error);
|
|
109
|
+
break;
|
|
110
|
+
case 'test:start':
|
|
111
|
+
this.running.set(event.test.id, { test: event.test, startedAt: event.startedAt, ...(event.worker !== undefined ? { worker: event.worker } : {}) });
|
|
112
|
+
this.tests.set(event.test.id, { ...this.tests.get(event.test.id), test: event.test, status: 'running', attempt: event.attempt, startedAt: event.startedAt, ...(event.worker !== undefined ? { worker: event.worker } : {}) });
|
|
113
|
+
break;
|
|
114
|
+
case 'test:end':
|
|
115
|
+
this.end(event);
|
|
116
|
+
break;
|
|
117
|
+
case 'test:evidence': {
|
|
118
|
+
const t = this.tests.get(event.testId);
|
|
119
|
+
if (t !== undefined && t.status !== 'running') t.evidence = event.files;
|
|
120
|
+
else this.earlyEvidence.set(event.testId, event.files);
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case 'run:error':
|
|
124
|
+
this.runErrors.push(event.error);
|
|
125
|
+
break;
|
|
126
|
+
case 'run:end':
|
|
127
|
+
this.finish(event.success, event.interrupted);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
snapshot(): RunStateSnapshot {
|
|
133
|
+
return {
|
|
134
|
+
phase: this.phase,
|
|
135
|
+
runner: this.runner,
|
|
136
|
+
reportedTotal: this.reportedTotal,
|
|
137
|
+
totalIsEstimate: this.totalIsEstimate,
|
|
138
|
+
...(this.workers !== undefined ? { workers: this.workers } : {}),
|
|
139
|
+
files: [...this.files],
|
|
140
|
+
counts: { ...this.counts },
|
|
141
|
+
done: this.done,
|
|
142
|
+
running: [...this.running.values()],
|
|
143
|
+
tests: [...this.tests.values()],
|
|
144
|
+
fileErrors: [...this.fileErrors],
|
|
145
|
+
runErrors: [...this.runErrors],
|
|
146
|
+
success: this.success,
|
|
147
|
+
interrupted: this.interrupted,
|
|
148
|
+
startedAt: this.startedAt,
|
|
149
|
+
endedAt: this.endedAt,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static fromSnapshot(s: RunStateSnapshot, now: () => number = Date.now): RunState {
|
|
154
|
+
const state = new RunState(now);
|
|
155
|
+
state.phase = s.phase;
|
|
156
|
+
state.runner = s.runner;
|
|
157
|
+
state.reportedTotal = s.reportedTotal;
|
|
158
|
+
state.totalIsEstimate = s.totalIsEstimate;
|
|
159
|
+
state.workers = s.workers;
|
|
160
|
+
state.files = [...s.files];
|
|
161
|
+
state.counts = { ...s.counts };
|
|
162
|
+
state.done = s.done;
|
|
163
|
+
for (const r of s.running) state.running.set(r.test.id, r);
|
|
164
|
+
for (const t of s.tests) state.tests.set(t.test.id, t);
|
|
165
|
+
for (const [file, e] of s.fileErrors) state.fileErrors.set(file, e);
|
|
166
|
+
state.runErrors.push(...s.runErrors);
|
|
167
|
+
state.success = s.success;
|
|
168
|
+
state.interrupted = s.interrupted;
|
|
169
|
+
state.startedAt = s.startedAt;
|
|
170
|
+
state.endedAt = s.endedAt;
|
|
171
|
+
return state;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The runner process exited (maybe without `run:end`: crash, Ctrl-C). */
|
|
175
|
+
runnerExited(code: number): void {
|
|
176
|
+
if (this.phase === 'ended') return;
|
|
177
|
+
this.finish(code === 0, code === 130 || code === 143);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private start(e: RunEventOf<'run:start'>): void {
|
|
181
|
+
this.phase = 'running';
|
|
182
|
+
this.reportedTotal = e.total;
|
|
183
|
+
this.totalIsEstimate = e.totalIsEstimate;
|
|
184
|
+
this.workers = e.workers;
|
|
185
|
+
this.files = [...e.files];
|
|
186
|
+
this.counts = emptyCounts();
|
|
187
|
+
this.done = 0;
|
|
188
|
+
this.running.clear();
|
|
189
|
+
this.fileErrors.clear();
|
|
190
|
+
this.runErrors.length = 0;
|
|
191
|
+
this.earlyEvidence.clear();
|
|
192
|
+
this.success = null;
|
|
193
|
+
this.interrupted = false;
|
|
194
|
+
this.startedAt = this.now();
|
|
195
|
+
this.endedAt = null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private end(e: RunEventOf<'test:end'>): void {
|
|
199
|
+
this.running.delete(e.test.id);
|
|
200
|
+
const prev = this.tests.get(e.test.id);
|
|
201
|
+
const evidence = this.earlyEvidence.get(e.test.id);
|
|
202
|
+
this.earlyEvidence.delete(e.test.id);
|
|
203
|
+
const state: TestState = {
|
|
204
|
+
test: { ...prev?.test, ...e.test },
|
|
205
|
+
status: e.willRetry ? 'queued' : e.status,
|
|
206
|
+
durationMs: e.durationMs,
|
|
207
|
+
...(e.error !== undefined ? { error: e.error } : {}),
|
|
208
|
+
...(e.logs !== undefined ? { logs: e.logs } : {}),
|
|
209
|
+
...(evidence !== undefined ? { evidence } : {}),
|
|
210
|
+
...(e.worker !== undefined ? { worker: e.worker } : prev?.worker !== undefined ? { worker: prev.worker } : {}),
|
|
211
|
+
...(prev?.attempt !== undefined ? { attempt: prev.attempt } : {}),
|
|
212
|
+
...(prev?.startedAt !== undefined ? { startedAt: prev.startedAt } : {}),
|
|
213
|
+
};
|
|
214
|
+
this.tests.set(e.test.id, state);
|
|
215
|
+
if (e.willRetry) return;
|
|
216
|
+
this.done++;
|
|
217
|
+
this.counts[e.status]++;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private finish(success: boolean, interrupted: boolean): void {
|
|
221
|
+
if (this.phase === 'idle') this.startedAt = this.now();
|
|
222
|
+
for (const [id, r] of this.running) {
|
|
223
|
+
const t = this.tests.get(id);
|
|
224
|
+
if (t !== undefined) t.status = 'notRun';
|
|
225
|
+
else this.tests.set(id, { test: r.test, status: 'notRun' });
|
|
226
|
+
this.counts.notRun++;
|
|
227
|
+
}
|
|
228
|
+
this.running.clear();
|
|
229
|
+
this.phase = 'ended';
|
|
230
|
+
this.success = success && this.counts.failed === 0 && this.runErrors.length === 0 && this.fileErrors.size === 0;
|
|
231
|
+
this.interrupted = interrupted;
|
|
232
|
+
this.endedAt = this.now();
|
|
233
|
+
}
|
|
234
|
+
}
|
package/src/session.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { randomUUID } from 'node:crypto';
|
|
7
7
|
|
|
8
|
-
import type { AgentConfig, Platform, TrackerState, TreeNode } from '@quiesco/protocol';
|
|
8
|
+
import type { AgentConfig, AgentMethods, Platform, ResultOf, TrackerState, TreeNode } from '@quiesco/protocol';
|
|
9
9
|
|
|
10
10
|
import type { QuiescoConfig } from './config.js';
|
|
11
11
|
import { DEFAULT_CONNECT_TIMEOUT_MS, ignoredAppErrors, serverPort, toAgentConfig } from './config.js';
|
|
@@ -16,6 +16,7 @@ import type { DeviceProvider, LaunchParams, LaunchUrlHandling, WorkerInfo } from
|
|
|
16
16
|
import { DeviceApi } from './device.js';
|
|
17
17
|
import type { AppRunState, DeviceHost } from './device.js';
|
|
18
18
|
import { Artifacts } from './artifacts.js';
|
|
19
|
+
import { describeBundle, staleBundleWarning } from './bundle-check.js';
|
|
19
20
|
import type { Attachment, ScenarioArtifacts } from './artifacts.js';
|
|
20
21
|
import { Driver } from './driver.js';
|
|
21
22
|
import { failureRects, highlight } from './screenshots.js';
|
|
@@ -62,6 +63,8 @@ export interface SessionOptions {
|
|
|
62
63
|
export interface Evidence {
|
|
63
64
|
screenshot?: Buffer;
|
|
64
65
|
tree?: TreeNode | null;
|
|
66
|
+
/** The condensed element list and the route (issue #2). */
|
|
67
|
+
elements?: ResultOf<AgentMethods['listElements']>;
|
|
65
68
|
idle?: TrackerState[];
|
|
66
69
|
/** Why a piece could not be collected. */
|
|
67
70
|
errors: string[];
|
|
@@ -214,10 +217,26 @@ export class Session {
|
|
|
214
217
|
this.appState = 'terminated';
|
|
215
218
|
await this.launch();
|
|
216
219
|
this.appState = 'running';
|
|
217
|
-
|
|
220
|
+
} else {
|
|
221
|
+
if (this.appState === 'background') await this.device.bringToForeground();
|
|
222
|
+
await this.driver.reset();
|
|
223
|
+
}
|
|
224
|
+
await this.expectInitialState();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** `reset.expectVisible`: the reset must leave that element visible (issue #9). */
|
|
228
|
+
private async expectInitialState(): Promise<void> {
|
|
229
|
+
const target = this.config.reset?.expectVisible;
|
|
230
|
+
if (target === undefined) return;
|
|
231
|
+
try {
|
|
232
|
+
await this.driver.expectVisible(target);
|
|
233
|
+
} catch (e) {
|
|
234
|
+
// A lost connection is not about the app's state: beforeScenario starts the app again for that.
|
|
235
|
+
if (!(e instanceof QuiescoError) || RESTART_ON.has(e.errorName)) throw e;
|
|
236
|
+
const route = await this.driver.currentRoute();
|
|
237
|
+
const message = `the app is not back in its initial state${route !== null ? ` (current route: ${route})` : ''}: ${e.message} (reset.expectVisible in quiesco.config)`;
|
|
238
|
+
throw new QuiescoError('RESET_FAILED', `Reset failed at expectVisible: ${message}`, { step: 'expectVisible', message });
|
|
218
239
|
}
|
|
219
|
-
if (this.appState === 'background') await this.device.bringToForeground();
|
|
220
|
-
await this.driver.reset();
|
|
221
240
|
}
|
|
222
241
|
|
|
223
242
|
/**
|
|
@@ -238,6 +257,7 @@ export class Session {
|
|
|
238
257
|
if (result.failed) {
|
|
239
258
|
const ev = await this.collectEvidence();
|
|
240
259
|
if (ev.screenshot !== undefined) evidence.push({ data: ev.screenshot, mediaType: 'image/png', fileName: 'failure.png' });
|
|
260
|
+
if (ev.elements !== undefined) evidence.push({ data: JSON.stringify(ev.elements, null, 2), mediaType: 'application/json', fileName: 'elements.json' });
|
|
241
261
|
if (ev.tree !== undefined) evidence.push({ data: JSON.stringify({ elementTree: ev.tree }, null, 2), mediaType: 'application/json', fileName: 'element-tree.json' });
|
|
242
262
|
if (ev.idle !== undefined) evidence.push({ data: JSON.stringify({ idleState: ev.idle }, null, 2), mediaType: 'application/json', fileName: 'idle-state.json' });
|
|
243
263
|
if (ev.errors.length > 0) evidence.push({ data: `Quiesco could not collect: ${ev.errors.join('; ')}`, mediaType: 'text/plain', fileName: 'evidence-errors.txt' });
|
|
@@ -257,6 +277,13 @@ export class Session {
|
|
|
257
277
|
(t) => (ev.tree = t),
|
|
258
278
|
(e: unknown) => ev.errors.push(`element tree: ${(e as Error).message}`),
|
|
259
279
|
),
|
|
280
|
+
this.driver.listElements().then(
|
|
281
|
+
(l) => (ev.elements = l),
|
|
282
|
+
// An agent older than this host does not have it: not worth an error line.
|
|
283
|
+
(e: unknown) => {
|
|
284
|
+
if (!(e instanceof QuiescoError && e.code === -32601)) ev.errors.push(`element list: ${(e as Error).message}`);
|
|
285
|
+
},
|
|
286
|
+
),
|
|
260
287
|
this.driver.getIdleState().then(
|
|
261
288
|
(s) => (ev.idle = s),
|
|
262
289
|
(e: unknown) => ev.errors.push(`idle state: ${(e as Error).message}`),
|
|
@@ -293,6 +320,21 @@ export class Session {
|
|
|
293
320
|
await this.provider.shutdown().catch((e: unknown) => this.logger.warn(`device shutdown failed: ${(e as Error).message}`));
|
|
294
321
|
}
|
|
295
322
|
|
|
323
|
+
/** Where the project's sources are, for the stale-bundle check: the test runner runs in the project root. */
|
|
324
|
+
private readonly projectRoot = process.cwd();
|
|
325
|
+
/** Bundles already checked (by build time): a reconnect of the same bundle is not checked again. */
|
|
326
|
+
private readonly checkedBundles = new Set<number>();
|
|
327
|
+
|
|
328
|
+
/** Issue #3: say when the app's JS bundle cannot have the project's latest changes. Once per bundle. */
|
|
329
|
+
private checkBundle(conn: AgentConnection): void {
|
|
330
|
+
const bundle = conn.info.bundle;
|
|
331
|
+
this.logger.debug(`app JS: ${describeBundle(bundle)}`);
|
|
332
|
+
if (bundle === undefined || this.checkedBundles.has(bundle.builtAt)) return;
|
|
333
|
+
this.checkedBundles.add(bundle.builtAt);
|
|
334
|
+
const warning = staleBundleWarning(bundle, this.projectRoot);
|
|
335
|
+
if (warning !== null) this.logger.warn(warning);
|
|
336
|
+
}
|
|
337
|
+
|
|
296
338
|
/**
|
|
297
339
|
* The open connection; after a disconnect (web page reload, app restart) waits for the app to come
|
|
298
340
|
* back within the connect timeout.
|
|
@@ -314,6 +356,7 @@ export class Session {
|
|
|
314
356
|
private adopt(conn: AgentConnection): void {
|
|
315
357
|
if (this.connection === conn) return;
|
|
316
358
|
this.connection = conn;
|
|
359
|
+
this.checkBundle(conn);
|
|
317
360
|
conn.on('log', (l) => this.logger[l.level](`[app] ${l.message}`));
|
|
318
361
|
this.driver.attach(conn);
|
|
319
362
|
}
|