import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import {readProjectDataDictionary,readAuthorizedDataDictionary} from '../src/data-dictionary.mjs'; import {describeBusinessSchema} from '../../skills/ksf-workshop-runtime/scripts/describe-business-schema.mjs'; import {runWorkshop} from '../../skills/ksf-workshop-runtime/scripts/ksf-workshop.mjs'; const dictionary=JSON.parse(fs.readFileSync(new URL('../references/business-data-dictionary.json',import.meta.url),'utf8')); const definitions=JSON.parse(fs.readFileSync(new URL('../references/runtime-data.json',import.meta.url),'utf8')); test('完整字典逐表逐列覆盖实际维护定义,来源、系统字段与写入口分开',()=>{ assert.deepEqual(Object.keys(dictionary.tables),Object.keys(definitions.tables)); assert.equal(dictionary.fieldCount,1388); for(const [table,defs]of Object.entries(definitions.tables)){ assert.deepEqual(Object.keys(dictionary.tables[table].fields),Object.keys(defs)); for(const [name,def]of Object.entries(defs)){ const field=dictionary.tables[table].fields[name];assert.ok(field.meaning);assert.equal(field.type,def.type); assert.equal(field.targetClass,def.targetClass); } } assert.equal(dictionary.tables.KsfServiceCredential.fields.tokenHash.allowedInGenericImport,false); assert.equal(dictionary.tables.KfProduct.fields.objectId.writeOwner,'service'); }); test('结构接口只查元信息,保留未知表列并拒绝SQL注入',async()=>{ const queries=[];const pg={any:async(sql,args)=>{ queries.push({sql,args});assert.ok(sql.includes('information_schema.'));assert.ok(!/SELECT\s+\*|INSERT|UPDATE|DELETE|ALTER/i.test(sql)); return sql.includes('information_schema.columns')?[ {table_name:'KfProduct',column_name:'name',data_type:'text',udt_name:'text',is_nullable:'YES',has_default:false}, {table_name:'KfProduct',column_name:'newField',data_type:'text',is_nullable:'YES'}, {table_name:'KfExtra',column_name:'newColumn',data_type:'text',is_nullable:'YES'}, {table_name:'KsfServiceCredential',column_name:'tokenHash',data_type:'text',is_nullable:'YES'}]:[]; }}; const result=await readProjectDataDictionary(pg,dictionary); assert.equal(result.databaseWrites,0);assert.equal(result.rowValuesReturned,false);assert.equal(result.tableCount,3); assert.equal(result.semanticCoverageComplete,false);assert.deepEqual(result.unknownFields,['KfProduct.newField','KfExtra.newColumn']); assert.ok(!JSON.stringify(result).includes('secret-value')); await assert.rejects(()=>readProjectDataDictionary(pg,dictionary,{table:'KfProduct;DROP TABLE x'}),{code:'VALIDATION_FAILED'}); assert.equal(queries.length,2); const compared=describeBusinessSchema(dictionary,{live:result});assert.equal(compared.live.readyForFieldMapping,false); }); test('workshop-schema只发结构查询,不创建身份或业务记录',async()=>{ const calls=[];const r=await runWorkshop('workshop-schema',{summary:true},{requestId:'schema-1',call:async(name,p)=>{ calls.push({name,p});return{readOnly:true,databaseWrites:0,tables:[]}; }}); assert.equal(r.databaseWrites,0);assert.deepEqual(calls,[{name:'ksfGetDataDictionary',p:{summary:true,requestId:'schema-1'}}]); }); test('匿名与缺data:read在读取实际表结构前停止,不因字典扩大行数据权限',async()=>{ let reads=0; const pg={any:async()=>{reads++;return[];}}; const error=code=>{throw Object.assign(Error(code),{code});}; const app={authenticate:async()=>null,need:actor=>{if(!actor)error('AUTH_REQUIRED');if(!actor.scopes.includes('data:read'))error('FORBIDDEN');},fail:error}; await assert.rejects(()=>readAuthorizedDataDictionary({app,pg,dictionary},{},{}),{code:'AUTH_REQUIRED'}); app.authenticate=async()=>({scopes:['ingest:write']}); await assert.rejects(()=>readAuthorizedDataDictionary({app,pg,dictionary},{},{}),{code:'FORBIDDEN'}); assert.equal(reads,0); }); test('真实列类型漂移可识别,摘要回执不能冒充逐字段验证',()=>{ const live={schema:'ksf.live-business-data-dictionary.v1',readOnly:true,databaseWrites:0, semanticCoverageComplete:true,unknownFields:[],tableCount:1,fieldCount:1, tables:[{name:'KfProduct',fields:[{name:'name',logicalName:'name',physicalType:'boolean'}]}]}; const r=describeBusinessSchema(dictionary,{live}); assert.equal(r.live.readyForFieldMapping,false); assert.ok(r.live.differences.some(x=>x.reason==='TYPE_MISMATCH'&&x.field==='name')); assert.throws(()=>describeBusinessSchema(dictionary,{live:{...live,tables:[{name:'KfProduct',fieldCount:1}]}}),/LIVE_DETAILED_SCHEMA_RECEIPT_REQUIRED/); }); test('独立生产结构快照全部1451列有说明,账号扩展仅列元信息并保留平台用途边界',async()=>{ const snapshot=dictionary.physicalSchemaSnapshot; assert.equal(snapshot.tableCount,50);assert.equal(snapshot.fieldCount,1451);assert.equal(snapshot.rowValuesReturned,false); assert.equal(snapshot.tables.reduce((n,t)=>n+t.fields.length,0),1451); for(const t of snapshot.tables)for(const f of t.fields)assert.ok(f.meaning,t.name+'.'+f.name); const extra=dictionary.platformFields._User; assert.equal(Object.keys(extra).length,6); for(const f of Object.values(extra))assert.equal(f.allowedInGenericImport,false); assert.equal(extra.hash.purposeStatus,'platform_reserved'); const pg={any:async sql=>sql.includes('information_schema.columns')?snapshot.tables.flatMap(t=>t.fields.map(f=>({table_name:t.name,column_name:f.name,data_type:f.physicalType,udt_name:f.userType,is_nullable:f.nullable?'YES':'NO',has_default:f.hasDefault}))):[]}; const actual=await readProjectDataDictionary(pg,dictionary); assert.deepEqual(actual.unknownFields,[]);assert.equal(actual.semanticCoverageComplete,true); assert.equal(actual.fieldCount,1451);assert.equal(actual.rowValuesReturned,false); const compared=describeBusinessSchema(dictionary,{live:actual}); assert.equal(compared.live.tablesReadyForFieldMapping.KfProduct,true); assert.ok(!compared.live.differences.some(x=>x.field==='ACL'||x.field==='password'||dictionary.tables[x.table]?.fields[x.field]?.type==='Relation')); assert.equal(dictionary.tables.KfConcept.fields.insightRefs.allowedInGenericImport,false); }); test('登记的Relation关系表纳入实际结构,ACL和密码的物理存储不误报缺列',async()=>{ const queries=[];const pg={any:async(sql,args)=>{queries.push(args);return sql.includes('information_schema.columns')?[ {table_name:'_Join:insightRefs:KfConcept',column_name:'owningId',data_type:'text',is_nullable:'NO'}, {table_name:'_Join:insightRefs:KfConcept',column_name:'relatedId',data_type:'text',is_nullable:'NO'}]:[];}}; const r=await readProjectDataDictionary(pg,dictionary,{table:'_Join:insightRefs:KfConcept'}); assert.equal(r.relationTableCount,1);assert.deepEqual(r.unknownFields,[]);assert.ok(queries[0][1].includes('_Join:insightRefs:KfConcept')); assert.match(r.tables[0].fields[1].meaning,/KfInsight/); await assert.rejects(()=>readProjectDataDictionary(pg,dictionary,{table:'_Join:secret:OtherApp'}),{code:'VALIDATION_FAILED'}); });