接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。 补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。 优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。 验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。 仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
124 lines
6.6 KiB
JavaScript
124 lines
6.6 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 { analysisStreamStatus, analysisIsRunning } from '../src/analysisConversationState.ts';
|
|
import { sanitizeAnalysisDisplayText } from '../src/analysisDisplayText.ts';
|
|
import { createAnalysisSessionClient } from '../src/agent/sessionClient.ts';
|
|
|
|
const CREATED_AT = '2026-11-02T01:00:00Z';
|
|
const STARTED_AT = '2026-11-02T01:00:05Z';
|
|
const initialTurn = { id: 't1', question: '质量查询', status: 'QUEUED', createdAt: CREATED_AT, eventCursor: '10' };
|
|
const page = (id, turn, activeTurnId = turn.id) => ({ id, activeTurnId, turns: [turn] });
|
|
|
|
function fixture(t) {
|
|
const connections = [];
|
|
const pages = { s1: page('s1', initialTurn), s2: page('s2', { id: 'other', status: 'RUNNING' }) };
|
|
let auxiliaryReads = 0;
|
|
const unexpectedRead = async () => { auxiliaryReads++; throw new Error('Unexpected auxiliary read'); };
|
|
const client = createAnalysisSessionClient({
|
|
userId: 'status-fixture',
|
|
transport: {
|
|
analysisSessions: async () => ({ items: [] }),
|
|
analysisSessionPage: unexpectedRead,
|
|
analysisTurn: unexpectedRead,
|
|
analysisTurnEvents: unexpectedRead,
|
|
analysisSessionTurns: unexpectedRead,
|
|
analysisSessionFollow: (id, { signal, onEvent }) => new Promise(resolve => {
|
|
connections.push({ id, signal, emit: onEvent, close: resolve });
|
|
signal.addEventListener('abort', resolve, { once: true });
|
|
onEvent({ event: 'snapshot', data: pages[id] });
|
|
}),
|
|
},
|
|
});
|
|
t.after(client.dispose);
|
|
return { client, connections, pages, auxiliaryReads: () => auxiliaryReads };
|
|
}
|
|
|
|
const currentTurn = client => client.getSnapshot().turns.at(-1);
|
|
const tick = () => new Promise(resolve => setImmediate(resolve));
|
|
|
|
test('tool status is independent of the turn status and terminal status stays settled', () => {
|
|
assert.equal(analysisStreamStatus('RUNNING', 'tool_started', undefined), 'RUNNING');
|
|
assert.equal(analysisStreamStatus('RUNNING', 'tool_completed', 'completed'), 'RUNNING');
|
|
assert.equal(analysisStreamStatus('RUNNING', 'completed', 'COMPLETED'), 'COMPLETED');
|
|
assert.equal(analysisStreamStatus('COMPLETED', 'progress', 'RUNNING'), 'COMPLETED');
|
|
});
|
|
|
|
test('the follow connection supplies the server start time without an auxiliary read', async t => {
|
|
const { client, connections, auxiliaryReads } = fixture(t);
|
|
assert.equal(await client.openSession('s1'), true);
|
|
assert.equal(currentTurn(client).status, 'QUEUED');
|
|
assert.equal(currentTurn(client).createdAt, CREATED_AT);
|
|
assert.equal(currentTurn(client).startedAt, undefined);
|
|
const { emit } = connections[0];
|
|
emit({ id: '11', event: 'progress', data: { turnId: 't1', status: 'RUNNING', startedAt: STARTED_AT } });
|
|
emit({ id: '12', event: 'tool_started', data: { turnId: 't1', step: { status: 'running' } } });
|
|
emit({ id: '13', event: 'tool_completed', data: { turnId: 't1', status: 'completed' } });
|
|
assert.equal(currentTurn(client).status, 'RUNNING');
|
|
assert.equal(currentTurn(client).startedAt, STARTED_AT);
|
|
assert.equal(client.getSnapshot().busy, true);
|
|
assert.equal(auxiliaryReads(), 0);
|
|
assert.equal(connections.length, 1);
|
|
});
|
|
|
|
test('late server clock events from an aborted connection cannot update the newly selected turn', async t => {
|
|
const { client, connections } = fixture(t);
|
|
await client.openSession('s1');
|
|
await client.openSession('s2');
|
|
assert.equal(connections[0].signal.aborted, true);
|
|
connections[0].emit({ id: '11', event: 'progress', data: { turnId: 't1', status: 'RUNNING', startedAt: STARTED_AT } });
|
|
assert.equal(currentTurn(client).id, 'other');
|
|
assert.equal(currentTurn(client).startedAt, undefined);
|
|
});
|
|
|
|
test('the terminal event settles immediately and the replacement snapshot supplies the saved duration', async t => {
|
|
const { client, connections, pages, auxiliaryReads } = fixture(t);
|
|
await client.openSession('s1');
|
|
const { emit, close } = connections[0];
|
|
emit({ id: '11', event: 'completed', data: { turnId: 't1', status: 'COMPLETED', result: { answer: '已核验答复' } } });
|
|
assert.equal(currentTurn(client).status, 'COMPLETED');
|
|
assert.equal(currentTurn(client).answer, '已核验答复');
|
|
assert.equal(client.getSnapshot().busy, false);
|
|
pages.s1 = page('s1', { ...initialTurn, status: 'COMPLETED', eventCursor: '11', durationMs: 6000, result: { answer: '已核验答复' } }, '');
|
|
close();
|
|
await tick();
|
|
assert.equal(connections.length, 2);
|
|
assert.equal(currentTurn(client).durationMs, 6000);
|
|
assert.equal(auxiliaryReads(), 0);
|
|
});
|
|
|
|
const source = fs.readFileSync(new URL('../src/agent/AgentConversation.tsx', import.meta.url), 'utf8');
|
|
const statusSource = source.slice(source.indexOf('function RunStatus('), source.indexOf('\nfunction ExecutionEvents('));
|
|
const statusJavaScript = ts.transpileModule(`${statusSource}\nreturn RunStatus;`, {
|
|
fileName: 'status.tsx', compilerOptions: { target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.React },
|
|
}).outputText;
|
|
const fixedClock = class extends Date { static now() { return Date.parse('2026-11-02T01:00:15Z'); } };
|
|
const record = value => value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
const string = value => typeof value === 'string' ? value : '';
|
|
const LoadingOutlined = () => React.createElement('i', { className: 'test-loading-icon' });
|
|
const RunStatus = new Function('React', 'useState', 'useEffect', 'analysisIsRunning', 'sanitizeAnalysisDisplayText',
|
|
'record', 'string', 'LoadingOutlined', 'Date', statusJavaScript)(React, React.useState, React.useEffect,
|
|
analysisIsRunning, sanitizeAnalysisDisplayText, record, string, LoadingOutlined, fixedClock);
|
|
const renderStatus = turn => renderToStaticMarkup(React.createElement(RunStatus, { turn }));
|
|
|
|
test('run status renders a submission clock and prefers the actual server start time while running', () => {
|
|
const waiting = renderStatus({ _uiSubmittedAt: '2026-11-02T00:59:45Z', status: 'SUBMITTING' });
|
|
assert.match(waiting, /提交中/);
|
|
assert.match(waiting, /<time>30 秒<\/time>/);
|
|
const running = renderStatus({ createdAt: CREATED_AT, startedAt: STARTED_AT, status: 'RUNNING' });
|
|
assert.match(running, /分析中/);
|
|
assert.match(running, /agent-run-status is-running/);
|
|
assert.match(running, /test-loading-icon/);
|
|
assert.match(running, /<time>10 秒<\/time>/);
|
|
});
|
|
|
|
test('a partial result renders its status once and shows the saved duration', () => {
|
|
const html = renderStatus({ durationMs: 6000, status: 'PARTIAL' });
|
|
assert.equal(html.match(/部分完成/g)?.length, 1);
|
|
assert.match(html, /<time>6 秒<\/time>/);
|
|
assert.doesNotMatch(html, /is-running|test-loading-icon/);
|
|
});
|