Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/isomorphic/trace/snapshotRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ import type { PageEntry } from './entries';
import type { LRUCache } from '../lruCache';

function findClosest<T>(items: T[], metric: (v: T) => number, target: number) {
return items.find((item, index) => {
if (index === items.length - 1)
return true;
const next = items[index + 1];
return Math.abs(metric(item) - target) < Math.abs(metric(next) - target);
});
let closest: T | undefined;
let closestDistance = Infinity;
// Chromium may deliver screencast frames out of order by swap wall time,
// see https://issues.chromium.org/issues/578117966.
for (const item of items) {
const distance = Math.abs(metric(item) - target);
if (distance <= closestDistance) {
closest = item;
closestDistance = distance;
}
}
return closest;
}

function isNodeNameAttributesChildNodesSnapshot(n: NodeSnapshot): n is NodeNameAttributesChildNodesSnapshot {
Expand Down
28 changes: 28 additions & 0 deletions tests/library/snapshot-renderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ function makeSnapshot(overrides: Partial<FrameSnapshot> = {}): FrameSnapshot {
};
}

test('TV-136 snapshot renderer selects the nearest out-of-order screencast frame', {
annotation: [
{ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43573' },
{ type: 'issue', description: 'https://issues.chromium.org/issues/578117966' },
],
}, () => {
// Chromium encodes screencast frames in parallel and sends each one when
// its encoding finishes. Frame 3 was swapped before frame 2, but took
// longer to encode, so it was received later.
const frameSwapWallTimes = [1000, 1017, 1050, 1033, 1067];
const frames = frameSwapWallTimes.map((frameSwapWallTime, index) => ({
file: `${index}.jpeg`,
width: 1280,
height: 720,
timestamp: 1000 + index * 17,
frameSwapWallTime,
}));
const snapshots = [
makeSnapshot({ timestamp: 1030, wallTime: 1030 }),
makeSnapshot({ timestamp: 1070, wallTime: 1070 }),
];
const renderer1 = new SnapshotRenderer(new LRUCache(1_000_000), [], snapshots, frames, 0);
expect(renderer1.closestScreenshot()).toBe('3.jpeg');
// Every later snapshot should not be stuck on the frame before the reversed pair.
const renderer2 = new SnapshotRenderer(new LRUCache(1_000_000), [], snapshots, frames, 1);
expect(renderer2.closestScreenshot()).toBe('4.jpeg');
});

for (const [name, overrides] of [
['callId', { callId: '</script><script>window.__pwned__=true;//' }],
['snapshotName', { callId: 'call-1', snapshotName: '</script><img src=x onerror=alert(1)>' }],
Expand Down
1 change: 1 addition & 0 deletions tests/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -5233,6 +5233,7 @@ subsystem, add a row to the Subsystems table. Then update this file:
| `TV-133` | `library/snapshot-renderer.spec.ts` | snapshot renderer still renders ordinary custom elements |
| `TV-134` | `library/snapshot-renderer.spec.ts` | stripAnsiEscapes should not exhibit polynomial backtracking |
| `TV-135` | `library/snapshot-renderer.spec.ts` | stripAnsiEscapes handles common ANSI sequences after fix |
| `TV-136` | `library/snapshot-renderer.spec.ts` | snapshot renderer selects the nearest out-of-order screencast frame |

### TZ — Timezone

Expand Down
Loading