import test from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { assistantSkippedResults } from '../../src/utils/business-assistant.js' import { assistantFileError, assistantPrepareAttachment, assistantSheetSelection, assistantAttachmentSource, assistantOutgoingMessage, assistantFollowScroll, isAssistantConversationBusy, assistantSessionRevision, assistantDuration, assistantImageCandidates, assistantImageViewerIndex, isAssistantExpired, isAssistantPending, canConfirmAssistantDraft, assistantOption, assistantRowsSignature, assistantDraftCounts, assistantTimeline, assistantHasUnlinkedPendingDraft, assistantDraftStatus, assistantPendingRowLabel, assistantReferenceConditions, assistantResultTable, assistantDisplayValue } from '../../src/utils/business-assistant.js' const readyDraft = { id: 'draft-1', version: 2, status: 'WAIT_CONFIRM', ready: true, expiresAt: '2099-01-01T00:00:00', rows: [{ _rowId: '1', quantity: 2 }] } test('elapsed display distinguishes actual duration from missing data and uses whole seconds while waiting', () => { for (const value of [undefined, null, '', '1960', -1, NaN, Infinity]) assert.equal(assistantDuration(value), '') assert.equal(assistantDuration(0), '0 秒') assert.equal(assistantDuration(1960), '1.96 秒') assert.equal(assistantDuration(1960, true), '1 秒') assert.equal(assistantDuration(65000), '1 分 5 秒') assert.equal(assistantDuration(180450, true), '3 分 0 秒') }) test('chat shows live request waiting and server reply duration without inventing elapsed times for historical messages', () => { const main = readFileSync(new URL('../../src/components/BusinessAssistant/index.vue', import.meta.url), 'utf8') assert.match(main, /entry.message.delivery === 'SENDING'[^\n]+now - Date.parse\(entry.message.createdAt\)/) assert.match(main, /entry.message.role === 'assistant' && assistantDuration\(entry.message.elapsedMs\)/) assert.match(main, /entry.message.delivery === 'FAILED' && assistantDuration\(entry.message.clientWaitedMs\)/) assert.doesNotMatch(main, /elapsedMs\s*\|\|/) assert.match(main, /setInterval\([^\n]+1000\)/) assert.match(main, /onBeforeUnmount\([^\n]+clearInterval\(timer\)/) }) test('outgoing bubble freezes only selected worksheet metadata and does not copy private preview paths', () => { const files = [{ id: 'excel', name: '采购.xlsx', type: 'excel', selectedSheetIndexes: [1], sheets: [{ index: 0, name: '总表', rowCount: 101 }, { index: 1, name: '子表', rowCount: 50 }], previewUrl: 'blob:private' }, { id: 'image', name: '单据.png', type: 'image', path: '/private', previewUrl: 'blob:private-image' }] const message = assistantOutgoingMessage('local-1', '登记到货', files, '2026-10-15T10:00:00Z') assert.equal(message.delivery, 'SENDING') assert.equal(message.role, 'user') assert.equal(message.content, '登记到货') assert.deepEqual(message.attachments[0].sheetIndexes, [1]) assert.equal(message.attachments[0].rowCount, 50) assert.deepEqual(message.attachments[1], { id: 'image', name: '单据.png', type: 'image' }) files[0].sheets[1].name = '后来更改' assert.equal(message.attachments[0].sheets[0].name, '子表') assert.deepEqual(assistantOutgoingMessage('retry', '', [{ ...message.attachments[0] }]).attachments[0].sheetIndexes, [1]) }) test('pending user message is appended without taking ownership of a previous confirmation card', () => { const pending = assistantOutgoingMessage('pending', '新请求', []) const session = { draft: readyDraft, messages: [{ id: 'previous', role: 'assistant', draftId: readyDraft.id }] } const entries = assistantTimeline(session, pending) assert.equal(entries.length, 2) assert.equal(entries[0].draft.id, readyDraft.id) assert.equal(entries[1].message.id, 'pending') assert.equal(entries[1].draft, null) assert.equal(entries[1].active, false) assert.equal(session.messages.length, 1) }) test('chat follows content growth but stops following after scrolling up until the reader returns to the bottom', () => { const metrics = (top, height = 1000) => ({ scrollTop: top, scrollHeight: height, clientHeight: 300 }) assert.equal(assistantFollowScroll(metrics(700, 1400), 700, true), true) assert.equal(assistantFollowScroll(metrics(500), 700, true), false) assert.equal(assistantFollowScroll(metrics(500, 1400), 500, false), false) assert.equal(assistantFollowScroll(metrics(800, 1400), 500, false), false) assert.equal(assistantFollowScroll(metrics(1080, 1400), 800, false), true) }) test('only the explicit concurrent-message rejection is considered not sent', () => { assert.equal(isAssistantConversationBusy(new Error('上一条消息仍在处理中,请稍候')), true) assert.equal(isAssistantConversationBusy('上一条消息仍在处理中,请稍候'), true) for (const error of [new Error('timeout'), new Error('模型处理失败'), new Error('请求处理中断'), null]) assert.equal(isAssistantConversationBusy(error), false) const previous = { id: 's1', messages: [{ id: 'a1', role: 'assistant', content: '上次结果' }] } assert.equal(assistantSessionRevision(previous), assistantSessionRevision({ ...previous, draft: { version: 9 }, updatedAt: 'later' })) assert.notEqual(assistantSessionRevision(previous), assistantSessionRevision({ ...previous, messages: [...previous.messages, { id: 'a2', content: '新结果' }] })) assert.notEqual(assistantSessionRevision(previous), assistantSessionRevision({ ...previous, id: 's2' })) }) test('recovery controls stay outside the scrolling conversation and allow editing a rejected concurrent message', () => { const main = readFileSync(new URL('../../src/components/BusinessAssistant/index.vue', import.meta.url), 'utf8') assert.match(main, /
{ const field = readFileSync(new URL('../../src/components/BusinessAssistant/AssistantField.vue', import.meta.url), 'utf8') assert.match(field, /