diff --git a/e2e/master-detail.spec.ts b/e2e/master-detail.spec.ts index b8acda7..dee8758 100644 --- a/e2e/master-detail.spec.ts +++ b/e2e/master-detail.spec.ts @@ -12,6 +12,9 @@ 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) => { @@ -30,10 +33,28 @@ async function adminToken(): Promise { 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 */ } }); @@ -93,4 +114,103 @@ test('主子表 · 浏览器全程点穿(建主+子 → 生成主子页 → // _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]); }); diff --git a/src/pages/MasterDetailShell.tsx b/src/pages/MasterDetailShell.tsx index b9d7c48..c594139 100644 --- a/src/pages/MasterDetailShell.tsx +++ b/src/pages/MasterDetailShell.tsx @@ -35,6 +35,9 @@ function attrCombo(name: string): Record { function buildSchema(): Record { return { type: 'page', + // seed childDerived=[] 使 ${childDerived} 恒解析为数组:派生字段 combo 未触碰时也不会发出 + // 未解析占位串导致 compile 反序列化 400(后端已另做 Object 容错,此处双保险)。 + data: { childDerived: [] }, body: { type: 'wizard', mode: 'horizontal', @@ -82,12 +85,14 @@ function buildSchema(): Record { title: '③ 生成主子页', subTitle: '编译落标准层', // 后端 recipe=master-detail-v1:先 ensureLink(MASTERID/DETAILS) 接线,再编译出主 CRUD + 子表 input-table。 + // childDerived:派生列(金额=数量×单价 类)→ 编译为只读 tpl 实时预览 + 写流 ?__derive= 服务端权威落库。 api: { method: 'post', url: '/api/design/recipe/compile', data: { tableName: '${masterTable}', recipe: 'master-detail-v1', title: '${title}', displayField: '${displayField}', childTable: '${childTablename}', linkColumn: 'MASTERID', listColumn: 'DETAILS', listLabel: '${childName}', + childDerived: '${childDerived}', }, responseData: { templateId: '${templateId}' }, }, @@ -95,6 +100,20 @@ function buildSchema(): Record { { type: 'static', label: '主表 / 子表', tpl: '${masterTable} / ${childTablename}' }, { type: 'input-text', name: 'title', label: '页面标题', required: true, placeholder: '采购申请(主子)' }, { type: 'input-text', name: 'displayField', label: '主表显示字段', placeholder: '可选,默认主表显示列' }, + { type: 'static', label: '可用子表列', tpl: '${childAttributes|pick:column|join:"、"}', + visibleOn: '${childAttributes && childAttributes.length}' }, + { + type: 'combo', name: 'childDerived', label: '派生字段', multiple: true, + addable: true, removable: true, addButtonText: '加派生字段', minLength: 0, + description: '可选。如 金额=数量×单价:目标列填 MONEY,公式填 QTY*PRICE。' + + '公式仅用子表列名与 + - * / ( ),落库前服务端逐行权威求算(避免前端竞态),同时单元格实时预览。', + items: [ + { type: 'input-text', name: 'column', label: '目标列', required: true, placeholder: 'MONEY', + validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{0,40}$/' }, + validationErrors: { matchRegexp: '须为大写字母开头的子表真实列' } }, + { type: 'input-text', name: 'formula', label: '公式', required: true, placeholder: 'QTY*PRICE' }, + ], + }, ], }, {