Files
KoalaSync/tests/e2e/helpers/content-source.mjs
T
KoalaDev 36e1291d2c feat: rank player candidates by ordered signals, add browser E2E suite
The weighted score summed incomparable units, so size could outvote traits
that disqualify an element outright. Measured on a real page: a display:none
preload reports its full 1080p intrinsic size and scored 2073600, beating a
visible unmuted player at 509920.

Selection now compares an ordered list of signals, highest priority first:
has a source, is rendered, is not a silent background loop, rendered size
bucket, is playing, has controls, duration. Rendered size replaces intrinsic
resolution, and mute state is gone from the ranking entirely: it is a viewer
preference, not evidence about which element is the player.

It stays a ranking rather than a filter, so a page of only bad candidates
still yields one and findVideo never returns null where a video exists.

The new tests/e2e suite runs the shipped finder against real fixture pages
and drives the packed extension for injection, reinjection and remote
play/pause/seek into a first-party frame. All five scoring scenarios fail
against the previous implementation.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-14 07:23:12 +02:00

66 lines
2.3 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..');
const contentPath = path.join(repoRoot, 'extension/content.js');
/**
* Pulls a top-level function out of content.js by brace matching.
*
* The E2E suite deliberately runs the shipped source rather than a copy: a
* fixture that passes against a reimplementation proves nothing about what the
* extension actually does.
*/
export function extractFunction(name, source = fs.readFileSync(contentPath, 'utf8')) {
const start = source.indexOf(`function ${name}`);
if (start === -1) throw new Error(`${name} not found in extension/content.js`);
let depth = 0;
for (let i = source.indexOf('{', start); i < source.length; i++) {
if (source[i] === '{') depth++;
if (source[i] === '}') {
depth--;
if (depth === 0) return source.slice(start, i + 1);
}
}
throw new Error(`${name} body did not terminate`);
}
/**
* Every function the video finder needs, as one evaluatable script that exposes
* window.__koalaFindVideo. Keeping this list here means a refactor that splits
* findVideo into helpers fails loudly instead of silently testing stale code.
*/
export const VIDEO_FINDER_EXPORTS = [
'findVideo',
'collectVideoCandidates',
'getRenderedVideoArea',
'getVideoSizeBucket',
'isVideoRendered',
'hasPlayableVideoSource',
'isBackgroundVideo',
'isVideoPlaying',
'compareVideoRanks',
'pickBestVideo'
];
/** The ranking table lives outside a function, so it is lifted by pattern. */
function extractRankingTable(source) {
const start = source.indexOf('const VIDEO_RANKING_SIGNALS');
if (start === -1) throw new Error('VIDEO_RANKING_SIGNALS not found in extension/content.js');
const end = source.indexOf('];', start);
if (end === -1) throw new Error('VIDEO_RANKING_SIGNALS did not terminate');
return source.slice(start, end + 2);
}
export function buildVideoFinderScript() {
const source = fs.readFileSync(contentPath, 'utf8');
const bodies = VIDEO_FINDER_EXPORTS.map(name => extractFunction(name, source));
return [
...bodies,
extractRankingTable(source),
'window.__koalaFindVideo = findVideo;'
].join('\n');
}