@app-studio/qa-browser-darwin-arm64 0.10.4 → 0.11.2

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/bin/lightpanda CHANGED
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/bin/lightpanda
3
- size: 89858904 bytes
4
- sha256: e245750f93f2cc10e87c8ea10fdc316d3af9a0fce889423aaabee5e2c18e44b5
3
+ size: 92614952 bytes
4
+ sha256: 901f6b5c618e57a0a5d7e43dfb745146644936664810d5021756487963aa2299
@@ -3,7 +3,7 @@
3
3
  "engine": "lightpanda",
4
4
  "platform": "darwin",
5
5
  "arch": "arm64",
6
- "version": "1.1.0-dev.10263+c5eb73e72",
6
+ "version": "1.1.0-dev.8814+46c56fa55",
7
7
  "rendering": "native-css",
8
8
  "formats": [
9
9
  "png",
@@ -13,13 +13,15 @@
13
13
  ],
14
14
  "build": "release-fast",
15
15
  "source": {
16
- "commit": "c5eb73e728f45a3e19a013cfe799bb8f39c3dfbd",
16
+ "commit": "46c56fa55fdb48b7fa30d83461c328c8a3635940",
17
17
  "dirty": false
18
18
  },
19
19
  "files": {
20
- "lightpanda": "e245750f93f2cc10e87c8ea10fdc316d3af9a0fce889423aaabee5e2c18e44b5",
21
- "lightpanda-capture.mjs": "4674247178fe8cb209f11984b770d612b7c86bfc5140568c66cc8cc6fa406a9f",
20
+ "lightpanda": "901f6b5c618e57a0a5d7e43dfb745146644936664810d5021756487963aa2299",
21
+ "lightpanda-capture.mjs": "0e2511970a1552e0ff470ba1e5ed95da0ad75f2d121d6b659b41be552e7462a9",
22
+ "lightpanda-scenario.mjs": "ad41177bd602e93f2d12c0e30444ebf06e957e1215f67179defc8cc68e6839e6",
23
+ "native-scenario.mjs": "b3efd2d0ad9eccff29f6d432e4eaa7eb9b2696c999577eed98f6fabc282a42b8",
22
24
  "LICENSE": "8486a10c4393cee1c25392769ddd3b2d6c242d6ec7928e1414efff7dfb2f07ef",
23
- "source.tar.gz": "059e9dea914094ff94c8cf1800d7e7edd6eee2f356ab919906bc1f6a0843cc67"
25
+ "source.tar.gz": "5493bde608e1c19cfcaed532eaa8516491220c18a0412e122e4e982c1e602484"
24
26
  }
25
27
  }
@@ -2,7 +2,8 @@
2
2
  // SPDX-License-Identifier: AGPL-3.0-or-later
3
3
 
4
4
  import { spawn } from 'node:child_process';
5
- import { mkdir, mkdtemp, link, rm, writeFile } from 'node:fs/promises';
5
+ import { existsSync, realpathSync } from 'node:fs';
6
+ import { mkdir, mkdtemp, link, open, rm, writeFile } from 'node:fs/promises';
6
7
  import { dirname, extname, join, resolve } from 'node:path';
7
8
  import { performance } from 'node:perf_hooks';
8
9
  import { pathToFileURL } from 'node:url';
@@ -15,19 +16,23 @@ const sleep = (ms, signal) => new Promise(resolve => {
15
16
  signal?.addEventListener('abort', done, { once: true });
16
17
  });
17
18
 
18
- async function bounded(task, milliseconds, operation) {
19
- let timer;
19
+ async function bounded(task, milliseconds, operation, signal) {
20
+ let timer, abort;
20
21
  try {
21
22
  return await Promise.race([task, new Promise((_, reject) => {
22
23
  timer = setTimeout(() => reject(new Error(`${operation} timed out after ${milliseconds}ms`)), milliseconds);
24
+ abort = () => reject(signal.reason ?? new Error(`${operation} aborted`));
25
+ if (signal?.aborted) abort();
26
+ else signal?.addEventListener('abort', abort, { once: true });
23
27
  })]);
24
- } finally { clearTimeout(timer); }
28
+ } finally { clearTimeout(timer); signal?.removeEventListener('abort', abort); }
25
29
  }
26
30
 
27
- async function capture(cdp, fullPage = false) {
31
+ async function capture(cdp, fullPage = false, timeout = 3000, signal) {
32
+ signal?.throwIfAborted();
28
33
  const { data } = await bounded(cdp.send('Page.captureScreenshot', {
29
34
  format: 'png', captureBeyondViewport: fullPage,
30
- }), 3000, 'Native frame capture');
35
+ }), timeout, 'Native frame capture', signal);
31
36
  const png = Buffer.from(data, 'base64');
