| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- const assert = require("node:assert/strict");
- const fs = require("node:fs");
- const path = require("node:path");
- const {chromium} = require(path.resolve(__dirname, "../../tools/board-viewer/node_modules/playwright"));
- const root = path.resolve(__dirname, "../..");
- const baseUrl = process.env.BOARD_VIEWER_URL || "http://127.0.0.1:8765";
- const origin = new URL(baseUrl).origin;
- const artifacts = process.env.BOARD_VIEWER_E2E_ARTIFACTS || path.resolve(process.env.TEMP || root, "board-viewer-e2e");
- const executablePath = process.env.BOARD_VIEWER_BROWSER || "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe";
- const boardData = JSON.parse(fs.readFileSync(path.join(root, "tools/board-viewer/generated/board-data.json"), "utf8"));
- fs.mkdirSync(artifacts, {recursive: true});
- function clone(value) {
- return JSON.parse(JSON.stringify(value));
- }
- async function createPage(browser, viewport, options = {}) {
- const context = await browser.newContext({
- viewport,
- permissions: ["clipboard-read", "clipboard-write"],
- locale: "zh-CN",
- });
- const consoleErrors = [];
- const requestFailures = [];
- const badResponses = [];
- const page = await context.newPage();
- page.on("console", (message) => {
- if (message.type() === "error") consoleErrors.push(message.text());
- });
- page.on("requestfailed", (request) => requestFailures.push(`${request.url()}: ${request.failure()?.errorText}`));
- page.on("response", (response) => {
- if (response.status() >= 400) badResponses.push(`${response.status()} ${response.url()}`);
- });
- await page.route("**/*", async (route) => {
- const url = new URL(route.request().url());
- if (url.origin !== origin) {
- await route.abort("blockedbyclient");
- return;
- }
- if (options.data && url.pathname === "/generated/board-data.json") {
- await route.fulfill({status: 200, contentType: "application/json", body: JSON.stringify(options.data)});
- return;
- }
- if (options.runtime && url.pathname === "/api/status") {
- await route.fulfill({status: 200, contentType: "application/json", body: JSON.stringify(options.runtime)});
- return;
- }
- await route.continue();
- });
- await page.goto(baseUrl, {waitUntil: "networkidle"});
- await page.waitForFunction(() => document.querySelectorAll(".hotspot").length === 110);
- return {context, page, consoleErrors, requestFailures, badResponses};
- }
- async function assertHealthy(session) {
- await session.page.waitForTimeout(50);
- assert.deepEqual(session.consoleErrors, [], `console errors: ${session.consoleErrors.join("\n")}`);
- assert.deepEqual(session.requestFailures, [], `request failures: ${session.requestFailures.join("\n")}`);
- assert.deepEqual(session.badResponses, [], `bad responses: ${session.badResponses.join("\n")}`);
- }
- async function assertNoViewportOverflow(page) {
- const metrics = await page.evaluate(() => {
- const viewport = {width: window.innerWidth, height: window.innerHeight};
- const topbar = document.querySelector(".topbar").getBoundingClientRect();
- const board = document.querySelector(".board-pane").getBoundingClientRect();
- const boardViewport = document.querySelector("#board-viewport").getBoundingClientRect();
- const footer = document.querySelector(".board-footer").getBoundingClientRect();
- const panel = document.querySelector("#detail-panel").getBoundingClientRect();
- return {
- viewport,
- scrollWidth: document.documentElement.scrollWidth,
- scrollHeight: document.documentElement.scrollHeight,
- topbar: {left: topbar.left, right: topbar.right, top: topbar.top, bottom: topbar.bottom},
- board: {left: board.left, right: board.right, top: board.top, bottom: board.bottom},
- boardViewport: {top: boardViewport.top, bottom: boardViewport.bottom, height: boardViewport.height},
- footer: {top: footer.top, bottom: footer.bottom, height: footer.height},
- panel: {left: panel.left, right: panel.right, top: panel.top, bottom: panel.bottom},
- loadingDisplay: getComputedStyle(document.querySelector("#board-loading")).display,
- };
- });
- assert.ok(metrics.scrollWidth <= metrics.viewport.width + 1, JSON.stringify(metrics));
- assert.ok(metrics.scrollHeight <= metrics.viewport.height + 1, JSON.stringify(metrics));
- assert.ok(metrics.topbar.left >= -1 && metrics.topbar.right <= metrics.viewport.width + 1, JSON.stringify(metrics));
- assert.ok(metrics.board.left >= -1 && metrics.board.right <= metrics.viewport.width + 1, JSON.stringify(metrics));
- assert.ok(metrics.panel.left >= -1 && metrics.panel.right <= metrics.viewport.width + 1, JSON.stringify(metrics));
- assert.equal(metrics.loadingDisplay, "none", JSON.stringify(metrics));
- if (metrics.viewport.width > 860) {
- assert.ok(metrics.boardViewport.height > 400, JSON.stringify(metrics));
- assert.ok(metrics.footer.height <= 34, JSON.stringify(metrics));
- assert.ok(metrics.footer.bottom >= metrics.viewport.height - 1, JSON.stringify(metrics));
- }
- }
- async function selectSearchResult(page, query, expectedKey) {
- const search = page.locator("#board-search");
- await search.fill(query);
- const result = page.locator(".search-result").filter({hasText: expectedKey}).first();
- await result.waitFor({state: "visible"});
- await result.click();
- }
- async function connectorGrid(page) {
- return page.locator(".connector-grid-row").evaluateAll((rows) =>
- rows.map((row) => Array.from(row.querySelectorAll(".connector-contact")).map((pin) => pin.textContent.trim())),
- );
- }
- async function assertRenderedPinPositionsMatchGeneratedData(page) {
- const rendered = await page.locator(".pin-group").evaluateAll((groups) => Object.fromEntries(
- groups.map((group) => {
- const dot = group.querySelector(".pin-dot");
- return [group.dataset.pinId, {x: Number(dot.getAttribute("cx")), y: Number(dot.getAttribute("cy"))}];
- }),
- ));
- const expected = Object.fromEntries(boardData.touchpoints.flatMap((touchpoint) =>
- touchpoint.pins.map((pin) => [pin.id, pin.view_position]),
- ));
- assert.equal(Object.keys(rendered).length, 472);
- assert.deepEqual(Object.keys(rendered).sort(), Object.keys(expected).sort());
- for (const [id, point] of Object.entries(expected)) {
- assert.ok(Math.abs(rendered[id].x - point.x) < 1e-9, `${id} x: ${rendered[id].x} != ${point.x}`);
- assert.ok(Math.abs(rendered[id].y - point.y) < 1e-9, `${id} y: ${rendered[id].y} != ${point.y}`);
- }
- }
- async function desktopWorkflow(browser, viewport, name) {
- const session = await createPage(browser, viewport);
- const {page} = session;
- try {
- await page.getByText("数据已校验", {exact: true}).waitFor();
- assert.equal(await page.locator(".hotspot").count(), 110);
- assert.equal(await page.locator(".pin-group").count(), 472);
- await assertRenderedPinPositionsMatchGeneratedData(page);
- assert.equal(await page.locator(".pin-group").first().getAttribute("tabindex"), "-1");
- assert.equal(await page.locator("#photo-layer").isDisabled(), true);
- assert.equal(await page.locator('#layer-controls input[value="copper"]').isChecked(), true);
- assert.equal(await page.locator("#layer-copper").isVisible(), true);
- assert.equal(await page.locator("#layer-mask").getAttribute("href"), "/generated/board-mask-edge.svg");
- await assertNoViewportOverflow(page);
- await page.screenshot({path: path.join(artifacts, `${name}-initial.png`), fullPage: true});
- const con78 = page.locator('.hotspot[data-reference="CON78"]');
- const con78Box = await con78.boundingBox();
- assert.ok(con78Box);
- const connectorPosition = {x: con78Box.width / 2, y: Math.min(1, con78Box.height / 4)};
- await con78.hover({position: connectorPosition});
- await page.getByRole("tooltip").getByText("CON78", {exact: true}).waitFor();
- await con78.click({position: connectorPosition});
- await page.locator(".detail-reference").getByText("CON78", {exact: true}).waitFor();
- await page.getByText("板正面接触针排列", {exact: true}).waitFor();
- await page.getByText("板正面俯视", {exact: false}).waitFor();
- await page.getByText("线缆端或对接面可能是镜像", {exact: false}).waitFor();
- assert.deepEqual(await connectorGrid(page), [["1"], ["2"], ["3"], ["4"]]);
- assert.equal(await page.locator('.pin-row[data-pad-role="contact"]').count(), 4);
- assert.equal(await page.locator('.pin-row[data-pad-role="shield"]').count(), 2);
- assert.equal(await page.locator('.pin-group[data-reference="CON78"][data-pin="5"]').getAttribute("data-pad-role"), "shield");
- await page.getByText("屏蔽固定焊盘", {exact: true}).first().waitFor();
- await page.getByText("上", {exact: true}).first().waitFor();
- await page.getByText("下", {exact: true}).first().waitFor();
- await selectSearchResult(page, "CON31", "CON31");
- assert.deepEqual(await connectorGrid(page), [["4"], ["3"], ["2"], ["1"]]);
- await selectSearchResult(page, "CON18", "CON18");
- assert.deepEqual(await connectorGrid(page), [
- ["13", "11", "9", "7", "5", "3", "1"],
- ["14", "12", "10", "8", "6", "4", "2"],
- ]);
- await selectSearchResult(page, "CON23", "CON23");
- assert.deepEqual(await connectorGrid(page), [["5", "1"], ["6", "2"], ["7", "3"], ["8", "4"]]);
- await selectSearchResult(page, "J3", "J3");
- await page.getByText("EF2L45BG256B FPGA JTAG 调试/配置接口(专用配置脚)", {exact: true}).waitFor();
- await page.locator(".category-tag").getByText("调试接口", {exact: true}).waitFor();
- assert.deepEqual(await connectorGrid(page), [
- ["6", "1"],
- ["7", "2"],
- ["8", "3"],
- ["9", "4"],
- ["10", "5"],
- ]);
- const j3Pin1Primary = await page.locator('.pin-row[data-pin="1"] .pin-primary > span').allTextContents();
- assert.match(j3Pin1Primary[0], /TCK/);
- assert.match(j3Pin1Primary[1], /原始网络.*N9532014/);
- await selectSearchResult(page, "J3.1", "J3.1");
- await page.getByText("FPGA 专用 JTAG 时钟输入", {exact: false}).first().waitFor();
- await page.getByText("U3.A7 专用 TCK", {exact: true}).waitFor();
- await page.getByText("R63 22R 串联", {exact: true}).first().waitFor();
- await page.getByText("不要在 RTL 顶层声明", {exact: false}).waitFor();
- await page.getByText("专用配置脚不使用用户 ADC 约束", {exact: true}).waitFor();
- assert.equal(await page.getByLabel("RTL 端口名").count(), 0);
- await page.screenshot({path: path.join(artifacts, `${name}-j3.png`), fullPage: true});
- await assertNoViewportOverflow(page);
- await selectSearchResult(page, "J3.4", "J3.4");
- await page.getByText("不是给板子供电", {exact: false}).first().waitFor();
- await page.getByText("R61 0R", {exact: false}).first().waitFor();
- await page.getByText("不要通过 VREF 反向给板子供电", {exact: false}).waitFor();
- await selectSearchResult(page, "J3.6", "J3.6");
- await page.getByText("不能把 NC 当作备用 GPIO", {exact: false}).waitFor();
- await selectSearchResult(page, "J11", "J11");
- await page.locator(".category-tag").getByText("安装孔", {exact: true}).waitFor();
- await page.getByText("M3 安装孔(非电气接口)", {exact: true}).waitFor();
- await selectSearchResult(page, "J8", "J8");
- await page.locator(".category-tag").getByText("制造标记", {exact: true}).waitFor();
- await selectSearchResult(page, "PL2001", "PL2001");
- await page.locator(".category-tag").getByText("电源器件", {exact: true}).waitFor();
- await page.getByText("+5V 开关电源电感(非连接器)", {exact: true}).waitFor();
- const beforeZoom = await page.locator("#board-svg").getAttribute("viewBox");
- await page.getByRole("button", {name: "放大"}).click();
- const afterZoom = await page.locator("#board-svg").getAttribute("viewBox");
- assert.notEqual(afterZoom, beforeZoom);
- await selectSearchResult(page, "CON78.1", "CON78.1");
- assert.equal(await page.locator('.pin-group[data-reference="CON78"][data-pin="1"]').getAttribute("tabindex"), "0");
- await page.getByText("U23 TRSF3232EIPWR", {exact: true}).first().waitFor();
- await page.locator(".pin-detail .detail-facts").getByText("FPGA M10", {exact: true}).waitFor();
- await page.locator(".pin-detail").getByText("RS232_TX2", {exact: true}).first().waitFor();
- await page.getByText("编程结论", {exact: true}).waitFor();
- await page.getByText("FPGA 输出", {exact: false}).waitFor();
- await page.getByText("连接到对端设备的接收端(RX)。", {exact: true}).waitFor();
- await page.getByText("不能直接接 3.3V TTL/CMOS 串口", {exact: false}).waitFor();
- await page.getByText("LOCATION = M10", {exact: true}).waitFor();
- await page.getByText("IOSTANDARD = LVCMOS33", {exact: true}).waitFor();
- const groundPin = page.locator('.pin-group[data-reference="CON78"][data-pin="3"] .pin-hit');
- await groundPin.hover();
- await page.getByRole("tooltip").getByText("CON78.3", {exact: true}).waitFor();
- await groundPin.click();
- assert.ok(await page.locator(".net-highlight").count() > 20);
- await selectSearchResult(page, "CON30.4", "CON30.4");
- await page.getByText("MOTOR_EN_N", {exact: false}).first().waitFor();
- await page.getByText("低电平有效", {exact: true}).waitFor();
- await page.getByText("R937 4.7K to +3V3", {exact: true}).waitFor();
- await page.getByText("LOCATION = A10", {exact: true}).waitFor();
- await selectSearchResult(page, "CON31.4", "CON31.4");
- await page.getByText("开集电极", {exact: false}).first().waitFor();
- await page.getByText("FPGA high", {exact: false}).first().waitFor();
- await page.getByText("LOCATION = E10", {exact: true}).waitFor();
- await selectSearchResult(page, "CON58.4", "CON58.4");
- await page.getByText("上升沿计步脉冲", {exact: true}).waitFor();
- await page.getByText("每个上升沿表示一步事件", {exact: false}).first().waitFor();
- await selectSearchResult(page, "CON65.4", "CON65.4");
- await page.getByText("状态选择(非有效电平)", {exact: true}).waitFor();
- await page.getByText("不存在“高电平有效”的通用含义", {exact: false}).waitFor();
- await selectSearchResult(page, "CON53.1", "CON53.1");
- await page.getByText("open-drain", {exact: false}).waitFor();
- await page.getByText("只拉低或释放", {exact: false}).waitFor();
- await selectSearchResult(page, "CON2.5", "CON2.5");
- await page.getByText("板端电源输入", {exact: false}).first().waitFor();
- await page.getByText("CON2.5 +12V_VIN", {exact: true}).waitFor();
- await selectSearchResult(page, "TP9.1", "TP9.1");
- await page.locator(".pin-detail .detail-facts").getByText("GD32F303CGT6 U27", {exact: true}).waitFor();
- await page.locator(".pin-detail .detail-facts").getByText("U27.10 (PA0)", {exact: true}).waitFor();
- await page.getByText("MCU 固件用法", {exact: true}).waitFor();
- await page.getByText("不要在 FPGA RTL/ADC 中声明此针", {exact: false}).waitFor();
- await page.locator("#candidate-tab").click();
- await page.locator("#capability-select").selectOption("pwm");
- const pwmCandidate = page.locator(".candidate-row").filter({hasText: "CON31.4 · E10"});
- await pwmCandidate.locator(".candidate-key").getByText("CON31.4 · E10", {exact: true}).waitFor();
- await pwmCandidate.getByText("FPGA high -> Q105", {exact: false}).waitFor();
- await page.locator("#capability-select").selectOption("gpio");
- assert.equal(await page.locator(".candidate-row").count(), 33);
- assert.equal(await page.locator(".candidate-row").filter({hasText: "CON31.4"}).count(), 0);
- await page.locator("#capability-select").selectOption("uart_tx");
- await page.getByText("没有满足全部证据条件的候选", {exact: true}).waitFor();
- await page.locator("#detail-tab").click();
- await page.locator("#board-search").fill("does-not-exist");
- await page.getByText("没有匹配的座子或针脚", {exact: true}).waitFor();
- await page.screenshot({path: path.join(artifacts, `${name}-selected.png`), fullPage: true});
- await assertNoViewportOverflow(page);
- await assertHealthy(session);
- } finally {
- await session.context.close();
- }
- }
- async function mobileWorkflow(browser) {
- const session = await createPage(browser, {width: 390, height: 844});
- const {page} = session;
- try {
- await assertNoViewportOverflow(page);
- await page.screenshot({path: path.join(artifacts, "390x844-initial.png"), fullPage: true});
- assert.equal(await page.locator("#app").evaluate((node) => node.classList.contains("detail-collapsed")), true);
- await selectSearchResult(page, "CON78", "CON78");
- assert.equal(await page.locator("#app").evaluate((node) => node.classList.contains("detail-collapsed")), false);
- await page.locator(".detail-reference").getByText("CON78", {exact: true}).waitFor();
- await page.screenshot({path: path.join(artifacts, "390x844-selected.png"), fullPage: true});
- await assertNoViewportOverflow(page);
- await selectSearchResult(page, "J3.1", "J3.1");
- await page.getByText("FPGA 专用 JTAG 时钟输入", {exact: false}).first().waitFor();
- await page.screenshot({path: path.join(artifacts, "390x844-j3.png"), fullPage: true});
- await assertNoViewportOverflow(page);
- await assertHealthy(session);
- } finally {
- await session.context.close();
- }
- }
- async function adcCopyWorkflow(browser) {
- const data = clone(boardData);
- const con78 = data.touchpoints.find((item) => item.reference === "CON78");
- const pin = con78.pins.find((item) => item.number === "1");
- pin.reserved = false;
- pin.recommendation = {eligible: true, reasons: ["capability_match", "verified_fpga_path", "external_level_known"]};
- const runtime = {
- status: "pass",
- staleSources: [],
- recommendationsEnabled: true,
- adcCopyEnabled: true,
- };
- const session = await createPage(browser, {width: 1366, height: 768}, {data, runtime});
- const {page} = session;
- try {
- await selectSearchResult(page, "CON78.1", "CON78.1");
- const input = page.getByLabel("RTL 端口名");
- assert.equal(await input.isEnabled(), true);
- await input.fill("uart2_tx");
- await page.getByRole("button", {name: "复制 ADC 片段"}).click();
- await page.getByText("ADC 片段已复制", {exact: true}).waitFor();
- const clipboard = await page.evaluate(() => navigator.clipboard.readText());
- assert.equal(clipboard, "set_pin_assignment { uart2_tx } { LOCATION = M10; IOSTANDARD = LVCMOS33; }");
- await assertHealthy(session);
- } finally {
- await session.context.close();
- }
- }
- async function staleWorkflow(browser) {
- const runtime = {
- status: "stale_sources",
- staleSources: ["sha256_mismatch:inventory"],
- recommendationsEnabled: false,
- adcCopyEnabled: false,
- };
- const session = await createPage(browser, {width: 1366, height: 768}, {runtime});
- const {page} = session;
- try {
- await page.getByText("来源已变化", {exact: true}).waitFor();
- await page.locator("#candidate-tab").click();
- await page.getByText("源文件状态不是当前值,选脚推荐已关闭。", {exact: true}).waitFor();
- await page.screenshot({path: path.join(artifacts, "stale-state.png"), fullPage: true});
- await assertHealthy(session);
- } finally {
- await session.context.close();
- }
- }
- (async () => {
- const browser = await chromium.launch({headless: true, executablePath});
- try {
- await desktopWorkflow(browser, {width: 1920, height: 1080}, "1920x1080");
- await desktopWorkflow(browser, {width: 1366, height: 768}, "1366x768");
- await mobileWorkflow(browser);
- await adcCopyWorkflow(browser);
- await staleWorkflow(browser);
- process.stdout.write(`Playwright board-viewer E2E passed. Artifacts: ${artifacts}\n`);
- } finally {
- await browser.close();
- }
- })().catch((error) => {
- console.error(error);
- process.exit(1);
- });
|