- MasterDetailShell step③ 派生字段 combo(目标列 + 公式),page data seed childDerived:[] 防 combo 未触碰时发出未解析占位串致 compile 反序列化 400 - e2e: 新增「派生字段全流程」用例(向导加派生 → 发布页含 tpl 预览+__derive → _ADD 服务端逐行落库 AMOUNT,经 _LOAD 回读验证;断言 quickEdit:false); 原主子表用例补 _LOAD 级联实证(_SEARCH 列表不嵌充子数组,_LOAD 才填充) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
217 lines
12 KiB
TypeScript
217 lines
12 KiB
TypeScript
/**
|
||
* 主子表(master-detail)· 浏览器全程点穿 e2e。
|
||
* amis wizard 四步:①建主实体 → ②建子表(传 masterTable 后端自动加 MASTERID 外键) → ③生成主子页
|
||
* (compile master-detail-v1,后端先 ensureLink 接线再编译) → ④发布。
|
||
* 断言:发布后经 API 对主表 _ADD 提交主+子行,引擎按主 LIST 列级联落库(status 0);_LOAD 回读子行。
|
||
* 后端走 8094(playwright.fivering.config 反代)。数据纪律:afterAll 删主/子实体;其余(RV/页/菜单/流)跑后 psql 清。
|
||
*/
|
||
import { expect, test } from '@playwright/test';
|
||
import http from 'node:http';
|
||
|
||
const BACKEND_PORT = 8094;
|
||
const TS = Date.now();
|
||
const MASTER = `EIP4PT_MDW${TS}M`;
|
||
const CHILD = `EIP4PT_MDW${TS}C`;
|
||
// 第二个用例(派生字段)专用表
|
||
const MASTER2 = `EIP4PT_MDD${TS}M`;
|
||
const CHILD2 = `EIP4PT_MDD${TS}C`;
|
||
|
||
function api(method: string, path: string, token?: string, body?: unknown): Promise<{ status: number; json: any }> {
|
||
return new Promise((resolve, reject) => {
|
||
const data = body ? JSON.stringify(body) : undefined;
|
||
const req = http.request({ hostname: 'localhost', port: BACKEND_PORT, path, method,
|
||
headers: { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||
...(data ? { 'Content-Length': Buffer.byteLength(data) } : {}) } },
|
||
(res) => { let b = ''; res.on('data', (c) => (b += c)); res.on('end', () => {
|
||
try { resolve({ status: res.statusCode ?? 0, json: b ? JSON.parse(b) : null }); }
|
||
catch { resolve({ status: res.statusCode ?? 0, json: b }); } }); });
|
||
req.on('error', reject); if (data) req.write(data); req.end();
|
||
});
|
||
}
|
||
async function adminToken(): Promise<string> {
|
||
const r = await api('POST', '/api/auth/login', undefined, { account: 'admin', password: '111111' });
|
||
return r.json.token as string;
|
||
}
|
||
|
||
// 在 schema 任意层的 input-table 里按列名找子列(不依赖 JSON 键序,标准层会重序列化打乱键序)。
|
||
function findInputTableColumn(node: any, colName: string): any {
|
||
if (Array.isArray(node)) {
|
||
for (const n of node) { const r = findInputTableColumn(n, colName); if (r) return r; }
|
||
return null;
|
||
}
|
||
if (node && typeof node === 'object') {
|
||
if (node.type === 'input-table' && Array.isArray(node.columns)) {
|
||
const c = node.columns.find((x: any) => x?.name === colName);
|
||
if (c) return c;
|
||
}
|
||
for (const k of Object.keys(node)) { const r = findInputTableColumn(node[k], colName); if (r) return r; }
|
||
}
|
||
return null;
|
||
}
|
||
|
||
test.afterAll(async () => {
|
||
try { const tok = await adminToken();
|
||
await api('DELETE', `/api/meta/entity/${CHILD}`, tok);
|
||
await api('DELETE', `/api/meta/entity/${MASTER}`, tok);
|
||
await api('DELETE', `/api/meta/entity/${CHILD2}`, tok);
|
||
await api('DELETE', `/api/meta/entity/${MASTER2}`, tok);
|
||
} catch { /* best effort */ }
|
||
});
|
||
|
||
test('主子表 · 浏览器全程点穿(建主+子 → 生成主子页 → 发布 → 级联保存子行)', async ({ page }) => {
|
||
test.setTimeout(120_000);
|
||
|
||
// 登录
|
||
await page.goto('/login');
|
||
await page.getByRole('textbox').first().fill('admin');
|
||
await page.locator('input[type="password"]').fill('111111');
|
||
await page.getByRole('button', { name: '登录' }).click();
|
||
await page.waitForURL((u) => !u.pathname.endsWith('/login'), { timeout: 15_000 });
|
||
|
||
// 设计中心 → + 新建主子表
|
||
await page.goto('/design-center');
|
||
await page.getByTestId('dc-new-feature-md').click();
|
||
await expect(page.getByTestId('master-detail-shell')).toBeVisible();
|
||
|
||
// 步① 建主实体
|
||
await expect(page.getByPlaceholder('OA_PURCHASE')).toBeVisible({ timeout: 15_000 });
|
||
await page.getByPlaceholder('OA_PURCHASE').fill(MASTER);
|
||
await page.getByPlaceholder('采购申请').fill('采购申请');
|
||
await page.locator('.cxd-Select').first().click(); // 所属分类
|
||
await page.locator('.cxd-Select-option').first().click();
|
||
await page.getByRole('button', { name: '加字段' }).click();
|
||
await expect(page.getByPlaceholder('TITLE').first()).toBeVisible({ timeout: 10_000 });
|
||
await page.getByPlaceholder('TITLE').first().fill('TITLE');
|
||
await page.getByPlaceholder('标题').first().fill('标题');
|
||
await page.getByRole('button', { name: '保存并下一步' }).click();
|
||
|
||
// 步② 建子表
|
||
await expect(page.getByPlaceholder('OA_PURCHASE_ITEM')).toBeVisible({ timeout: 15_000 });
|
||
await page.getByPlaceholder('OA_PURCHASE_ITEM').fill(CHILD);
|
||
await page.getByPlaceholder('采购明细').fill('采购明细');
|
||
await page.getByRole('button', { name: '加字段' }).click();
|
||
await expect(page.getByPlaceholder('TITLE').first()).toBeVisible({ timeout: 10_000 });
|
||
await page.getByPlaceholder('TITLE').first().fill('ITEMNAME');
|
||
await page.getByPlaceholder('标题').first().fill('物料');
|
||
await page.getByRole('button', { name: '保存并下一步' }).click();
|
||
|
||
// 步③ 生成主子页
|
||
await expect(page.getByPlaceholder('采购申请(主子)')).toBeVisible({ timeout: 20_000 });
|
||
await page.getByPlaceholder('采购申请(主子)').fill('采购申请主子');
|
||
await page.getByRole('button', { name: '保存并下一步' }).click();
|
||
|
||
// 步④ 发布
|
||
await page.getByRole('button', { name: '完成(发布)' }).click();
|
||
|
||
// ★ 后端断言:发布后主表 _ADD 主+2子行级联落库(LIST 列经引擎自动回填子表外键)
|
||
const tok = await adminToken();
|
||
await expect.poll(async () => {
|
||
const r = await api('POST', `/api/flow/${MASTER}_ADD`, tok,
|
||
{ TITLE: '采购单A', DETAILS: [{ ITEMNAME: '水泥' }, { ITEMNAME: '钢筋' }] });
|
||
return r.json?.status;
|
||
}, { timeout: 25_000, message: '发布后主表 _ADD(含 DETAILS 子行)应级联成功' }).toBe(0);
|
||
|
||
// _SEARCH 应能查到主记录(确认主表写流通)
|
||
const s = await api('POST', `/api/flow/${MASTER}_SEARCH`, tok, {});
|
||
expect(s.json?.status).toBe(0);
|
||
// 子行经 _LOAD 回读,实证级联真落库(_SEARCH 列表不嵌充子数组,_LOAD 才填充)。
|
||
const rowA = (s.json?.data?.items ?? []).find((r: any) => r.TITLE === '采购单A');
|
||
expect(rowA, '应查到采购单A').toBeTruthy();
|
||
const pk = `${MASTER}_ID`;
|
||
const loaded = await api('POST', `/api/flow/${MASTER}_LOAD`, tok, { [pk]: String(rowA[pk]).trim() });
|
||
const names = (loaded.json?.data?.result?.DETAILS ?? []).map((d: any) => d.ITEMNAME).sort();
|
||
expect(names, '子行水泥/钢筋应级联落库').toEqual(['水泥', '钢筋']);
|
||
});
|
||
|
||
test('主子表派生字段 · 向导加派生字段 → 发布页含 tpl 预览+__derive → _ADD 服务端逐行落库 AMOUNT', async ({ page }) => {
|
||
test.setTimeout(120_000);
|
||
|
||
// 登录
|
||
await page.goto('/login');
|
||
await page.getByRole('textbox').first().fill('admin');
|
||
await page.locator('input[type="password"]').fill('111111');
|
||
await page.getByRole('button', { name: '登录' }).click();
|
||
await page.waitForURL((u) => !u.pathname.endsWith('/login'), { timeout: 15_000 });
|
||
|
||
await page.goto('/design-center');
|
||
await page.getByTestId('dc-new-feature-md').click();
|
||
await expect(page.getByTestId('master-detail-shell')).toBeVisible();
|
||
|
||
// 步① 建主实体
|
||
await expect(page.getByPlaceholder('OA_PURCHASE')).toBeVisible({ timeout: 15_000 });
|
||
await page.getByPlaceholder('OA_PURCHASE').fill(MASTER2);
|
||
await page.getByPlaceholder('采购申请').fill('派生采购');
|
||
await page.locator('.cxd-Select').first().click();
|
||
await page.locator('.cxd-Select-option').first().click();
|
||
await page.getByRole('button', { name: '加字段' }).click();
|
||
await expect(page.getByPlaceholder('TITLE').first()).toBeVisible({ timeout: 10_000 });
|
||
await page.getByPlaceholder('TITLE').first().fill('TITLE');
|
||
await page.getByPlaceholder('标题').first().fill('标题');
|
||
await page.getByRole('button', { name: '保存并下一步' }).click();
|
||
|
||
// 步② 建子表:4 列 ITEMNAME/ITEMQTY/ITEMPRICE/AMOUNT(默认 string,求值器会数字强转)
|
||
await expect(page.getByPlaceholder('OA_PURCHASE_ITEM')).toBeVisible({ timeout: 15_000 });
|
||
await page.getByPlaceholder('OA_PURCHASE_ITEM').fill(CHILD2);
|
||
await page.getByPlaceholder('采购明细').fill('派生明细');
|
||
const childCols = ['ITEMNAME', 'ITEMQTY', 'ITEMPRICE', 'AMOUNT'];
|
||
for (let i = 0; i < childCols.length; i++) {
|
||
await page.getByRole('button', { name: '加字段' }).click();
|
||
await expect(page.getByPlaceholder('TITLE').nth(i)).toBeVisible({ timeout: 10_000 });
|
||
await page.getByPlaceholder('TITLE').nth(i).fill(childCols[i]);
|
||
}
|
||
await page.getByRole('button', { name: '保存并下一步' }).click();
|
||
|
||
// 步③ 生成主子页 + 派生字段 AMOUNT=ITEMQTY*ITEMPRICE
|
||
await expect(page.getByPlaceholder('采购申请(主子)')).toBeVisible({ timeout: 20_000 });
|
||
await page.getByPlaceholder('采购申请(主子)').fill('派生采购主子');
|
||
await page.getByRole('button', { name: '加派生字段' }).click();
|
||
await expect(page.getByPlaceholder('MONEY')).toBeVisible({ timeout: 10_000 });
|
||
await page.getByPlaceholder('MONEY').fill('AMOUNT');
|
||
await page.getByPlaceholder('QTY*PRICE').fill('ITEMQTY*ITEMPRICE');
|
||
|
||
// 捕获 compile 响应拿 templateId(验 UI→编译产物)
|
||
const compileResp = page.waitForResponse(
|
||
(r) => r.url().includes('/api/design/recipe/compile') && r.request().method() === 'POST');
|
||
await page.getByRole('button', { name: '保存并下一步' }).click();
|
||
const tid = (await (await compileResp).json())?.data?.templateId as string;
|
||
expect(tid, 'compile 应返回 templateId').toBeTruthy();
|
||
|
||
// 步④ 发布
|
||
await page.getByRole('button', { name: '完成(发布)' }).click();
|
||
|
||
const tok = await adminToken();
|
||
|
||
// ★ 断言1:UI 走出的编译产物 = 子表 AMOUNT 列只读 tpl 实时预览 + 表单 api 透传 __derive
|
||
const schema = (await api('GET', `/api/page/schema/${tid}`, tok)).json;
|
||
expect(JSON.stringify(schema), '表单 api 应透传 __derive').toContain('__derive');
|
||
// 按列查 AMOUNT(不依赖键序):应为只读 tpl 实时预览 ${ITEMQTY*ITEMPRICE},无 quickEdit。
|
||
const amountCol = findInputTableColumn(schema, 'AMOUNT');
|
||
expect(amountCol, 'AMOUNT 列应存在于 input-table').toBeTruthy();
|
||
expect(amountCol.type).toBe('tpl');
|
||
expect(amountCol.tpl).toBe('${ITEMQTY*ITEMPRICE}');
|
||
// 必须显式 quickEdit:false——否则 amis needConfirm:false 会强注可编辑框覆盖 tpl 只读预览。
|
||
expect(amountCol.quickEdit, '派生列 quickEdit 必须显式 false 才保留只读 tpl 预览').toBe(false);
|
||
|
||
// ★ 断言2:_ADD 带 __derive,子行不带 AMOUNT,服务端逐行算 30/25 落库
|
||
await expect.poll(async () => {
|
||
const r = await api('POST',
|
||
`/api/flow/${MASTER2}_ADD?__derive=${encodeURIComponent('DETAILS|AMOUNT=ITEMQTY*ITEMPRICE')}`, tok,
|
||
{ TITLE: '派生单A', DETAILS: [
|
||
{ ITEMNAME: '水泥', ITEMQTY: 3, ITEMPRICE: 10 },
|
||
{ ITEMNAME: '钢筋', ITEMQTY: 2, ITEMPRICE: '12.5' }] });
|
||
return r.json?.status;
|
||
}, { timeout: 25_000, message: '派生 _ADD 应成功' }).toBe(0);
|
||
|
||
const s = await api('POST', `/api/flow/${MASTER2}_SEARCH`, tok, {});
|
||
expect(s.json?.status).toBe(0);
|
||
const rows: any[] = s.json?.data?.items ?? [];
|
||
const rowA = rows.find((r) => r.TITLE === '派生单A');
|
||
expect(rowA, '应查到派生单A').toBeTruthy();
|
||
// 子行经 _LOAD(单记录)回读:_SEARCH 列表不嵌充 LIST 子数组,_LOAD 才填充(与运行时编辑弹窗一致)。
|
||
const pk = `${MASTER2}_ID`;
|
||
const loaded = await api('POST', `/api/flow/${MASTER2}_LOAD`, tok, { [pk]: String(rowA[pk]).trim() });
|
||
const details: any[] = loaded.json?.data?.result?.DETAILS ?? [];
|
||
const amts = details.map((d) => Number(d.AMOUNT)).sort((a, b) => a - b);
|
||
expect(amts, '服务端派生 AMOUNT=数量×单价(3×10=30,2×12.5=25)').toEqual([25, 30]);
|
||
});
|