32
37
  if (png.length < 24 || !png.subarray(0, 8).equals(Buffer.from('89504e470d0a1a0a', 'hex'))) {
33
38
  throw new Error('The browser returned an invalid PNG');
@@ -42,6 +47,59 @@ export async function screenshot(cdp, { path, fullPage = true }) {
42
47
  await writeFile(path, png, { flag: 'wx' });
43
48
  }
44
49
 
50
+ function encoding(extension) {
51
+ // Per-frame palettes stream without buffering the whole GIF or dithering UI colors.
52
+ return extension === '.gif'
53
+ ? ['-filter_complex', '[0:v]split[frames][palette];[palette]palettegen=stats_mode=single[colors];[frames][colors]paletteuse=new=1:dither=none', '-c:v', 'gif', '-loop', '0']
54
+ : ['-vf', 'pad=ceil(iw/2)*2:ceil(ih/2)*2', ...(extension === '.mp4'
55
+ ? ['-c:v', 'libx264', '-preset', 'veryfast', '-crf', '23', '-movflags', '+faststart']
56
+ : ['-c:v', 'libvpx-vp9', '-deadline', 'realtime', '-cpu-used', '8', '-crf', '32', '-b:v', '0']), '-pix_fmt', 'yuv420p'];
57
+ }
58
+
59
+ /** Encode bounded native tool checkpoints without replaying or waiting in real time. */
60
+ export async function encodeFrames({ directory, count, path, fps = 5, ffmpeg = 'ffmpeg', signal }) {
61
+ const extension = extname(path ?? '').toLowerCase();
62
+ if (!['.mp4', '.webm', '.gif'].includes(extension)) throw new Error('Recording path must end in .mp4, .webm or .gif');
63
+ if (!Number.isInteger(count) || count < 1 || count > 128) throw new Error('Checkpoint count must be between 1 and 128');
64
+ if (!Number.isFinite(fps) || fps < 1 || fps > 60) throw new Error('fps must be between 1 and 60');
65
+ signal?.throwIfAborted();
66
+ let first;
67
+ for (let index = 1; index <= count; index++) {
68
+ const file = await open(join(directory, `${String(index).padStart(6, '0')}.png`));
69
+ const png = Buffer.alloc(24);
70
+ try {
71
+ const { bytesRead } = await file.read(png, 0, 24, 0);
72
+ if (bytesRead < 24 || png.subarray(0, 8).toString('hex') !== '89504e470d0a1a0a') throw new Error('Invalid checkpoint PNG');
73
+ } finally { await file.close(); }
74
+ first ??= png;
75
+ if (!png.subarray(16, 24).equals(first.subarray(16, 24))) throw new Error('The viewport size changed during recording');
76
+ signal?.throwIfAborted();
77
+ }
78
+ await mkdir(dirname(resolve(path)), { recursive: true });
79
+ const temporary = await mkdtemp(join(dirname(resolve(path)), '.lightpanda-video-'));
80
+ const output = join(temporary, `video${extension}`);
81
+ const encoder = spawn(ffmpeg, ['-hide_banner', '-loglevel', 'error', '-nostdin', '-xerror', '-y', '-framerate', String(fps), '-start_number', '1', '-i', '%06d.png', '-frames:v', String(count), '-an', ...encoding(extension), output], { cwd: resolve(directory), stdio: ['ignore', 'ignore', 'pipe'] });
82
+ let diagnostics = '', spawnError;
83
+ encoder.stderr.on('data', chunk => { diagnostics = (diagnostics + chunk).slice(-8192); });
84
+ const closed = new Promise(resolve => { encoder.once('error', error => { spawnError = error; }); encoder.once('close', resolve); });
85
+ const cancel = () => encoder.kill('SIGKILL');
86
+ signal?.addEventListener('abort', cancel, { once: true });
87
+ try {
88
+ const code = await bounded(closed, 30000, 'Checkpoint encoding');
89
+ signal?.throwIfAborted();
90
+ if (code !== 0) throw new Error(spawnError ? `Cannot start FFmpeg: ${spawnError.message}` : `FFmpeg exited with ${code}: ${diagnostics.trim()}`);
91
+ await link(output, resolve(path));
92
+ return { frames: count, duration: count / fps, width: first.readUInt32BE(16), height: first.readUInt32BE(20), mode: 'tool-checkpoints' };
93
+ } finally {
94
+ signal?.removeEventListener('abort', cancel);
95
+ if (encoder.exitCode === null && encoder.signalCode === null) {
96
+ encoder.kill('SIGKILL');
97
+ await bounded(closed, 1000, 'FFmpeg shutdown').catch(() => encoder.unref());
98
+ }
99
+ await rm(temporary, { recursive: true, force: true });
100
+ }
101
+ }
102
+
45
103
  /**
46
104
  * Sample the native viewport into MP4, WebM or GIF. Only one CDP capture and one
47
105
  * encoder write run at a time. Missed intervals hold the preceding image,
@@ -57,93 +115,149 @@ export async function recordVideo(cdp, { path, fps = 10, duration = 10, ffmpeg =
57
115
  const extension = extname(path ?? '').toLowerCase();
58
116
  if (!['.mp4', '.webm', '.gif'].includes(extension)) throw new Error('Recording path must end in .mp4, .webm or .gif');
59
117
  signal?.throwIfAborted();
60
- const first = await capture(cdp);
61
- const width = first.readUInt32BE(16);
62
- const height = first.readUInt32BE(20);
63
- await mkdir(dirname(resolve(path)), { recursive: true });
64
- const temporary = await mkdtemp(join(dirname(resolve(path)), '.lightpanda-video-'));
65
- const output = join(temporary, `video${extension}`);
66
- // Per-frame palettes keep GIF encoding streaming; a whole-clip palette
67
- // would buffer frames until recording ends. Avoid dithering flat UI colors,
68
- // so subtle differences between card backgrounds and borders stay intact.
69
- // GIF retains odd dimensions.
70
- const encoding = extension === '.gif'
71
- ? ['-filter_complex', '[0:v]split[frames][palette];[palette]palettegen=stats_mode=single[colors];[frames][colors]paletteuse=new=1:dither=none', '-c:v', 'gif', '-loop', '0']
72
- : ['-vf', 'pad=ceil(iw/2)*2:ceil(ih/2)*2', ...(extension === '.mp4'
73
- ? ['-c:v', 'libx264', '-preset', 'veryfast', '-crf', '23', '-movflags', '+faststart']
74
- : ['-c:v', 'libvpx-vp9', '-deadline', 'realtime', '-cpu-used', '8', '-crf', '32', '-b:v', '0']), '-pix_fmt', 'yuv420p'];
75
- const encoder = spawn(ffmpeg, [
76
- '-hide_banner', '-loglevel', 'error', '-nostdin', '-y',
77
- '-f', 'image2pipe', '-framerate', String(fps), '-vcodec', 'png', '-i', 'pipe:0',
78
- '-an', ...encoding, output,
79
- ], { stdio: ['pipe', 'ignore', 'pipe'] });
80
- let diagnostics = '';
81
- encoder.stderr.on('data', chunk => { diagnostics = (diagnostics + chunk.toString()).slice(-8192); });
82
- // Observe errors immediately, even when FFmpeg fails between captures.
83
- encoder.stdin.on('error', () => {});
84
- let spawnError;
85
- const closed = new Promise(resolve => {
86
- encoder.once('error', error => { spawnError = error; });
87
- encoder.once('close', code => resolve(code));
88
- });
89
- const write = png => bounded(new Promise((resolve, reject) =>
90
- encoder.stdin.write(png, error => error ? reject(error) : resolve())
91
- ), 2000, 'FFmpeg input');
92
- const terminate = async () => {
93
- encoder.stdin.destroy();
94
- if (encoder.exitCode === null && encoder.signalCode === null) encoder.kill('SIGTERM');
95
- try { await bounded(closed, 1000, 'FFmpeg shutdown'); }
96
- catch {
97
- encoder.kill('SIGKILL');
98
- await bounded(closed, 1000, 'FFmpeg forced shutdown').catch(() => {
99
- // Do not retain Node handles if the OS has not reaped the killed child.
100
- encoder.stderr.destroy();
101
- encoder.unref();
102
- });
103
- }
104
- };
105
118
  const started = performance.now();
106
- const period = 1000 / fps;
107
- const total = Math.ceil(duration * fps);
108
- let frames = 0;
109
- let previous = first;
110
- const sample = async () => {
111
- const png = await capture(cdp);
112
- if (png.readUInt32BE(16) !== width || png.readUInt32BE(20) !== height) {
113
- throw new Error('The viewport size changed during recording');
114
- }
115
- return png;
119
+ const initialDeadline = started + 30_000;
120
+ if (typeof cdp.on !== 'function' || typeof cdp.off !== 'function') {
121
+ throw new Error('Recording needs CDP notifications from the same page session');
122
+ }
123
+ let frameId, loaderId, frameUrl, revision = 0, ready = false, observing = false, initial = true;
124
+ const initialStop = new AbortController();
125
+ const abortInitial = () => initialStop.abort(signal.reason);
126
+ const detached = () => initialStop.abort(new Error('Recording CDP session detached'));
127
+ const navigated = ({ frame }) => {
128
+ if (!frame || frame.parentId) return;
129
+ frameId = frame.id; loaderId = frame.loaderId; frameUrl = frame.url;
130
+ revision++; ready = false;
131
+ };
132
+ const hasDocument = () => frameUrl && !/^about:blank(?:[?#]|$)/.test(frameUrl);
133
+ const lifecycle = event => {
134
+ // Enabling lifecycle notifications can replay old milestones, even for a
135
+ // waiting about:blank frame. Only matching, observed main-document events count.
136
+ if (observing && hasDocument() && event.name === 'DOMContentLoaded'
137
+ && event.frameId === frameId && event.loaderId === loaderId) ready = true;
138
+ };
139
+ cdp.on('Page.frameNavigated', navigated);
140
+ cdp.on('Page.lifecycleEvent', lifecycle);
141
+ cdp.on('Inspector.detached', detached);
142
+ signal?.addEventListener('abort', abortInitial, { once: true });
143
+ const remaining = () => {
144
+ const ms = initialDeadline - performance.now();
145
+ if (ms <= 0) throw new Error('Initial document capture timed out after 30000ms');
146
+ return ms;
147
+ };
148
+ const command = (method, params) => {
149
+ initialStop.signal.throwIfAborted();
150
+ return bounded(cdp.send(method, params), Math.min(3000, remaining()), `Recording ${method}`, initialStop.signal);
116
151
  };
117
152
  try {
118
- while (frames < total && (frames === 0 || !signal?.aborted)) {
119
- await write(previous);
153
+ // Resolve identity/readiness before the first PNG so an already-loaded
154
+ // document enters steady mode immediately on its first successful capture.
155
+ // These commands do not await a future navigation or move the film clock.
156
+ await command('Page.enable');
157
+ await command('Page.setLifecycleEventsEnabled', { enabled: true });
158
+ const before = revision;
159
+ const { frameTree } = await command('Page.getFrameTree');
160
+ if (revision === before && frameTree?.frame) {
161
+ const frame = frameTree.frame;
162
+ frameId = frame.id; loaderId = frame.loaderId; frameUrl = frame.url;
163
+ }
164
+ observing = true;
165
+ const current = revision;
166
+ const state = await command('Runtime.evaluate', { expression: 'document.readyState', returnByValue: true });
167
+ if (revision === current && hasDocument()
168
+ && ['interactive', 'complete'].includes(state.result?.value)) ready = true;
169
+ const firstRevision = revision, firstReady = ready;
170
+ const first = await capture(cdp, false, remaining(), initialStop.signal);
171
+ if (firstReady && revision === firstRevision) initial = false;
172
+ const width = first.readUInt32BE(16);
173
+ const height = first.readUInt32BE(20);
174
+ await mkdir(dirname(resolve(path)), { recursive: true });
175
+ const temporary = await mkdtemp(join(dirname(resolve(path)), '.lightpanda-video-'));
176
+ const output = join(temporary, `video${extension}`);
177
+ const encoder = spawn(ffmpeg, [
178
+ '-hide_banner', '-loglevel', 'error', '-nostdin', '-y',
179
+ '-f', 'image2pipe', '-framerate', String(fps), '-vcodec', 'png', '-i', 'pipe:0',
180
+ '-an', ...encoding(extension), output,
181
+ ], { stdio: ['pipe', 'ignore', 'pipe'] });
182
+ let diagnostics = '';
183
+ encoder.stderr.on('data', chunk => { diagnostics = (diagnostics + chunk.toString()).slice(-8192); });
184
+ // Observe errors immediately, even when FFmpeg fails between captures.
185
+ encoder.stdin.on('error', () => {});
186
+ let spawnError;
187
+ const closed = new Promise(resolve => {
188
+ encoder.once('error', error => { spawnError = error; });
189
+ encoder.once('close', code => resolve(code));
190
+ });
191
+ const write = png => bounded(new Promise((resolve, reject) =>
192
+ encoder.stdin.write(png, error => error ? reject(error) : resolve())
193
+ ), 2000, 'FFmpeg input');
194
+ const terminate = async () => {
195
+ encoder.stdin.destroy();
196
+ if (encoder.exitCode === null && encoder.signalCode === null) encoder.kill('SIGTERM');
197
+ try { await bounded(closed, 1000, 'FFmpeg shutdown'); }
198
+ catch {
199
+ encoder.kill('SIGKILL');
200
+ await bounded(closed, 1000, 'FFmpeg forced shutdown').catch(() => {
201
+ // Do not retain Node handles if the OS has not reaped the killed child.
202
+ encoder.stderr.destroy();
203
+ encoder.unref();
204
+ });
205
+ }
206
+ };
207
+ const period = 1000 / fps;
208
+ const total = Math.ceil(duration * fps);
209
+ let frames = 0;
210
+ let previous = first;
211
+ const sample = async () => {
212
+ const generation = revision;
213
+ const mainReady = ready;
214
+ const png = await capture(cdp, false, initial ? remaining() : 3000, initial ? initialStop.signal : undefined);
215
+ // A capture already in flight at DCL keeps its original deadline. Require a
216
+ // successful capture begun after that milestone before entering steady mode.
217
+ if (initial && mainReady && generation === revision) initial = false;
218
+ if (png.readUInt32BE(16) !== width || png.readUInt32BE(20) !== height) {
219
+ throw new Error('The viewport size changed during recording');
220
+ }
221
+ return png;
222
+ };
223
+ try {
224
+ while (frames < total && (frames === 0 || !signal?.aborted)) {
225
+ await write(previous);
226
+ frames++;
227
+ if (frames === total) break;
228
+ await sleep(started + frames * period - performance.now(), signal);
229
+ if (signal?.aborted) break;
230
+ const current = await sample();
231
+ const due = Math.min(total, Math.floor((performance.now() - started) / period));
232
+ // Repeat the previous sample for intervals spent capturing/encoding.
233
+ while (frames < due && !signal?.aborted) { await write(previous); frames++; }
234
+ previous = current;
235
+ }
236
+ // Abort may arrive between low-FPS samples or during an in-flight capture.
237
+ // Capture after sampling stops, while the caller still owns the open page;
238
+ // serialize this final sample through the same bounded encoder write.
239
+ await write(await sample());
120
240
  frames++;
121
- if (frames === total) break;
122
- await sleep(started + frames * period - performance.now(), signal);
123
- if (signal?.aborted) break;
124
- const current = await sample();
125
- const due = Math.min(total, Math.floor((performance.now() - started) / period));
126
- // Repeat the previous sample for intervals spent capturing/encoding.
127
- while (frames < due && !signal?.aborted) { await write(previous); frames++; }
128
- previous = current;
241
+ if (initial) throw new Error('Recording ended before the first main document capture');
242
+ encoder.stdin.end();
243
+ const code = await bounded(closed, 5000, 'FFmpeg finalization');
244
+ if (code !== 0) throw new Error(spawnError?.message ?? `FFmpeg exited with ${code}: ${diagnostics.trim()}`);
245
+ // Publish only a finalized file. link() fails if the destination exists.
246
+ await link(output, resolve(path));
247
+ return { frames, duration: frames / fps, width, height };
248
+ } catch (error) {
249
+ await terminate();
250
+ if (spawnError) throw new Error(`Cannot start FFmpeg: ${spawnError.message}`, { cause: error });
251
+ throw error;
252
+ } finally {
253
+ await rm(temporary, { recursive: true, force: true });
129
254
  }
130
- // Abort may arrive between low-FPS samples or during an in-flight capture.
131
- // Capture after sampling stops, while the caller still owns the open page;
132
- // serialize this final sample through the same bounded encoder write.
133
- await write(await sample());
134
- frames++;
135
- encoder.stdin.end();
136
- const code = await bounded(closed, 5000, 'FFmpeg finalization');
137
- if (code !== 0) throw new Error(spawnError?.message ?? `FFmpeg exited with ${code}: ${diagnostics.trim()}`);
138
- // Publish only a finalized file. link() fails if the destination exists.
139
- await link(output, resolve(path));
140
- return { frames, duration: frames / fps, width, height };
141
- } catch (error) {
142
- await terminate();
143
- if (spawnError) throw new Error(`Cannot start FFmpeg: ${spawnError.message}`, { cause: error });
144
- throw error;
145
255
  } finally {
146
- await rm(temporary, { recursive: true, force: true });
256
+ initialStop.abort();
257
+ signal?.removeEventListener('abort', abortInitial);
258
+ cdp.off('Page.frameNavigated', navigated);
259
+ cdp.off('Page.lifecycleEvent', lifecycle);
260
+ cdp.off('Inspector.detached', detached);
147
261
  }
148
262
  }
149
263
 
@@ -151,16 +265,26 @@ export async function recordVideo(cdp, { path, fps = 10, duration = 10, ffmpeg =
151
265
  export async function connect(endpoint, timeout = 30000) {
152
266
  const socket = new WebSocket(endpoint);
153
267
  const pending = new Map();
268
+ const listeners = new Map();
154
269
  let sequence = 0;
155
270
  const fail = error => {
156
271
  for (const { reject, timer } of pending.values()) { clearTimeout(timer); reject(error); }
157
272
  pending.clear();
273
+ for (const subscription of listeners.values()) {
274
+ if (subscription.method === 'Inspector.detached') for (const listener of subscription.callbacks) listener({});
275
+ }
276
+ listeners.clear();
158
277
  };
159
278
  socket.addEventListener('close', () => fail(new Error('CDP connection closed')));
160
279
  socket.addEventListener('error', () => fail(new Error('CDP connection failed')));
161
280
  socket.addEventListener('message', event => {
162
281
  let message;
163
282
  try { message = JSON.parse(event.data); } catch { return; }
283
+ if (message.method) {
284
+ const subscription = listeners.get(JSON.stringify([message.sessionId, message.method]));
285
+ if (subscription) for (const listener of [...subscription.callbacks]) listener(message.params ?? {});
286
+ return;
287
+ }
164
288
  const request = pending.get(message.id);
165
289
  if (!request) return;
166
290
  clearTimeout(request.timer);
@@ -186,6 +310,18 @@ export async function connect(endpoint, timeout = 30000) {
186
310
  catch (error) { clearTimeout(timer); pending.delete(id); reject(error); }
187
311
  });
188
312
  },
313
+ on(method, listener, sessionId) {
314
+ const key = JSON.stringify([sessionId, method]);
315
+ let subscription = listeners.get(key);
316
+ if (!subscription) listeners.set(key, subscription = { method, callbacks: new Set() });
317
+ subscription.callbacks.add(listener);
318
+ },
319
+ off(method, listener, sessionId) {
320
+ const key = JSON.stringify([sessionId, method]);
321
+ const subscription = listeners.get(key);
322
+ subscription?.callbacks.delete(listener);
323
+ if (subscription?.callbacks.size === 0) listeners.delete(key);
324
+ },
189
325
  close() { fail(new Error('CDP connection closed')); socket.close(); },
190
326
  };
191
327
  }
@@ -216,7 +352,11 @@ async function main() {
216
352
  try {
217
353
  ({ targetId } = await connection.send('Target.createTarget', { url: 'about:blank' }));
218
354
  const { sessionId } = await connection.send('Target.attachToTarget', { targetId, flatten: true });
219
- const cdp = { send: (method, params) => connection.send(method, params, sessionId) };
355
+ const cdp = {
356
+ send: (method, params) => connection.send(method, params, sessionId),
357
+ on: (method, listener) => connection.on(method, listener, sessionId),
358
+ off: (method, listener) => connection.off(method, listener, sessionId),
359
+ };
220
360
  await cdp.send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: false });
221
361
  await cdp.send('Page.enable');
222
362
  const navigation = await cdp.send('Page.navigate', { url: values.url });
@@ -244,6 +384,6 @@ async function main() {
244
384
  }
245
385
  }
246
386
 
247
- if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
387
+ if (process.argv[1] && existsSync(process.argv[1]) && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
248
388
  main().catch(error => { console.error(error.message); process.exitCode = 1; });
249
389
  }
@@ -0,0 +1,249 @@
1
+ // Copyright (C) 2026 Lightpanda (Selecy SAS)
2
+ // SPDX-License-Identifier: AGPL-3.0-or-later
3
+
4
+ import { existsSync, realpathSync } from 'node:fs';
5
+ import { mkdir, readFile, readdir, stat, writeFile } from 'node:fs/promises';
6
+ import { createRequire } from 'node:module';
7
+ import { dirname, join, resolve } from 'node:path';
8
+ import { setTimeout as delay } from 'node:timers/promises';
9
+ import { fileURLToPath, pathToFileURL } from 'node:url';
10
+ import { parseArgs } from 'node:util';
11
+
12
+ const formats = ['mp4', 'webm', 'gif'];
13
+ const message = error => error instanceof Error ? error.message : String(error);
14
+ const escape = value => String(value ?? '').replace(/[&<>"']/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[c]);
15
+ const href = path => `./${path.split('/').map(encodeURIComponent).join('/')}`;
16
+
17
+ export function clean(value) {
18
+ let text = String(value ?? '');
19
+ for (const [name, secret] of Object.entries(process.env).filter(([name, value]) => name.startsWith('LP_') && value).sort((a, b) => b[1].length - a[1].length)) {
20
+ text = text.replaceAll(secret, `$${name}`);
21
+ }
22
+ return text.replace(/https?:\/\/[^\s<>"']+/g, value => {
23
+ try { const u = new URL(value); u.username = ''; u.password = ''; u.search = ''; u.hash = ''; return u.href; }
24
+ catch { return '[invalid URL]'; }
25
+ }).replace(/((?:authorization|password|secret|token|api[_-]?key)\s*[:=]\s*)[^\s,;]+/gi, '$1[redacted]')
26
+ .replace(/Bearer\s+[^\s,;]+/gi, 'Bearer [redacted]').slice(0, 512);
27
+ }
28
+
29
+ function matchesPath(actual, wanted) {
30
+ const parts = path => path.split(/[?#]/)[0].split('/').filter(Boolean);
31
+ const a = parts(actual), b = parts(wanted);
32
+ return a.length === b.length && b.every((part, i) => part.startsWith(':') || part === a[i]);
33
+ }
34
+
35
+ /** A passed UI assertion must not hide an unexpected application/API failure. */
36
+ export function assessReport(report, diagnostics = {}, { requireAssertions = true } = {}) {
37
+ const issues = new Set();
38
+ if (report.status !== 'passed') issues.add(clean(report.message ?? 'Scenario assertions failed'));
39
+ const assertions = (report.steps ?? []).flatMap(step => step.assertions ?? []);
40
+ if (requireAssertions && !assertions.length) issues.add('No assertions were executed');
41
+ for (const step of report.steps ?? []) if (step.status === 'failed') issues.add(clean(`${step.stepId}: ${step.message ?? 'failed'}`));
42
+ for (const error of [...(report.consoleErrors ?? []), ...(report.renderErrors ?? [])]) issues.add(clean(error));
43
+ const expected = assertions.filter(a => a.status === 'passed' && a.assertion?.type === 'request-made' && a.assertion.request?.status >= 400 && a.assertion.request.count !== 0)
44
+ .map(a => a.assertion.request);
45
+ const allowed = request => expected.some(wanted => request.status === wanted.status && (!wanted.method || wanted.method === request.method) && matchesPath(request.path, wanted.path));
46
+ for (const request of report.requests ?? []) {
47
+ if (request.status >= 400 && !allowed(request)) issues.add(clean(`${request.method} ${request.path}: HTTP ${request.status}`));
48
+ }
49
+ for (const record of diagnostics.records ?? []) {
50
+ if (['pageerror', 'browser-error', 'requestfailed'].includes(record.kind) || record.failure) issues.add(clean(record.message ?? record.failure ?? `Request failed: ${record.url}`));
51
+ if (record.status >= 400 && !['script', 'stylesheet', 'image', 'font', 'media', 'manifest'].includes(record.resourceType)) {
52
+ let path = record.path;
53
+ try { path ??= new URL(record.url).pathname; } catch { /* Keep unknown failures visible. */ }
54
+ if (!path || !allowed({ ...record, path })) issues.add(clean(`${record.method ?? 'HTTP'} ${path ?? record.url}: HTTP ${record.status}`));
55
+ }
56
+ }
57
+ if (diagnostics.recordingError) issues.add(clean(diagnostics.recordingError));
58
+ return { status: issues.size ? 'failed' : 'passed', issues: [...issues].slice(0, 8), issueCount: issues.size };
59
+ }
60
+
61
+ async function loadQa(root, qaRoot) {
62
+ let req = createRequire(join(root, 'package.json'));
63
+ let entry;
64
+ try {
65
+ if (qaRoot) {
66
+ const studio = join(resolve(qaRoot), 'packages', 'qa-studio');
67
+ entry = join(studio, 'dist', 'puppeteer', 'index.js');
68
+ req = createRequire(join(studio, 'package.json'));
69
+ } else entry = req.resolve('@app-studio/qa-studio/puppeteer');
70
+ const qa = await import(pathToFileURL(entry).href);
71
+ let puppeteer;
72
+ try { puppeteer = req.resolve('puppeteer-core'); }
73
+ catch { puppeteer = createRequire(entry).resolve('puppeteer-core'); }
74
+ return { qa, puppeteer: (await import(pathToFileURL(puppeteer).href)).default };
75
+ } catch (cause) {
76
+ throw new Error('Scenario v3 needs @app-studio/qa-studio and puppeteer-core in the project, or --qa-root pointing to a built QA checkout.', { cause });
77
+ }
78
+ }
79
+
80
+ async function json(path, optional = false) {
81
+ try {
82
+ if ((await stat(path)).size > 4 * 1024 * 1024) throw new Error(`Report exceeds 4 MiB: ${path}`);
83
+ return JSON.parse(await readFile(path, 'utf8'));
84
+ } catch (error) { if (optional && error.code === 'ENOENT') return undefined; throw error; }
85
+ }
86
+
87
+ function html(summary) {
88
+ return `<!doctype html><html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width">
89
+ <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'; img-src 'self'; media-src 'self'; base-uri 'none'; form-action 'none'">
90
+ <title>Scenario ${escape(summary.status)}</title><style>body{font:16px/1.5 system-ui;margin:2rem auto;padding:0 1rem;max-width:65rem;color:#17212b;background:#fff}a{color:#1354a1}table{border-collapse:collapse;width:100%}td,th{text-align:left;padding:.5rem;border-bottom:1px solid #ccd2d8}code{overflow-wrap:anywhere}small{color:#445}details,article{margin:1.5rem 0}img{max-width:100%;height:auto}video{max-width:100%}.failed{color:#a21b16}.passed{color:#176029}.recorded{color:#755000}</style>
91
+ <h1 class="${summary.status}">${escape(summary.status.toUpperCase())}</h1>
92
+ <p>${summary.cases.length} scenario(s). Target: <code>${escape(summary.baseUrl)}</code>.</p>
93
+ <p>Native CSS approximation; assertions determine behavior. Images are optional diagnostics.</p>
94
+ ${summary.error ? `<p class="failed">${escape(summary.error)}</p>` : ''}
95
+ <p><a href="./summary.json">Machine-readable summary</a></p>
96
+ ${summary.cases.map(c => `<article><h2>${escape(c.id)} — <span class="${c.status}">${c.status}</span></h2>
97
+ <p>${c.passedSteps}/${c.totalSteps} steps; ${c.assertions} assertions; ${c.durationMs ?? '?'} ms.</p>
98
+ ${c.verification ? `<p>Verification: ${escape(c.verification)}</p>` : ''}
99
+ ${c.issues.map(issue => `<p class="failed">${escape(issue)}</p>`).join('')}
100
+ ${c.droppedRecords ? `<p>Diagnostic history lost ${c.droppedRecords} records; absence of other errors is unproven.</p>` : ''}
101
+ <p>${c.artifacts.map(path => `<a href="${href(path)}">${escape(path.split('/').at(-1))}</a>`).join(' · ')}</p>
102
+ ${c.artifacts.filter(path => /\/(solution\.png|recording\.(gif|mp4|webm))$/.test(path)).map(path => `<details><summary>${escape(path.split('/').at(-1))}</summary>${/\.(mp4|webm)$/.test(path)
103
+ ? `<video controls preload="none" aria-label="Scenario recording" src="${href(path)}"><a href="${href(path)}">Open recording</a></video>`
104
+ : `<img loading="lazy" src="${href(path)}" alt="${escape(path.endsWith('.gif') ? 'Scenario recording' : 'Final scenario screenshot')}">`}</details>`).join('')}
105
+ <details><summary>Steps${c.stepsTruncated ? ' (first 200)' : ''}</summary><table><thead><tr><th>Step</th><th>Result</th><th>Checks</th></tr></thead><tbody>
106
+ ${c.steps.map(step => `<tr><td>${escape(step.label)}</td><td>${escape(step.status)}</td><td>${step.checks.map(escape).join('<br>')}</td></tr>`).join('')}</tbody></table></details></article>`).join('')}
107
+ <p><small>Detailed QA reports and recordings can contain application data. Review them before sharing.</small></p></html>`;
108
+ }
109
+
110
+ export async function writeSummary(summary) {
111
+ if (!summary.cases.length) summary.error ??= 'No scenario executed';
112
+ summary.status = summary.error || summary.cases.some(c => c.status === 'failed') ? 'failed'
113
+ : summary.cases.some(c => c.status === 'recorded') ? 'recorded' : 'passed';
114
+ await writeFile(join(summary.output, 'summary.json'), JSON.stringify(summary, null, 2) + '\n');
115
+ await writeFile(summary.report, html(summary));
116
+ return summary;
117
+ }
118
+
119
+ /** Uses QA's original graph, preparation, driver, assertions and checkpoint contract. */
120
+ export async function runScenarios(options) {
121
+ if (options.scenarios?.some(path => /\.m?js$/i.test(path))) {
122
+ if (!options.scenarios.every(path => /\.m?js$/i.test(path))) throw new Error('Run saved JavaScript and Scenario v3 files in separate invocations');
123
+ return (await import('./native-scenario.mjs')).runNativeScripts(options, { assessReport, clean, writeSummary });
124
+ }
125
+ const root = resolve(options.root ?? process.cwd());
126
+ const baseUrl = new URL(options.url);
127
+ if (!['http:', 'https:'].includes(baseUrl.protocol)) throw new Error('--url must use HTTP or HTTPS');
128
+ if (!options.scenarios?.length) throw new Error('Provide at least one Scenario v3 file or glob');
129
+ if (options.record !== undefined && !formats.includes(options.record)) throw new Error('--record must be mp4, webm or gif');
130
+ const fps = Number(options.fps ?? 5), pace = Number(options.pace ?? (options.record ? 300 : 0));
131
+ const timeoutMs = Number(options.timeoutMs ?? 120000);
132
+ if (!Number.isFinite(fps) || fps < 1 || fps > 30) throw new Error('--fps must be between 1 and 30');
133
+ if (!Number.isFinite(pace) || pace < 0 || pace > 10000) throw new Error('--pace-ms must be between 0 and 10000');
134
+ if (!Number.isInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 600000) throw new Error('--timeout-ms must be between 1 and 600000');
135
+ const width = Number(options.width ?? 1280), height = Number(options.height ?? 720);
136
+ if (![width, height].every(n => Number.isInteger(n) && n > 0 && n <= 8192)) throw new Error('Viewport dimensions must be integers between 1 and 8192');
137
+ const { qa, puppeteer } = await loadQa(root, options.qaRoot);
138
+ const hooks = options.prepare ? await import(pathToFileURL(resolve(root, options.prepare)).href) : {};
139
+ const prepare = hooks.default?.prepare ?? hooks.prepare;
140
+ const onSystemAction = hooks.default?.onSystemAction ?? hooks.onSystemAction;
141
+ const output = resolve(root, options.output ?? join('.lp-cache', 'scenario-runs', `${new Date().toISOString().replace(/[:.]/g, '-')}-${process.pid}`));
142
+ await mkdir(dirname(output), { recursive: true });
143
+ await mkdir(output); // A rerun cannot inherit or overwrite an earlier verdict/media.
144
+ const summary = { schemaVersion: 1, kind: 'lightpanda-scenario-summary', baseUrl: clean(baseUrl.href), status: 'failed', cases: [], output, report: join(output, 'index.html') };
145
+ const observations = new Map();
146
+ const abort = new AbortController();
147
+ const cancel = () => abort.abort(options.signal?.reason ?? new Error('Scenario cancelled'));
148
+ options.signal?.addEventListener('abort', cancel, { once: true });
149
+ const timer = setTimeout(() => abort.abort(new Error(`Scenario run timed out after ${timeoutMs}ms`)), timeoutMs);
150
+ let browser;
151
+ try {
152
+ options.signal?.throwIfAborted();
153
+ const adjacent = join(dirname(fileURLToPath(import.meta.url)), 'lightpanda');
154
+ browser = await qa.launchLightpanda({ connect: args => puppeteer.connect(args), executable: options.browser ?? (existsSync(adjacent) ? adjacent : undefined), viewport: { width, height }, timeoutMs: Math.min(timeoutMs, 30000) });
155
+ abort.signal.throwIfAborted();
156
+ const running = qa.runPuppeteerScenarioSuite({
157
+ browser, baseUrl: baseUrl.href, rootDir: root, pattern: options.scenarios,
158
+ subscenarios: options.subscenarios, forms: options.forms, outputDir: output,
159
+ evidence: options.screenshots ? 'proof' : 'none', screenshots: false,
160
+ diagnostics: { enabled: true, video: options.record ?? false, fps, maxDuration: timeoutMs / 1000, ...(options.ffmpeg ? { ffmpeg: options.ffmpeg } : {}) },
161
+ async prepare(args) {
162
+ const observed = new Map();
163
+ observations.set(args.scenario.id, observed);
164
+ const probe = args.driver.probe.bind(args.driver);
165
+ args.driver.probe = async (...values) => {
166
+ const result = await probe(...values);
167
+ const key = JSON.stringify(values[0]);
168
+ if (observed.size < 64 || observed.has(key)) observed.set(key, {
169
+ target: values[0], visible: result?.visible ?? false,
170
+ ...(values[3] === true && result ? { enabled: result.enabled, text: clean(result.text) } : {}),
171
+ });
172
+ return result;
173
+ };
174
+ if (pace) for (const action of ['navigate', 'reload', 'click', 'fill', 'select', 'press']) {
175
+ const perform = args.driver[action]?.bind(args.driver);
176
+ if (perform) args.driver[action] = async (...values) => { await perform(...values); await delay(pace, undefined, { signal: abort.signal }); };
177
+ }
178
+ const prepared = await prepare?.({ ...args, baseUrl: baseUrl.href, signal: abort.signal });
179
+ return prepared?.world ?? prepared ?? { schemaVersion: 1, seed: {}, variables: {} };
180
+ },
181
+ ...(onSystemAction ? { onSystemAction: call => onSystemAction({ ...call, baseUrl: baseUrl.href, signal: abort.signal }) } : {}),
182
+ });
183
+ await new Promise((resolveRun, reject) => {
184
+ const interrupted = () => reject(abort.signal.reason);
185
+ abort.signal.addEventListener('abort', interrupted, { once: true });
186
+ running.then(resolveRun, reject).finally(() => abort.signal.removeEventListener('abort', interrupted));
187
+ });
188
+ } catch (error) { summary.error = clean(message(error)); }
189
+ finally {
190
+ clearTimeout(timer);
191
+ options.signal?.removeEventListener('abort', cancel);
192
+ try { await browser?.close(); } catch (error) { summary.error ??= clean(message(error)); }
193
+ }
194
+ for (const entry of await readdir(output, { withFileTypes: true })) {
195
+ if (!entry.isDirectory()) continue;
196
+ const directory = join(output, entry.name);
197
+ const report = await json(join(directory, 'scenario-report.json'), true);
198
+ if (!report) continue;
199
+ const diagnostics = await json(join(directory, 'diagnostics.json'), true);
200
+ const assessed = assessReport(report, diagnostics);
201
+ const ui = observations.get(report.scenarioId);
202
+ await writeFile(join(directory, 'observed-ui.json'), JSON.stringify({ source: 'last assertion probes', complete: false, limit: 64, controls: [...(ui?.values() ?? [])] }, null, 2) + '\n');
203
+ const files = (await readdir(directory)).filter(name => /\.(json|png|mp4|webm|gif)$/.test(name));
204
+ summary.cases.push({ id: report.scenarioId, ...assessed, durationMs: report.durationMs, passedSteps: report.steps.filter(step => step.status === 'passed').length,
205
+ totalSteps: report.steps.length, assertions: report.steps.reduce((n, step) => n + step.assertions.length, 0), droppedRecords: diagnostics?.droppedRecords ?? 0,
206
+ stepsTruncated: report.steps.length > 200, steps: report.steps.slice(0, 200).map(step => ({ label: clean(step.label), status: step.status, checks: step.assertions.map(a => clean(`${a.status}: ${a.message}`)) })),
207
+ artifacts: files.map(name => `${entry.name}/${name}`),
208
+ });
209
+ }
210
+ if (!summary.cases.length) summary.error ??= 'No Scenario v3 case executed';
211
+ return writeSummary(summary);
212
+ }
213
+
214
+ async function main() {
215
+ const { values, positionals } = parseArgs({ allowPositionals: true, options: {
216
+ url: { type: 'string' }, root: { type: 'string' }, output: { type: 'string' },
217
+ 'qa-root': { type: 'string' }, browser: { type: 'string' }, prepare: { type: 'string' },
218
+ subscenarios: { type: 'string', multiple: true }, forms: { type: 'string', multiple: true },
219
+ id: { type: 'string' }, record: { type: 'string' }, screenshots: { type: 'boolean' }, fps: { type: 'string' },
220
+ 'expect-http-error': { type: 'string', multiple: true },
221
+ 'pace-ms': { type: 'string' }, 'timeout-ms': { type: 'string' }, ffmpeg: { type: 'string' },
222
+ width: { type: 'string' }, height: { type: 'string' }, json: { type: 'boolean' }, help: { type: 'boolean' },
223
+ } });
224
+ if (values.help) {
225
+ console.log('node tools/scenario.mjs <scenario.json|glob> --url http://localhost:3000\nnode tools/scenario.mjs <saved.js> [options]\n [--id SCENARIO_ID] [--record gif|mp4|webm] [--screenshots] [--output NEW_DIR]\n [--root PROJECT] [--qa-root QA_CHECKOUT] [--browser EXECUTABLE]\n [--prepare MODULE] [--subscenarios GLOB] [--forms GLOB]\n [--fps 5] [--pace-ms 300] [--timeout-ms 120000] [--width 1280 --height 720] [--ffmpeg ffmpeg] [--json]\n Native scripts: [--expect-http-error STATUS] (repeatable).\nDefault: assertions and bounded text evidence; no screenshots or FFmpeg. QA_SCENARIO_ID/FEATURE/MODE are honored for Scenario v3.');
226
+ return;
227
+ }
228
+ const stop = new AbortController();
229
+ if (values.id !== undefined) {
230
+ if (!values.id.trim()) throw new Error('--id must name a scenario');
231
+ process.env.QA_SCENARIO_ID = values.id;
232
+ }
233
+ const interrupt = () => stop.abort(new Error('Scenario cancelled'));
234
+ process.once('SIGINT', interrupt);
235
+ process.once('SIGTERM', interrupt);
236
+ try {
237
+ const summary = await runScenarios({ ...values, scenarios: positionals, qaRoot: values['qa-root'], pace: values['pace-ms'], timeoutMs: values['timeout-ms'], expectedHttpErrors: values['expect-http-error'], signal: stop.signal });
238
+ const compact = { kind: summary.kind, status: summary.status, cases: summary.cases.length, failed: summary.cases.filter(c => c.status === 'failed').map(c => ({ id: c.id, issues: c.issues })).slice(0, 5), ...(summary.error ? { error: summary.error } : {}), output: summary.output, report: summary.report };
239
+ console.log(values.json ? JSON.stringify(compact) : `${summary.status.toUpperCase()}: ${summary.cases.length} scenario(s)\n${compact.failed.flatMap(c => c.issues.map(issue => `${c.id}: ${issue}`)).join('\n')}${summary.error ? `\n${summary.error}` : ''}\nReport: ${summary.report}`);
240
+ process.exitCode = summary.status === 'failed' ? 1 : 0;
241
+ } finally {
242
+ process.removeListener('SIGINT', interrupt);
243
+ process.removeListener('SIGTERM', interrupt);
244
+ }
245
+ }
246
+
247
+ if (process.argv[1] && existsSync(process.argv[1]) && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
248
+ main().catch(error => { console.error(clean(message(error))); process.exitCode = 1; });
249
+ }
@@ -0,0 +1,109 @@
1
+ // Copyright (C) 2026 Lightpanda (Selecy SAS)
2
+ // SPDX-License-Identifier: AGPL-3.0-or-later
3
+
4
+ import { spawn } from 'node:child_process';
5
+ import { existsSync } from 'node:fs';
6
+ import { copyFile, mkdir, readFile, readdir, stat, writeFile } from 'node:fs/promises';
7
+ import { basename, dirname, join, relative, resolve } from 'node:path';
8
+ import { fileURLToPath } from 'node:url';
9
+
10
+ async function execute(binary, args, { cwd, timeoutMs, signal, clean, trace }) {
11
+ if (signal?.aborted) return { code: null, error: clean(message(signal.reason)), stdout: '', stderr: '' };
12
+ const child = spawn(binary, args, { cwd, stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, LIGHTPANDA_DISABLE_TELEMETRY: 'true', LIGHTPANDA_DISABLE_CORE_DUMP: 'true' } });
13
+ let stdout = '', stderr = '', error, kill;
14
+ child.stdout.on('data', bytes => { stdout = (stdout + bytes).slice(-16384); });
15
+ child.stderr.on('data', bytes => { stderr = (stderr + bytes).slice(-16384); });
16
+ child.once('error', value => { error = value; });
17
+ const stop = reason => {
18
+ error ??= reason;
19
+ child.kill('SIGTERM');
20
+ kill ??= setTimeout(() => child.kill('SIGKILL'), 1000);
21
+ };
22
+ const cancel = () => stop(signal.reason ?? new Error('Native script cancelled'));
23
+ signal?.addEventListener('abort', cancel, { once: true });
24
+ const timeout = setTimeout(() => stop(new Error(`Native script timed out after ${timeoutMs}ms`)), timeoutMs);
25
+ const budget = setInterval(() => {
26
+ stat(trace).then(file => { if (file.size > 4 * 1024 * 1024) stop(new Error('Native trace exceeds 4 MiB; script stopped to bound evidence')); }).catch(() => {});
27
+ }, 100);
28
+ const code = await new Promise(resolve => child.once('close', resolve));
29
+ clearTimeout(timeout); clearTimeout(kill); clearInterval(budget);
30
+ signal?.removeEventListener('abort', cancel);
31
+ return { code, error: error ? clean(error.message) : undefined, stdout, stderr };
32
+ }
33
+
34
+ /** Execute original --save files in their native V8 runtime, without source rewriting. */
35
+ export async function runNativeScripts(options, { assessReport, clean, writeSummary }) {
36
+ if (options.prepare || options.forms?.length || options.subscenarios?.length) throw new Error('--prepare/--forms/--subscenarios apply to Scenario v3; saved scripts retain their own setup');
37
+ const root = resolve(options.root ?? process.cwd());
38
+ if (options.url && !['http:', 'https:'].includes(new URL(options.url).protocol)) throw new Error('--url must use HTTP or HTTPS');
39
+ const fps = Number(options.fps ?? 5), timeoutMs = Number(options.timeoutMs ?? 120000);
40
+ const width = Number(options.width ?? 1280), height = Number(options.height ?? 720);
41
+ if (!Number.isFinite(fps) || fps < 1 || fps > 30) throw new Error('--fps must be between 1 and 30');
42
+ if (!Number.isInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 600000) throw new Error('--timeout-ms must be between 1 and 600000');
43
+ if (![width, height].every(n => Number.isInteger(n) && n > 0 && n <= 8192)) throw new Error('Viewport dimensions must be integers between 1 and 8192');
44
+ if (options.record !== undefined && !['gif', 'mp4', 'webm'].includes(options.record)) throw new Error('--record must be mp4, webm or gif');
45
+ const expected = new Set((options.expectedHttpErrors ?? []).map(Number));
46
+ if ([...expected].some(status => !Number.isInteger(status) || status < 400 || status > 599)) throw new Error('--expect-http-error must be an HTTP status between 400 and 599');
47
+ const adjacent = join(dirname(fileURLToPath(import.meta.url)), 'lightpanda');
48
+ const binary = resolve(options.browser ?? (existsSync(adjacent) ? adjacent : join(dirname(fileURLToPath(import.meta.url)), '../zig-out/bin/lightpanda')));
49
+ const output = resolve(root, options.output ?? join('.lp-cache', 'scenario-runs', `${new Date().toISOString().replace(/[:.]/g, '-')}-${process.pid}`));
50
+ await mkdir(dirname(output), { recursive: true }); await mkdir(output);
51
+ const summary = { schemaVersion: 1, kind: 'lightpanda-scenario-summary', baseUrl: options.url ? clean(options.url) : 'URLs declared by the saved script', status: 'failed', cases: [], output, report: join(output, 'index.html') };
52
+ const scripts = options.scenarios.map(path => resolve(root, path)).filter(path => !options.id || options.id === basename(path) || options.id === relative(root, path));
53
+ for (const [index, script] of scripts.entries()) {
54
+ const id = relative(root, script);
55
+ const name = `${index + 1}-${basename(script).replace(/[^A-Za-z0-9._-]/g, '-')}`;
56
+ const directory = join(output, name), frames = join(directory, 'frames'), trace = join(directory, 'trace.jsonl');
57
+ await mkdir(directory);
58
+ const start = Date.now();
59
+ const result = await execute(binary, ['agent', script, '--no-llm', '--trace', trace, '--viewport-width', String(width), '--viewport-height', String(height), '--load-resources', 'stylesheet', ...(options.url ? ['--url', options.url] : []), ...(options.record || options.screenshots ? ['--capture-dir', frames] : []), ...(options.screenshots && !options.record ? ['--capture-final-only'] : [])], { cwd: root, timeoutMs, signal: options.signal, clean, trace });
60
+ let records = [], traceError;
61
+ try {
62
+ if ((await stat(trace)).size > 4 * 1024 * 1024) throw new Error('Native trace exceeds 4 MiB; inspect trace.jsonl');
63
+ records = (await readFile(trace, 'utf8')).split('\n').filter(Boolean).map(line => JSON.parse(line));
64
+ } catch (error) { traceError = clean(error.message); }
65
+ const tools = records.filter(record => record.type === 'tool');
66
+ const browser = records.findLast(record => record.type === 'browser');
67
+ const events = browser?.diagnostics?.events ?? tools.flatMap(tool => tool.diagnostics?.events ?? []);
68
+ const report = { scenarioId: id, status: result.code === 0 && !result.error && !traceError && tools.length ? 'passed' : 'failed',
69
+ message: result.error ?? traceError ?? (result.code !== 0 ? clean(result.stderr.slice(-2048)) || `Native script exited with ${result.code}` : tools.length ? undefined : 'No browser operation executed'),
70
+ steps: tools.map(tool => ({ stepId: `tool-${tool.step}`, label: clean(`${tool.name}${tool.args?.selector ? ` ${tool.args.selector}` : ''}`), status: tool.error ? 'failed' : 'passed', message: tool.errorMessage, assertions: tool.verification?.status === 'verified' ? [{ status: 'passed', assertion: { type: 'native-predicate' }, message: 'Requested predicate verified' }] : [] })),
71
+ requests: events.filter(e => e.kind === 'network' && e.status >= 400 && !expected.has(e.status)).map(e => ({ method: e.method, path: e.url, status: e.status })),
72
+ consoleErrors: events.filter(e => e.kind === 'console' && e.level === 'error').map(e => e.message),
73
+ renderErrors: events.filter(e => ['exception', 'rejection', 'application'].includes(e.kind) && e.level === 'error').map(e => e.message),
74
+ };
75
+ const assessed = assessReport(report, {}, { requireAssertions: false });
76
+ if (browser?.diagnostics?.hasMore) assessed.issues.push('Diagnostic budget exceeded; inspect trace.jsonl before claiming verification');
77
+ for (const event of events) if (event.kind === 'network' && !event.status && event.level === 'error') assessed.issues.push(clean(event.message));
78
+ for (const record of records) if (record.capture?.err || record.capture?.truncated) assessed.issues.push(record.capture.truncated ? 'Native checkpoint capture limit reached (128); media is incomplete' : `Native capture failed: ${record.capture.err}`);
79
+ const captured = records.filter(record => record.capture?.path).length;
80
+ if (options.screenshots && !captured) assessed.issues.push('No final page was available for the requested screenshot');
81
+ let recording;
82
+ if (options.record) {
83
+ try {
84
+ const filename = existsSync(join(dirname(fileURLToPath(import.meta.url)), 'lightpanda-capture.mjs')) ? './lightpanda-capture.mjs' : './capture.mjs';
85
+ const { encodeFrames } = await import(new URL(filename, import.meta.url).href);
86
+ recording = await encodeFrames({ directory: frames, count: captured, path: join(directory, `recording.${options.record}`), fps, ffmpeg: options.ffmpeg, signal: options.signal });
87
+ } catch (error) { assessed.issues.push(clean(message(error))); }
88
+ }
89
+ if (options.screenshots && captured) await copyFile(join(frames, `${String(captured).padStart(6, '0')}.png`), join(directory, 'solution.png'));
90
+ const assertions = report.steps.reduce((n, step) => n + step.assertions.length, 0);
91
+ const issues = [...new Set(assessed.issues)];
92
+ const status = assessed.status === 'failed' || issues.length ? 'failed' : assertions ? 'passed' : 'recorded';
93
+ let snapshot;
94
+ try { if (browser?.snapshot) snapshot = JSON.parse(browser.snapshot); } catch { /* Raw trace remains available. */ }
95
+ await writeFile(join(directory, 'observed-ui.json'), JSON.stringify({ source: 'native semantic snapshot', ...(snapshot ?? { complete: false, unavailable: true }) }, null, 2) + '\n');
96
+ await writeFile(join(directory, 'script-report.json'), JSON.stringify({ ...report, executionStatus: report.status, status, issues, expectedHttpErrors: [...expected], recording, diagnostics: browser?.diagnostics, scriptExitCode: result.code }, null, 2) + '\n');
97
+ await writeFile(join(directory, 'process-output.txt'), clean(result.stdout) + '\n' + clean(result.stderr) + '\n');
98
+ summary.cases.push({ id: clean(id), status, issues: issues.slice(0, 8), issueCount: Math.max(assessed.issueCount, issues.length), durationMs: Date.now() - start,
99
+ passedSteps: report.steps.filter(step => step.status === 'passed').length, totalSteps: tools.length, assertions, verification: assertions ? 'native predicates verified' : report.status === 'passed' ? 'script completed; application outcome not asserted' : 'script did not complete; outcome unverified',
100
+ droppedRecords: browser?.diagnostics?.droppedTotal ?? 0, stepsTruncated: tools.length > 200,
101
+ steps: report.steps.slice(0, 200).map((step, i) => ({ label: step.label, status: step.status, checks: [clean(step.message ?? tools[i].verification?.status ?? 'dispatched')] })),
102
+ artifacts: (await readdir(directory)).filter(path => /\.(json|jsonl|txt|png|mp4|webm|gif)$/.test(path)).map(path => `${name}/${path}`),
103
+ });
104
+ if (status === 'failed') break;
105
+ }
106
+ return writeSummary(summary);
107
+ }
108
+
109
+ const message = error => error instanceof Error ? error.message : String(error);
package/bin/source.tar.gz CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@app-studio/qa-browser-darwin-arm64",
3
- "version": "0.10.4",
3
+ "version": "0.11.2",
4
4
  "description": "Studio native CSS Lightpanda binary",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0-or-later",
