import type { Page, TestInfo } from "@playwright/test"; import { expect, test } from "./support/deployed-test"; import { receipt, shot } from "./support/deployed"; import { evaluateStep, loadLessons, loadPbrLessons, resolveOne, runTeachAction, stepPassed } from "./support/teach-checks"; import type { TeachLesson, TeachStep } from "../../app/src/teach/types"; /** * The teaching hub replayed against the deployed bundle. * * Each case walks one chapter exactly as a learner would: the lesson JSON is * the same file the in-page coach loads, the demo actions are replayed through * real clicks, and every step is verified with the lesson's own checks. A * lesson that drifts away from the product therefore fails here by chapter and * by step, instead of pointing at nothing in class. */ /* The deployed asset library seeds through SHA-256 + OPFS on first use and the * engine reloads per chapter; on the cloud the assignment steps need well over * the suite's default 60 s case budget. Only this file is widened. */ test.describe.configure({ timeout: 180_000 }); const MAIN_LESSONS = loadLessons(); const PBR_LESSONS = loadPbrLessons(); const LESSONS = [...MAIN_LESSONS, ...PBR_LESSONS]; const PANEL = 'aside[aria-label="教学向导"]'; const PROGRESS_KEY = "blender-web:teach-progress:v1"; /** Roles whose element reacts to a click; container targets are only verified. */ const CLICKABLE_ROLES = new Set(["button", "menuitem", "menuitemcheckbox", "menuitemradio", "treeitem", "tab", "link", "option"]); /* Fresh progress per case: a resumed chapter would start on the stored step and * silently skip the steps this case is meant to walk. */ test.beforeEach(async ({ page }) => { await page.addInitScript((key) => { try { window.localStorage.removeItem(key); } catch { /* storage disabled */ } }, PROGRESS_KEY); }); async function waitForStep(page: Page, panel: ReturnType, step: TeachStep): Promise { await expect(panel).toHaveAttribute("data-teach-step", step.id, { timeout: 20_000 }); await page.waitForTimeout(150); } /** Identity of the open project; every open assigns a new one. */ async function projectId(page: Page): Promise { return await page.locator("main.blender-app").getAttribute("data-project-id", { timeout: 2_000 }).catch(() => "") ?? ""; } async function actStep(page: Page, step: TeachStep): Promise { if (step.demo?.length) { for (const action of step.demo) await runTeachAction(page, action); return; } if (step.target && step.target.role && CLICKABLE_ROLES.has(step.target.role)) { await (await resolveOne(page, step.target)).click({ force: true, timeout: 15_000 }); await page.waitForTimeout(300); } } async function waitForChecks(page: Page, step: TeachStep, timeout = 45_000) { let results = await evaluateStep(page, step); const deadline = Date.now() + timeout; while (!stepPassed(results) && Date.now() < deadline) { await page.waitForTimeout(500); results = await evaluateStep(page, step); } return results; } async function walkChapter(page: Page, lesson: TeachLesson, testInfo: TestInfo): Promise { const started = Date.now(); const marks: Record = {}; const mark = (name: string) => { marks[name] = Date.now() - started; }; const query = `teach=${lesson.id}&mode=guide&step=${lesson.steps[0].id}`; const url = lesson.setup.entry === "native" ? `/?${query}` : `/?ui=web&${query}`; await page.goto(url, { waitUntil: "domcontentloaded", timeout: 30_000 }); await page.locator("main.blender-app").waitFor({ timeout: 30_000 }); mark("app"); const panel = page.locator(PANEL); await expect(panel).toHaveAttribute("data-teach-chapter", lesson.id, { timeout: 30_000 }); /* `data-teach-ready` covers both the lesson fetch and a ready engine. */ await expect(panel).toHaveAttribute("data-teach-ready", "true", { timeout: 60_000 }); mark("ready"); if (lesson.setup.fixture) { const prepare = panel.getByRole("button", { name: "准备本章练习工程" }); await expect(prepare).toBeVisible({ timeout: 15_000 }); const before = await projectId(page); await prepare.click({ force: true }); /* Wait for the fixture itself, not for the boot template: the startup scene * also bumps the revision, which made a plain revision poll pass while the * default cube scene was still on screen. */ await expect.poll(async () => projectId(page), { timeout: 25_000, intervals: [400] }).not.toBe(before); await page.waitForTimeout(800); mark("open"); /* The fixture open and the workspace restore hold the project action lock; * a step that starts a file import while it is held is dropped silently. */ await expect(panel).toHaveAttribute("data-teach-ready", "true", { timeout: 20_000 }); } const walked: string[] = []; let checkCount = 0; try { for (const [index, step] of lesson.steps.entries()) { await waitForStep(page, panel, step); await actStep(page, step); const results = await waitForChecks(page, step); checkCount += results.length; const failed = results.filter((item) => !item.ok); if (failed.length) { throw new Error(`第 ${index + 1} 步「${step.title}」(${step.id}) 未通过:${failed.map((item) => `${item.label}(实际 ${item.actual})`).join(";")}`); } walked.push(step.id); mark(step.id); if (index < lesson.steps.length - 1) { const next = panel.getByRole("button", { name: "下一步" }); /* Guide mode gates the button on the coach's own poll; wait for it. */ await expect(next).toBeEnabled({ timeout: 15_000 }); await next.click(); } else { /* Same 300 ms coach poll as 下一步: the button stays disabled until the * coach itself sees the checks pass, and a disabled click is a no-op. */ const finish = panel.getByRole("button", { name: "完成本章" }); await expect(finish).toBeEnabled({ timeout: 15_000 }); await finish.click({ force: true }); } } /* The last click must have been recorded, or the chapter would reappear as * unfinished on the catalogue page. */ await expect.poll(async () => { const raw = await page.evaluate((key) => window.localStorage.getItem(key), PROGRESS_KEY).catch(() => null); const entry = raw ? JSON.parse(raw)?.chapters?.[lesson.id] : null; return Array.isArray(entry?.completedStepIds) ? entry.completedStepIds.length : 0; }, { timeout: 10_000 }).toBe(lesson.steps.length); const image = await shot(page, testInfo, "walked"); receipt(testInfo, { workflow: `/?ui=web&teach=${lesson.id}&mode=guide → ${lesson.steps.length} 步(真实点击 + 逐项校验)`, chapter: lesson.id, title: lesson.title, steps: walked, checks: checkCount, timings: marks, screenshot: image, status: "PASS", }); } catch (error) { const image = await shot(page, testInfo, "failure").catch(() => ""); receipt(testInfo, { workflow: `/?ui=web&teach=${lesson.id}&mode=guide`, chapter: lesson.id, title: lesson.title, walked, checks: checkCount, timings: marks, screenshot: image, status: "FAIL", error: error instanceof Error ? error.message : String(error), }); throw error; } } for (const lesson of LESSONS) { /* Tagged `teach-chNN`, not `chNN`: the runner counts `chNN` as functional * chapter coverage, and a lesson replay must not inflate that number. */ test(`[teach-${lesson.id}] 教学第 ${lesson.number} ${lesson.unit ?? "章"}:${lesson.title}(${lesson.steps.length} 步)`, async ({ page }, testInfo) => { try { await walkChapter(page, lesson, testInfo); } finally { /* Each case loads the 300 MB engine again; blanking the tab between * chapters releases the previous wasm heap instead of accumulating it. */ await page.goto("about:blank", { timeout: 10_000 }).catch(() => undefined); } }); } test("[teach-catalog] 教学目录页列出 32 章并按模式生成入口", async ({ page }, testInfo) => { await page.goto("/teach/index.html", { waitUntil: "domcontentloaded", timeout: 20_000 }); const cards = page.locator(".teach-card"); await expect(cards).toHaveCount(MAIN_LESSONS.length, { timeout: 20_000 }); const start = page.locator('[data-chapter-start="ch01"]'); /* Whole-prefix assertion: a `/?ui=web?teach=…` typo still contains "teach=ch01" * while opening the wrong entry, which is exactly how a bad href slipped through. */ await expect(start).toHaveAttribute("href", /^\/\?ui=web&teach=ch01&mode=guide/, { timeout: 10_000 }); await expect(page.locator('[data-chapter-start="ch26"]')).toHaveAttribute("href", /^\/\?teach=ch26&mode=guide/); await page.getByRole("radio", { name: "自动演示" }).check({ force: true }); await expect.poll(async () => start.getAttribute("href"), { timeout: 5_000 }).toContain("mode=demo"); const search = page.locator("#teach-search"); await search.fill("动画"); await expect.poll(async () => page.locator(".teach-card:not([hidden])").count(), { timeout: 10_000 }).toBeLessThan(MAIN_LESSONS.length); expect(await page.locator(".teach-card:not([hidden])").count()).toBeGreaterThan(0); await search.fill(""); await expect.poll(async () => page.locator(".teach-card:not([hidden])").count(), { timeout: 10_000 }).toBe(MAIN_LESSONS.length); await expect(page.locator("#teach-version")).toContainText(/当前线上版本身份|读取失败/, { timeout: 15_000 }); const image = await shot(page, testInfo, "catalog"); receipt(testInfo, { workflow: "/teach/index.html → 32 张章节卡、模式改写入口、搜索过滤、版本身份", chapters: await cards.count(), screenshot: image, status: "PASS", }); }); test("[teach-pbr-catalog] PBR 教程页列出四节并生成应用入口", async ({ page }, testInfo) => { await page.goto("/teach/pbr/index.html", { waitUntil: "domcontentloaded", timeout: 20_000 }); const cards = page.locator(".teach-card"); await expect(cards).toHaveCount(PBR_LESSONS.length, { timeout: 20_000 }); const start = page.locator('[data-chapter-start="pbr-01"]'); await expect(start).toHaveAttribute("href", /^\/\?ui=web&teach=pbr-01&mode=guide/, { timeout: 10_000 }); await page.getByRole("radio", { name: "自动演示" }).check({ force: true }); await expect.poll(async () => start.getAttribute("href"), { timeout: 5_000 }).toContain("mode=demo"); /* The page drives the shared catalogue script from its own body attributes; * losing them means it would silently read the 32-chapter catalogue. */ await expect(page.locator("body")).toHaveAttribute("data-catalog", "/teach/pbr/catalog.json"); await expect(page.locator("body")).toHaveAttribute("data-unit", "节"); const image = await shot(page, testInfo, "pbr-catalog"); receipt(testInfo, { workflow: "/teach/pbr/index.html → 4 张课程卡、模式改写入口", lessons: await cards.count(), screenshot: image, status: "PASS", }); });