From 4b9c9338f33eb918c2c098681c0fa8102a8cec63 Mon Sep 17 00:00:00 2001 From: zhengwr Date: Sat, 20 Jun 2026 09:35:43 +0800 Subject: [PATCH] feat(modeler): meta/recipe/publish api modules + vitest jest-dom setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit listRecipes 按 §B.9-2 改为静态常量({id,label},不调 /api/ai/page-recipes)。 Co-Authored-By: Claude Opus 4.8 (1M context) --- src/api/meta.test.ts | 49 ++++++++++++++++++++++++++++ src/api/meta.ts | 72 +++++++++++++++++++++++++++++++++++++++++ src/api/publish.test.ts | 19 +++++++++++ src/api/publish.ts | 26 +++++++++++++++ src/api/recipe.test.ts | 31 ++++++++++++++++++ src/api/recipe.ts | 40 +++++++++++++++++++++++ src/setupTests.ts | 9 ++++++ vitest.config.ts | 1 + 8 files changed, 247 insertions(+) create mode 100644 src/api/meta.test.ts create mode 100644 src/api/meta.ts create mode 100644 src/api/publish.test.ts create mode 100644 src/api/publish.ts create mode 100644 src/api/recipe.test.ts create mode 100644 src/api/recipe.ts create mode 100644 src/setupTests.ts diff --git a/src/api/meta.test.ts b/src/api/meta.test.ts new file mode 100644 index 0000000..d4dc963 --- /dev/null +++ b/src/api/meta.test.ts @@ -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'); + }); +}); diff --git a/src/api/meta.ts b/src/api/meta.ts new file mode 100644 index 0000000..499863d --- /dev/null +++ b/src/api/meta.ts @@ -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//list';recipe 页不消费它(recipe 列表走 /api/flow/
_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 { + status: number; + data: T; +} + +/** POST /api/meta/entity —— 建表(标准列由后端自动追加)。 */ +export async function createEntity(body: CreateEntityBody): Promise { + const resp = await http.post>('/api/meta/entity', body); + return resp.data.data; +} + +/** GET /api/meta/entities —— 实体目录(供引用列选择器)。 */ +export async function listEntities(): Promise { + const resp = await http.get>('/api/meta/entities'); + return resp.data.data; +} + +/** GET /api/meta/entity/{table}/attributes —— 某表的列定义(含 displayColumn)。 */ +export async function listEntityColumns(table: string): Promise { + const resp = await http.get>(`/api/meta/entity/${encodeURIComponent(table)}/attributes`); + return resp.data.data; +} diff --git a/src/api/publish.test.ts b/src/api/publish.test.ts new file mode 100644 index 0000000..e84b919 --- /dev/null +++ b/src/api/publish.test.ts @@ -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'); + }); +}); diff --git a/src/api/publish.ts b/src/api/publish.ts new file mode 100644 index 0000000..10813b4 --- /dev/null +++ b/src/api/publish.ts @@ -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 { status: number; data: T; } + +/** POST /api/design/publish。 */ +export async function publishFeature(body: PublishFeatureBody): Promise { + const resp = await http.post>('/api/design/publish', body); + return resp.data.data; +} diff --git a/src/api/recipe.test.ts b/src/api/recipe.test.ts new file mode 100644 index 0000000..81029c0 --- /dev/null +++ b/src/api/recipe.test.ts @@ -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(); + }); +}); diff --git a/src/api/recipe.ts b/src/api/recipe.ts new file mode 100644 index 0000000..481dbce --- /dev/null +++ b/src/api/recipe.ts @@ -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 { status: number; data: T; } + +/** POST /api/design/recipe/compile —— 服务端建蓝图→编译→校验→PageSchemaWriter.upsertStandard。 */ +export async function compileRecipe(body: CompileRecipeBody): Promise { + const resp = await http.post>('/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' }, + ]; +} diff --git a/src/setupTests.ts b/src/setupTests.ts new file mode 100644 index 0000000..fdba8ec --- /dev/null +++ b/src/setupTests.ts @@ -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(); +}); diff --git a/vitest.config.ts b/vitest.config.ts index ddc15c0..f4e01da 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -8,6 +8,7 @@ export default defineConfig({ test: { environment: 'jsdom', globals: true, + setupFiles: ['./src/setupTests.ts'], include: ['src/**/*.test.ts', 'src/**/*.test.tsx'], }, });