browser-commander 0.18.0 → 0.19.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.
@@ -20,6 +20,7 @@ import path from 'node:path';
20
20
  import { openBrowserCdpSession } from '../../src/downloads/sources.js';
21
21
  import { launchE2EBrowser } from '../helpers/e2e-browser.js';
22
22
  import {
23
+ BLOB_PDF_BODY,
23
24
  PDF_BODY,
24
25
  REPORT_BODY,
25
26
  startDownloadServer,
@@ -98,6 +99,52 @@ function describeDownloads(engine) {
98
99
  assert.strictEqual(await fs.readFile(artifact.path, 'utf8'), REPORT_BODY);
99
100
  });
100
101
 
102
+ it('should not publish a completed download before its bytes are readable', async () => {
103
+ // Issue #92: Chromium reports `Browser.downloadProgress` with
104
+ // `state: "completed"` before the GUID-named staging file is readable,
105
+ // and the manager then failed with
106
+ // `ENOENT ... .browser-commander-staging/<guid>`. A PDF the page builds
107
+ // itself is the shape the issue reported.
108
+ const completed = [];
109
+ const failures = [];
110
+ downloads.on('completed', (artifact) => completed.push(artifact));
111
+ downloads.on('failed', (artifact) => failures.push(artifact));
112
+
113
+ const artifact = await captureClickOn('#blob-pdf');
114
+
115
+ assert.deepStrictEqual(failures, []);
116
+ assert.strictEqual(artifact.state, 'completed');
117
+ assert.strictEqual(path.basename(artifact.path), 'generated.pdf');
118
+ assert.strictEqual(
119
+ await fs.readFile(artifact.path, 'utf8'),
120
+ BLOB_PDF_BODY
121
+ );
122
+ assert.strictEqual(artifact.bytes, Buffer.byteLength(BLOB_PDF_BODY));
123
+
124
+ const published = completed.filter(
125
+ (entry) => entry.path === artifact.path
126
+ );
127
+ assert.strictEqual(
128
+ published.length,
129
+ 1,
130
+ `exactly one completed artifact should be published: ${published.length}`
131
+ );
132
+
133
+ const staged = await fs
134
+ .readdir(path.join(directory, '.browser-commander-staging'))
135
+ .catch(() => []);
136
+ assert.deepStrictEqual(
137
+ staged,
138
+ [],
139
+ `no staged file should survive a saved download: ${staged}`
140
+ );
141
+ const entries = await fs.readdir(directory);
142
+ assert.ok(
143
+ !entries.some((entry) => entry.endsWith('.partial')),
144
+ `no partial file should be left behind: ${entries}`
145
+ );
146
+ });
147
+
101
148
  it('should save a navigation that turned into a download', async () => {
102
149
  const artifact = await captureClickOn('#navigating');
103
150
 
@@ -190,6 +190,20 @@ function describeTraces(engine) {
190
190
  */
191
191
  const changesIn = (batches) => batches.flatMap((batch) => batch.records);
192
192
 
193
+ /**
194
+ * Every mutation record a stopped trace holds, whichever interval it fell in.
195
+ *
196
+ * @param {Object} reader - An open trace
197
+ * @returns {Promise<Object[]>} The records, in order
198
+ */
199
+ const allChanges = async (reader) => {
200
+ const records = [];
201
+ for (let index = 0; index <= reader.checkpoints.length; index++) {
202
+ records.push(...changesIn(await reader.mutations(index)));
203
+ }
204
+ return records;
205
+ };
206
+
193
207
  it('should capture the state a user made, not the state the server sent', async () => {
194
208
  const trace = await record({ screenshots: 'checkpoints' });
195
209
 
@@ -232,7 +246,12 @@ function describeTraces(engine) {
232
246
  const stopped = await trace.stop();
233
247
 
234
248
  const reader = await readTrace(stopped.path);
235
- const batches = await reader.mutations(1);
249
+ // A continuous trace takes its own base snapshot first (issue #93), so
250
+ // the interval a moment belongs to is looked up by that moment's name
251
+ // rather than counted from the caller's first checkpoint.
252
+ const indexOf = (name) =>
253
+ stopped.checkpoints.find((entry) => entry.name === name).index;
254
+ const batches = await reader.mutations(indexOf('loaded'));
236
255
  const added = changesIn(batches)
237
256
  .flatMap((change) => change.added ?? [])
238
257
  .filter((node) => node?.id);
@@ -253,14 +272,19 @@ function describeTraces(engine) {
253
272
 
254
273
  // The navigation threw the in-page recorder away with the document it
255
274
  // lived in; a batch after it proves a new one took its place.
256
- const afterNavigating = await reader.mutations(3);
275
+ const afterNavigating = await reader.mutations(
276
+ indexOf('the next document')
277
+ );
257
278
  assert.ok(
258
279
  changesIn(afterNavigating)
259
280
  .flatMap((change) => change.added ?? [])
260
281
  .some((node) => node?.id === 'grown'),
261
282
  `expected the grown item, got ${JSON.stringify(afterNavigating)}`
262
283
  );
263
- assert.match((await reader.state(3)).url, /\/next$/);
284
+ assert.match(
285
+ (await reader.state(indexOf('the next document'))).url,
286
+ /\/next$/
287
+ );
264
288
  });
265
289
 
266
290
  it('should put everything that happened on one ordered timeline', async () => {
@@ -404,6 +428,238 @@ function describeTraces(engine) {
404
428
  );
405
429
  });
406
430
 
431
+ it('should record what a new document did while it was still loading', async () => {
432
+ const trace = await record({ mode: TRACE_MODE.CONTINUOUS });
433
+
434
+ // No checkpoint between the navigation and the change: the recorder has
435
+ // to already be in the new document when the document's own script runs,
436
+ // which is the interval issue #93 says used to be lost entirely.
437
+ await page.goto(`${server.baseUrl}/init`);
438
+ await trace.checkpoint('the new document initialized itself');
439
+ const stopped = await trace.stop();
440
+
441
+ const reader = await readTrace(stopped.path);
442
+ const records = await allChanges(reader);
443
+ const built = records.find((change) =>
444
+ (change.added ?? []).some(
445
+ (node) =>
446
+ node?.id === 'made-while-loading' ||
447
+ /made-while-loading/.test(node?.html ?? '')
448
+ )
449
+ );
450
+ assert.ok(
451
+ built,
452
+ `no initialization mutation among ${records.length} records`
453
+ );
454
+ const marked = records.find(
455
+ (change) =>
456
+ change.kind === 'attributes' &&
457
+ change.attribute === 'data-initialized'
458
+ );
459
+ assert.strictEqual(marked?.after, 'yes');
460
+ });
461
+
462
+ it('should record live control state as it changes, not only at checkpoints', async () => {
463
+ const trace = await record({ mode: TRACE_MODE.CONTINUOUS });
464
+
465
+ await page.click('#name');
466
+ await page.type('#name', 'ada');
467
+ await page.click('#terms');
468
+ await choose('#plan', 'pro');
469
+ await commander.evaluate(() => window.scrollTo(0, 400));
470
+ // A scroll event is dispatched after the scroll, not during it, so the
471
+ // checkpoint that drains the queue waits for a frame to be painted.
472
+ await commander.evaluate(
473
+ () =>
474
+ new Promise((resolve) => {
475
+ window.requestAnimationFrame(() =>
476
+ window.requestAnimationFrame(() => resolve(true))
477
+ );
478
+ })
479
+ );
480
+ await trace.checkpoint('the form was filled in');
481
+ const stopped = await trace.stop();
482
+
483
+ const reader = await readTrace(stopped.path);
484
+ const live = (await allChanges(reader)).filter(
485
+ (change) => change.kind === 'live-state'
486
+ );
487
+ const properties = new Set(live.map((change) => change.property));
488
+ for (const property of [
489
+ 'value',
490
+ 'checked',
491
+ 'selected',
492
+ 'focus',
493
+ 'scroll',
494
+ ]) {
495
+ assert.ok(
496
+ properties.has(property),
497
+ `no ${property} record among ${[...properties].join(', ')}`
498
+ );
499
+ }
500
+
501
+ // Each keystroke is its own state: a stepwise replay of typing has to be
502
+ // able to show "a", then "ad", then "ada".
503
+ const typed = live.filter(
504
+ (change) =>
505
+ change.property === 'value' && change.target?.path === '#name'
506
+ );
507
+ assert.deepStrictEqual(
508
+ typed.map((change) => change.after),
509
+ ['a', 'ad', 'ada']
510
+ );
511
+ const checked = live.find((change) => change.property === 'checked');
512
+ assert.strictEqual(checked.after, true);
513
+ const selected = live.find((change) => change.property === 'selected');
514
+ assert.deepStrictEqual(selected.after, ['pro']);
515
+ const scrolled = live
516
+ .filter((change) => change.property === 'scroll')
517
+ .pop();
518
+ assert.strictEqual(scrolled.after.top, 400);
519
+ });
520
+
521
+ it('should replay an insertion, a move, a removal and a replacement exactly', async () => {
522
+ const trace = await record({ mode: TRACE_MODE.CONTINUOUS });
523
+
524
+ await page.click('#insert-first');
525
+ await page.click('#move-last');
526
+ await page.click('#remove-one');
527
+ await page.click('#replace-subtree');
528
+ const settled = await trace.checkpoint('the list settled');
529
+ const stopped = await trace.stop();
530
+ const viewer = await writeTraceViewer(stopped.path);
531
+
532
+ await page.goto(`file://${viewer}`);
533
+ const batches = await page.evaluate(
534
+ () =>
535
+ JSON.parse(document.getElementById('trace-data').textContent)
536
+ .mutations[1].length
537
+ );
538
+ assert.ok(batches > 0, 'the interval recorded nothing to replay');
539
+ for (let step = 0; step < batches; step++) {
540
+ await page.click('#step-forward');
541
+ }
542
+
543
+ // Both documents reach the comparison as strings this test holds: the
544
+ // replayed one read out of the frame, the recorded one read from the
545
+ // bundle on disk. Taking text straight back out of the page and parsing
546
+ // it as HTML in the same breath is what CodeQL reports as
547
+ // `js/xss-through-dom` (alert 20 on this pull request), and reading the
548
+ // recorded side from the bundle is the stronger check anyway: it holds
549
+ // the viewer to the file it was built from rather than to the copy it
550
+ // embedded in itself.
551
+ const replayedHtml = await page.evaluate(() =>
552
+ document.getElementById('stage').getAttribute('srcdoc')
553
+ );
554
+ const capturedHtml = await (
555
+ await readTrace(stopped.path)
556
+ ).html(settled.index);
557
+ assert.ok(capturedHtml, 'the bundle kept no HTML for the checkpoint');
558
+
559
+ const compared = await page.evaluate(
560
+ ([replayedSource, capturedSource]) => {
561
+ // The viewer highlights what it touched and annotates what a static
562
+ // document cannot show; neither is part of the recorded DOM.
563
+ const clean = (source) => {
564
+ const parsed = new DOMParser().parseFromString(source, 'text/html');
565
+ for (const element of parsed.querySelectorAll('*')) {
566
+ element.removeAttribute('style');
567
+ for (const attribute of [...element.attributes]) {
568
+ if (attribute.name.startsWith('data-bc-')) {
569
+ element.removeAttribute(attribute.name);
570
+ }
571
+ }
572
+ }
573
+ return parsed;
574
+ };
575
+ const replayed = clean(replayedSource);
576
+ const captured = clean(capturedSource);
577
+ const sub = (parsed, selector) =>
578
+ parsed.querySelector(selector)?.innerHTML.trim() ?? null;
579
+ return {
580
+ items: [sub(replayed, '#items'), sub(captured, '#items')],
581
+ subtree: [sub(replayed, '#subtree'), sub(captured, '#subtree')],
582
+ };
583
+ },
584
+ [replayedHtml, capturedHtml]
585
+ );
586
+
587
+ assert.strictEqual(compared.items[0], compared.items[1]);
588
+ assert.strictEqual(compared.subtree[0], compared.subtree[1]);
589
+ assert.match(compared.items[1], /id="item-b"[\s\S]*id="inserted"/);
590
+ assert.doesNotMatch(compared.items[1], /id="item-a"/);
591
+ assert.match(compared.subtree[1], /id="new-child"/);
592
+ });
593
+
594
+ it('should resolve every record to one unambiguous page and frame', async () => {
595
+ const second = await browser.newPage();
596
+ await second.goto(`${server.baseUrl}/`);
597
+ const here = await record({ mode: TRACE_MODE.CONTINUOUS });
598
+ const there = await startTrace({
599
+ page: second,
600
+ output: path.join(artifacts, `owners-${++runs}`),
601
+ screenshots: false,
602
+ mode: TRACE_MODE.CONTINUOUS,
603
+ });
604
+
605
+ // Opening a second tab pushes the first into the background, where
606
+ // requestAnimationFrame stops firing and Puppeteer's click, which waits
607
+ // for the element to settle, never returns. Whichever page is being
608
+ // driven is the one in front.
609
+ await page.bringToFront();
610
+ await page.click('#add');
611
+ await second.bringToFront();
612
+ await second.click('#add');
613
+ await page.bringToFront();
614
+ // A change inside the iframe, so this run holds two documents of one page
615
+ // as well as two pages of one browser.
616
+ await commander.evaluate(() => {
617
+ const framed = document.getElementById('inner').contentDocument;
618
+ framed.body.appendChild(framed.createElement('span'));
619
+ });
620
+ await here.checkpoint('both pages moved');
621
+ await there.checkpoint('both pages moved');
622
+ const stoppedHere = await here.stop();
623
+ const stoppedThere = await there.stop();
624
+ await second.close();
625
+
626
+ const readHere = await readTrace(stoppedHere.path);
627
+ const readThere = await readTrace(stoppedThere.path);
628
+ const pagesOf = (events) => new Set(events.map((event) => event.pageId));
629
+ assert.strictEqual(pagesOf(readHere.events).size, 1);
630
+ assert.strictEqual(pagesOf(readThere.events).size, 1);
631
+ assert.notStrictEqual(
632
+ [...pagesOf(readHere.events)][0],
633
+ [...pagesOf(readThere.events)][0]
634
+ );
635
+ for (const event of [...readHere.events, ...readThere.events]) {
636
+ assert.ok(event.traceId, `an event with no trace: ${event.kind}`);
637
+ assert.ok(
638
+ event.navigationId,
639
+ `an event with no document: ${event.kind}`
640
+ );
641
+ }
642
+
643
+ const batches = [];
644
+ for (let index = 0; index <= readHere.checkpoints.length; index++) {
645
+ batches.push(...(await readHere.mutations(index)));
646
+ }
647
+ assert.ok(
648
+ batches.every(
649
+ (batch) => batch.frameId && batch.pageId && batch.traceId
650
+ ),
651
+ 'a batch arrived without an owner'
652
+ );
653
+ assert.ok(
654
+ batches.some((batch) => batch.mainFrame === false),
655
+ 'nothing was recorded inside the iframe'
656
+ );
657
+ assert.ok(
658
+ new Set(batches.map((batch) => batch.frameId)).size >= 2,
659
+ 'the page and its iframe reported the same frame'
660
+ );
661
+ });
662
+
407
663
  it('should leave a viewer that opens with no network and replays the run', async () => {
408
664
  const trace = await record({ mode: TRACE_MODE.CONTINUOUS });
409
665
  await recordAnAppUpdate(trace);
@@ -416,26 +672,55 @@ function describeTraces(engine) {
416
672
  page.on('request', onRequest);
417
673
  await page.goto(`file://${viewer}`);
418
674
 
675
+ // The viewer opens on the run's base snapshot, and nothing happened
676
+ // before it by definition (issue #93), so the interval with the app's
677
+ // changes in it is the one that starts at the checkpoint named here.
678
+ const selected = await page.evaluate(() => {
679
+ const entry = [...document.querySelectorAll('#timeline li')].find(
680
+ (li) => li.querySelector('.what').textContent === 'loaded'
681
+ );
682
+ if (!entry) {
683
+ return false;
684
+ }
685
+ entry.click();
686
+ return true;
687
+ });
688
+ assert.ok(selected, 'the timeline has no checkpoint named "loaded"');
689
+
419
690
  const shown = await page.evaluate(() => ({
420
691
  events: document.querySelectorAll('#timeline li').length,
421
692
  step: document.getElementById('step').textContent,
422
693
  details: document.getElementById('details').textContent,
694
+ replay: document.querySelector('.meta.replay').textContent,
423
695
  framed: document.getElementById('stage').getAttribute('srcdoc'),
424
696
  }));
425
697
  assert.ok(shown.events > 0, 'the timeline is empty');
426
698
  assert.match(shown.details, /Traced app/);
427
699
  assert.match(shown.step, /[1-9]\d* mutation batches/);
428
700
  assert.match(shown.framed, /id="items"/);
701
+ // The viewer says what it can and cannot reproduce rather than implying
702
+ // the recording is the whole run (issue #93).
703
+ assert.match(shown.replay, /partial diagnostic replay/);
704
+ assert.match(shown.replay, /live control state/);
429
705
 
430
706
  // Stepping replays the recorded mutations into the captured document.
431
- await page.click('#step-forward');
432
- await page.click('#step-forward');
433
- const replayed = await page.evaluate(() => ({
434
- step: document.getElementById('step').textContent,
435
- framed: document.getElementById('stage').getAttribute('srcdoc'),
436
- }));
437
- assert.match(replayed.step, /batch \d+ of \d+/);
438
- assert.match(replayed.framed, /id="item-1"|data-state="touched"/);
707
+ // One step is one batch, and a click reports the focus and the scroll it
708
+ // caused as well as what it changed in the DOM (issue #93), so the run's
709
+ // changes are reached by stepping to the end of the interval rather than
710
+ // by assuming which batch they landed in.
711
+ const batches = Number(shown.step.match(/^(\d+) mutation batches$/)[1]);
712
+ assert.ok(batches > 0, `nothing to step through: ${shown.step}`);
713
+ let replayed = null;
714
+ for (let step = 0; step < batches; step++) {
715
+ await page.click('#step-forward');
716
+ replayed = await page.evaluate(() => ({
717
+ step: document.getElementById('step').textContent,
718
+ framed: document.getElementById('stage').getAttribute('srcdoc'),
719
+ }));
720
+ assert.match(replayed.step, /batch \d+ of \d+/);
721
+ }
722
+ assert.match(replayed.framed, /id="item-1"/);
723
+ assert.match(replayed.framed, /data-state="touched"/);
439
724
  page.off('request', onRequest);
440
725
  // Opening a colleague's trace must not call anyone's server.
441
726
  assert.deepStrictEqual(
@@ -15,6 +15,15 @@ export const REPORT_BODY = 'quarterly report body';
15
15
  /** A body only its leading bytes identify as a PDF. */
16
16
  export const PDF_BODY = `%PDF-1.7\n${REPORT_BODY}`;
17
17
 
18
+ /**
19
+ * A PDF large enough that Chromium writes it in more than one chunk.
20
+ *
21
+ * Issue #92 is a race between `Browser.downloadProgress` reporting
22
+ * `completed` and the staged bytes being readable, so a body the browser can
23
+ * write in a single instant is the one body that would not exercise it.
24
+ */
25
+ export const BLOB_PDF_BODY = `%PDF-1.7\n${'blob pdf payload line\n'.repeat(40000)}%%EOF\n`;
26
+
18
27
  /** A page offering the download shapes the issue lists. */
19
28
  export const DOWNLOADS_PAGE = `<!doctype html>
20
29
  <html><body style="margin:0">
@@ -23,17 +32,28 @@ export const DOWNLOADS_PAGE = `<!doctype html>
23
32
  <a id="unnamed" href="/unnamed" download>Download without a name</a>
24
33
  <a id="broken" href="/aborted" download>Download a broken file</a>
25
34
  <button id="blob">Build and download</button>
35
+ <button id="blob-pdf">Build and download a PDF</button>
26
36
  <script>
27
- document.getElementById('blob').addEventListener('click', () => {
28
- const blob = new Blob([${JSON.stringify(REPORT_BODY)}], {
29
- type: 'text/plain',
30
- });
31
- const url = URL.createObjectURL(blob);
37
+ const downloadBlob = (parts, type, name) => {
38
+ const url = URL.createObjectURL(new Blob(parts, { type }));
32
39
  const anchor = document.createElement('a');
33
40
  anchor.href = url;
34
- anchor.download = 'generated.txt';
41
+ anchor.download = name;
35
42
  document.body.appendChild(anchor);
36
43
  anchor.click();
44
+ };
45
+ document.getElementById('blob').addEventListener('click', () => {
46
+ downloadBlob([${JSON.stringify(REPORT_BODY)}], 'text/plain', 'generated.txt');
47
+ });
48
+ document.getElementById('blob-pdf').addEventListener('click', () => {
49
+ // Built from a repeated unit rather than shipped as one literal: the
50
+ // page has to produce the bytes, not receive them over the wire.
51
+ const line = ${JSON.stringify('blob pdf payload line\n')};
52
+ downloadBlob(
53
+ ['%PDF-1.7\\n', line.repeat(40000), '%%EOF\\n'],
54
+ 'application/pdf',
55
+ 'generated.pdf'
56
+ );
37
57
  });
38
58
  </script>
39
59
  </body></html>`;
@@ -50,7 +50,10 @@ export function makeSnapshot(overrides = {}) {
50
50
  /**
51
51
  * Create a fake page the recorder can drive.
52
52
  *
53
- * @param {Object} [options] - `{snapshot, mutations, screenshot}`
53
+ * @param {Object} [options] - `{snapshot, mutations, screenshot, frames}`
54
+ * @param {Object[]} [options.frames] - Extra frames, each `{mutations}`; the
55
+ * page itself is always the main frame, and a page given no frames has no
56
+ * `frames()` at all, the way a page object from neither engine would
54
57
  * @returns {Object} The fake page
55
58
  */
56
59
  export function createFakePage(options = {}) {
@@ -62,10 +65,11 @@ export function createFakePage(options = {}) {
62
65
  screenshot: options.screenshot ?? Buffer.from('fake-png'),
63
66
  screenshots: 0,
64
67
  evaluated: [],
68
+ initScripts: [],
65
69
  failures: new Map(),
66
70
  };
67
71
 
68
- return {
72
+ const page = {
69
73
  state,
70
74
  /**
71
75
  * Make one in-page call fail, the way a closed page does.
@@ -112,7 +116,17 @@ export function createFakePage(options = {}) {
112
116
  const batches = state.mutations.splice(0, state.mutations.length);
113
117
  const dropped = state.droppedMutations;
114
118
  state.droppedMutations = 0;
115
- return { batches, dropped, installed: true };
119
+ return {
120
+ batches: batches.map((batch) => ({
121
+ frameId: 'main',
122
+ mainFrame: true,
123
+ ...batch,
124
+ })),
125
+ dropped,
126
+ installed: true,
127
+ frameId: 'main',
128
+ mainFrame: true,
129
+ };
116
130
  }
117
131
  return true;
118
132
  },
@@ -120,11 +134,73 @@ export function createFakePage(options = {}) {
120
134
  state.screenshots += 1;
121
135
  return state.screenshot;
122
136
  },
137
+ /**
138
+ * Register a script for every future document, the Playwright spelling.
139
+ *
140
+ * @param {Function} fn - The function to run
141
+ * @param {*} argument - Its argument
142
+ * @returns {Promise<void>} Resolves once registered
143
+ */
144
+ addInitScript: async (fn, argument) => {
145
+ state.initScripts.push({
146
+ name: typeof fn === 'function' ? fn.name : String(fn),
147
+ argument,
148
+ });
149
+ },
123
150
  on: (event, listener) => emitter.on(event, listener),
124
151
  off: (event, listener) => emitter.off(event, listener),
125
152
  emit: (event, payload) => emitter.emit(event, payload),
126
153
  listenerCount: (event) => emitter.listenerCount(event),
127
154
  };
155
+
156
+ if (options.frames) {
157
+ const children = options.frames.map((frame, index) =>
158
+ createFakeFrame(state, frame, `child-${index + 1}`)
159
+ );
160
+ page.frames = () => [
161
+ createFakeFrame(state, null, 'main', page),
162
+ ...children,
163
+ ];
164
+ }
165
+
166
+ return page;
167
+ }
168
+
169
+ /**
170
+ * One frame of a fake page.
171
+ *
172
+ * The main frame answers out of the page's own state, so a page with frames
173
+ * behaves exactly like one without for everything except the extra documents.
174
+ *
175
+ * @param {Object} state - The page's state
176
+ * @param {Object|null} frame - `{mutations, dropped}` for a child frame
177
+ * @param {string} frameId - What this frame calls itself
178
+ * @param {Object} [page] - The page, when this is the main frame
179
+ * @returns {Object} A frame with `evaluate`
180
+ */
181
+ function createFakeFrame(state, frame, frameId, page) {
182
+ if (page) {
183
+ return { evaluate: page.evaluate };
184
+ }
185
+ const queued = frame?.mutations ? [...frame.mutations] : [];
186
+ return {
187
+ evaluate: async (fn, argument) => {
188
+ const name = typeof fn === 'function' ? fn.name : String(fn);
189
+ state.evaluated.push({ name, argument, frameId });
190
+ if (name === 'drainMutationsInPage') {
191
+ return {
192
+ batches: queued
193
+ .splice(0, queued.length)
194
+ .map((batch) => ({ frameId, mainFrame: false, ...batch })),
195
+ dropped: frame?.dropped ?? 0,
196
+ installed: true,
197
+ frameId,
198
+ mainFrame: false,
199
+ };
200
+ }
201
+ return true;
202
+ },
203
+ };
128
204
  }
129
205
 
130
206
  /**
@@ -25,12 +25,19 @@ export const TRACE_PAGE = `<!doctype html>
25
25
  <select id="plan"><option value="free">Free</option><option value="pro">Pro</option></select>
26
26
  <label><input id="terms" type="checkbox"> I agree</label>
27
27
  </form>
28
- <ul id="items"></ul>
28
+ <ul id="items"><li id="item-a">a</li><li id="item-b">b</li></ul>
29
+ <div id="subtree"><p id="old-child">old</p></div>
29
30
  <p id="status" data-state="idle">idle</p>
31
+ <textarea id="notes"></textarea>
32
+ <div id="tall" style="height:3000px"></div>
30
33
  <iframe id="inner" name="inner" src="/frame" style="width:80px;height:40px"></iframe>
31
34
  <a id="next" href="/next">Go to the next page</a>
32
35
  <a id="download" href="/file/report.txt" download>Download the report</a>
33
36
  <button id="add" type="button">Add an item</button>
37
+ <button id="insert-first" type="button">Insert before the first item</button>
38
+ <button id="move-last" type="button">Move the last item to the front</button>
39
+ <button id="remove-one" type="button">Remove an item</button>
40
+ <button id="replace-subtree" type="button">Replace a subtree</button>
34
41
  <button id="touch" type="button">Change the status</button>
35
42
  <button id="log" type="button">Log something</button>
36
43
  <button id="boom" type="button">Throw</button>
@@ -44,6 +51,30 @@ export const TRACE_PAGE = `<!doctype html>
44
51
  item.textContent = 'item ' + added;
45
52
  document.getElementById('items').appendChild(item);
46
53
  });
54
+ // Issue #93's third regression case: a replay that only appends gets
55
+ // every one of these wrong.
56
+ const items = () => document.getElementById('items');
57
+ document.getElementById('insert-first').addEventListener('click', () => {
58
+ const item = document.createElement('li');
59
+ item.id = 'inserted';
60
+ item.textContent = 'inserted first';
61
+ items().insertBefore(item, items().firstChild);
62
+ });
63
+ document.getElementById('move-last').addEventListener('click', () => {
64
+ items().insertBefore(items().lastElementChild, items().firstChild);
65
+ });
66
+ document.getElementById('remove-one').addEventListener('click', () => {
67
+ const gone = document.getElementById('item-a');
68
+ if (gone) gone.remove();
69
+ });
70
+ document.getElementById('replace-subtree').addEventListener('click', () => {
71
+ const subtree = document.getElementById('subtree');
72
+ subtree.replaceChildren();
73
+ const fresh = document.createElement('p');
74
+ fresh.id = 'new-child';
75
+ fresh.textContent = 'new';
76
+ subtree.appendChild(fresh);
77
+ });
47
78
  document.getElementById('touch').addEventListener('click', () => {
48
79
  const status = document.getElementById('status');
49
80
  status.setAttribute('data-state', 'touched');
@@ -68,6 +99,27 @@ export const TRACE_PAGE = `<!doctype html>
68
99
  </script>
69
100
  </body></html>`;
70
101
 
102
+ /**
103
+ * A page that changes its own DOM while it is still loading.
104
+ *
105
+ * Issue #93's first regression case: the interval between a navigation and the
106
+ * next checkpoint used to be unrecorded, because the in-page observer died
107
+ * with the previous document. Everything this page builds happens before any
108
+ * automation could install an observer after the fact.
109
+ */
110
+ export const INIT_PAGE = `<!doctype html>
111
+ <html><head><title>Initializing page</title></head>
112
+ <body style="margin:0">
113
+ <div id="root"></div>
114
+ <script>
115
+ const made = document.createElement('p');
116
+ made.id = 'made-while-loading';
117
+ made.textContent = 'built during initialization';
118
+ document.getElementById('root').appendChild(made);
119
+ document.getElementById('root').setAttribute('data-initialized', 'yes');
120
+ </script>
121
+ </body></html>`;
122
+
71
123
  /** The page a full navigation lands on. */
72
124
  export const NEXT_PAGE = `<!doctype html>
73
125
  <html><head><title>Next page</title></head>
@@ -96,6 +148,10 @@ export async function startTraceServer() {
96
148
  sendHtml(res, TRACE_PAGE);
97
149
  return;
98
150
  }
151
+ if (path === '/init') {
152
+ sendHtml(res, INIT_PAGE);
153
+ return;
154
+ }
99
155
  if (path === '/next') {
100
156
  sendHtml(res, NEXT_PAGE);
101
157
  return;