#!/usr/bin/env node /** * Focused case: add Circle, inject a worker crash (?worker-fault=1), verify the * project is restored with the native Main intact. * * node tools/web/complete/probe-worker-recovery.mjs --base-url 'http://127.0.0.1:5176/?ui=web&worker-fault=1' */ import { ASSETS, parseArgs, launchBrowser, newPage, openFile, waitScene } from "../course/lib/lesson-recorder.mjs"; const args = parseArgs(); const BASE = args.baseUrl || "http://127.0.0.1:5176/?ui=web&worker-fault=1"; const browser = await launchBrowser(args.gpu); const { context, page, consoleErrors } = await newPage(browser, { baseUrl: BASE }); page.setDefaultTimeout(90_000); const log = (...parts) => console.log(new Date().toISOString().slice(11, 19), ...parts); const state = async () => page.evaluate(() => ({ stats: document.querySelector('[data-testid="scene-stats"]')?.textContent ?? "", status: document.querySelector('[data-testid="engine-status"]')?.textContent ?? "", recovery: document.querySelector('main.blender-app')?.getAttribute('data-worker-recovery-status') ?? "", alerts: Array.from(document.querySelectorAll('[role="alert"]')).map((node) => node.textContent).slice(0, 3), outliner: document.querySelector('.outliner-tree, [aria-label="Outliner"]')?.textContent?.slice(0, 240) ?? "", })); try { await openFile(page, ASSETS.studio); await waitScene(page, { objects: 6, timeout: 240_000 }); await page.getByRole("button", { name: "添加", exact: true }).click(); await page.getByRole("menuitem", { name: "网格", exact: true }).click(); await page.getByRole("menuitem", { name: "圆环", exact: true }).click(); const deadline = Date.now() + 90_000; while (Date.now() < deadline && !/Circle/.test((await state()).stats)) await page.waitForTimeout(250); log("before crash", JSON.stringify(await state())); await page.getByTestId("inject-worker-crash").click(); for (let index = 0; index < 120; index++) { await page.waitForTimeout(500); const current = await state(); if (current.recovery === "SUCCEEDED" || /Recovery:/i.test(current.status) || current.alerts.length) { log("after crash", JSON.stringify(current)); break; } if (index === 119) log("timeout", JSON.stringify(current)); } const restart = page.getByRole("button", { name: "重启并恢复", exact: true }); if (await restart.count()) { await restart.first().click(); for (let index = 0; index < 180; index++) { await page.waitForTimeout(500); const current = await state(); if (current.recovery === "SUCCEEDED" || /restored|recovered|已恢复/i.test(current.status)) { log("recovered", JSON.stringify(current)); break; } if (index === 179) log("recovery timeout", JSON.stringify(current)); } } else log("no restart button"); } catch (error) { log("probe error", error instanceof Error ? error.message : String(error)); log("state", JSON.stringify(await state().catch(() => null))); } finally { log("consoleErrors", JSON.stringify(consoleErrors.slice(-12))); await context.close(); await browser.close(); }