gongxiangp2/apps/detail-agent-pi/test/enterprise-context.test.mjs

98 lines
5.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import path from 'node:path';
import os from 'node:os';
import {EnterpriseContext} from '../src/enterprise-context.mjs';
import {EnterpriseKnowledge} from '../src/enterprise-knowledge.mjs';
test('selected resource guides are read completely with their own versions',async t=>{
const root=await fs.mkdtemp(path.join(os.tmpdir(),'xiaoyi-guides-'));
t.after(()=>fs.rm(root,{recursive:true}));
const calls=[],documents=[];
const display=['SKILL.md','SKILL.md','验图全文.md'];
const assets=display.map((name,index)=>({id:'guide-'+index,role:'guide',required:true,status:'available',
file:{name,path:'Skills/'+index+'/'+name,fileId:'file-'+index,revision:'revision-'+index}}));
assets.push({id:'optional',role:'guide',required:false,status:'available',file:{fileId:'unneeded'}});
const client={
call:async(name,params)=>{
calls.push([name,params]);
if(name==='list_libraries')return {result:{navigation:[{name:'Skills',path:'Skills'}]}};
return {result:{revision:'view',resourceSet:{brand:'顾家',category:'电脑桌'},components:[{id:'visual_guides',status:'available',assets}]}};
},
readDocument:async (id,revision)=>{
documents.push([id,revision]);
return {result:{text:'第一页\n第二页',pages:2,revision}};
},
};
const ctx=new EnterpriseContext({root,appendEvent:async()=>{}},client);
const result=await ctx.load({task:'image',brandName:'顾家',categoryName:'电脑桌',resourceSetId:'selected'});
assert.deepEqual(calls.map(([name])=>name),['list_libraries','get_resource_set']);
assert.equal(calls[1][1].resourceSetId,'selected');
assert.deepEqual(documents,assets.slice(0,3).map(a=>[a.file.fileId,a.file.revision]));
assert.equal(result.skills.length,2);
assert.equal(result.rules[0].text,'第一页\n第二页');
assert.equal(result.rules[0].pages,2);
const production=await ctx.load({task:'production',brandName:'顾家',categoryName:'电脑桌',resourceSetId:'selected',outputId:'detail'});
assert.deepEqual(production.skills,result.skills);
assert.deepEqual(calls.slice(2).map(([name])=>name),['list_libraries','get_resource_set']);
});
test('explicit template skill sources are read by their published file identity and version', async t => {
const root=await fs.mkdtemp(path.join(os.tmpdir(),'xiaoyi-template-guides-'));
t.after(()=>fs.rm(root,{recursive:true}));
const calls=[];
const docs=[];
const skillSources=[
{fileId:'workflow',path:'Skills/Lighting/SKILL.md',revision:'7',kind:'skill'},
{fileId:'rules',path:'Skills/Lighting/review.md',revision:'11',kind:'review'},
];
const client={
async call(name){
calls.push(name);
return {result:{navigation:[{name:'Skills',path:'Skills'}]}};
},
async readDocument(id,revision) {
docs.push([id,revision]);
return {result:{text:id+' rules',revision}};
},
};
const ctx=new EnterpriseContext({root,appendEvent:async()=>{}},client);
const input={task:'production',brandName:'Lighting Lab',categoryName:'灯具',releaseId:'release-3'};
const result=await ctx.load({...input,skillSources});
assert.deepEqual(calls,['list_libraries']);
assert.deepEqual(docs,[['workflow','7'],['rules','11']]);
assert.equal(result.resourceRevision,'release-3');
assert.equal(result.rules[0].text,'rules rules');
assert.equal(result.skills[0].path,'Skills/Lighting/SKILL.md');
await assert.rejects(ctx.load(input),/尚未发布制作 Skill/u);
});
test('knowledge responses accept separate JSON blocks without replacing the requested revision', async () => {
const payload = {revision:'view',resourceSet:{resourceSetId:'selected'},components:[]};
const taskRules = {task:'production',required:true};
const knowledge = new EnterpriseKnowledge({
endpoints:['https://knowledge.test'],
credentialProvider:async()=> 'test-credential',
fetchImpl:async (_url,options)=>{
if(!options.body) return Response.json({status:'ok',service:'enterprise-knowledge-base'});
const {id,method} = JSON.parse(options.body);
if(method==='notifications/initialized') return new Response(null,{status:202});
const result={
initialize:{protocolVersion:'2025-11-25'},
'tools/list':{tools:[{name:'get_resource_set',inputSchema:{type:'object',
properties:{resourceSetId:{type:'string'},revision:{type:'string'}},
required:['resourceSetId'],additionalProperties:false}}]},
'tools/call':{content:[payload,{taskRules}].map(value=>({type:'text',text:JSON.stringify(value)}))},
}[method];
const envelope={jsonrpc:'2.0',id,result};
return new Response('event: message\ndata: '+JSON.stringify(envelope)+'\n\n',
{headers:{'content-type':'text/event-stream'}});
},
});
const {result} = await knowledge.call('get_resource_set',{resourceSetId:'selected',revision:'view'});
assert.deepEqual(result,{...payload,taskRules});
await assert.rejects(knowledge.call('get_resource_set',{resourceSetId:'selected',revision:'stale'}),{status:409});
});