fix(modeler): 打开器改 by-key 载入(修 options source=model 语义缺口)
live 验收发现:本库 /api/process-select/options 的 143 个选项 source 全为 'model',
option.id 是 act_re_model id(非 procDefId)→ 按 id 载入永远 found:false;option.key 才是流程 key。
- processModeler.ts:加 fetchProcDefSourceByKey(key) → GET
/api/workflow/process-definitions/by-key/{key}/source(key 常纯中文,encodeURIComponent;
返回形状同 fetchProcDefSource,共用 normalizeProcDefSource:found:false 不解构 bpmnXml/idMap)。
ProcessOption 加 source 字段。
- ProcessModelerPage:打开器双参分派——source==='processDefinition' 用 ?procDefId=<id>,
其余(model/history)用 ?procKey=<key>;loadEffect 按参数派 by-id / by-key;
过滤规则 !!o.id 改为 source==='processDefinition' ? !!o.id : !!o.key;
选择器用索引取回选项对象避免路由二义。
测试:processModeler.test.ts +2(纯中文 key 编码 / by-key found:false)+ source 映射断言;
ProcessModelerPage.test.tsx +3(?procKey= 走 by-key 并 importXML / 选 model 选项派 by-key /
选 processDefinition 选项派 by-id)。全量 vitest 22 文件 117 例绿;tsc -b 干净;npm run build 成功。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { SYNTHETIC_BPMN } from '../modeler/moddle/fixtures/synthetic-full-surface.bpmn';
|
||||
|
||||
@@ -25,6 +25,7 @@ vi.mock('bpmn-js-properties-panel', () => ({
|
||||
}));
|
||||
vi.mock('../api/processModeler', () => ({
|
||||
fetchProcDefSource: vi.fn(),
|
||||
fetchProcDefSourceByKey: vi.fn(),
|
||||
fetchProcessOptions: vi.fn(),
|
||||
}));
|
||||
vi.mock('../auth/token', async (importOriginal) => ({
|
||||
@@ -33,10 +34,11 @@ vi.mock('../auth/token', async (importOriginal) => ({
|
||||
}));
|
||||
|
||||
import { ProcessModelerPage } from './ProcessModelerPage';
|
||||
import { fetchProcDefSource, fetchProcessOptions } from '../api/processModeler';
|
||||
import { fetchProcDefSource, fetchProcDefSourceByKey, fetchProcessOptions } from '../api/processModeler';
|
||||
import { getProfile } from '../auth/token';
|
||||
|
||||
const mSource = vi.mocked(fetchProcDefSource);
|
||||
const mSourceByKey = vi.mocked(fetchProcDefSourceByKey);
|
||||
const mOptions = vi.mocked(fetchProcessOptions);
|
||||
const mProfile = vi.mocked(getProfile);
|
||||
|
||||
@@ -55,6 +57,7 @@ beforeEach(() => {
|
||||
onMock.mockReset();
|
||||
ModelerCtor.mockClear();
|
||||
mSource.mockReset().mockResolvedValue({ found: true, bpmnXml: SYNTHETIC_BPMN, idMap: { '总包合同交底': 'Proc_zbht' } });
|
||||
mSourceByKey.mockReset().mockResolvedValue({ found: true, bpmnXml: SYNTHETIC_BPMN, idMap: { '总包合同交底': 'Proc_zbht' } });
|
||||
mOptions.mockReset().mockResolvedValue([]);
|
||||
mProfile.mockReset().mockReturnValue({ userId: 'ADMIN', account: 'admin', name: '管理员', roles: [] });
|
||||
});
|
||||
@@ -87,11 +90,40 @@ describe('ProcessModelerPage', () => {
|
||||
expect(importXMLMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('无 procDefId 时展示「打开存量流程」选择器(数据源 process-select)', async () => {
|
||||
mOptions.mockResolvedValue([{ label: '总包合同交底', value: '总包合同交底', id: 'PD-9', key: 'zbht' }]);
|
||||
it('无参时展示「打开存量流程」选择器(数据源 process-select)', async () => {
|
||||
mOptions.mockResolvedValue([{ label: '总包合同交底', value: '总包合同交底', source: 'model', id: 'MDL-1', key: 'zbht' }]);
|
||||
renderPage();
|
||||
await waitFor(() => expect(mOptions).toHaveBeenCalled());
|
||||
expect(screen.getByTestId('process-modeler-open-select')).toBeInTheDocument();
|
||||
expect(await screen.findByText('总包合同交底')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('?procKey= 时走 by-key 载入并 importXML(source=model 语义缺口修复)', async () => {
|
||||
renderPage('/process-modeler?procKey=' + encodeURIComponent('总包合同交底'));
|
||||
await waitFor(() => expect(mSourceByKey).toHaveBeenCalledWith('总包合同交底'));
|
||||
await waitFor(() => expect(importXMLMock).toHaveBeenCalledWith(SYNTHETIC_BPMN));
|
||||
expect(mSource).not.toHaveBeenCalled(); // 未走 by-id 路径
|
||||
});
|
||||
|
||||
it('选中 source=model 选项 → 派到 by-key(procKey),不走 by-id', async () => {
|
||||
mOptions.mockResolvedValue([{ label: '模型流程', value: '模型流程', source: 'model', id: 'MDL-9', key: 'MKEY' }]);
|
||||
renderPage();
|
||||
const select = await screen.findByTestId('process-modeler-open-select');
|
||||
await screen.findByText('模型流程');
|
||||
fireEvent.change(select, { target: { value: '0' } });
|
||||
await waitFor(() => expect(mSourceByKey).toHaveBeenCalledWith('MKEY'));
|
||||
expect(mSource).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('选中 source=processDefinition 选项 → 派到 by-id(procDefId)', async () => {
|
||||
mOptions.mockResolvedValue([
|
||||
{ label: '部署流程', value: '部署流程', source: 'processDefinition', id: '部署流程:3:uuid', key: 'k' },
|
||||
]);
|
||||
renderPage();
|
||||
const select = await screen.findByTestId('process-modeler-open-select');
|
||||
await screen.findByText('部署流程');
|
||||
fireEvent.change(select, { target: { value: '0' } });
|
||||
await waitFor(() => expect(mSource).toHaveBeenCalledWith('部署流程:3:uuid'));
|
||||
expect(mSourceByKey).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,7 +12,12 @@ import 'bpmn-js/dist/assets/bpmn-font/css/bpmn.css';
|
||||
import '@bpmn-io/properties-panel/dist/assets/properties-panel.css';
|
||||
import activitiModdle from '../modeler/moddle/activiti.json';
|
||||
import { getProfile, isAdminProfile } from '../auth/token';
|
||||
import { fetchProcDefSource, fetchProcessOptions, type ProcessOption } from '../api/processModeler';
|
||||
import {
|
||||
fetchProcDefSource,
|
||||
fetchProcDefSourceByKey,
|
||||
fetchProcessOptions,
|
||||
type ProcessOption,
|
||||
} from '../api/processModeler';
|
||||
|
||||
// bpmn-js/lib/Modeler 自带类型,但选项对象里的标准面板模块(本项目无类型)与 moddle JSON
|
||||
// 会与官方选项类型产生摩擦;照 ProcessDiagram 惯例把构造器收窄为本页用到的最小契约再实例化。
|
||||
@@ -31,7 +36,11 @@ export function ProcessModelerPage() {
|
||||
const isAdmin = isAdminProfile(profile);
|
||||
const navigate = useNavigate();
|
||||
const [params, setParams] = useSearchParams();
|
||||
// 打开存量流程两条路:?procDefId=(source=processDefinition,id 即 procDefId)
|
||||
// 或 ?procKey=(source=model/history,按流程 key 载入最新版)。
|
||||
const procDefId = params.get('procDefId') ?? '';
|
||||
const procKey = params.get('procKey') ?? '';
|
||||
const hasTarget = !!procDefId || !!procKey;
|
||||
|
||||
const canvasRef = useRef<HTMLDivElement>(null);
|
||||
const panelRef = useRef<HTMLDivElement>(null);
|
||||
@@ -44,9 +53,9 @@ export function ProcessModelerPage() {
|
||||
const [options, setOptions] = useState<ProcessOption[]>([]);
|
||||
const [optionsError, setOptionsError] = useState('');
|
||||
|
||||
// 无参时拉存量流程选项(数据源复用旧 ProcessSelectDialog 端点)。
|
||||
// 未打开任何流程时拉存量流程选项(数据源复用旧 ProcessSelectDialog 端点)。
|
||||
useEffect(() => {
|
||||
if (!isAdmin || procDefId) return;
|
||||
if (!isAdmin || hasTarget) return;
|
||||
let alive = true;
|
||||
fetchProcessOptions()
|
||||
.then((opts) => alive && setOptions(opts))
|
||||
@@ -54,11 +63,11 @@ export function ProcessModelerPage() {
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [isAdmin, procDefId]);
|
||||
}, [isAdmin, hasTarget]);
|
||||
|
||||
// 有 procDefId 时挂载 Modeler 并载入净化后的存量 BPMN。
|
||||
// 有目标(procDefId 或 procKey)时挂载 Modeler 并载入净化后的存量 BPMN。
|
||||
useEffect(() => {
|
||||
if (!isAdmin || !procDefId) return;
|
||||
if (!isAdmin || !hasTarget) return;
|
||||
let alive = true;
|
||||
let modeler: ModelerLike | null = null;
|
||||
setLoadState('loading');
|
||||
@@ -66,7 +75,10 @@ export function ProcessModelerPage() {
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const src = await fetchProcDefSource(procDefId);
|
||||
// procDefId 优先(source=processDefinition);否则按 key(source=model/history)。
|
||||
const src = procDefId
|
||||
? await fetchProcDefSource(procDefId)
|
||||
: await fetchProcDefSourceByKey(procKey);
|
||||
if (!alive) return;
|
||||
if (!src.found || !src.bpmnXml) {
|
||||
setLoadError('未找到该流程的 BPMN 源(可能未部署或无布局信息)');
|
||||
@@ -115,17 +127,29 @@ export function ProcessModelerPage() {
|
||||
}
|
||||
modelerRef.current = null;
|
||||
};
|
||||
}, [isAdmin, procDefId]);
|
||||
}, [isAdmin, procDefId, procKey]);
|
||||
|
||||
if (!isAdmin) {
|
||||
return <div style={{ padding: 24, color: 'var(--text-3)' }}>流程设计器仅管理员可用。</div>;
|
||||
}
|
||||
|
||||
function openProcess(id: string) {
|
||||
if (id) setParams({ procDefId: id });
|
||||
}
|
||||
// 可载入 = 有可用打开参数:processDefinition 需 id(procDefId);其余(model/history)需 key。
|
||||
const loadableOptions = options.filter((o) =>
|
||||
o.source === 'processDefinition' ? !!o.id : !!o.key,
|
||||
);
|
||||
|
||||
const loadableOptions = options.filter((o) => !!o.id);
|
||||
// 选择器按 source 分派:processDefinition→?procDefId=<id>;其余→?procKey=<key>。
|
||||
// 用索引取回选项对象(避免把 id/key 直接塞进 <option value> 造成路由二义)。
|
||||
function openSelected(indexValue: string) {
|
||||
if (indexValue === '') return;
|
||||
const opt = loadableOptions[Number(indexValue)];
|
||||
if (!opt) return;
|
||||
if (opt.source === 'processDefinition' && opt.id) {
|
||||
setParams({ procDefId: opt.id });
|
||||
} else if (opt.key) {
|
||||
setParams({ procKey: opt.key });
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -146,7 +170,7 @@ export function ProcessModelerPage() {
|
||||
>
|
||||
<span style={{ fontWeight: 600, fontSize: 15 }}>流程设计器</span>
|
||||
<span className="muted" style={{ fontSize: 12, flex: 1 }}>
|
||||
{procDefId ? `已打开:${procDefId}` : '在线编辑 BPMN 流程定义(可编辑存量 / 新建)'}
|
||||
{hasTarget ? `已打开:${procDefId || procKey}` : '在线编辑 BPMN 流程定义(可编辑存量 / 新建)'}
|
||||
</span>
|
||||
<button className="btn btn-sm" data-testid="process-modeler-save-draft" disabled title="保存草稿(下阶段)">
|
||||
保存草稿
|
||||
@@ -165,7 +189,7 @@ export function ProcessModelerPage() {
|
||||
<div ref={canvasRef} data-testid="process-modeler-canvas" style={{ width: '100%', height: '100%' }} />
|
||||
|
||||
{/* 未打开任何流程:叠加「打开存量流程」选择器 + 新建占位 */}
|
||||
{!procDefId ? (
|
||||
{!hasTarget ? (
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
@@ -182,12 +206,12 @@ export function ProcessModelerPage() {
|
||||
<select
|
||||
data-testid="process-modeler-open-select"
|
||||
defaultValue=""
|
||||
onChange={(e) => openProcess(e.target.value)}
|
||||
onChange={(e) => openSelected(e.target.value)}
|
||||
style={{ minWidth: 280, padding: '6px 8px' }}
|
||||
>
|
||||
<option value="">打开存量流程…</option>
|
||||
{loadableOptions.map((o) => (
|
||||
<option key={o.id} value={o.id}>
|
||||
{loadableOptions.map((o, i) => (
|
||||
<option key={o.id ?? o.key ?? i} value={i}>
|
||||
{o.label}
|
||||
</option>
|
||||
))}
|
||||
@@ -202,12 +226,12 @@ export function ProcessModelerPage() {
|
||||
) : null}
|
||||
|
||||
{/* 载入中 / 载入失败 */}
|
||||
{procDefId && loadState === 'loading' ? (
|
||||
{hasTarget && loadState === 'loading' ? (
|
||||
<div style={overlayMsgStyle('var(--text-3)')} data-testid="process-modeler-loading">
|
||||
加载流程中…
|
||||
</div>
|
||||
) : null}
|
||||
{procDefId && loadState === 'error' ? (
|
||||
{hasTarget && loadState === 'error' ? (
|
||||
<div style={overlayMsgStyle('var(--warn, #d48806)')} data-testid="process-modeler-error">
|
||||
{loadError}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user