Files
quality_analyse/web/verification/analysis_round1.test.mjs
T

337 lines
23 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import ts from 'typescript';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { analysisScopeText, analysisComposerState, restoredAnalysisDraft, analysisFollowRequestReady } from '../src/analysisScope.ts';
import { analysisIsRunning, analysisScrollFollows, compactAnalysisTimeline, duplicatesFinalAnalysisAnswer } from '../src/analysisConversationState.ts';
import { sanitizeAnalysisDisplayText } from '../src/analysisDisplayText.ts';
import { modelThinkingEntries, thinkingStatus, orderedAnalysisProcess, groupAdjacentThinking } from '../src/analysisModelThinking.ts';
const visual = (overrides = {}) => ({
id:'main',type:'column',title:'期间比较',subtitle:'',unit:'ratio',
categories:[{key:'prior',label:'8月'},{key:'current',label:'9月'}],
series:[{key:'composite_rate',label:'综合合格率',values:[.95,.94]}],
evidenceRefs:['Q1234ABCD'],requiredFields:['black_good_weight','black_total_weight','bright_good_weight','bright_total_weight'],
scope:{year:2026,month:9,releaseId:'release-a',includeUnassigned:false},period:{year:2026,month:9},comparisonPeriod:{year:2026,month:8},
metric:'composite_rate',coverage:{totalRows:2,returnedRows:2,truncated:false},originTurnId:'turn-a',...overrides,
});
test('a draft does not invent a period or company scope from question prose',()=>{
assert.equal(analysisScopeText({question:'2026年9月全公司整体情况',status:'QUEUED'}),'');
assert.equal(analysisScopeText({plan:{context:{releaseId:'private'}}}),'');
});
test('resolved per-turn scope wins over frozen submission and unrelated chart scope',()=>{
const scope=analysisScopeText({id:'turn-a',plan:{context:{year:2026,quarter:3,product:'18X15X4+T',stage:'bright',includeUnassigned:false},frozenContext:{year:2025,month:1},semantic:{compare:true,priorContext:{year:2026,quarter:2}}},visuals:[visual()]});
assert.equal(scope,'2026年第3季度 · 对比 2026年第2季度 · 产品:18X15X4+T · 亮片 · 不含未分类');
});
test('a single-value query does not treat previous context as a comparison period',()=>{
for(const compare of [false,undefined]){
const scope=analysisScopeText({plan:{context:{year:2026,month:9,product:'新产品'},semantic:{compare,priorContext:{year:2026,month:8,product:'旧产品'}}}});
assert.equal(scope,'2026年9月 · 产品:新产品');
}
});
test('changing products in the same month does not add an implicit comparison',()=>{
const scope=analysisScopeText({plan:{context:{year:2026,month:9,product:'B'},semantic:{compare:false,priorContext:{year:2026,month:9,product:'A'}}}});
assert.equal(scope,'2026年9月 · 产品:B');
});
test('native historical scope uses the unique broadest binding, not local product charts',()=>{
const scope=analysisScopeText({id:'turn-a',plan:{context:{releaseId:'release-a'},frozenContext:{releaseId:'release-a'}},visuals:[
visual({id:'local',scope:{year:2026,month:9,product:'小规格',stage:'black',releaseId:'release-a'},stage:'black'}),visual(),
]});
assert.equal(scope,'2026年9月 · 对比 2026年8月 · 当前授权范围 · 不含未分类');
});
test('equal-depth conflicting periods are never joined into one scope',()=>{
const scope=analysisScopeText({id:'turn-a',visuals:[visual(),visual({id:'other',period:{year:2026,month:7}})]});
assert.equal(scope,'');
});
test('two stages combine only when every other binding is identical',()=>{
assert.equal(analysisScopeText({id:'turn-a',visuals:[visual({id:'black',stage:'black'}),visual({id:'bright',stage:'bright'})]}),
'2026年9月 · 对比 2026年8月 · 当前授权范围 · 黑片与亮片 · 不含未分类');
});
test('native product follow-up keeps its product and both explicit stages',()=>{
const scope={year:2026,month:9,product:'16.33X15.5X3.75+T',includeUnassigned:false,releaseId:'release-a'};
assert.equal(analysisScopeText({id:'turn-a',plan:{context:{releaseId:'release-a'},frozenContext:{releaseId:'release-a'}},visuals:[
visual({id:'black',metric:'black_rate',scope:{...scope,stage:'black'},stage:'black'}),
visual({id:'bright',metric:'bright_rate',scope:{...scope,stage:'bright'},stage:'bright'}),
]}), '2026年9月 · 对比 2026年8月 · 产品:16.33X15.5X3.75+T · 黑片与亮片 · 不含未分类');
});
test('opposite stages cannot combine different products, periods or data releases',()=>{
const scope={year:2026,month:9,product:'A',releaseId:'release-a'};
const first=visual({id:'black',scope:{...scope,stage:'black'},stage:'black'});
for(const change of [{scope:{...scope,product:'B',stage:'bright'}},{period:{year:2026,month:7}},{scope:{...scope,releaseId:'release-b',stage:'bright'}}]){
assert.equal(analysisScopeText({id:'turn-a',visuals:[first,visual({id:'bright',scope:{...scope,stage:'bright'},stage:'bright',...change})]}),'');
}
});
test('scope ignores a visual explicitly bound to another turn',()=>{
assert.equal(analysisScopeText({id:'turn-b',visuals:[visual()]}),'');
});
test('identical root bindings are accepted while nested result payloads are supported',()=>{
assert.equal(analysisScopeText({id:'turn-a',result:{visuals:[visual(),visual({id:'second'})]}}),
'2026年9月 · 对比 2026年8月 · 当前授权范围 · 不含未分类');
});
test('compatible actual chart period refines a year-only submitted context',()=>{
assert.equal(analysisScopeText({id:'turn-a',plan:{context:{year:2026}},visuals:[visual()]}),
'2026年9月 · 对比 2026年8月 · 当前授权范围 · 不含未分类');
});
test('every bound extra condition stays visible instead of claiming a broader scope',()=>{
assert.equal(analysisScopeText({plan:{context:{year:2026,month:9,group:'["国内","海外"]',extraFilters:{厂区:['一厂']},exclusions:{product:['旧规格']}}}}),
'2026年9月 · 业务分组:国内、海外 · 厂区:一厂 · 排除产品:旧规格');
});
test('a product keyword remains visible even with the overall view',()=>{
assert.equal(analysisScopeText({plan:{context:{year:2026,month:9,view:'overall',productSearch:'16.33'}}}),
'2026年9月 · 产品检索:16.33');
});
test('a visual carrying the same keyword may refine the period without losing the filter',()=>{
const scope={year:2026,month:9,releaseId:'release-a',productSearch:'16.33',includeUnassigned:false};
assert.equal(analysisScopeText({id:'turn-a',plan:{context:{year:2026,productSearch:'16.33'}},visuals:[visual({scope})]}),
'2026年9月 · 对比 2026年8月 · 产品检索:16.33 · 不含未分类');
});
test('a different visual keyword cannot overwrite or enrich the bound query',()=>{
assert.equal(analysisScopeText({id:'turn-a',plan:{context:{year:2026,month:9,productSearch:'16.33'}},
visuals:[visual({scope:{year:2026,month:9,releaseId:'release-a',productSearch:'18X15'}})]}),
'2026年9月 · 产品检索:16.33');
});
test('equal-depth visual bindings with different keywords are not combined',()=>{
assert.equal(analysisScopeText({id:'turn-a',visuals:[
visual({id:'first',scope:{year:2026,month:9,releaseId:'release-a',productSearch:'16.33'}}),
visual({id:'second',scope:{year:2026,month:9,releaseId:'release-a',productSearch:'18X15'}}),
]}), '');
});
test('a broad visual remains the root when another visual is narrowed by keyword',()=>{
assert.equal(analysisScopeText({id:'turn-a',visuals:[visual(),
visual({id:'filtered',scope:{year:2026,month:9,releaseId:'release-a',productSearch:'16.33'}}),
]}), '2026年9月 · 对比 2026年8月 · 当前授权范围 · 不含未分类');
});
test('a multi-period historical report never inherits the current query month or its broadest chart',()=>{
const value={completionMethod:'confirmed_history_report',plan:{context:{year:2026,month:9,view:'overall'}},
historySources:[{turnId:'a',available:true},{turnId:'b',available:true}],
historySections:[{sourceTurnId:'a',period:{year:2026,month:9},filters:{department:'消费电子产业部',group:'消费电子-3C'}},
{sourceTurnId:'b',period:{year:2026,month:8},filters:{}}],visuals:[visual()]};
assert.equal(analysisScopeText(value),'');
assert.equal(analysisScopeText({...value,historySections:[value.historySections[0],{...value.historySections[0],filters:{department:'其他产业部'}}]}),'');
});
test('a single historical scope uses its authorized source frame, including its actual comparison only',()=>{
const source={sourceTurnId:'a',period:{year:2026,month:8},filters:{department:'产业部甲',group:'分组甲',includeUnassigned:false}};
const value={completionMethod:'confirmed_history_report',plan:{context:{year:2026,month:9},semantic:{compare:true,priorContext:{year:2025,month:1}}},
historySources:[{turnId:'a',available:true}],historySections:[source,{...source}]};
assert.equal(analysisScopeText(value),'2026年8月 · 产业部:产业部甲 · 业务分组:分组甲 · 不含未分类');
assert.equal(analysisScopeText({...value,historySources:[{turnId:'a',available:false}]}),'');
});
test('the current nested result cannot borrow stale flattened historical scope metadata',()=>{
const value={completionMethod:'confirmed_history_report',historySources:[{turnId:'old',available:true}],
historySections:[{sourceTurnId:'old',period:{year:2025,month:1},filters:{group:'旧分组'}}],
result:{completionMethod:'formal_metrics',plan:{context:{year:2026,month:9,group:'当前分组'}}}};
assert.equal(analysisScopeText(value),'2026年9月 · 业务分组:当前分组');
});
test('typing and queuing followups never removes the stop control',()=>{
for(const draft of ['','follow-up'])for(const queued of ['','queued']){
const state=analysisComposerState(true,false,false,draft,queued);
assert.equal(state.showStop,true);assert.equal(state.stopDisabled,false);
assert.equal(state.sendDisabled,!draft || !!queued);assert.equal(state.sendLabel,'排队发送');
}
assert.equal(analysisComposerState(true,true,false,'draft','').stopDisabled,true);
assert.equal(analysisComposerState(true,true,false,'draft','').sendDisabled,true);
assert.equal(analysisComposerState(false,false,false,'draft','').showStop,false);
});
test('restoring a queued draft does not overwrite another unsent question',()=>{
assert.equal(restoredAnalysisDraft('new','queued'),'queued\n\nnew');
assert.equal(restoredAnalysisDraft('','queued'),'queued');
assert.equal(restoredAnalysisDraft('same','same'),'same');
});
const processSource=fs.readFileSync(new URL('../src/AnalysisProcess.tsx',import.meta.url),'utf8').replace(/^import .*;$/gm,'').replace('export function AnalysisProcess','function AnalysisProcess');
const icon=()=>React.createElement('i');
const thinkingSource=fs.readFileSync(new URL('../src/ModelThinkingPanel.tsx',import.meta.url),'utf8').replace(/^import .*;$/gm,'').replace('export function AnalysisThinkingBlock','function AnalysisThinkingBlock');
const Thinking=new Function('React','useEffect','useLayoutEffect','useRef','useState','LoadingOutlined','RightOutlined','analysisScrollFollows','modelThinkingEntries','thinkingStatus',
ts.transpileModule(`${thinkingSource}\nreturn AnalysisThinkingBlock;`,{fileName:'thinking.tsx',compilerOptions:{target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.React}}).outputText
)(React,React.useEffect,React.useLayoutEffect,React.useRef,React.useState,icon,icon,analysisScrollFollows,modelThinkingEntries,thinkingStatus);
const Process=new Function('React','useEffect','useRef','useState','Spin','CheckCircleOutlined','CloseCircleOutlined','ClockCircleOutlined','RightOutlined','analysisIsRunning','compactAnalysisTimeline','duplicatesFinalAnalysisAnswer','sanitizeAnalysisDisplayText','AiMarkdown','orderedAnalysisProcess','groupAdjacentThinking','AnalysisThinkingBlock',
ts.transpileModule(`${processSource}\nreturn AnalysisProcess;`,{fileName:'process.tsx',compilerOptions:{target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.React}}).outputText
)(React,React.useEffect,React.useRef,React.useState,icon,icon,icon,icon,icon,analysisIsRunning,compactAnalysisTimeline,duplicatesFinalAnalysisAnswer,sanitizeAnalysisDisplayText,({content})=>React.createElement('p',null,content),orderedAnalysisProcess,groupAdjacentThinking,Thinking);
const processTurn=status=>({status,steps:[{tool:'read',label:'读取数据',status:'completed'}]});
test('settled process is collapsed, with the original records retained inside',()=>{
for(const status of ['COMPLETED','PARTIAL','FAILED','CANCELLED']){
const html=renderToStaticMarkup(React.createElement(Process,{turn:processTurn(status)}));
assert.match(html,/<details class="analysis-process-history analysis-agent-process"/);assert.match(html,/已完成/);assert.match(html,/读取数据/);
assert.doesNotMatch(html,/<details class="analysis-process-history analysis-agent-process" open/);
}
});
test('running process stays folded by default while retaining its current records inside',()=>{
const html=renderToStaticMarkup(React.createElement(Process,{turn:processTurn('RUNNING')}));
assert.match(html,/读取数据/);assert.match(html,/<details class="analysis-process-history analysis-agent-process"/);
assert.doesNotMatch(html,/<details class="analysis-process-history analysis-agent-process" open/);
});
test('a submitted follow-up rearms following only when its own turn is rendered',()=>{
const key='fresh-request';
assert.equal(analysisFollowRequestReady(key,{id:'previous-turn',question:'same question',status:'COMPLETED',_uiFollowKey:'previous-key'}),false);
assert.equal(analysisFollowRequestReady(key,{question:'same question',status:'QUEUED',_uiFollowKey:key}),true);
assert.equal(analysisFollowRequestReady(key,{id:'fresh-turn',status:'COMPLETED',_uiFollowKey:key}),true);
assert.equal(analysisFollowRequestReady(null,{id:'fresh-turn',status:'COMPLETED',_uiFollowKey:key}),false);
});
test('manual downward movement near the bottom resumes follow, while layout scroll events preserve the state',()=>{
assert.equal(analysisScrollFollows(24,false,'down'),true);
assert.equal(analysisScrollFollows(25,false,'down'),false);
assert.equal(analysisScrollFollows(0,false,null),false);
assert.equal(analysisScrollFollows(0,true,null),true);
assert.equal(analysisScrollFollows(0,true,'up'),false);
});
// Exercise the component's scrolling and update effect, including the browser's
// scroll-range clamp. Rectangles are zoomed, while scrollTop stays in layout units.
const pageSource=fs.readFileSync(new URL('../src/ManagementPages.tsx',import.meta.url),'utf8');
const scrollSource=pageSource.slice(pageSource.indexOf(' const scrollToLatest ='),pageSource.indexOf('\n useEffect(() => {\n if (!busy)'));
const updateStart=pageSource.indexOf(' useEffect(() => {\n if (analysisFollowRequestReady');
const updateSource=pageSource.slice(updateStart,pageSource.indexOf('\n\n const loadSessions',updateStart));
function scrollFixture({scale=.8,turnOffset=1200,maxScroll=2000,following=true}={}){
const body=`
let lastTurn=null,busy=true,active=true,turns=[],sessionId='session-a',hasNewContent=false;
const followOutput={current:following},explicitlyFollowing={current:false},followRequest={current:null},seenConclusion={current:null};
const previousScrollTop={current:0},readingPosition={current:0};
const ANALYSIS_FINAL_STATUSES=new Set(['COMPLETED','PARTIAL','FAILED','CANCELLED','NEEDS_CLARIFICATION']);
const setHasNewContent=value=>{hasNewContent=value;};
let position=0,updateEffect;
const latest={dataset:{},getBoundingClientRect:()=>({top:56.5+(turnOffset-position)*scale})};
const element={
offsetHeight:600,clientTop:0,scrollHeight:maxScroll+600,
get scrollTop(){return position;},set scrollTop(value){position=Math.max(0,Math.min(value,maxScroll));},
getBoundingClientRect:()=>({top:56.5,height:600*scale}),querySelector:()=>latest,
};
const conversation={current:element},useEffect=callback=>{updateEffect=callback;};
${scrollSource}
${updateSource}
return {
update(turn,running=true){lastTurn=turn;busy=running;latest.dataset={analysisStatus:turn.status,analysisTurnId:turn.id};updateEffect();},
scroll(top,intent='up'){
element.scrollTop=top;
const distance=maxScroll-position;
followOutput.current=analysisScrollFollows(distance,followOutput.current,intent);
if(intent==='up') explicitlyFollowing.current=false;
else if(intent==='down'&&followOutput.current) explicitlyFollowing.current=true;
if(followOutput.current) hasNewContent=false;
previousScrollTop.current=position;readingPosition.current=position;
},
programmaticScroll(top){
element.scrollTop=top;previousScrollTop.current=position;readingPosition.current=position;
followOutput.current=analysisScrollFollows(maxScroll-position,followOutput.current,null);
},
returnToLatest(){
followOutput.current=true;explicitlyFollowing.current=true;hasNewContent=false;
element.scrollTop=element.scrollHeight;previousScrollTop.current=position;readingPosition.current=position;
},
submit(key){followRequest.current=key;},
get:()=>({position,hasNewContent,following:followOutput.current,seen:seenConclusion.current}),
};
`;
return new Function('analysisFollowRequestReady','analysisScrollFollows','scale','turnOffset','maxScroll','following',
ts.transpileModule(body,{compilerOptions:{target:ts.ScriptTarget.ES2022}}).outputText
)(analysisFollowRequestReady,analysisScrollFollows,scale,turnOffset,maxScroll,following);
}
test('completion anchors the same layout position at workspace zoom levels',()=>{
for(const scale of [.8,1,1.25]){
const ui=scrollFixture({scale});
ui.update({id:'turn-a',status:'COMPLETED'});
assert.equal(ui.get().position,1200);
assert.equal(ui.get().seen,'turn-a');
}
});
test('short conclusions safely clamp at the end of the conversation',()=>{
const ui=scrollFixture({maxScroll:1150});
ui.update({id:'turn-a',status:'COMPLETED'});
assert.equal(ui.get().position,1150);
assert.equal(ui.get().hasNewContent,false);
});
test('terminal detail and history refreshes cannot resurrect the unread indicator',()=>{
const ui=scrollFixture();
ui.update({id:'turn-a',status:'RUNNING'});
ui.update({id:'turn-a',status:'COMPLETED'}); // Terminal SSE, busy remains true.
assert.equal(ui.get().following,false);
ui.update({id:'turn-a',status:'COMPLETED',answer:'saved answer'}); // Durable detail.
ui.update({id:'turn-a',status:'COMPLETED',answer:'saved answer'}); // History refresh.
ui.update({id:'turn-a',status:'COMPLETED',answer:'saved answer'},false);
assert.equal(ui.get().hasNewContent,false);
assert.equal(ui.get().position,1200);
});
test('completion still signals unread output when the reader stayed on an older turn',()=>{
const ui=scrollFixture({following:false});
ui.update({id:'turn-a',status:'RUNNING'});
ui.update({id:'turn-a',status:'COMPLETED'});
ui.update({id:'turn-a',status:'COMPLETED'},false);
assert.equal(ui.get().hasNewContent,true);
assert.equal(ui.get().position,0);
});
test('scrolling up pauses, returning near the bottom resumes, and completed output stays followed after an explicit return',()=>{
const ui=scrollFixture();
ui.update({id:'turn-a',status:'RUNNING'});
ui.scroll(1500,'up');
ui.update({id:'turn-a',status:'RUNNING',answer:'streaming update'});
assert.equal(ui.get().position,1500);
assert.equal(ui.get().following,false);
ui.scroll(1990,'down');
assert.equal(ui.get().following,true);
assert.equal(ui.get().hasNewContent,false);
ui.update({id:'turn-a',status:'COMPLETED'});
assert.equal(ui.get().position,2000);
assert.equal(ui.get().following,true);
ui.returnToLatest();
ui.update({id:'turn-a',status:'COMPLETED',answer:'final durable answer'});
assert.equal(ui.get().following,true);
assert.equal(ui.get().position,2000);
});
test('layout-driven movement to the bottom does not rearm a reader who moved up',()=>{
const ui=scrollFixture({following:false});
ui.programmaticScroll(2000);
assert.equal(ui.get().position,2000);
assert.equal(ui.get().following,false);
});
test('a fresh request resets the seen conclusion and follows its own output',()=>{
const ui=scrollFixture();
ui.update({id:'old-turn',status:'COMPLETED'},false);
ui.submit('new-request');
ui.update({id:'new-turn',status:'RUNNING',_uiFollowKey:'new-request'});
assert.equal(ui.get().following,true);
assert.equal(ui.get().seen,null);
assert.equal(ui.get().position,2000);
ui.scroll(300);
ui.update({id:'new-turn',status:'COMPLETED'});
assert.equal(ui.get().hasNewContent,true);
assert.equal(ui.get().position,300);
});
test('provider thinking is complete, escaped and separate from conclusions',()=>{
const text='完整接口文本'.repeat(2000)+'<img src=x onerror=alert(1)>';
const turn={id:'thinking',status:'RUNNING',modelThinking:[{id:'api-abcd1234-1',model:'fixture',phase:'analysis',status:'running',version:1,source:'api',text,streaming:true}]};
const html=renderToStaticMarkup(React.createElement(Process,{turn}));
assert.ok(html.includes('完整接口文本'.repeat(2000)));
assert.ok(html.includes('&lt;img'));
assert.ok(!html.includes('<img src=x'));
assert.doesNotMatch(html.match(/<details[^>]*class="analysis-thinking-block"[^>]*>/)[0],/\sopen(?:=|\s|>)/);
const archived=renderToStaticMarkup(React.createElement(Process,{turn:{...turn,status:'COMPLETED'}}));
assert.doesNotMatch(archived.match(/<details[^>]*class="analysis-thinking-block"[^>]*>/)[0],/\sopen(?:=|\s|>)/);
assert.ok(archived.includes('完整接口文本'.repeat(2000)));
});
test('agent process interleaves thoughts, tool calls and text with one runtime header',()=>{
const stamp=n=>`2026-10-26T00:00:0${n}Z`;
const blocks=[
{id:'api-abcd1234-1',model:'model-hidden',phase:'planning',status:'completed',version:1,source:'api',text:'先核对范围',streaming:true,at:stamp(1)},
{id:'api-abcd1234-2',model:'model-hidden',phase:'finalizing',status:'completed',version:1,source:'api',text:'再整理结果',streaming:true,at:stamp(4)},
];
const turn={id:'agent',status:'COMPLETED',modelThinking:blocks,timeline:[
{kind:'assistant',sequence:1,at:stamp(2),text:'我来查询两个期间。'},
{kind:'tool_start',sequence:2,at:stamp(3),tool:'read',toolCallId:'call-1',label:'读取数据'},
{kind:'tool_result',sequence:3,at:stamp(5),tool:'read',toolCallId:'call-1',status:'completed',text:'查询已完成'},
]};
const html=renderToStaticMarkup(React.createElement(Process,{turn,metadata:React.createElement('span',null,'运行时长 25秒')}));
assert.equal((html.match(/运行时长/g)||[]).length,1);
const positions=['先核对范围','我来查询两个期间。','读取数据','再整理结果'].map(value=>html.indexOf(value));
assert.ok(positions.every((p,i)=>p>=0&&(!i||p>positions[i-1])));
assert.equal((html.match(/class="analysis-thinking-block"/g)||[]).length,2);
for(const hidden of ['model-hidden','任务理解','整理结论','analysis-thinking-heading','展示接口实际返回'])assert.ok(!html.includes(hidden),hidden);
});