diff --git a/e2e/process-modeler.spec.ts b/e2e/process-modeler.spec.ts new file mode 100644 index 0000000..98953ad --- /dev/null +++ b/e2e/process-modeler.spec.ts @@ -0,0 +1,264 @@ +/** + * 在线流程 Modeler(B3)· 浏览器全程点穿 live e2e(实施计划 Task 4.2)。 + * + * 三场景(真部署真起实例真验收,不 mock): + * A 改存量全链:登录 admin → /process-modeler?procKey=总包合同交底 → 画布渲染 → 点会签 userTask「编写交底」 + * → EIP4 侧板会签控件可见且「启用会签」勾选(保真读回)→ 保存草稿(URL 回填 ?modelId=)→ 发布 + * (PublishConfirm 断言含流程 key「总包合同交底」+ 版本 + 运行中实例数)→ 确认 → apiJson 起实例 + * → trace 断言有 current userTask(证部署的存量流程经 moddle 往返后小写元素仍被引擎识别、可派任)。 + * B 新建全链:新建流程 → 选「会签」模板 + 填名称/key → 进编辑器(画布非空)→ 选 task_cosign 填会签办理人 + * content=ADMIN(否则空集合运行期被跳过、不生成任务)→ 保存草稿 → 发布 → 确认 → apiJson 起实例 + * → trace 断言 current userTask「会签审批」(证 bpmn-js 新建的小写 BPMN 部署后真产任务)。 + * C 走查 + 权限:①中文节点名渲染正常 ②palette 元素齐 ③标准属性面板 + EIP4 侧板同屏 + * ④超管门:非超管访问 deploy/models 网关端点断言 403(见下方 alice 降级说明)。 + * + * 依赖:8093 网关/后端 + 8089 flowable-service + 5173 vite dev 均在线(见 playwright.process-modeler.config.ts)。 + * 自检:curl -s -o /dev/null -w '%{http_code}' localhost:8093/v3/api-docs(200)、curl localhost:5173(200)。 + * 跑法:npx playwright test -c playwright.process-modeler.config.ts + * + * 数据纪律:afterAll 删本次建的草稿(act_re_model,DELETE /api/workflow/models/{id})。发布产生的流程新版本 + * 无运行时下架端点(同 §F net-new),属 live 验收固有副作用——每跑一次「总包合同交底」版本 +1、B 新建一个净新 key。 + * + * ⚠️ 场景 C④ alice 降级:dev 库无 alice/123456 账号(登录返 success:false),无法用真实非超管 token 打 deploy。 + * 降级为等价断言:非超管凭据(无 token)打 deploy/models → 403(走的正是 ProcessModelerController.requireSuperAdmin + * 的 principal==null → 非 isSystemAdmin → 403 分支,与「合法非超管 token → 403」同一段代码),并用 admin token + * 打 models → 200 作对照,证超管门确在按身份区分放行/拒绝。alice 登录失败一并断言以记录账号缺席事实。 + */ +import { expect, test } from '@playwright/test'; +import http from 'node:http'; + +// apiJson 直连后端 8093(与 UI 经 5173 vite 代理的目标同一后端,故 UI 建的草稿 apiJson 可见)。 +const BACKEND_PORT = 8093; +const ZONGBAO_KEY = '总包合同交底'; +// 存量会签流程「总包合同交底」的会签 userTask id(净化源里 sid- 前缀为 ASCII 不被改写,即 bpmn-js data-element-id)。 +const COSIGN_TASK_ID = 'sid-2AB0790B-7C9B-4872-8753-BC3DB644568C'; + +// 本次运行建的草稿 id,afterAll 清理。 +const createdModelIds: string[] = []; + +function apiJson( + 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 buf = ''; + res.on('data', (c) => (buf += c)); + res.on('end', () => { + try { + resolve({ status: res.statusCode ?? 0, json: buf ? JSON.parse(buf) : null }); + } catch { + resolve({ status: res.statusCode ?? 0, json: buf }); + } + }); + }, + ); + req.on('error', reject); + if (data) req.write(data); + req.end(); + }); +} + +async function adminToken(): Promise { + const r = await apiJson('POST', '/api/auth/login', undefined, { account: 'admin', password: '111111' }); + return r.json.token as string; +} + +// 真实 React 登录表单(复用 five-ring 惯例:第一个 textbox=账号、type=password=密码、按钮「登录」)。 +async function loginAdmin(page: import('@playwright/test').Page) { + 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 }); +} + +// 点击画布上指定 BPMN 元素(bpmn-js 在 shape g 上挂 data-element-id)。 +async function selectBpmnElement(page: import('@playwright/test').Page, elementId: string) { + const el = page.locator(`[data-testid="process-modeler-canvas"] [data-element-id="${elementId}"]`); + await expect(el).toBeVisible({ timeout: 15_000 }); + await el.click(); +} + +// 记住 URL 里回填的 ?modelId=,供 afterAll 清理。 +function rememberModelId(page: import('@playwright/test').Page): string { + const id = new URL(page.url()).searchParams.get('modelId') ?? ''; + if (id && !createdModelIds.includes(id)) createdModelIds.push(id); + return id; +} + +// 起实例 + trace 断言:部署的流程 start 后必有 current userTask(证小写元素被引擎识别并派任)。 +async function assertTaskGenerated(key: string, businessKey: string, token: string): Promise { + const started = await apiJson('POST', '/api/process/start', token, { + processDefinitionKey: key, + businessKey, + }); + expect(started.status, 'start 应 200').toBe(200); + const iid = started.json?.id; + expect(iid, 'start 应返回流程实例 id').toBeTruthy(); + await expect + .poll( + async () => { + const t = await apiJson('GET', `/api/flow/process/${iid}/trace`, token); + const rows = Array.isArray(t.json) ? t.json : (t.json?.data ?? []); + return (rows as any[]).filter((r) => r && r.current && r.taskId).length; + }, + { timeout: 15_000, message: `${key} start 后应有 current userTask(证部署流程含真 userTask)` }, + ) + .toBeGreaterThan(0); +} + +test.afterAll(async () => { + try { + const tok = await adminToken(); + for (const id of createdModelIds) { + await apiJson('DELETE', `/api/workflow/models/${id}`, tok); + } + } catch { + /* 清理尽力而为 */ + } +}); + +// ──────────────────────────────────────────────────────────────────────────── +// 场景 A:改存量全链(打开→会签保真读回→存草稿→发布→起实例→任务生成) +// ──────────────────────────────────────────────────────────────────────────── +test('场景A 改存量全链:总包合同交底 打开→会签读回→存草稿→发布→起实例生成任务', async ({ page }) => { + test.setTimeout(150_000); + await loginAdmin(page); + + // 打开存量会签流程(中文 key 必须 encodeURIComponent 进 URL)。 + await page.goto(`/process-modeler?procKey=${encodeURIComponent(ZONGBAO_KEY)}`); + // 画布渲染完成:出现 bpmn 元素(.djs-element)。 + await expect(page.locator('[data-testid="process-modeler-canvas"] .djs-element').first()).toBeVisible({ + timeout: 30_000, + }); + + // 点会签 userTask「编写交底」→ EIP4 侧板会签控件可见 + 「启用会签」勾选(保真读回存量会签配置)。 + await selectBpmnElement(page, COSIGN_TASK_ID); + await expect(page.getByTestId('process-modeler-eip4-panel')).toBeVisible(); + await expect(page.getByTestId('eip4-cosign-toggle')).toBeChecked(); + + // 保存草稿 → URL 回填 ?modelId=(存量按 key 打开走 createDraft)。 + await page.getByTestId('process-modeler-save-draft').click(); + await page.waitForURL(/[?&]modelId=/, { timeout: 20_000 }); + const modelId = rememberModelId(page); + expect(modelId, '存草稿后 URL 应回填 modelId').toBeTruthy(); + + // 存草稿会以 modelId 重载画布——等发布按钮重新可用(modelerReady)再点发布。 + await expect(page.getByTestId('process-modeler-publish')).toBeEnabled({ timeout: 20_000 }); + await page.getByTestId('process-modeler-publish').click(); + + // 同人确认弹窗:断言影响摘要含流程 key「总包合同交底」+ 版本迁移 + 运行中实例数。 + await expect(page.getByTestId('process-modeler-publish-confirm')).toBeVisible({ timeout: 20_000 }); + await expect(page.getByTestId('publish-impact-key')).toContainText(ZONGBAO_KEY); + await expect(page.getByTestId('publish-impact-version')).toContainText(/首次发布|v\d+\s*→\s*v\d+/); + await expect(page.getByTestId('publish-impact-instances')).toHaveText(/\d/); + + // 确认发布 → 成功后弹窗关闭(onPublished→onClose)。 + await page.getByTestId('publish-confirm-btn').click(); + await expect(page.getByTestId('process-modeler-publish-confirm')).toBeHidden({ timeout: 30_000 }); + + // 起实例 + trace 断言:部署的新版本 start 后有 current userTask。 + const tok = await adminToken(); + await assertTaskGenerated(ZONGBAO_KEY, `E2E-A-${Date.now()}`, tok); +}); + +// ──────────────────────────────────────────────────────────────────────────── +// 场景 B:新建全链(会签模板→填办理人→存草稿→发布→起实例→任务生成) +// ──────────────────────────────────────────────────────────────────────────── +test('场景B 新建全链:会签模板新建→填办理人→存草稿→发布→起实例生成任务', async ({ page }) => { + test.setTimeout(150_000); + const newKey = `e2anew${Date.now()}`; // ASCII NCName → 直接作 process id,无需 idMap 还原 + await loginAdmin(page); + + await page.goto('/process-modeler'); + // 打开器「+ 新建流程」→ NewProcessDialog。 + await page.getByTestId('process-modeler-new').click(); + await page.getByTestId('new-process-name').fill('会签E2E演示'); + await page.getByTestId('new-process-key').fill(newKey); + await page.getByTestId('new-process-template').selectOption('cosign'); + await page.getByTestId('new-process-confirm').click(); + + // 进编辑器:画布非空(模板实例化)。 + await expect(page.locator('[data-testid="process-modeler-canvas"] .djs-element').first()).toBeVisible({ + timeout: 20_000, + }); + + // 选会签 userTask(模板 id=task_cosign)→ 会签已启用(保真)→ 填办理人 content=ADMIN + //(会签集合非空,运行期才真产任务;空 content 会被 MI 跳过导致流程直接结束、无任务)。 + await selectBpmnElement(page, 'task_cosign'); + await expect(page.getByTestId('eip4-cosign-toggle')).toBeChecked(); + await page.getByTestId('eip4-cosign-content-0').fill('ADMIN'); + + // 保存草稿 → URL 回填 modelId。 + await page.getByTestId('process-modeler-save-draft').click(); + await page.waitForURL(/[?&]modelId=/, { timeout: 20_000 }); + rememberModelId(page); + + // 发布。 + await expect(page.getByTestId('process-modeler-publish')).toBeEnabled({ timeout: 20_000 }); + await page.getByTestId('process-modeler-publish').click(); + await expect(page.getByTestId('process-modeler-publish-confirm')).toBeVisible({ timeout: 20_000 }); + await expect(page.getByTestId('publish-impact-key')).toContainText(newKey); + await page.getByTestId('publish-confirm-btn').click(); + await expect(page.getByTestId('process-modeler-publish-confirm')).toBeHidden({ timeout: 30_000 }); + + // 起实例 + trace 断言:新建部署的会签流程 start 后有 current userTask。 + const tok = await adminToken(); + await assertTaskGenerated(newKey, `E2E-B-${Date.now()}`, tok); +}); + +// ──────────────────────────────────────────────────────────────────────────── +// 场景 C:走查(中文节点/palette/双面板同屏)+ 权限(超管门 403) +// ──────────────────────────────────────────────────────────────────────────── +test('场景C 走查+权限:中文节点/palette/标准面板+EIP4侧板同屏 + 超管门403', async ({ page }) => { + test.setTimeout(120_000); + await loginAdmin(page); + await page.goto(`/process-modeler?procKey=${encodeURIComponent(ZONGBAO_KEY)}`); + const canvas = page.locator('[data-testid="process-modeler-canvas"]'); + await expect(canvas.locator('.djs-element').first()).toBeVisible({ timeout: 30_000 }); + + // ① 中文节点名渲染正常(存量会签流程节点「编写交底」「接收交底」)。 + await expect(canvas.getByText('编写交底')).toBeVisible(); + await expect(canvas.getByText('接收交底')).toBeVisible(); + + // ② palette 元素齐(bpmn-js Modeler 左侧工具箱有多个可拖建条目)。 + await expect(page.locator('[data-testid="process-modeler-root"] .djs-palette').first()).toBeVisible(); + expect(await page.locator('[data-testid="process-modeler-root"] .djs-palette .entry').count()).toBeGreaterThan(4); + + // ③ 标准属性面板 + EIP4 侧板同屏(选中会签 userTask 后 EIP4 侧板出真控件,与标准面板宿主并存)。 + await selectBpmnElement(page, COSIGN_TASK_ID); + await expect(page.getByTestId('process-modeler-eip4-panel')).toBeVisible(); + await expect(page.getByTestId('eip4-cosign-toggle')).toBeVisible(); + await expect(page.getByTestId('process-modeler-props')).toBeVisible(); + + // ④ 超管门 403:dev 库无 alice/123456 → 降级为「非超管凭据打 deploy/models → 403」等价断言。 + const alice = await apiJson('POST', '/api/auth/login', undefined, { account: 'alice', password: '123456' }); + expect(alice.json?.success, 'dev 库无 alice 账号(记录账号缺席)').toBeFalsy(); + + // 无 token(非超管)打 Modeler 网关端点 → requireSuperAdmin 拒 403(与合法非超管 token 同一拒绝分支)。 + const denyDeploy = await apiJson('POST', '/api/workflow/models/nonexistent/deploy'); + expect(denyDeploy.status, '非超管访问 deploy 端点应 403').toBe(403); + const denyModels = await apiJson('GET', '/api/workflow/models'); + expect(denyModels.status, '非超管访问 models 列表应 403').toBe(403); + + // 对照:admin(超管)打 models → 200,证超管门按身份区分放行,非一律拒。 + const tok = await adminToken(); + const allowModels = await apiJson('GET', '/api/workflow/models', tok); + expect(allowModels.status, 'admin 超管访问 models 应 200').toBe(200); +}); diff --git a/playwright.process-modeler.config.ts b/playwright.process-modeler.config.ts new file mode 100644 index 0000000..8bce749 --- /dev/null +++ b/playwright.process-modeler.config.ts @@ -0,0 +1,36 @@ +import { defineConfig, devices } from '@playwright/test'; + +// 在线流程 Modeler live e2e 专用配置(T4.2)。 +// +// 复用主会话已在跑的 vite dev(5173,vite.config 默认 proxy /api → http://localhost:8093), +// 后端指向当前 live 的 8093 网关 + 8089 flowable-service + 真库 eip4(含 T3.1/T3.2/T4.1 全部新代码)。 +// 与 fivering 配置(自起 vite 5174 → 8094)隔离:本配置 reuseExistingServer 复用 5173,不新起端口。 +// +// 依赖(跑前须在线,spec 头注释有 curl 自检): +// - 8093 网关/后端(curl -s -o /dev/null -w '%{http_code}' localhost:8093/v3/api-docs → 200) +// - 8089 flowable-service(经 8093 转发;start/trace 走它) +// - 5173 vite dev(curl localhost:5173 → 200;apiJson 直连 8093,与 UI 经 5173 代理同一后端) +// 跑法:npx playwright test -c playwright.process-modeler.config.ts +export default defineConfig({ + testDir: './e2e', + testMatch: /process-modeler\.spec\.ts/, + fullyParallel: false, + workers: 1, + timeout: 180_000, + reporter: [['list']], + use: { + baseURL: 'http://localhost:5173', + trace: 'off', + screenshot: 'only-on-failure', + }, + projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + webServer: { + // reuseExistingServer:true → 5173 已在跑(主会话 npm run dev)则复用、不新起; + // 未跑时按 dev 同口径(VITE_BACKEND_TARGET=8093)拉起。 + command: + 'NODE_OPTIONS=--max-http-header-size=65536 VITE_BACKEND_TARGET=http://localhost:8093 npx vite --port 5173 --strictPort', + url: 'http://localhost:5173', + reuseExistingServer: true, + timeout: 120_000, + }, +});