统一:DSH 智能分析、Top100 与图表静默恢复
接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。 补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。 优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。 验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。 仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
This commit is contained in:
@@ -1,77 +1,188 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import ts from 'typescript';
|
||||
import {analysisComposerState} from '../src/analysisScope.ts';
|
||||
const source=fs.readFileSync(new URL('../src/ManagementPages.tsx',import.meta.url),'utf8');
|
||||
const tick=()=>new Promise(resolve=>setImmediate(resolve));
|
||||
const deferred=()=>{let resolve;const promise=new Promise(r=>resolve=r);return{promise,resolve};};
|
||||
function restoreFixture({sessionId='',turnId='',link=false}={}){
|
||||
const list=deferred(),opens=[];let effect,loading=true;
|
||||
const env={active:true,restoreStarted:{current:false},mounted:{current:true},navigationGeneration:{current:0},initialSessionId:sessionId,initialTurnId:turnId,initialLink:{present:link},user:{id:'u'},
|
||||
useEffect:fn=>{effect=fn;},setHistoryLoading:v=>{loading=v;},setSessions(){},setSessionsLoaded(){},setSessionsLoading(){},collectionItems:v=>v.items||[],
|
||||
loadSessions:async()=>(await list.promise).items,api:{analysisSessions:()=>list.promise,analysisTurn:async()=>({sessionId:'linked-session'})},defaultAnalysisSession:(_u,items)=>items[0]?.id,
|
||||
openSession:async(...args)=>{opens.push(args);loading=false;return true;},onUnauthorized:()=>false,setError(){},apiMessage:e=>e.message};
|
||||
const marker=' useEffect(() => {\n if (!active || restoreStarted.current)';const start=source.indexOf(marker);const end=source.indexOf('\n useEffect(() => {',start+marker.length);
|
||||
const code=ts.transpileModule(source.slice(start,end),{compilerOptions:{target:ts.ScriptTarget.ES2022}}).outputText;
|
||||
new Function(...Object.keys(env),code)(...Object.values(env));effect();
|
||||
return{list,opens,get loading(){return loading;}};
|
||||
import { createAnalysisSessionClient } from '../src/agent/sessionClient.ts';
|
||||
|
||||
const tick = () => new Promise(resolve => setImmediate(resolve));
|
||||
const deferred = () => {
|
||||
let resolve, reject;
|
||||
const promise = new Promise((yes, no) => { resolve = yes; reject = no; });
|
||||
return { promise, resolve, reject };
|
||||
};
|
||||
|
||||
function fixture(t, overrides = {}) {
|
||||
const calls = { lists: 0, followed: [], details: [], options: 0, submissions: 0 };
|
||||
const transport = {
|
||||
analysisSessions: async () => { calls.lists++; return { items: [] }; },
|
||||
analysisTurn: async id => {
|
||||
calls.details.push(id);
|
||||
return { id, sessionId: 'linked-session', status: 'COMPLETED' };
|
||||
},
|
||||
analysisSessionFollow: async (id, { onEvent }) => {
|
||||
calls.followed.push(id);
|
||||
onEvent({ event: 'snapshot', data: {
|
||||
id, title: '授权会话', turns: [{ id: 'requested-turn', status: 'COMPLETED', result: { answer: '已保存答复' } }],
|
||||
} });
|
||||
},
|
||||
options: async () => { calls.options++; return {}; },
|
||||
createAnalysisSession: async () => { calls.submissions++; throw new Error('Unexpected submission'); },
|
||||
startAnalysisTurn: async () => { calls.submissions++; throw new Error('Unexpected submission'); },
|
||||
...overrides,
|
||||
};
|
||||
const client = createAnalysisSessionClient({ userId: 'loading-fixture', transport });
|
||||
t.after(client.dispose);
|
||||
return { client, calls };
|
||||
}
|
||||
test('an explicit history link loads its body without waiting for the unrelated session list',async()=>{
|
||||
const ui=restoreFixture({sessionId:'requested-session'});await tick();assert.deepEqual(ui.opens,[['requested-session',undefined]]);
|
||||
ui.list.resolve({items:[]});await tick();
|
||||
});
|
||||
test('a turn link can resolve its session while the session list is still pending',async()=>{
|
||||
const ui=restoreFixture({turnId:'requested-turn'});await tick();assert.deepEqual(ui.opens,[['linked-session','requested-turn']]);
|
||||
ui.list.resolve({items:[]});await tick();
|
||||
});
|
||||
test('default history selection still waits for the list before choosing a session',async()=>{
|
||||
const ui=restoreFixture();await tick();assert.equal(ui.opens.length,0);ui.list.resolve({items:[{id:'recent'}]});await tick();assert.deepEqual(ui.opens,[['recent',undefined]]);
|
||||
|
||||
test('an explicit session opens its saved answer while the session list is still pending', async t => {
|
||||
const list = deferred();
|
||||
const { client, calls } = fixture(t, { analysisSessions: () => list.promise });
|
||||
assert.equal(await client.openInitial({ sessionId: 'requested-session' }), true);
|
||||
assert.deepEqual(calls.followed, ['requested-session']);
|
||||
assert.equal(client.getSnapshot().turns[0].answer, '已保存答复');
|
||||
assert.equal(client.getSnapshot().historyLoading, false);
|
||||
assert.equal(client.getSnapshot().sessionsLoading, true);
|
||||
list.reject(new Error('list unavailable'));
|
||||
await tick();
|
||||
assert.equal(client.getSnapshot().sessionsError, 'list unavailable');
|
||||
assert.equal(client.getSnapshot().historyError, '');
|
||||
assert.equal(client.getSnapshot().turns[0].answer, '已保存答复');
|
||||
});
|
||||
|
||||
function listFixture(){
|
||||
const read=deferred();let reads=0,state={busy:false,loaded:false,items:[],error:''};
|
||||
const env={mounted:{current:true},active:true,sessionReadRequest:{current:null},setSessionsLoading:v=>{state.busy=v;},setSessions:v=>{state.items=v;},setSessionsLoaded:v=>{state.loaded=v;},setSessionsError:v=>{state.error=v;},onUnauthorized:()=>false,apiMessage:e=>e.message,collectionItems:v=>v.items||[],api:{analysisSessions:()=>{reads++;return read.promise;}}};
|
||||
const start=source.indexOf(' const loadSessions = async');const end=source.indexOf('\n const retrySessions =',start);
|
||||
const code=ts.transpileModule(source.slice(start,end)+'\nreturn loadSessions;',{compilerOptions:{target:ts.ScriptTarget.ES2022}}).outputText;
|
||||
return{read,state,reads:()=>reads,load:new Function(...Object.keys(env),code)(...Object.values(env))};
|
||||
}
|
||||
test('session-list reads are shared, and failure stays within the list resource',async()=>{
|
||||
const ui=listFixture();const a=ui.load(),b=ui.load();assert.equal(ui.reads(),1);assert.equal(ui.state.busy,true);
|
||||
ui.read.resolve({items:[{id:'recent'}]});assert.deepEqual(await a,[{id:'recent'}]);await b;assert.equal(ui.state.loaded,true);assert.equal(ui.state.busy,false);
|
||||
ui.read.resolve({items:[]});
|
||||
});
|
||||
test('a failed list read ends its own loading state and cannot masquerade as an empty successful list',async()=>{
|
||||
const ui=listFixture();const work=ui.load();ui.read.resolve(Promise.reject(new Error('list unavailable')));assert.equal(await work,undefined);
|
||||
assert.equal(ui.state.busy,false);assert.equal(ui.state.loaded,false);assert.equal(ui.state.error,'list unavailable');
|
||||
});
|
||||
test('ordinary and explicit historical conversations never download the unused options resource',()=>{
|
||||
const start=source.indexOf(' useEffect(() => {\n if (!active || !initialLink.present');assert.notEqual(start,-1);
|
||||
const end=source.indexOf('\n\n',start);const code=ts.transpileModule(source.slice(start,end),{compilerOptions:{target:ts.ScriptTarget.ES2022}}).outputText;
|
||||
for(const [present,session,turn] of [[false,'',''],[false,'s',''],[true,'s',''],[true,'','t']]){
|
||||
let reads=0;const env={useEffect:fn=>fn(),active:true,initialLink:{present},initialSessionId:session,initialTurnId:turn,optionsRequestStarted:{current:false},optionsRead:{run:()=>{reads++;}},onUnauthorized:()=>false};
|
||||
new Function(...Object.keys(env),code)(...Object.values(env));assert.equal(reads,0);
|
||||
}
|
||||
test('a turn-only link resolves and focuses its session while the list is pending', async t => {
|
||||
const list = deferred();
|
||||
const { client, calls } = fixture(t, { analysisSessions: () => list.promise });
|
||||
assert.equal(await client.openInitial({ turnId: 'requested-turn' }), true);
|
||||
assert.deepEqual(calls.details, ['requested-turn']);
|
||||
assert.deepEqual(calls.followed, ['linked-session']);
|
||||
assert.equal(client.getSnapshot().focusTurnId, 'requested-turn');
|
||||
assert.equal(client.getSnapshot().sessionsLoading, true);
|
||||
list.resolve({ items: [] });
|
||||
});
|
||||
|
||||
function retryFixture(sessionId='',turnId=''){
|
||||
const list=deferred(),opens=[],navigationGeneration={current:0},state={historyLoading:false};
|
||||
const env={sessionId,navigationGeneration,pendingTurnLink:{current:turnId},mounted:{current:true},initialLink:{present:false},user:{id:'u'},setHistoryLoading:v=>{state.historyLoading=v;},loadSessions:()=>list.promise,api:{analysisTurn:async()=>({sessionId:'target-session'})},openSession:async(id,focus)=>opens.push([id,focus]),defaultAnalysisSession:(_u,items)=>items[0]?.id,onUnauthorized:()=>false,setError(){},apiMessage:e=>e.message};
|
||||
const start=source.indexOf(' const retrySessions = async');const end=source.indexOf('\n useEffect(',start);
|
||||
const code=ts.transpileModule(source.slice(start,end)+'\nreturn retrySessions;',{compilerOptions:{target:ts.ScriptTarget.ES2022}}).outputText;
|
||||
return{list,opens,state,navigationGeneration,retry:new Function(...Object.keys(env),code)(...Object.values(env))};
|
||||
}
|
||||
test('retrying a linked history does not wait for a slow list',async()=>{
|
||||
const ui=retryFixture('selected');await ui.retry();assert.deepEqual(ui.opens,[['selected',undefined]]);ui.list.resolve([]);
|
||||
test('default restoration waits for the recent session list', async t => {
|
||||
const list = deferred();
|
||||
const { client, calls } = fixture(t, { analysisSessions: () => list.promise });
|
||||
const opening = client.openInitial();
|
||||
await tick();
|
||||
assert.deepEqual(calls.followed, []);
|
||||
assert.equal(client.getSnapshot().historyLoading, true);
|
||||
list.resolve({ items: [{ id: 'recent' }] });
|
||||
assert.equal(await opening, true);
|
||||
assert.deepEqual(calls.followed, ['recent']);
|
||||
assert.equal(client.getSnapshot().historyLoading, false);
|
||||
});
|
||||
test('default-list retry cannot navigate over a newer user selection',async()=>{
|
||||
const ui=retryFixture();const work=ui.retry();ui.navigationGeneration.current++;ui.list.resolve([{id:'old-default'}]);await work;assert.deepEqual(ui.opens,[]);
|
||||
|
||||
test('concurrent sidebar refreshes share a single list request', async t => {
|
||||
const list = deferred();
|
||||
let reads = 0;
|
||||
const { client } = fixture(t, { analysisSessions: () => { reads++; return list.promise; } });
|
||||
const first = client.refreshSessions();
|
||||
const second = client.refreshSessions();
|
||||
assert.equal(first, second);
|
||||
await tick();
|
||||
assert.equal(reads, 1);
|
||||
assert.equal(client.getSnapshot().sessionsLoading, true);
|
||||
list.resolve({ items: [{ id: 'recent' }] });
|
||||
assert.deepEqual(await first, [{ id: 'recent' }]);
|
||||
assert.deepEqual(client.getSnapshot().sessions, [{ id: 'recent' }]);
|
||||
assert.equal(client.getSnapshot().sessionsLoading, false);
|
||||
assert.equal(client.getSnapshot().sessionsError, '');
|
||||
});
|
||||
test('retry of a turn-only link re-resolves the requested turn instead of opening an unrelated default',async()=>{
|
||||
const ui=retryFixture('','requested-turn');ui.list.resolve([{id:'unrelated-default'}]);await ui.retry();assert.deepEqual(ui.opens,[['target-session','requested-turn']]);
|
||||
|
||||
test('a failed list request exposes its own error and permits retry', async t => {
|
||||
let reads = 0;
|
||||
const { client } = fixture(t, { analysisSessions: async () => {
|
||||
if (++reads === 1) throw new Error('list unavailable');
|
||||
return { items: [{ id: 'restored' }] };
|
||||
} });
|
||||
await client.refreshSessions();
|
||||
assert.equal(client.getSnapshot().sessionsLoading, false);
|
||||
assert.equal(client.getSnapshot().sessionsError, 'list unavailable');
|
||||
assert.equal(client.getSnapshot().historyError, '');
|
||||
await client.refreshSessions();
|
||||
assert.equal(client.getSnapshot().sessionsError, '');
|
||||
assert.deepEqual(client.getSnapshot().sessions, [{ id: 'restored' }]);
|
||||
});
|
||||
test('default-history retry locks sending until its target resolves, so a late list cannot replace a new submission',async()=>{
|
||||
const ui=retryFixture();const work=ui.retry();assert.equal(ui.state.historyLoading,true);
|
||||
assert.equal(analysisComposerState(false,false,ui.state.historyLoading,'新问题').sendDisabled,true);
|
||||
ui.list.resolve([{id:'default'}]);await work;assert.equal(ui.state.historyLoading,false);
|
||||
|
||||
test('ordinary and linked history restoration do not request analysis options', async t => {
|
||||
for (const initial of [{}, { sessionId: 'selected' }, { turnId: 'requested-turn' }, { selectRecent: false }]) {
|
||||
const { client, calls } = fixture(t);
|
||||
assert.equal(await client.openInitial(initial), true);
|
||||
assert.equal(calls.lists, 1);
|
||||
assert.equal(calls.options, 0);
|
||||
}
|
||||
const workspace = fs.readFileSync(new URL('../src/agent/AgentWorkspace.tsx', import.meta.url), 'utf8');
|
||||
assert.doesNotMatch(workspace, /\b(?:optionsRead|analysisOptions)\b|\bapi\.options\s*\(/);
|
||||
});
|
||||
|
||||
test('retrying an explicitly selected history opens its snapshot while the list is slow', async t => {
|
||||
const list = deferred();
|
||||
const reads = [];
|
||||
const { client } = fixture(t, {
|
||||
analysisSessions: () => list.promise,
|
||||
analysisSessionFollow: async (id, { onEvent }) => {
|
||||
reads.push(id);
|
||||
if (reads.length === 1) throw new Error('history unavailable');
|
||||
onEvent({ event: 'snapshot', data: { id, turns: [] } });
|
||||
},
|
||||
});
|
||||
assert.equal(await client.openInitial({ sessionId: 'selected' }), false);
|
||||
const failed = client.getSnapshot();
|
||||
assert.equal(failed.historyError, 'history unavailable');
|
||||
assert.equal(await client.openSession(failed.sessionId, failed.focusTurnId), true);
|
||||
assert.deepEqual(reads, ['selected', 'selected']);
|
||||
assert.equal(client.getSnapshot().sessionsLoading, true);
|
||||
list.resolve({ items: [] });
|
||||
});
|
||||
|
||||
test('a late default-list retry cannot navigate over a newer user selection', async t => {
|
||||
const retry = deferred();
|
||||
let reads = 0;
|
||||
const { client, calls } = fixture(t, { analysisSessions: async () => {
|
||||
if (++reads === 1) throw new Error('list unavailable');
|
||||
return retry.promise;
|
||||
} });
|
||||
assert.equal(await client.openInitial(), false);
|
||||
const retrying = client.openInitial();
|
||||
assert.equal(await client.openSession('selected'), true);
|
||||
retry.resolve({ items: [{ id: 'old-default' }] });
|
||||
assert.equal(await retrying, false);
|
||||
assert.deepEqual(calls.followed, ['selected']);
|
||||
assert.equal(client.getSnapshot().sessionId, 'selected');
|
||||
});
|
||||
|
||||
test('retrying an unresolved turn link resolves the same turn before choosing any default', async t => {
|
||||
const reads = [];
|
||||
const { client, calls } = fixture(t, {
|
||||
analysisSessions: async () => ({ items: [{ id: 'unrelated-default' }] }),
|
||||
analysisTurn: async id => {
|
||||
reads.push(id);
|
||||
if (reads.length === 1) throw new Error('turn unavailable');
|
||||
return { id, sessionId: 'linked-session', status: 'COMPLETED' };
|
||||
},
|
||||
});
|
||||
const initial = { turnId: 'requested-turn' };
|
||||
assert.equal(await client.openInitial(initial), false);
|
||||
assert.equal(await client.openInitial(initial), true);
|
||||
assert.deepEqual(reads, ['requested-turn', 'requested-turn']);
|
||||
assert.deepEqual(calls.followed, ['linked-session']);
|
||||
assert.equal(client.getSnapshot().focusTurnId, 'requested-turn');
|
||||
});
|
||||
|
||||
test('retrying default restoration keeps submission locked until its target resolves', async t => {
|
||||
const retry = deferred();
|
||||
let reads = 0;
|
||||
const { client, calls } = fixture(t, { analysisSessions: async () => {
|
||||
if (++reads === 1) throw new Error('list unavailable');
|
||||
return retry.promise;
|
||||
} });
|
||||
assert.equal(await client.openInitial(), false);
|
||||
const retrying = client.openInitial();
|
||||
assert.equal(client.getSnapshot().historyLoading, true);
|
||||
assert.equal(await client.submit('新问题'), false);
|
||||
assert.equal(calls.submissions, 0);
|
||||
retry.resolve({ items: [{ id: 'default' }] });
|
||||
assert.equal(await retrying, true);
|
||||
assert.equal(client.getSnapshot().historyLoading, false);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user