guanghulab/scripts/hli-contract-check.js
Guanghu Domestic Migration d1e47f4565
Some checks are pending
自动更新代码和重启 / update-and-restart (push) Waiting to run
CI检查 + 自动部署 / check (push) Waiting to run
CI检查 + 自动部署 / deploy (push) Blocked by required conditions
重启聊天服务 / restart (push) Waiting to run
chore: import sanitized domestic snapshot for REPO-002
Source snapshot: ca48d3ddf926d79aa138306164169baf764bb829
2026-07-17 15:54:41 +08:00

94 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
// scripts/hli-contract-check.js
// HLI 接口契约校验 — 验证所有 schema 文件结构完整性
// 版权:国作登字-2026-A-00037559
'use strict';
const fs = require('fs');
const path = require('path');
const SCHEMA_DIR = path.resolve(__dirname, '../src/schemas/hli');
let passed = 0;
let failed = 0;
function check(condition, message) {
if (condition) {
passed++;
} else {
failed++;
console.error(`${message}`);
}
}
console.log('🔍 HLI 接口契约校验\n');
// 扫描所有域目录
const domains = fs.readdirSync(SCHEMA_DIR, { withFileTypes: true })
.filter(d => d.isDirectory())
.map(d => d.name);
for (const domain of domains) {
const domainDir = path.join(SCHEMA_DIR, domain);
const schemaFiles = fs.readdirSync(domainDir).filter(f => f.endsWith('.schema.json'));
for (const file of schemaFiles) {
const filePath = path.join(domainDir, file);
const relPath = `src/schemas/hli/${domain}/${file}`;
let schema;
try {
const content = fs.readFileSync(filePath, 'utf8');
schema = JSON.parse(content);
} catch (e) {
failed++;
console.error(`${relPath}: JSON 解析失败 — ${e.message}`);
continue;
}
console.log(` 📋 ${relPath}`);
// 必须包含 hli_id
check(
typeof schema.hli_id === 'string' && schema.hli_id.startsWith('HLI-'),
`${relPath}: 缺少有效的 hli_id`
);
// route 和 method如存在则校验格式
if (schema.route !== undefined) {
check(
typeof schema.route === 'string' && schema.route.startsWith('/hli/'),
`${relPath}: route 格式无效(应以 /hli/ 开头)`
);
}
if (schema.method !== undefined) {
check(
typeof schema.method === 'string',
`${relPath}: method 格式无效`
);
}
// 必须包含 input 和 output
check(
schema.input !== undefined,
`${relPath}: 缺少 input 定义`
);
check(
schema.output !== undefined,
`${relPath}: 缺少 output 定义`
);
}
}
console.log(`\n📊 契约校验: ${passed} 通过, ${failed} 失败`);
if (failed > 0) {
console.error('❌ HLI Contract Check FAILED');
process.exit(1);
} else {
console.log('✅ HLI Contract Check PASSED');
}