oh-my-muse/muse-studio/e2e/work-schema-binding.spec.ts
lili fa651682b4 test(studio): work-schema-binding e2e 修 flaky(waitForResponse 前置)
schema-options 由 CreateWorkModal(首页即挂载、staleTime 60s)预取,全量跑(后端热)请求常
早于 waitForResponse 注册→race→30s timeout;注册移到 goto 前,覆盖请求早发/晚发两种时机。
全量 e2e 复跑该 spec 转绿(单跑本就绿、仅全量 flaky)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-21 10:20:28 -07:00

56 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { expect, test, type Page } from '@playwright/test';
/**
* 作品创建时绑定 Meta Schema 的 e2e真实后端MSW off
*
* 覆盖 D1 链路端到端:打开新建作品弹窗 → 真实 GET /works/schema-options 渲染 Schema 下拉 →
* 真实 POST /works 创建。不依赖 DB 是否已 seed active schema(无 schema 时走"不绑定"基线,链路仍通)。
*/
const TOKEN = 'test1';
async function seedToken(page: Page): Promise<void> {
await page.addInitScript((t) => {
window.localStorage.setItem('accessToken', t);
window.localStorage.setItem('tenantId', '1');
}, TOKEN);
}
test('用户新建作品时可见 Schema 选择并经真实后端创建', async ({ page }) => {
await seedToken(page);
const uniqueTitle = `Schema作品-e2e-${Date.now()}`;
// schema-options 由 CreateWorkModal(首页即挂载、staleTime 60s)预取,全量跑(后端热)时常在 goto 阶段就发完——
// 故 waitForResponse 必须在 goto 前注册,否则请求早于注册→race→30s timeout(flaky 根因);弹窗触发的迟发场景也被同一 promise 覆盖。
const schemaOptionsPromise = page.waitForResponse((r) =>
r.url().includes('/works/schema-options') && r.request().method() === 'GET'
);
await page.goto('/');
await expect(page.getByRole('heading', { name: '我的创作台' })).toBeVisible({ timeout: 15_000 });
// D1-4:schema-options 经真实后端 200(端到端打通,非 mock)。
const schemaOptionsResp = await schemaOptionsPromise;
expect(schemaOptionsResp.status()).toBe(200);
// 打开新建作品弹窗(有作品→"新建作品",空状态→"立即创建",均触发同一弹窗);数据已预取,下拉直接渲染。
await page.getByRole('button', { name: /新建作品|立即创建/ }).first().click();
const schemaSelect = page.getByLabel('作品结构 Schema');
await expect(schemaSelect).toBeVisible({ timeout: 15_000 });
// 填标题并创建(不绑定 schema 走基线,验证 work 创建链路在带 schema 字段后仍通)。
await page.getByPlaceholder('请输入小说或书籍的标题,例如:星海迷途').fill(uniqueTitle);
const createResponsePromise = page.waitForResponse((response) =>
response.url().includes('/app-api/muse/works') &&
response.request().method() === 'POST'
);
await page.getByRole('button', { name: '确认创建' }).click();
const createResponse = await createResponsePromise;
// work 创建成功为 HTTP 200CommonResult success区别于 agent 创建的 201
expect(createResponse.status()).toBe(200);
const createBody = await createResponse.json();
expect(createBody.code).toBe(0);
expect(createBody.data?.id).toBeTruthy();
// 作品创建成功后 workSchemaId 字段存在(本用例不绑定故为 null,验证 D1 字段贯通前后端)。
expect(createBody.data).toHaveProperty('workSchemaId');
});