@quiesco/core 0.2.0 → 0.4.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.
@@ -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
@@ -16,7 +16,8 @@ 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
+ import { REUSED_DEV_SERVER_ENV, describeBundle, metroConfigUsesQuiesco, staleBundleWarning, unknownBundleWarning } from './bundle-check.js';
20
+ import type { BundleContext } from './bundle-check.js';
20
21
  import type { Attachment, ScenarioArtifacts } from './artifacts.js';
21
22
  import { Driver } from './driver.js';
22
23
  import { failureRects, highlight } from './screenshots.js';
@@ -125,6 +126,11 @@ export class Session {
125
126
  private offline = false;
126
127
  private readonly defaultArgs: LaunchArgs;
127
128
 
129
+ /** The platform under test (issue #22). */
130
+ get platform(): Platform {
131
+ return this.provider.platform;
132
+ }
133
+
128
134
  private constructor(
129
135
  readonly config: QuiescoConfig,
130
136
  readonly provider: DeviceProvider,
@@ -324,12 +330,24 @@ export class Session {
324
330
  private readonly projectRoot = process.cwd();
325
331
  /** Bundles already checked (by build time): a reconnect of the same bundle is not checked again. */
326
332
  private readonly checkedBundles = new Set<number>();
333
+ /** The unknown-bundle warning (issue #19) is given once per session, not on every reconnect. */
334
+ private unknownBundleWarned = false;
327
335
 
328
336
  /** Issue #3: say when the app's JS bundle cannot have the project's latest changes. Once per bundle. */
329
337
  private checkBundle(conn: AgentConnection): void {
330
338
  const bundle = conn.info.bundle;
331
- this.logger.debug(`app JS: ${describeBundle(bundle)}`);
332
- if (bundle === undefined || this.checkedBundles.has(bundle.builtAt)) return;
339
+ const reused = process.env[REUSED_DEV_SERVER_ENV];
340
+ const context: BundleContext = { metroConfigUsesQuiesco: metroConfigUsesQuiesco(this.projectRoot), ...(reused !== undefined && reused !== '' ? { reusedDevServer: reused } : {}) };
341
+ this.logger.debug(`app JS: ${describeBundle(bundle, context)}`);
342
+ if (bundle === undefined) {
343
+ const warning = this.unknownBundleWarned ? null : unknownBundleWarning(bundle, context);
344
+ if (warning !== null) {
345
+ this.unknownBundleWarned = true;
346
+ this.logger.warn(warning);
347
+ }
348
+ return;
349
+ }
350
+ if (this.checkedBundles.has(bundle.builtAt)) return;
333
351
  this.checkedBundles.add(bundle.builtAt);
334
352
  const warning = staleBundleWarning(bundle, this.projectRoot);
335
353
  if (warning !== null) this.logger.warn(warning);