import { test } from 'node:test'; import assert from 'node:assert/strict'; import { createCustomsTools } from '../src/harness/tools/index.js'; import { ToolRegistry, defineTool } from '../src/harness/registry.js'; import { EmbeddedHarness } from '../src/harness/runtime.js'; import { ReplayLlmClient } from '../src/harness/llm.js'; import { presetFromObject } from '../src/harness/preset.js'; import type { ToolsClient } from '../src/tools/client.js'; const context={callId:'contract_call',sessionId:'contract_session',workspaceId:'contract_workspace'}; function fixture() { const payloads: any[]=[]; const client={ async listObjects() {return {items:[{id:'obj1',name:'test',kind:'stat',rows:100,created_at:'2026-11-18',tables:[{name:'查询_品牌型号月度',rows:100,columns:['品牌','型号','金额'],catalog:{subject:'品牌型号',row_grain:'方向×年月×品牌×型号',coverage:'全部纳入记录',aggregation:{non_additive:['价格指数']},metric_definitions:{金额:'严格Decimal总计'},preferred_for:'全量金额数量'}}]}]};}, async dataRead(payload:any) {payloads.push(payload);return {object_id:'obj1',table:'查询_品牌型号月度',columns:['金额'],rows:[{values:{金额:'0.3'},_trace:{object_id:'obj1',table:'查询_品牌型号月度',row_index:0,row_hash:'sha256:row'}}],total:null,total_exact:false,truncated:true,truncation_reason:'row_limit',next_cursor:'bound-cursor',warning:'仅返回受限子集,不能作为全量',cache_hit:true,offset:0,limit:50,exhausted:false};}, } as unknown as ToolsClient; return {tools:createCustomsTools({client,maxRowsPerRead:50,maxToolResultChars:24000}),payloads}; } test('单表目录保留业务粒度、定义与不可加指标',async()=>{ const {tools}=fixture();const list=tools.find(t=>t.name==='list_objects')!; const output=await list.execute({object_id:'obj1',table:'查询_品牌型号月度'},context); const content=output.content as any; assert.equal(content.objects[0].tables[0].catalog.coverage,'全部纳入记录'); assert.deepEqual(content.objects[0].tables[0].catalog.aggregation.non_additive,['价格指数']); }); test('查询参数透传、limit受限,未计数不能伪装成零',async()=>{ const {tools,payloads}=fixture();const read=tools.find(t=>t.name==='data_read')!; const output=await read.execute({object_id:'obj1',limit:500,cursor:'cursor_in',count_mode:'none'},context); assert.equal(payloads[0].limit,50);assert.equal(payloads[0].cursor,'cursor_in');assert.equal(payloads[0].count_mode,'none'); assert.equal((output.content as any).total_matching_rows,null); assert.equal((output.content as any).total_exact,false); assert.equal((output.content as any).truncated,true); assert.equal((output.content as any).next_cursor,'bound-cursor'); assert.equal(output.rows![0]._trace.row_hash,'sha256:row'); }); test('截断状态和警告须进入流式工具结果,供界面直接显示',async()=>{ const registry=new ToolRegistry();registry.register(defineTool({name:'bounded',description:'bounded',parameters:{type:'object'},async execute(){return {content:{truncated:true,warning:'只显示一页,不能视为全部'},rows:[]};}})); const llm=new ReplayLlmClient([{toolCalls:[{name:'bounded',arguments:{}}]},{text:'这里只展示一页。'}]); const resolve=()=>({provider:{id:'deepseek',displayName:'test',baseUrl:'http://unused',apiKey:'test',api:'openai-completions' as const,models:['deepseek-flash'],supportsTools:true},model:'deepseek-flash',temperature:0.1,maxOutputTokens:1000}); const runtime=new EmbeddedHarness({registry,llm,resolve}); const preset=presetFromObject({name:'bounded-test',model:{provider:'deepseek',name:'deepseek-flash'},tools:['bounded'],systemPrompt:'test',limits:{maxSteps:3}}); const events=[]; for await(const event of runtime.run({sessionId:'s',workspaceId:'w',messages:[{role:'user',content:'query'}],preset}))events.push(event); const event=events.find(e=>e.type==='tool_result') as any; assert.equal(event.truncated,true);assert.equal(event.warning,'只显示一页,不能视为全部'); });