250 lines
12 KiB
JavaScript
250 lines
12 KiB
JavaScript
import { writeFileSync } from "node:fs";
|
|
|
|
const cdpPort = process.env.CDP_PORT ?? "9226";
|
|
const baseUrl = process.env.SITE_URL ?? "http://127.0.0.1:4325";
|
|
const pages = await fetch(`http://127.0.0.1:${cdpPort}/json/list`).then((response) => response.json());
|
|
const page = pages.find((entry) => entry.type === "page");
|
|
if (!page) throw new Error(`CDP ${cdpPort} 没有可用页面`);
|
|
|
|
const socket = new WebSocket(page.webSocketDebuggerUrl);
|
|
await new Promise((resolve, reject) => {
|
|
socket.addEventListener("open", resolve, { once: true });
|
|
socket.addEventListener("error", reject, { once: true });
|
|
});
|
|
|
|
let nextId = 0;
|
|
const pending = new Map();
|
|
const exceptions = [];
|
|
socket.addEventListener("message", (event) => {
|
|
const message = JSON.parse(event.data);
|
|
if (message.id && pending.has(message.id)) {
|
|
const { resolve, reject } = pending.get(message.id);
|
|
pending.delete(message.id);
|
|
if (message.error) reject(new Error(message.error.message));
|
|
else resolve(message.result);
|
|
}
|
|
if (message.method === "Runtime.exceptionThrown") {
|
|
exceptions.push(message.params.exceptionDetails.exception?.description ?? message.params.exceptionDetails.text);
|
|
}
|
|
});
|
|
|
|
const command = (method, params = {}) => new Promise((resolve, reject) => {
|
|
const id = ++nextId;
|
|
pending.set(id, { resolve, reject });
|
|
socket.send(JSON.stringify({ id, method, params }));
|
|
});
|
|
const pause = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
const evaluate = async (expression) => {
|
|
const result = await command("Runtime.evaluate", { expression, returnByValue: true, awaitPromise: true });
|
|
if (result.exceptionDetails) throw new Error(result.exceptionDetails.exception?.description ?? result.exceptionDetails.text);
|
|
return result.result.value;
|
|
};
|
|
const navigate = async (path) => {
|
|
await command("Page.navigate", { url: `${baseUrl}${path}` });
|
|
for (let attempt = 0; attempt < 50; attempt += 1) {
|
|
await pause(100);
|
|
if (await evaluate("document.readyState === 'complete'")) return;
|
|
}
|
|
throw new Error(`${path} 加载超时`);
|
|
};
|
|
const screenshot = async (path) => {
|
|
const result = await command("Page.captureScreenshot", { format: "png", captureBeyondViewport: false });
|
|
writeFileSync(path, Buffer.from(result.data, "base64"));
|
|
};
|
|
|
|
await command("Page.enable");
|
|
await command("Runtime.enable");
|
|
await command("Emulation.setDeviceMetricsOverride", {
|
|
width: 1440,
|
|
height: 1100,
|
|
deviceScaleFactor: 1,
|
|
mobile: false,
|
|
});
|
|
await navigate("/pretraining/data/");
|
|
await screenshot("/tmp/llm-atlas-data-desktop.png");
|
|
|
|
const pipeline = await evaluate(`(() => {
|
|
const root = document.querySelector("[data-data-lab]");
|
|
const measure = () => ({
|
|
trainable: root.querySelector('[data-pipeline-metric="trainable"]').textContent,
|
|
retention: root.querySelector('[data-pipeline-metric="retention"]').textContent,
|
|
quarantine: root.querySelector('[data-pipeline-metric="quarantine"]').textContent,
|
|
rows: root.querySelectorAll(".funnel-row").length,
|
|
tail: root.querySelector('[data-pipeline-slice-label="tail"]').textContent,
|
|
});
|
|
const balanced = measure();
|
|
root.querySelector('[data-pipeline-preset="strict"]').click();
|
|
return { balanced, strict: measure() };
|
|
})()`);
|
|
|
|
const dedup = await evaluate(`(() => {
|
|
const root = document.querySelector("[data-data-lab]");
|
|
root.querySelector('[data-data-tab="dedup"]').click();
|
|
const measure = () => ({
|
|
precision: root.querySelector('[data-dedup-metric="precision"]').textContent,
|
|
recall: root.querySelector('[data-dedup-metric="recall"]').textContent,
|
|
falsePositive: root.querySelector('[data-dedup-metric="false"]').textContent,
|
|
rare: root.querySelector('[data-dedup-metric="rare"]').textContent,
|
|
rows: root.querySelectorAll("[data-dedup-example]").length,
|
|
});
|
|
root.querySelector('[data-dedup-mode="exact"]').click();
|
|
const exact = measure();
|
|
root.querySelector('[data-dedup-mode="semantic"]').click();
|
|
const threshold = root.querySelector('[data-dedup-input="threshold"]');
|
|
threshold.value = "85";
|
|
threshold.dispatchEvent(new Event("input", { bubbles: true }));
|
|
return { exact, semantic: measure() };
|
|
})()`);
|
|
|
|
const mixture = await evaluate(`(() => {
|
|
const root = document.querySelector("[data-data-lab]");
|
|
root.querySelector('[data-data-tab="mixture"]').click();
|
|
const measure = () => ({
|
|
weights: [...root.querySelectorAll("[data-mixture-output]")].slice(0, 5).map((item) => item.textContent),
|
|
epochs: [...root.querySelectorAll("[data-mixture-ledger] small")].map((item) => item.textContent),
|
|
scores: [...root.querySelectorAll("[data-mixture-slices] strong")].map((item) => item.textContent),
|
|
rows: root.querySelectorAll("[data-mixture-ledger] > div").length,
|
|
});
|
|
root.querySelector('[data-mixture-preset="balanced"]').click();
|
|
const balanced = measure();
|
|
root.querySelector('[data-mixture-preset="math"]').click();
|
|
return { balanced, math: measure() };
|
|
})()`);
|
|
|
|
const transform = await evaluate(`(() => {
|
|
const root = document.querySelector("[data-data-lab]");
|
|
root.querySelector('[data-data-tab="transform"]').click();
|
|
const measure = () => ({
|
|
variants: root.querySelector('[data-transform-metric="variants"]').textContent,
|
|
errors: root.querySelector('[data-transform-metric="errors"]').textContent,
|
|
cost: root.querySelector('[data-transform-metric="cost"]').textContent,
|
|
context: root.querySelector('[data-transform-metric="context"]').textContent,
|
|
stages: root.querySelectorAll("[data-transform-timeline] > div").length,
|
|
errorDots: root.querySelectorAll('[data-transform-facts] > i[data-kind="error"]').length,
|
|
});
|
|
root.querySelector('[data-transform-preset="production"]').click();
|
|
const production = measure();
|
|
root.querySelector('[data-transform-preset="risky"]').click();
|
|
const risky = measure();
|
|
const firstTab = root.querySelector('[data-data-tab="pipeline"]');
|
|
firstTab.focus();
|
|
firstTab.dispatchEvent(new KeyboardEvent("keydown", { key: "End", bubbles: true }));
|
|
return {
|
|
production,
|
|
risky,
|
|
keyboard: {
|
|
selected: root.querySelector('[data-data-tab][aria-selected="true"]').dataset.dataTab,
|
|
visible: root.querySelector("[data-data-view]:not([hidden])").dataset.dataView,
|
|
},
|
|
};
|
|
})()`);
|
|
|
|
const layout = await evaluate(`(() => {
|
|
const nav = document.querySelector(".top-nav");
|
|
const meta = document.querySelector(".header-meta");
|
|
return {
|
|
documentOverflow: document.documentElement.scrollWidth - document.documentElement.clientWidth,
|
|
navGap: Number((meta.getBoundingClientRect().left - nav.getBoundingClientRect().right).toFixed(1)),
|
|
navLinks: document.querySelectorAll(".top-nav a").length,
|
|
articleSections: document.querySelectorAll(".article-section").length,
|
|
paperLinks: document.querySelectorAll(".paper-chain a").length,
|
|
labTabs: document.querySelectorAll("[data-data-tab]").length,
|
|
views: document.querySelectorAll("[data-data-view]").length,
|
|
};
|
|
})()`);
|
|
|
|
await evaluate(`(() => {
|
|
document.documentElement.style.scrollBehavior = "auto";
|
|
document.querySelector("[data-data-lab]").scrollIntoView({ block: "start", behavior: "instant" });
|
|
})()`);
|
|
await pause(150);
|
|
await screenshot("/tmp/llm-atlas-data-lab-desktop.png");
|
|
|
|
await command("Emulation.setDeviceMetricsOverride", {
|
|
width: 390,
|
|
height: 844,
|
|
deviceScaleFactor: 1,
|
|
mobile: true,
|
|
});
|
|
await navigate("/pretraining/data/");
|
|
await evaluate(`document.querySelector("[data-data-lab]").scrollIntoView({ block: "start", behavior: "instant" })`);
|
|
await pause(120);
|
|
await screenshot("/tmp/llm-atlas-data-lab-mobile.png");
|
|
const mobile = await evaluate(`(() => {
|
|
const toggle = document.querySelector("#menu-toggle");
|
|
toggle.click();
|
|
return {
|
|
documentOverflow: document.documentElement.scrollWidth - document.documentElement.clientWidth,
|
|
menuVisible: getComputedStyle(toggle).display !== "none",
|
|
menuOpen: toggle.getAttribute("aria-expanded"),
|
|
mobileLinks: document.querySelectorAll("#mobile-nav a").length,
|
|
selectedTab: document.querySelector('[data-data-tab][aria-selected="true"]').dataset.dataTab,
|
|
};
|
|
})()`);
|
|
|
|
await command("Emulation.setDeviceMetricsOverride", {
|
|
width: 1440,
|
|
height: 1100,
|
|
deviceScaleFactor: 1,
|
|
mobile: false,
|
|
});
|
|
await navigate("/");
|
|
const home = await evaluate(`({
|
|
documentOverflow: document.documentElement.scrollWidth - document.documentElement.clientWidth,
|
|
releaseCards: document.querySelectorAll(".release-card").length,
|
|
firstRelease: document.querySelector(".release-card h2").textContent,
|
|
firstHref: document.querySelector(".release-card").getAttribute("href"),
|
|
paperCount: document.querySelector(".hero-stats div:nth-child(3) b").textContent,
|
|
navLinks: document.querySelectorAll(".top-nav a").length,
|
|
})`);
|
|
|
|
await navigate("/papers/");
|
|
const papers = await evaluate(`(() => {
|
|
const button = [...document.querySelectorAll("[data-filter]")].find((item) => item.dataset.filter === "数据");
|
|
button?.click();
|
|
return {
|
|
hasDataFilter: Boolean(button),
|
|
visible: [...document.querySelectorAll("[data-paper]")].filter((item) => !item.hidden).length,
|
|
total: document.querySelectorAll("[data-paper]").length,
|
|
};
|
|
})()`);
|
|
|
|
const report = { pipeline, dedup, mixture, transform, layout, mobile, home, papers, exceptions };
|
|
console.log(JSON.stringify(report, null, 2));
|
|
|
|
const numeric = (value) => Number.parseFloat(value);
|
|
const failures = [];
|
|
if (pipeline.balanced.rows !== 5 || pipeline.strict.rows !== 5) failures.push("Pipeline 漏斗阶段数量异常");
|
|
if (numeric(pipeline.strict.retention) >= numeric(pipeline.balanced.retention)) failures.push("激进过滤没有降低文档保留率");
|
|
if (dedup.exact.rows !== 6 || dedup.semantic.rows !== 6) failures.push("去重教学样本数量异常");
|
|
if (numeric(dedup.semantic.recall) <= numeric(dedup.exact.recall) || Number(dedup.semantic.falsePositive) < 1) {
|
|
failures.push("Semantic 去重没有体现召回—误伤权衡");
|
|
}
|
|
if (mixture.balanced.rows !== 5 || mixture.math.rows !== 5) failures.push("Mixture 五领域账本没有完整渲染");
|
|
if (numeric(mixture.math.scores[2]) <= numeric(mixture.balanced.scores[2])) failures.push("数学强化没有提升教学 Math slice");
|
|
if (numeric(transform.risky.errors) <= numeric(transform.production.errors) || transform.risky.errorDots <= transform.production.errorDots) {
|
|
failures.push("失控合成没有增加错误副本");
|
|
}
|
|
if (transform.production.stages !== 4 || transform.risky.stages !== 4) failures.push("公开课程阶段数量异常");
|
|
if (transform.keyboard.selected !== "transform" || transform.keyboard.visible !== "transform") failures.push("实验页签键盘导航异常");
|
|
if (layout.articleSections !== 18 || layout.paperLinks !== 31 || layout.labTabs !== 4 || layout.views !== 4) {
|
|
failures.push("章节、论文或实验数量异常");
|
|
}
|
|
if (layout.navLinks !== 15 || mobile.mobileLinks !== 15 || home.navLinks !== 15) failures.push("全站导航未同步数值专题");
|
|
if (layout.documentOverflow > 0 || mobile.documentOverflow > 0 || home.documentOverflow > 0) failures.push("页面存在横向溢出");
|
|
if (layout.navGap < 0) failures.push(`桌面导航碰撞:${layout.navGap}px`);
|
|
if (!mobile.menuVisible || mobile.menuOpen !== "true") failures.push("移动端菜单不可用");
|
|
if (home.releaseCards !== 10 || !home.firstRelease.includes("DPO 没有") || home.firstHref !== "/post-training/alignment/") {
|
|
failures.push("首页 Transformer 新章入口异常");
|
|
}
|
|
if (home.paperCount !== "280") failures.push(`首页论文总数异常:${home.paperCount}`);
|
|
if (!papers.hasDataFilter || papers.total !== 280 || papers.visible < 25) failures.push("论文库数据标签或论文总数异常");
|
|
if (exceptions.length) failures.push(`浏览器脚本异常:${exceptions.join("; ")}`);
|
|
|
|
socket.close();
|
|
if (failures.length) {
|
|
failures.forEach((failure) => console.error(`- ${failure}`));
|
|
process.exit(1);
|
|
}
|
|
console.log("数据工程专题真实 Chrome 断言通过。");
|