package/verify.mjs CHANGED
@@ -1,12 +1,14 @@
1
1
  import {createHash} from 'node:crypto';
2
2
  import {readFileSync} from 'node:fs';
3
3
  import {dirname, join} from 'node:path';
4
- import {fileURLToPath} from 'node:url';
4
+ import {fileURLToPath, pathToFileURL} from 'node:url';
5
5
  const root = dirname(fileURLToPath(import.meta.url));
6
6
  const pkg = JSON.parse(readFileSync(join(root, 'package.json')));
7
7
  const manifest = JSON.parse(readFileSync(join(root, 'bin/lightpanda-build.json')));
8
8
  if (manifest.rendering !== 'native-css' || manifest.platform !== pkg.os[0] || manifest.arch !== pkg.cpu[0]) throw new Error('Missing or mismatched native CSS build');
9
- for (const name of ['lightpanda', 'lightpanda-capture.mjs', 'LICENSE', 'source.tar.gz']) {
9
+ for (const name of ['lightpanda', 'lightpanda-capture.mjs', 'lightpanda-scenario.mjs', 'native-scenario.mjs', 'LICENSE', 'source.tar.gz']) {
10
10
  const hash = createHash('sha256').update(readFileSync(join(root, 'bin', name))).digest('hex');
11
11
  if (hash !== manifest.files[name]) throw new Error(`Native build checksum mismatch: ${name}`);
12
12
  }
13
+ const recorder = await import(pathToFileURL(join(root, 'bin/lightpanda-capture.mjs')).href);
14
+ if (typeof recorder.encodeFrames !== 'function') throw new Error(`Native package ${pkg.name} recorder is missing encodeFrames. Adopt a complete native bundle; JavaScript helpers do not require Zig.`);