import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import test from 'node:test'; import { fileURLToPath } from 'node:url'; import vm from 'node:vm'; import ts from 'typescript'; const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const fromProjectRoot = (...segments) => path.join(projectRoot, ...segments); function loadResources() { const compiled = ts.transpileModule(fs.readFileSync(fromProjectRoot('src/api/resources.ts'), 'utf8'), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, }).outputText; const resourceModule = { exports: {} }; vm.runInNewContext(compiled, { module: resourceModule, exports: resourceModule.exports }); return resourceModule.exports.resources; } test('资源字段声明保留数字、布尔、时间与 JSON 类型', () => { const resources = loadResources(); const field = (resource, key) => resources.find((item) => item.name === resource).fields.find((item) => item.key === key); assert.equal(field('ec_product', 'price_amount').type, 'number'); assert.equal(field('ec_product_image', 'is_cover').type, 'boolean'); assert.equal(field('delivery_track', 'started_at').type, 'datetime'); assert.equal(field('dev_telemetry', 'payload').type, 'json'); }); test('表单载荷构造器省略空的可选关系并转换字段类型', async () => { const resourceFormPath = fromProjectRoot('src/api/resource-form.ts'); assert.ok(fs.existsSync(resourceFormPath), 'resource-form.ts should define the payload boundary'); const source = fs.readFileSync(resourceFormPath, 'utf8'); const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, }).outputText; const resourceModule = { exports: {} }; vm.runInNewContext(compiled, { module: resourceModule, exports: resourceModule.exports }); const fields = [ { key: 'gas_basic_identity', label: '气站', type: 'identity' }, { key: 'quantity', label: '数量', type: 'number' }, { key: 'selected', label: '选中', type: 'boolean' }, ]; assert.deepEqual( JSON.parse(JSON.stringify(resourceModule.exports.buildResourcePayload(fields, { gas_basic_identity: '', quantity: '2', selected: false, }))), { quantity: 2, selected: false }, ); }); test('审批只读页提供同意和驳回操作', () => { const source = fs.readFileSync(fromProjectRoot('src/views/shared/ReadOnlyListPage.vue'), 'utf8'); const resources = fs.readFileSync(fromProjectRoot('src/api/resources.ts'), 'utf8'); assert.match(resources, /aud_approval[\s\S]*\/audit\/aud_approval\/:identity\/approve/); assert.match(source, /submitDetailAction/); }); test('树页面通过资源归档接口归档节点', () => { const source = fs.readFileSync(fromProjectRoot('src/views/shared/TreePage.vue'), 'utf8'); assert.match(source, /resourceApi\.archive/); assert.match(source, /Modal\.(warning|confirm)/); });