// Assembly helpers shared by the line-course E17 episode. export async function executeCommand(page, command, payload, timeout = 300000) { const before = await page.evaluate(() => window.__freecadWorkbenchState?.response?.requestId ?? null); await page.locator(`button[data-help-page="${command}"]`).first().click(); await page.locator("#payload-editor").fill(JSON.stringify(payload)); await page.getByRole("button", { name: "执行选中命令" }).click(); await page.waitForFunction(id => { const response = window.__freecadWorkbenchState?.response; return Boolean(response) && response.requestId !== id; }, before, { timeout }); const response = await page.evaluate(() => window.__freecadWorkbenchState?.response); if (response?.kind !== "result") throw new Error(`${command}: ${response?.error?.code} ${response?.error?.message}`); return response.result?.records?.find(record => record.status === "exposed")?.result ?? response.result; } export async function createAssembly(page, { document = "LineAssembly", label = "生产线总装" } = {}) { const create = page.getByRole("region", { name: "Create assembly" }); await create.waitFor({ state: "visible", timeout: 60000 }); await page.locator('input[aria-label="Assembly document name"]').fill(document); await page.locator('input[aria-label="Assembly label"]').fill(label); await create.getByRole("button", { name: "Create", exact: true }).click(); const linksTask = page.getByRole("region", { name: "Insert assembly link" }); await linksTask.waitFor({ state: "visible", timeout: 120000 }); return linksTask; } export async function insertLinks(page, linksTask, source = "line-course-E16.FCStd", expectedLinks = 27) { await page.locator('select[aria-label="Insert link source file"]').selectOption(source); await linksTask.getByRole("button", { name: "Load source", exact: true }).click(); await page.waitForFunction(() => { const select = document.querySelector('select[aria-label="Insert link source object"]'); return select && select.options.length > 1; }, null, { timeout: 180000 }); const options = (await page.locator('select[aria-label="Insert link source object"] option').allTextContents()) .filter(option => option && !option.startsWith("Choose")); const select = page.locator('select[aria-label="Insert link source object"]'); const list = linksTask.locator('ul[aria-label="Inserted links"] li'); for (const option of options) { await select.selectOption({ label: option }); await page.waitForTimeout(350); await linksTask.getByRole("button", { name: "Insert", exact: true }).click(); await page.waitForTimeout(600); } await page.waitForTimeout(1200); await linksTask.getByRole("button", { name: "Accept", exact: true }).click(); await page.waitForTimeout(1500); const items = await list.allTextContents(); const linkNames = items.map(text => /→\s+([^(]+?)\s+\(/.exec(text)?.[1]?.trim()).filter(Boolean); if (items.length !== expectedLinks) throw new Error(`expected ${expectedLinks} links, got ${items.length}`); return { linkNames, items }; } export async function buildAssembly(page, options = {}) { const linksTask = await createAssembly(page, options); const result = await insertLinks(page, linksTask, options.source, options.expectedLinks ?? 27); return { document: options.document ?? "LineAssembly", ...result }; }