import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' import { fileURLToPath } from 'node:url' import { ref } from 'vue' import { buildApplicationPreviewFooterMessage, buildApplicationPreviewRows, buildApplicationPreviewSubmitText, buildApplicationTemplatePreview, applyApplicationPolicyEstimateResult, buildApplicationPolicyEstimateRequest, buildLocalApplicationPreview, buildLocalApplicationPreviewMessage, buildModelRefinedApplicationPreview, normalizeApplicationPreview, shouldUseLocalApplicationPreview } from '../src/utils/expenseApplicationPreview.js' import { renderMarkdown } from '../src/utils/markdown.js' import { createMessage as createConversationMessage, hasMeaningfulSessionMessages } from '../src/views/scripts/travelReimbursementConversationModel.js' import { useTravelReimbursementFlow } from '../src/views/scripts/useTravelReimbursementFlow.js' const submitComposerScript = readFileSync( fileURLToPath(new URL('../src/views/scripts/useTravelReimbursementSubmitComposer.js', import.meta.url)), 'utf8' ) const createViewScript = readFileSync( fileURLToPath(new URL('../src/views/scripts/TravelReimbursementCreateView.js', import.meta.url)), 'utf8' ) const createViewTemplate = readFileSync( fileURLToPath(new URL('../src/views/TravelReimbursementCreateView.vue', import.meta.url)), 'utf8' ) const messageItemTemplate = readFileSync( fileURLToPath(new URL('../src/components/travel/TravelReimbursementMessageItem.vue', import.meta.url)), 'utf8' ) const messageItemStyles = readFileSync( fileURLToPath(new URL('../src/assets/styles/components/travel-reimbursement-message-item.css', import.meta.url)), 'utf8' ) const applicationMessageStyles = readFileSync( fileURLToPath(new URL('../src/assets/styles/components/travel-reimbursement-message-application.css', import.meta.url)), 'utf8' ) const conversationModelScript = readFileSync( fileURLToPath(new URL('../src/views/scripts/travelReimbursementConversationModel.js', import.meta.url)), 'utf8' ) const previewEditorScript = readFileSync( fileURLToPath(new URL('../src/views/scripts/useApplicationPreviewEditor.js', import.meta.url)), 'utf8' ) const flowScript = readFileSync( fileURLToPath(new URL('../src/views/scripts/useTravelReimbursementFlow.js', import.meta.url)), 'utf8' ) function createFlowHarness() { return useTravelReimbursementFlow({ activeSessionType: ref('application'), reviewDrawerMode: ref(''), insightPanelCollapsed: ref(true), isKnowledgeSession: ref(false), fetchAgentRunDetail: async () => null, buildLocalIntentPreview: () => '本地意图预览', buildLocalExtractionProgressMessages: () => ['正在抽取信息'], summarizeSemanticIntentDetail: () => '模型已完成意图识别', summarizeSemanticParseDetail: () => '模型已完成信息抽取', SCENARIO_LABELS: {}, INTENT_LABELS: {}, EXPENSE_TYPE_LABELS: {}, FLOW_STEP_FALLBACKS: { intent: { title: '意图识别', tool: 'SemanticRouter', runningText: '正在识别业务意图...', completedText: '已识别业务意图' }, extraction: { title: '信息抽取', tool: 'SemanticExtractor', runningText: '正在抽取关键信息...', completedText: '已抽取关键信息' }, 'application-submit-success': { title: '申请单提交成功', tool: 'ApplicationSubmit', runningText: '正在提交申请单...', completedText: '申请单提交成功' } }, REVIEW_DRAWER_MODE_FLOW: 'flow', REVIEW_DRAWER_MODE_REVIEW: 'review', FLOW_STEP_STATUS_PENDING: 'pending', FLOW_STEP_STATUS_RUNNING: 'running', FLOW_STEP_STATUS_COMPLETED: 'completed', FLOW_STEP_STATUS_FAILED: 'failed' }) } test('application intent uses local preview instead of immediate orchestrator call', () => { const prompt = '申请 2026-05-20 至 2026-05-23 去上海支撑上海电力部署项目,出差3天,高铁,预计金额2358元' assert.equal( shouldUseLocalApplicationPreview(prompt, { sessionType: 'application', attachmentCount: 0, reviewAction: '', systemGenerated: false }), true ) assert.equal( shouldUseLocalApplicationPreview('帮我查询申请状态', { sessionType: 'application', attachmentCount: 0, reviewAction: '', systemGenerated: false }), false ) const preview = buildLocalApplicationPreview(prompt, { name: '李文静', departmentName: '财务部', grade: 'P5' }) assert.equal(preview.fields.applicationType, '差旅费用申请') assert.equal(preview.fields.time, '2026-05-20 至 2026-05-23') assert.equal(preview.fields.location, '上海') assert.equal(preview.fields.days, '3天') assert.equal(preview.fields.transportMode, '火车') assert.equal(preview.fields.amount, '2358元') assert.equal(preview.fields.grade, 'P5') assert.equal(preview.readyToSubmit, true) assert.doesNotMatch(buildLocalApplicationPreviewMessage(preview), /#application-submit/) assert.match(buildApplicationPreviewFooterMessage(preview), /#application-submit/) assert.match(buildLocalApplicationPreviewMessage(preview), /点击对应行即可直接编辑/) }) test('application preview renders ordered editable rows and submit text uses edited values', () => { const preview = buildLocalApplicationPreview('申请 2026-05-25 至 2026-05-28 去新疆,伊犁出差,服务美团业务部署,火车,预计费用1800元', { name: '李文静', grade: 'P5' }) assert.equal(preview.fields.location, '新疆,伊犁') assert.equal(preview.fields.reason, '服务美团业务部署') const editedPreview = normalizeApplicationPreview({ ...preview, fields: { ...preview.fields, reason: '客户现场项目支持', amount: '1900元' } }) const rows = buildApplicationPreviewRows(editedPreview) assert.deepEqual( rows.map((row) => row.label), ['申请类型', '职级', '发生时间', '地点', '事由', '天数', '出行方式', '住宿上限/天', '补贴标准/天', '交通费用口径', '规则测算参考', '用户预估费用'] ) assert.equal(rows.find((row) => row.key === 'amount')?.value, '1900元') assert.equal(rows.find((row) => row.key === 'amount')?.highlight, true) assert.equal(rows.find((row) => row.key === 'grade')?.editable, false) assert.equal(rows.find((row) => row.key === 'lodgingDailyCap')?.editable, false) assert.match(buildApplicationPreviewSubmitText(editedPreview), /事由:客户现场项目支持/) assert.match(buildApplicationPreviewSubmitText(editedPreview), /用户预估费用:1900元/) }) test('application preview cleans empty time labels and keeps only business reason', () => { const preview = buildLocalApplicationPreview('发生时间:,去九江出差3天,服务美团业务部署,预计费用1800元,火车', { name: '李文静', grade: 'P5' }, { today: '2026-05-29' }) assert.equal(preview.fields.time, '2026-05-29 至 2026-05-31') assert.equal(preview.fields.location, '九江') assert.equal(preview.fields.days, '3天') assert.equal(preview.fields.reason, '服务美团业务部署') assert.equal(preview.fields.transportMode, '火车') assert.doesNotMatch(preview.fields.reason, /发生时间|去九江|出差3天/) }) test('application preview can be refined by ontology model extraction', () => { const rawText = '发生时间:,去九江出差3天,服务美团业务部署,预计费用1800元,火车' const localPreview = buildLocalApplicationPreview(rawText, { name: '李文静', grade: 'P5' }, { today: '2026-05-29' }) const refinedPreview = buildModelRefinedApplicationPreview( localPreview, { parse_strategy: 'llm_primary', entities: [ { type: 'expense_type', value: '差旅费', normalized_value: 'travel' }, { type: 'location', value: '九江', normalized_value: '九江' }, { type: 'reason', value: '服务美团业务部署', normalized_value: '服务美团业务部署' }, { type: 'transport_mode', value: '火车', normalized_value: '火车' }, { type: 'amount', value: '1800元', normalized_value: '1800' } ], time_range: {}, missing_slots: [] }, rawText, { name: '李文静', grade: 'P5' } ) assert.equal(refinedPreview.modelRefined, true) assert.equal(refinedPreview.parseStrategy, 'llm_primary') assert.equal(refinedPreview.modelReviewStatus, 'completed') assert.equal(refinedPreview.fields.applicationType, '差旅费用申请') assert.equal(refinedPreview.fields.time, '2026-05-29 至 2026-05-31') assert.equal(refinedPreview.fields.reason, '服务美团业务部署') assert.equal(refinedPreview.fields.transportMode, '火车') }) test('application preview precomputes a date range from today when only days are provided', () => { const preview = buildLocalApplicationPreview( '去北京出差3天,支撑国网仿生产环境部署,飞机,预计费用12000元', { name: '李文静', grade: 'P5' }, { today: '2026-05-29' } ) assert.equal(preview.fields.time, '2026-05-29 至 2026-05-31') assert.equal(preview.fields.days, '3天') assert.equal(preview.readyToSubmit, true) }) test('application preview keeps rule fallback distinct from model reviewed result', () => { const rawText = '申请 2026-05-20 至 2026-05-23 去上海支撑服务器部署,出差3天,火车,预计费用1800元' const localPreview = buildLocalApplicationPreview(rawText, { name: '李文静', grade: 'P5' }) const fallbackPreview = buildModelRefinedApplicationPreview( localPreview, { parse_strategy: 'rule_fallback', entities: [ { type: 'expense_type', value: '差旅费', normalized_value: 'travel' }, { type: 'location', value: '上海', normalized_value: '上海' }, { type: 'amount', value: '1800元', normalized_value: '1800' } ], time_range: { start: '2026-05-20', end: '2026-05-23' }, missing_slots: [] }, rawText, { name: '李文静', grade: 'P5' } ) const message = buildLocalApplicationPreviewMessage(fallbackPreview) const footer = buildApplicationPreviewFooterMessage(fallbackPreview) assert.equal(fallbackPreview.modelReviewStatus, 'fallback') assert.match(message, /规则兜底/) assert.match(footer, /请确认上述的信息是否填写正确/) assert.match(footer, /#application-submit/) }) test('application preview with missing budget stays in chat and asks for补充信息', () => { const preview = buildLocalApplicationPreview('我想申请去北京出差,高铁,但是不知道预算', { name: '李文静', grade: 'P5' }) assert.equal(preview.fields.amount, '待测算') assert.equal(preview.readyToSubmit, false) assert.match(buildLocalApplicationPreviewMessage(preview), /下方表格/) assert.doesNotMatch(buildLocalApplicationPreviewMessage(preview), /当前还需要补充/) assert.match(buildApplicationPreviewFooterMessage(preview), /当前还需要补充/) assert.doesNotMatch(buildLocalApplicationPreviewMessage(preview), /#application-submit/) }) test('application quick start renders a template without model review', () => { const preview = buildApplicationTemplatePreview({ name: '李文静', departmentName: '财务部', grade: 'P5' }) const message = buildLocalApplicationPreviewMessage(preview) assert.equal(preview.modelReviewStatus, 'template') assert.equal(preview.fields.applicationType, '费用申请') assert.equal(preview.fields.applicant, '李文静') assert.equal(preview.fields.department, '财务部') assert.equal(preview.fields.grade, 'P5') assert.equal(buildApplicationPreviewRows(preview).find((row) => row.key === 'grade')?.editable, false) assert.match(message, /不调用大模型/) assert.match(message, /点击对应行直接填写/) assert.doesNotMatch(message, /#application-submit/) assert.match(buildApplicationPreviewFooterMessage(preview), /当前还需要补充/) }) test('application quick start template counts as deletable session content', () => { const welcomeMessage = createConversationMessage('assistant', '欢迎语', [], { isWelcome: true, welcomeQuickActions: [{ label: '快速发起申请', action: 'start_guided_application' }] }) const templateMessage = createConversationMessage('assistant', '申请模板', [], { applicationPreview: buildApplicationTemplatePreview({ name: '测试员工', departmentName: '财务部', grade: 'P5' }) }) assert.equal(hasMeaningfulSessionMessages([welcomeMessage]), false) assert.equal(hasMeaningfulSessionMessages([welcomeMessage, templateMessage]), true) }) test('application session shows intent flow, persists preview, and supports inline table edit', () => { assert.match(submitComposerScript, /shouldUseLocalApplicationPreview/) assert.match(submitComposerScript, /buildLocalApplicationPreviewMessage/) assert.match(submitComposerScript, /buildApplicationPreviewWithModelReview/) assert.match(submitComposerScript, /fetchOntologyParse/) assert.match(submitComposerScript, /calculateTravelReimbursement/) assert.match(submitComposerScript, /buildApplicationPolicyEstimateRequest/) assert.match(submitComposerScript, /模型复核中/) assert.match(submitComposerScript, /startFlowStep\('intent'/) assert.match(submitComposerScript, /startFlowStep\('application-review-preview'/) assert.match(submitComposerScript, /completeFlowStep\('intent'/) assert.doesNotMatch(submitComposerScript, /insightPanelCollapsed\.value = true/) assert.doesNotMatch(submitComposerScript, /void refineApplicationPreviewWithModel/) assert.match(submitComposerScript, /return null[\s\S]*const hasUnsavedReviewDraft/) assert.ok( submitComposerScript.indexOf('shouldUseLocalApplicationPreview') < submitComposerScript.indexOf('const payload = await runOrchestrator') ) assert.match(createViewScript, /const isApplicationSession = computed/) assert.match(createViewScript, /insightPanelCollapsed,/) assert.doesNotMatch(createViewScript, /if \(isApplicationSession\.value\) \{\s*return false\s*\}/) assert.match(createViewScript, /activeFlowSteps\.value\.length > 0/) assert.match(createViewScript, /useApplicationPreviewEditor/) assert.match(createViewScript, /message-bubble-application-preview/) assert.match(createViewScript, /buildApplicationPreviewFooterMessage/) assert.match(createViewScript, /function buildApplicationPreviewFooterText\(message\)/) assert.match(createViewScript, /buildApplicationPreviewSubmitText/) assert.match(createViewScript, /user_input_text: applicationSubmitText/) assert.match(conversationModelScript, /applicationPreview: null/) assert.match(conversationModelScript, /applicationPreview: message\.applicationPreview \|\| null/) assert.match(conversationModelScript, /\|\| message\.applicationPreview/) assert.match(createViewScript, /hasMeaningfulSessionMessages\(messages\.value\)/) assert.match(messageItemTemplate, /class="application-preview-table"/) assert.match(messageItemTemplate, /class="application-preview-footer application-preview-footer-missing"/) assert.match(messageItemTemplate, /application-preview-missing-chip/) assert.match(messageItemTemplate, /当前还需要补充:/) assert.match(messageItemTemplate, /补齐后我再帮您提交申请。/) assert.match(messageItemTemplate, /class="application-preview-footer message-answer-content message-answer-markdown"/) assert.match(messageItemTemplate, /v-html="ui\.renderMarkdown\(ui\.buildApplicationPreviewFooterText\(message\)\)"/) assert.doesNotMatch(messageItemTemplate, /class="application-date-editor-layer"/) assert.doesNotMatch(messageItemTemplate, /ui\.commitApplicationPreviewDateEditor\(message\)/) assert.match(messageItemTemplate, /application-preview-date-chip/) assert.match(messageItemTemplate, /申请单据已生成/) assert.match(messageItemTemplate, /application-draft-head/) assert.match(messageItemTemplate, /mdi mdi-file-document-check-outline/) assert.match(messageItemTemplate, /'is-primary': item\.label === '单号'/) assert.match(messageItemTemplate, /完整审批链、附件和明细可在单据详情中[\s\S]*application-draft-detail-link[\s\S]*>查看<\/button>/) assert.doesNotMatch(messageItemTemplate, /application-draft-detail-btn/) assert.match(messageItemTemplate, /ui\.openApplicationDraftDetail\(message\)/) assert.match(messageItemTemplate, / { const flow = createFlowHarness() flow.resetFlowRun({ startedAt: Date.parse('2026-05-29T00:00:00.000Z') }) flow.startFlowStep('intent', '正在识别业务意图...') flow.completeFlowStep('intent', '本地预览完成', 80) flow.startFlowStep('extraction', '正在抽取关键信息...') flow.completeFlowStep('extraction', '本地抽取完成', 90) flow.startFlowStep('application-submit-success', { title: '申请单提交成功', tool: 'ApplicationSubmit', detail: '正在提交申请单...' }) flow.completeFlowStep('application-submit-success', '本地提交完成', 100) flow.mergeFlowRunDetail({ started_at: '2026-05-29T00:00:00.000Z', finished_at: '2026-05-29T00:00:05.000Z', status: 'succeeded', semantic_parse: {}, ontology_json: {}, tool_calls: [ { id: 'submit-1', run_id: 'run-1', tool_type: 'application', tool_name: 'application.submit', request_json: {}, response_json: { status: 'submitted', draft_payload: { status: 'submitted' } }, status: 'succeeded', duration_ms: 2360, created_at: '2026-05-29T00:00:04.000Z' } ] }) const durationByKey = Object.fromEntries(flow.flowSteps.value.map((step) => [step.key, step.durationMs])) assert.equal(durationByKey.intent, 1400) assert.equal(durationByKey.extraction, 2600) assert.equal(durationByKey['application-submit-success'], 2360) assert.equal(flow.flowTotalDurationText.value, '5.0s') assert.equal(flow.formatFlowStepDuration({ status: 'completed', durationMs: 0 }), '--') assert.equal(flow.formatFlowStepDuration({ status: 'completed', durationMs: null }), '--') }) test('assistant markdown tables render with component-scoped table styling', () => { const rendered = renderMarkdown([ '| 项目 | 标准口径 | 天数 | 小计 |', '| --- | --- | ---: | ---: |', '| 住宿费 | 武汉 / P5 标准:330.00 元/天 | 1 | 330.00 元 |', '| 出差补贴 | 其他地区:伙食 55.00 元 + 基本 35.00 元 | 1 | 90.00 元 |' ].join('\n')) assert.match(rendered, /
/) assert.match(rendered, //) assert.match(rendered, /
{ const preview = buildLocalApplicationPreview('申请 2026-05-25 至 2026-05-28 去上海出差3天,服务项目部署,火车,预计费用1800元', { name: '李文静', grade: 'P5' }) const request = buildApplicationPolicyEstimateRequest(preview, { grade: 'P5' }) assert.equal(request.canCalculate, true) assert.deepEqual(request.payload, { days: 3, location: '上海', grade: 'P5' }) const estimatedPreview = applyApplicationPolicyEstimateResult(preview, { days: 3, location: '上海', matched_city: '上海', grade: 'P5', hotel_rate: 600, hotel_amount: 1800, total_allowance_rate: 120, allowance_amount: 360, total_amount: 2160, rule_name: '公司差旅费报销规则', rule_version: '2026版' }, { grade: 'P5' }) assert.equal(estimatedPreview.fields.lodgingDailyCap, '600元/天') assert.equal(estimatedPreview.fields.subsidyDailyCap, '120元/天') assert.match(estimatedPreview.fields.transportPolicy, /实报实销/) assert.match(estimatedPreview.fields.policyEstimate, /2,160元/) assert.equal(buildApplicationPreviewRows(estimatedPreview).find((row) => row.key === 'policyEstimate')?.highlight, true) })