#!/usr/bin/env node /** * Focused case: Shading workspace Image Editor selects an image and fits it. * * node tools/web/complete/probe-image-editor.mjs --base-url http://127.0.0.1:5176 */ import { ASSETS, parseArgs, launchBrowser, newPage, openFile, waitScene, switchWorkspace } 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(() => ({ images: Array.from(document.querySelectorAll('select[aria-label="图像"] option')).map((node) => ({ value: node.value, text: node.textContent })), hasImage: Boolean(document.querySelector('[aria-label="Image view"] img')), viewStatus: document.querySelector('.image-view-status')?.textContent ?? null, status: document.querySelector('[data-testid="engine-status"]')?.textContent ?? "", })); try { await openFile(page, ASSETS.studio); await waitScene(page, { objects: 6, timeout: 240_000 }); await switchWorkspace(page, "Shading"); const view = page.locator('[aria-label="Image view"]').first(); await view.waitFor({ timeout: 30_000 }); log("before", JSON.stringify(await state())); const select = page.locator('select[aria-label="图像"]').first(); const options = await select.locator('option').evaluateAll((nodes) => nodes.map((node) => node.value).filter(Boolean)); if (!options.length) throw new Error("no image options"); await select.selectOption(options[0]); for (let index = 0; index < 60; index++) { await page.waitForTimeout(500); const current = await state(); if (current.hasImage) { log("loaded", JSON.stringify(current)); break; } if (index === 59) log("timeout", JSON.stringify(current)); } await page.locator('button:visible').filter({ hasText: "Fit View" }).first().evaluate((element) => element.click()); await page.waitForTimeout(800); log("fit", 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(-8))); await context.close(); await browser.close(); }