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 { scrollTopFromVisualOffset, transcriptFollowState } from '../src/agent/transcriptScroll.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,/
{ const html=renderToStaticMarkup(React.createElement(Process,{turn:processTurn('RUNNING')})); assert.match(html,/读取数据/);assert.match(html,/
{ 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); }); const viewport = (scrollTop, zoom = 1, scrollHeight = 2600) => ({ scrollTop, scrollHeight, clientHeight: 600, offsetHeight: 600, getBoundingClientRect: () => ({ height: 600 * zoom }), }); test('focusing and prepending history convert visual positions to layout pixels at each zoom', () => { for (const zoom of [.8, 1, 1.25]) { assert.equal(scrollTopFromVisualOffset(viewport(500, zoom), 316 * zoom, 16), 800); assert.equal(scrollTopFromVisualOffset(viewport(500, zoom), 240 * zoom), 740); } }); test('follow remains active through new output when the reader stayed at the bottom', () => { assert.deepEqual(transcriptFollowState(viewport(2000, 1, 2600), true, null), { follows: true, showLatest: false }); assert.deepEqual(transcriptFollowState(viewport(2000, 1, 3200), true, null), { follows: true, showLatest: false }); }); test('scrolling up keeps the reader in history as live output arrives', () => { assert.deepEqual(transcriptFollowState(viewport(1000), true, 'up'), { follows: false, showLatest: true }); assert.deepEqual(transcriptFollowState(viewport(1000, 1, 3200), false, null), { follows: false, showLatest: true }); }); test('layout clamp cannot restore follow after the reader moved up', () => { assert.deepEqual(transcriptFollowState(viewport(500, 1, 1100), false, null), { follows: false, showLatest: false }); }); test('returning downward near the end re-arms follow and clears the latest button', () => { assert.deepEqual(transcriptFollowState(viewport(1960), false, 'down'), { follows: false, showLatest: true }); assert.deepEqual(transcriptFollowState(viewport(1980), false, 'down'), { follows: true, showLatest: false }); }); test('provider thinking is complete, escaped and separate from conclusions',()=>{ const text='完整接口文本'.repeat(2000)+''; 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('<img')); assert.ok(!html.includes(']*class="analysis-thinking-block"[^>]*>/)[0],/\sopen(?:=|\s|>)/); const archived=renderToStaticMarkup(React.createElement(Process,{turn:{...turn,status:'COMPLETED'}})); assert.doesNotMatch(archived.match(/]*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); assert.doesNotMatch(html,/我来查询两个期间。/); 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); });