114 lines
5.3 KiB
JavaScript
114 lines
5.3 KiB
JavaScript
// E02: reference table in a spreadsheet, unit conversion and formula.
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { createSceneRunner } from "../../../carrier-course/lib/scene.mjs";
|
|
import { openLineReference, spec } from "../../lib/ui.mjs";
|
|
|
|
export async function run({ page, rec, script, stillsDir, rawDir }) {
|
|
const scene = createSceneRunner({ rec, page, script });
|
|
const shot = name => rec.shot(stillsDir, name);
|
|
const log = [];
|
|
const logStep = entry => {
|
|
log.push({ at: new Date().toISOString(), ...entry });
|
|
fs.writeFileSync(path.join(rawDir, "live-log.json"), `${JSON.stringify(log, null, 2)}\n`);
|
|
};
|
|
const labels = () => page.locator(".fc-tree-text").allTextContents();
|
|
const round = value => Math.round(value * 10) / 10;
|
|
|
|
const setCell = async (address, value) => {
|
|
const cell = page.locator(`[role="gridcell"][data-cell-address="${address}"]`).first();
|
|
await cell.waitFor({ state: "visible", timeout: 30000 });
|
|
await cell.dblclick();
|
|
await page.waitForTimeout(300);
|
|
const input = page.locator('input[aria-label="Cell contents"]').first();
|
|
await input.fill(String(value));
|
|
await page.waitForTimeout(250);
|
|
const apply = page.locator('form button:has-text("Apply")').first();
|
|
if (await apply.isEnabled().catch(() => false)) await apply.click();
|
|
else await input.press("Enter");
|
|
await page.waitForTimeout(650);
|
|
};
|
|
const readCell = async address => {
|
|
const text = await page.locator(`[role="gridcell"][data-cell-address="${address}"]`).first().textContent();
|
|
return String(text ?? "").replace(new RegExp(`^${address}`), "").trim();
|
|
};
|
|
|
|
await scene("S00", async () => { await shot("S00-title"); });
|
|
|
|
await scene("S01", async () => {
|
|
const panel = await openLineReference(page);
|
|
for (let attempt = 0; attempt < 60; attempt++) {
|
|
await page.waitForTimeout(500);
|
|
if (/个网格/.test(await panel.locator("footer").textContent().catch(() => "") ?? "")) break;
|
|
}
|
|
const options = await panel.locator('select[aria-label="Reference model"] option').count();
|
|
if (options !== 27) throw new Error(`expected 27 reference models, got ${options}`);
|
|
await shot("S01-reference");
|
|
// Close the floating reference panel so the spreadsheet is unobstructed.
|
|
await panel.getByRole("button", { name: "Close reference viewer" }).click();
|
|
await page.waitForTimeout(400);
|
|
});
|
|
|
|
const rows = ["CabinetA", "Hopper", "ConveyorFeeder", "PickFeeder", "ABB_IRB_120", "GroundPlane"];
|
|
const chosen = rows.map(name => {
|
|
const model = spec().models.find(item => item.name === name);
|
|
const center = [0, 1, 2].map(axis => round((model.overallBBox[axis] + model.overallBBox[axis + 3]) / 2));
|
|
return { name, label: model.label, dims: model.overallDims.map(round), center };
|
|
});
|
|
|
|
await scene("S02", async () => {
|
|
const before = await labels();
|
|
await page.locator('select[aria-label="Workbench"]').selectOption("Spreadsheet");
|
|
await page.waitForTimeout(600);
|
|
await rec.clickSelector(page, 'button:has-text("Create spreadsheet")');
|
|
let sheet = null;
|
|
for (let attempt = 0; attempt < 30; attempt++) {
|
|
await page.waitForTimeout(500);
|
|
sheet = (await labels()).find(label => !before.includes(label)) ?? null;
|
|
if (sheet) break;
|
|
}
|
|
if (!sheet) throw new Error(`spreadsheet not created: ${JSON.stringify(await labels())}`);
|
|
logStep({ step: "sheet", sheet });
|
|
const header = ["模型", "长X", "宽Y", "高Z", "中心X", "中心Y", "中心Z"];
|
|
for (let column = 0; column < header.length; column++) await setCell(`${String.fromCharCode(65 + column)}1`, header[column]);
|
|
for (let row = 0; row < chosen.length; row++) {
|
|
await setCell(`A${row + 2}`, chosen[row].name);
|
|
const values = [...chosen[row].dims, ...chosen[row].center];
|
|
for (let column = 0; column < values.length; column++) await setCell(`${String.fromCharCode(66 + column)}${row + 2}`, values[column]);
|
|
logStep({ step: "row", name: chosen[row].name, dims: chosen[row].dims, center: chosen[row].center });
|
|
}
|
|
await shot("S02-table");
|
|
});
|
|
|
|
await scene("S03", async () => {
|
|
const first = chosen[0];
|
|
const expectedBefore = round(first.dims[0] + first.dims[1]);
|
|
const expectedAfter = round(1600 + first.dims[1]);
|
|
await setCell("A8", "长X+宽Y");
|
|
await setCell("B8", "=B2+C2");
|
|
const before = await readCell("B8");
|
|
await setCell("B2", 1600);
|
|
const after = await readCell("B8");
|
|
await setCell("B2", first.dims[0]);
|
|
const restored = await readCell("B8");
|
|
logStep({ step: "formula", before, after, restored });
|
|
const near = (actual, expected) => Math.abs(Number(actual) - expected) < 0.01;
|
|
if (!near(before, expectedBefore) || !near(after, expectedAfter) || !near(restored, expectedBefore))
|
|
throw new Error(`formula mismatch: ${before}/${expectedBefore} -> ${after}/${expectedAfter} -> ${restored}`);
|
|
await shot("S03-formula");
|
|
});
|
|
|
|
await scene("S04", async () => {
|
|
logStep({ step: "audit", rows: chosen.length, sheetVisible: await page.locator('[role="gridcell"][data-cell-address="A6"]').count() });
|
|
await shot("S04-audit");
|
|
});
|
|
|
|
await scene("S05", async () => {
|
|
fs.writeFileSync(path.join(rawDir, "E02-state.json"), `${JSON.stringify({
|
|
schema: "freecad-web-line-course/E02/state/v1",
|
|
generatedAt: new Date().toISOString(), rows: chosen, log,
|
|
}, null, 2)}\n`);
|
|
await shot("S05-recap");
|
|
});
|
|
}
|