#!/usr/bin/env node /** * Focused case: add Circle -> save to OPFS -> close project -> reopen from * recent projects; verify the object survives. * * node tools/web/complete/probe-save-reopen.mjs --base-url http://127.0.0.1:5176 */ 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"; 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 ?? "", alerts: Array.from(document.querySelectorAll('[role="alert"]')).map((node) => node.textContent).slice(0, 3), project: document.querySelector('main.blender-app')?.getAttribute('data-project-id') ?? "", recent: Array.from(document.querySelectorAll('select[data-testid="recent-projects"] option')).map((option) => ({ value: option.value, text: option.textContent })), outliner: document.querySelector('.outliner-tree, [aria-label="Outliner"]')?.textContent?.slice(0, 300) ?? "", })); const count = async () => Number((await state()).stats.match(/Objects\s+(\d+)/)?.[1] ?? -1); 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 addDeadline = Date.now() + 90_000; while (Date.now() < addDeadline && await count() !== 7) await page.waitForTimeout(250); log("added", JSON.stringify(await state())); await page.getByRole("button", { name: "保存项目", exact: true }).click(); await page.waitForTimeout(4000); log("saved", JSON.stringify(await state())); await page.getByRole("button", { name: "关闭项目", exact: true }).click(); await page.waitForTimeout(2000); log("closed", JSON.stringify(await state())); const select = page.locator('select[data-testid="recent-projects"]'); await select.waitFor({ timeout: 30_000 }); const options = await page.evaluate(() => Array.from(document.querySelectorAll('select[data-testid="recent-projects"] option')).filter((option) => option.value).map((option) => ({ value: option.value, text: option.textContent }))); log("recent", JSON.stringify(options)); if (!options.length) throw new Error("no recent project after save"); await select.selectOption(options[0].value); await waitScene(page, { timeout: 240_000 }); for (let index = 0; index < 24 && await count() < 0; index++) await page.waitForTimeout(500); log("reopened", JSON.stringify(await state())); } 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(); }