Files
mcn-short-video/project/短视频脚本创作/V1.0/mcn-work-shop/cdp-verify-videoinfo2.mjs
T

88 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* cdp-verify-videoinfo2.mjs — 验证:视频详情标题去重 + 第2/3行字段等宽统一(09-01) */
const CDP = 'http://127.0.0.1:9223';
async function getWs(targetId) {
const tabs = await (await fetch(CDP + '/json')).json();
const t = tabs.find((x) => x.id === targetId);
if (!t) throw new Error('target not found: ' + targetId);
return t.webSocketDebuggerUrl;
}
function cdp(ws, method, params = {}) {
return new Promise((resolve, reject) => {
const id = ++cdp._id;
const w = new WebSocket(ws);
const timer = setTimeout(() => { try { w.close(); } catch (e) {} reject(new Error('timeout: ' + method)); }, 30000);
w.onopen = () => w.send(JSON.stringify({ id, method, params }));
w.onmessage = (ev) => {
const m = JSON.parse(ev.data);
if (m.id !== id) return;
clearTimeout(timer);
try { w.close(); } catch (e) {}
m.error ? reject(new Error(method + ': ' + JSON.stringify(m.error))) : resolve(m.result);
};
w.onerror = () => { clearTimeout(timer); reject(new Error('ws error: ' + method)); };
});
}
cdp._id = 0;
async function evalJs(ws, expr) {
const r = await cdp(ws, 'Runtime.evaluate', { expression: expr, awaitPromise: true, returnByValue: true });
if (r.exceptionDetails) throw new Error('eval err: ' + JSON.stringify(r.exceptionDetails).slice(0, 300));
return r.result && r.result.value;
}
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const waitFor = async (ws, expr, timeout = 15000, label = '') => {
const t0 = Date.now();
while (Date.now() - t0 < timeout) {
if (await evalJs(ws, expr)) return true;
await sleep(300);
}
throw new Error('waitFor timeout: ' + label);
};
async function main() {
const tabs = await (await fetch(CDP + '/json')).json();
const tab = tabs.find((x) => x.url.includes('localhost:8900')) || tabs.find((x) => x.type === 'page');
if (!tab) throw new Error('no 8900 tab found');
const ws = await getWs(tab.id);
await cdp(ws, 'Page.enable');
await cdp(ws, 'Runtime.enable');
let pass = 0, fail = 0;
const check = (name, cond) => { cond ? pass++ : fail++; console.log((cond ? 'PASS' : 'FAIL') + ' | ' + name); };
await cdp(ws, 'Page.navigate', { url: 'http://localhost:8900/?v=vinfo2#/video/680' });
await waitFor(ws, `!!document.querySelector('.video-detail-title')`, 15000, '视频详情渲染');
const r = await evalJs(ws, `(() => {
const card = document.querySelector('.video-info');
const title = card ? card.querySelector('.video-detail-title') : null;
const toolbarTitle = document.querySelector('.page-toolbar .page-title');
const rows = card ? [...card.querySelectorAll('.info-row')] : [];
const rowWidths = rows.slice(0, 2).map((row) => [...row.querySelectorAll('.info-item')].map((it) => Math.round(it.getBoundingClientRect().width)));
const titleText = title ? title.textContent.trim() : '';
const titleOccur = document.body.textContent.split(titleText).length - 1;
return {
toolbarTitle: toolbarTitle ? toolbarTitle.textContent.trim() : '',
hasCardTitle: !!title,
cardTitle: titleText.slice(0, 20),
titleOccur,
rowCount: rows.length,
rowWidths,
hasRenderErr: document.body.textContent.includes('渲染失败')
};
})()`);
check('toolbar 标题=「视频详情」静态(' + r.toolbarTitle + ')', r.toolbarTitle === '视频详情');
check('信息卡标题行存在(' + r.cardTitle + ')', r.hasCardTitle && !!r.cardTitle);
check('视频标题全页仅出现 1 次(' + r.titleOccur + ')', r.titleOccur === 1);
check('共 3 组信息行', r.rowCount === 3);
const w1 = r.rowWidths[0] || [], w2 = r.rowWidths[1] || [];
check('第2行 4 字段等宽(' + JSON.stringify(w1) + ')', w1.length === 4 && new Set(w1).size === 1);
check('第3行 4 字段等宽(' + JSON.stringify(w2) + ')', w2.length === 4 && new Set(w2).size === 1);
check('第2/3行字段宽度一致(' + (w1[0] || '') + ' vs ' + (w2[0] || '') + ')', w1.length === 4 && w2.length === 4 && w1[0] === w2[0]);
check('无渲染失败', !r.hasRenderErr);
console.log(`\n结果: ${pass} PASS / ${fail} FAIL`);
process.exit(fail ? 1 : 0);
}
main().catch((e) => { console.error('ERROR:', e.message); process.exit(1); });