Files
lyun f1a2fbd494 统一:DSH 智能分析、Top100 与图表静默恢复
接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。
补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。
优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。

验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。
仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
2026-11-12 11:57:41 +08:00

189 lines
8.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
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 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, '已保存答复');
});
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: [] });
});
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('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('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('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);
});