同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。 补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。 统一午夜黑及其他主题的控件细节、中文和业务数字字体。 排除本地业务数据、凭据、预览、发布与验收产物。 验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
103 lines
7.5 KiB
JavaScript
103 lines
7.5 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 { analysisClarificationRequest, clarificationContinuation, pendingClarificationTurnId } from '../src/analysisClarificationModel.ts';
|
|
|
|
const turn = () => ({ id: 'clarify-1', status: 'NEEDS_CLARIFICATION', result: { clarificationRequest: {
|
|
question: '请选择分析规格。', options: [{ label: '规格 T', value: '18X15X4+T', description: '授权候选' }, { label: '规格 Z', value: '18X15X4+Z' }], allowCustom: true,
|
|
} } });
|
|
const find = (tree, predicate) => {
|
|
if (!tree || typeof tree !== 'object') return;
|
|
if (predicate(tree)) return tree;
|
|
for (const child of React.Children.toArray(tree.props?.children)) { const found = find(child, predicate); if (found) return found; }
|
|
};
|
|
|
|
function fixture(initial = turn(), onContinue = async () => {}) {
|
|
let cursor = 0, queued = [], dirty = false, tree;
|
|
const state = new Map(), refs = new Map(), effects = new Map();
|
|
const hooks = {
|
|
useState(initial) { const id = cursor++; if (!state.has(id)) state.set(id, initial); return [state.get(id), value => { const next = typeof value === 'function' ? value(state.get(id)) : value; if (!Object.is(next, state.get(id))) { state.set(id, next); dirty = true; } }]; },
|
|
useRef(initial) { const id = cursor++; if (!refs.has(id)) refs.set(id, { current: initial }); return refs.get(id); },
|
|
useEffect(fn, deps) { const id = cursor++, old = effects.get(id); if (!old || deps.some((value, i) => !Object.is(value, old[i]))) { effects.set(id, deps); queued.push(fn); } },
|
|
};
|
|
const source = fs.readFileSync(new URL('../src/AnalysisClarification.tsx', import.meta.url), 'utf8');
|
|
const body = ts.transpileModule(source.slice(source.indexOf('export function')).replace('export function', 'function'), {
|
|
fileName: 'clarification.tsx', compilerOptions: { target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.React },
|
|
}).outputText;
|
|
const Component = new Function('React', 'hooks', 'analysisClarificationRequest', 'clarificationContinuation', `
|
|
const {useState,useRef,useEffect}=hooks; const Button='Button',Input={TextArea:'TextArea'};
|
|
${body}
|
|
return AnalysisClarification;
|
|
`)(React, hooks, analysisClarificationRequest, clarificationContinuation);
|
|
const props = { turn: initial, onContinue, disabled: false };
|
|
const render = () => { let n = 0; do { dirty = false; cursor = 0; queued = []; tree = Component(props); queued.forEach(fn => fn()); if (++n > 15) throw Error('render loop'); } while (dirty); return tree; };
|
|
return { props, render, choose(value) { find(tree, node => node.type === 'input' && node.props.value === value).props.onChange(); return render(); },
|
|
input(value) { find(tree, node => node.type === 'TextArea').props.onChange({ target: { value } }); return render(); },
|
|
confirm() { return find(tree, node => node.type === 'Button').props.onClick(); },
|
|
button: () => find(tree, node => node.type === 'Button'),
|
|
radios: () => { const nodes = []; const visit = node => { if (!node || typeof node !== 'object') return; if (node.type === 'input') nodes.push(node); React.Children.toArray(node.props?.children).forEach(visit); }; visit(tree); return nodes; },
|
|
};
|
|
}
|
|
|
|
test('saved product ambiguity exposes only server candidate names, not names parsed from prose', () => {
|
|
const current = { status: 'NEEDS_CLARIFICATION', answer: '请选择 A 或 B', plan: { task: { productResolution: { productMatch: { status: 'ambiguous', candidates: [{ name: 'A' }, { name: 'B' }] } } } } };
|
|
assert.deepEqual(analysisClarificationRequest(current).options.map(item => item.value), ['A', 'B']);
|
|
assert.equal(analysisClarificationRequest({ status: 'NEEDS_CLARIFICATION', answer: '请选择 A 或 B' }).options.length, 0);
|
|
assert.equal(analysisClarificationRequest({ ...current, status: 'FAILED' }), null);
|
|
});
|
|
|
|
test('continuations reject forged choices and preserve the original goal, selected value, and supplement', () => {
|
|
const request = analysisClarificationRequest(turn());
|
|
assert.equal(clarificationContinuation(request, 'forged', ''), null);
|
|
assert.equal(clarificationContinuation(request, '', ' '), null);
|
|
const result = clarificationContinuation(request, '18X15X4+T', '只看黑片');
|
|
assert.match(result, /保留未修改的原目标和条件/); assert.match(result, /我的选择:18X15X4\+T/); assert.match(result, /补充说明:只看黑片/);
|
|
assert.doesNotMatch(result, /18X15X4\+Z/);
|
|
});
|
|
|
|
test('no option is preselected or automatically submitted; explicit confirmation sends one selection', async () => {
|
|
const calls = []; let done;
|
|
const ui = fixture(turn(), question => { calls.push(question); return new Promise(resolve => { done = resolve; }); });
|
|
ui.render(); assert.equal(calls.length, 0); assert.equal(ui.button().props.disabled, true); assert.ok(ui.radios().every(input => !input.props.checked));
|
|
ui.confirm(); assert.equal(calls.length, 0);
|
|
ui.choose('18X15X4+T'); assert.equal(calls.length, 0); assert.equal(ui.button().props.disabled, false);
|
|
ui.confirm(); ui.confirm(); ui.render(); assert.equal(calls.length, 1); assert.equal(ui.button().props.disabled, true);
|
|
done(); await new Promise(resolve => setImmediate(resolve)); ui.render();
|
|
assert.match(calls[0], /18X15X4\+T/);
|
|
});
|
|
|
|
test('free text alone can continue a missing-condition question and failure keeps it editable', async () => {
|
|
let calls = 0;
|
|
const current = { id: 'missing', status: 'NEEDS_CLARIFICATION', result: { clarificationRequest: { question: '请补充比较期间。', options: [] } } };
|
|
const ui = fixture(current, () => { calls++; throw Error('连接中断'); });
|
|
ui.render(); ui.input('2026年9月与8月'); ui.confirm(); await new Promise(resolve => setImmediate(resolve)); const tree = ui.render();
|
|
assert.equal(calls, 1); assert.ok(find(tree, node => node.props?.role === 'alert')); assert.equal(ui.button().props.disabled, false);
|
|
});
|
|
|
|
test('a refreshed question clears prior selections; disabled views cannot submit', () => {
|
|
const calls = [], ui = fixture(turn(), question => { calls.push(question); });
|
|
ui.render(); ui.choose('18X15X4+Z'); ui.input('保留原期间');
|
|
ui.props.turn = { ...turn(), id: 'clarify-2' }; ui.render(); assert.ok(ui.radios().every(input => !input.props.checked)); assert.equal(ui.button().props.disabled, true);
|
|
ui.choose('18X15X4+T'); ui.props.disabled = true; ui.render(); ui.confirm(); assert.deepEqual(calls, []);
|
|
});
|
|
|
|
test('current page wires clarification only to the latest editable turn through normal submission', () => {
|
|
const source = fs.readFileSync(new URL('../src/ManagementPages.tsx', import.meta.url), 'utf8');
|
|
const historical = source.slice(source.indexOf('{turns.map((turn, index)'), source.indexOf('{lastTurn && <AnalysisTurn'));
|
|
assert.doesNotMatch(historical, /onClarify=/);
|
|
assert.match(source, /onClarify=\{sessionReadOnly \? undefined : question => submit\(question\)\}/);
|
|
});
|
|
|
|
|
|
test('reopened conversations restore only the newest unanswered clarification to the current turn', () => {
|
|
const waiting = turn();
|
|
assert.equal(pendingClarificationTurnId([]), '');
|
|
assert.equal(pendingClarificationTurnId([{id:'old', status:'COMPLETED'}, waiting]), waiting.id);
|
|
assert.equal(pendingClarificationTurnId([waiting, {id:'answered',status:'COMPLETED'}]), '');
|
|
assert.equal(pendingClarificationTurnId([{id:'failed', status:'FAILED'}]), '');
|
|
const source = fs.readFileSync(new URL('../src/ManagementPages.tsx', import.meta.url), 'utf8');
|
|
assert.match(source, /focusTurnId \|\| \(active \? String\(active.id \|\| ''\) : pendingClarificationTurnId\(historyTurns\)\)/);
|
|
});
|