feat(modeler): meta/recipe/publish api modules + vitest jest-dom setup

listRecipes 按 §B.9-2 改为静态常量({id,label},不调 /api/ai/page-recipes)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-20 09:35:43 +08:00
parent ae0155eb2c
commit 4b9c9338f3
8 changed files with 247 additions and 0 deletions

49
src/api/meta.test.ts Normal file
View File

@@ -0,0 +1,49 @@
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { http } from './http';
import { createEntity, listEntities, listEntityColumns } from './meta';
vi.mock('./http', () => ({
http: { get: vi.fn(), post: vi.fn() },
}));
const mGet = vi.mocked(http.get);
const mPost = vi.mocked(http.post);
beforeEach(() => {
mGet.mockReset();
mPost.mockReset();
});
describe('meta api', () => {
it('createEntity POST /api/meta/entity 并解包 data', async () => {
mPost.mockResolvedValue({
data: { status: 0, data: { entityId: 'E1', tablename: 'OA_DEMO', attributeCount: 4, indexCount: 1, listApi: '/api/entity/OA_DEMO/list' } },
} as never);
const r = await createEntity({ tablename: 'OA_DEMO', name: '演示', attributes: [{ column: 'TITLE', type: 'string', length: 200, mandatory: true }] });
expect(mPost).toHaveBeenCalledWith('/api/meta/entity', {
tablename: 'OA_DEMO',
name: '演示',
attributes: [{ column: 'TITLE', type: 'string', length: 200, mandatory: true }],
});
expect(r.entityId).toBe('E1');
expect(r.listApi).toBe('/api/entity/OA_DEMO/list');
});
it('listEntities GET /api/meta/entities 并解包数组', async () => {
mGet.mockResolvedValue({ data: { status: 0, data: [{ entityId: 'E1', tablename: 'OA_DEMO', name: '演示' }] } } as never);
const r = await listEntities();
expect(mGet).toHaveBeenCalledWith('/api/meta/entities');
expect(r).toHaveLength(1);
expect(r[0].tablename).toBe('OA_DEMO');
});
it('listEntityColumns GET /api/meta/entity/{table}/attributes 并解包', async () => {
mGet.mockResolvedValue({
data: { status: 0, data: { tablename: 'SYS_DEPART', displayColumn: 'DEPARTNAME', columns: [{ column: 'DEPARTNAME', display: '部门名', type: 'string' }] } },
} as never);
const r = await listEntityColumns('SYS_DEPART');
expect(mGet).toHaveBeenCalledWith('/api/meta/entity/SYS_DEPART/attributes');
expect(r.displayColumn).toBe('DEPARTNAME');
expect(r.columns[0].column).toBe('DEPARTNAME');
});
});

72
src/api/meta.ts Normal file
View File

@@ -0,0 +1,72 @@
// 元数据建模 API —— 实体目录 / 实体属性 / 建表。
// 后端信封统一 {status:0,data:...},本模块解包返回内层 data(镜像 auth.ts 用法)。
import { http } from './http';
export type AttrType = 'string' | 'integer' | 'number' | 'date' | 'datetime' | 'boolean' | 'reference';
export interface EntityAttr {
column: string;
type: AttrType;
length?: number;
display?: string;
mandatory?: boolean;
/** type==='reference' 时必填:外键目标表名(DECISION-1)。 */
referenceTable?: string;
}
export interface CreateEntityBody {
tablename: string;
name?: string;
attributes: EntityAttr[];
}
export interface CreateEntityResult {
entityId: string;
tablename: string;
attributeCount: number;
indexCount: number;
/** 引擎遗留返回 '/api/entity/<TABLE>/list';recipe 页不消费它(recipe 列表走 /api/flow/<TABLE>_SEARCH,见 §B.2/§B.9-11)。 */
listApi: string;
}
export interface EntitySummary {
entityId: string;
tablename: string;
name: string;
}
export interface EntityColumn {
column: string;
display: string;
type: string;
referenceTable?: string;
}
export interface EntityColumns {
tablename: string;
displayColumn: string;
columns: EntityColumn[];
}
interface Envelope<T> {
status: number;
data: T;
}
/** POST /api/meta/entity —— 建表(标准列由后端自动追加)。 */
export async function createEntity(body: CreateEntityBody): Promise<CreateEntityResult> {
const resp = await http.post<Envelope<CreateEntityResult>>('/api/meta/entity', body);
return resp.data.data;
}
/** GET /api/meta/entities —— 实体目录(供引用列选择器)。 */
export async function listEntities(): Promise<EntitySummary[]> {
const resp = await http.get<Envelope<EntitySummary[]>>('/api/meta/entities');
return resp.data.data;
}
/** GET /api/meta/entity/{table}/attributes —— 某表的列定义(含 displayColumn)。 */
export async function listEntityColumns(table: string): Promise<EntityColumns> {
const resp = await http.get<Envelope<EntityColumns>>(`/api/meta/entity/${encodeURIComponent(table)}/attributes`);
return resp.data.data;
}

19
src/api/publish.test.ts Normal file
View File

@@ -0,0 +1,19 @@
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { http } from './http';
import { publishFeature } from './publish';
vi.mock('./http', () => ({ http: { get: vi.fn(), post: vi.fn() } }));
const mPost = vi.mocked(http.post);
beforeEach(() => { mPost.mockReset(); });
describe('publish api', () => {
it('publishFeature POST /api/design/publish 并解包', async () => {
mPost.mockResolvedValue({ data: { status: 0, data: { menuId: 'M1', flows: ['OA_DEMO_ADD'], deploymentPackage: { id: 'pkg-1' } } } } as never);
const r = await publishFeature({ entityId: 'E1', tableName: 'OA_DEMO', templateId: 'tpl-uuid', menuName: '演示', generateDeploymentPackage: true });
expect(mPost).toHaveBeenCalledWith('/api/design/publish', {
entityId: 'E1', tableName: 'OA_DEMO', templateId: 'tpl-uuid', menuName: '演示', generateDeploymentPackage: true,
});
expect(r.menuId).toBe('M1');
expect(r.flows).toContain('OA_DEMO_ADD');
});
});

26
src/api/publish.ts Normal file
View File

@@ -0,0 +1,26 @@
// 发布 API —— 五环终点:触发 5 条默认流 + 注册菜单 + 可选生产部署包(DECISION-2)。
// 发布恒人审:本端点只做「本环境即用 + 生产部署包生成」,生产上线仍走人工人审流程。
import { http } from './http';
export interface PublishFeatureBody {
entityId: string;
tableName: string;
templateId: string;
menuName: string;
parentMenuId?: string;
generateDeploymentPackage?: boolean;
}
export interface PublishFeatureResult {
menuId: string;
flows: string[];
deploymentPackage?: unknown;
}
interface Envelope<T> { status: number; data: T; }
/** POST /api/design/publish。 */
export async function publishFeature(body: PublishFeatureBody): Promise<PublishFeatureResult> {
const resp = await http.post<Envelope<PublishFeatureResult>>('/api/design/publish', body);
return resp.data.data;
}

31
src/api/recipe.test.ts Normal file
View File

@@ -0,0 +1,31 @@
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { http } from './http';
import { compileRecipe, listRecipes } from './recipe';
vi.mock('./http', () => ({ http: { get: vi.fn(), post: vi.fn() } }));
const mGet = vi.mocked(http.get);
const mPost = vi.mocked(http.post);
beforeEach(() => { mGet.mockReset(); mPost.mockReset(); });
describe('recipe api', () => {
it('compileRecipe POST /api/design/recipe/compile 并解包', async () => {
mPost.mockResolvedValue({ data: { status: 0, data: { templateId: 'tpl-uuid', etag: 'W/"1"' } } } as never);
const r = await compileRecipe({ tableName: 'OA_DEMO', recipe: 'maintenance-crud-v1', title: '演示维护', displayField: 'TITLE' });
expect(mPost).toHaveBeenCalledWith('/api/design/recipe/compile', {
tableName: 'OA_DEMO', recipe: 'maintenance-crud-v1', title: '演示维护', displayField: 'TITLE',
});
expect(r.templateId).toBe('tpl-uuid');
expect(r.etag).toBe('W/"1"');
});
// §B.9-2:listRecipes 改静态常量(非 async、不调 /api/ai/page-recipes)。
it('listRecipes 返回 v1 固定两 recipe(静态 {id,label})', () => {
const r = listRecipes();
expect(r).toEqual([
{ id: 'maintenance-crud-v1', label: '维护型 CRUD' },
{ id: 'tree-crud-v1', label: '树形 CRUD' },
]);
// 不触网:listRecipes 是同步数组,不调 http.get
expect(mGet).not.toHaveBeenCalled();
});
});

40
src/api/recipe.ts Normal file
View File

@@ -0,0 +1,40 @@
// recipe 编译 API —— 把实体 + recipe 编译为标准页(templateId 由服务端生成)。
import { http } from './http';
export type RecipeId = 'maintenance-crud-v1' | 'tree-crud-v1';
export interface CompileRecipeBody {
tableName: string;
recipe: RecipeId;
title: string;
displayField?: string;
pageSize?: number;
}
export interface CompileRecipeResult {
templateId: string;
etag: string;
}
export interface RecipeOption {
id: string;
label: string;
}
interface Envelope<T> { status: number; data: T; }
/** POST /api/design/recipe/compile —— 服务端建蓝图→编译→校验→PageSchemaWriter.upsertStandard。 */
export async function compileRecipe(body: CompileRecipeBody): Promise<CompileRecipeResult> {
const resp = await http.post<Envelope<CompileRecipeResult>>('/api/design/recipe/compile', body);
return resp.data.data;
}
// §B.9-2:v1 固定两 recipe(YAGNI),无需调 /api/ai/page-recipes
// (其裸数组 {recipe,schemaVersion,title,...} 形状与本前端 {id,label} 契约不符)。
// recipe id 全程统一 *-v1 形(normalizePageRecipe 只接受 *-v1,见 §B.8/§B.9-1)。
export function listRecipes(): RecipeOption[] {
return [
{ id: 'maintenance-crud-v1', label: '维护型 CRUD' },
{ id: 'tree-crud-v1', label: '树形 CRUD' },
];
}

9
src/setupTests.ts Normal file
View File

@@ -0,0 +1,9 @@
// vitest 全局测试基建:注册 jest-dom 断言匹配器 + 每个用例后自动卸载组件树。
// 在 vitest.config.ts 的 test.setupFiles 里登记,所有 *.test.tsx 共享。
import '@testing-library/jest-dom/vitest';
import { afterEach } from 'vitest';
import { cleanup } from '@testing-library/react';
afterEach(() => {
cleanup();
});

View File

@@ -8,6 +8,7 @@ export default defineConfig({
test: {
environment: 'jsdom',
globals: true,
setupFiles: ['./src/setupTests.ts'],
include: ['src/**/*.test.ts', 'src/**/*.test.tsx'],
},
});