- {{ query.summary || query.amount || '等待运营商回复' }}
- {{ stateLabel(query) }}
+ {{ balanceResultText(query) }}
+
+ {{ presentBalanceState(query).label }}
+
{{ query.device_id }}
diff --git a/web/src/components/commands/CommandChat.vue b/web/src/components/commands/CommandChat.vue
index e3450c39..527621ac 100644
--- a/web/src/components/commands/CommandChat.vue
+++ b/web/src/components/commands/CommandChat.vue
@@ -5,6 +5,7 @@ import CommandTimeline from './CommandTimeline.vue'
import type { DeviceMgmtListItem } from '../../types/api'
import type { BalanceQuery, CommandDefinition, CommandEvent } from '../../types/commands'
import { commandTargetDevice } from '../../utils/commandInput'
+import { balanceResultText } from '../../utils/commandPresentation'
import {
Bot24Regular,
Delete24Regular,
@@ -49,11 +50,7 @@ const emit = defineEmits<{
function latestBalanceText(query?: BalanceQuery) {
if (!query) return ''
- if (query.amount) return [query.amount, query.currency].filter(Boolean).join(' ')
- if (query.summary) return query.summary
- if (query.state === 'completed') return '余额已返回'
- if (query.state === 'failed' || query.state === 'timed_out') return '余额查询失败'
- return '余额查询中'
+ return balanceResultText(query)
}
diff --git a/web/src/components/commands/CommandTimeline.vue b/web/src/components/commands/CommandTimeline.vue
index f8d033ee..765f56db 100644
--- a/web/src/components/commands/CommandTimeline.vue
+++ b/web/src/components/commands/CommandTimeline.vue
@@ -4,6 +4,7 @@ import BalanceMessage from './BalanceMessage.vue'
import CommandAudioPlayer from './CommandAudioPlayer.vue'
import type { BalanceQuery, CommandEvent } from '../../types/commands'
import { formatDeviceDateTime } from '../../utils/deviceTime'
+import { presentCommandEvent } from '../../utils/commandPresentation'
import { Bot24Regular } from '@vicons/fluent'
const props = defineProps<{
@@ -26,16 +27,8 @@ const timelineItems = computed(() => {
return [...commands, ...balances].sort((left, right) => Date.parse(left.createdAt) - Date.parse(right.createdAt))
})
-function eventLabel(event: CommandEvent) {
- if (event.kind === 'accepted') return '你'
- if (event.kind === 'progress') return '处理中'
- if (event.kind === 'error') return '执行失败'
- return 'VoHive'
-}
-
function eventText(event: CommandEvent) {
- if (event.kind === 'accepted' && event.execution?.input) return event.execution.input
- return event.text
+ return presentCommandEvent(event).detail
}
function audioAttachments(event: CommandEvent) {
@@ -61,7 +54,7 @@ function audioAttachments(event: CommandEvent) {
}"
>
): CommandEvent {
+ return {
+ id: 1,
+ execution_id: 'exec-1',
+ kind: 'result',
+ text: '',
+ created_at: '2026-08-13T12:00:00Z',
+ ...overrides
+ }
+}
+
+function balanceQuery(overrides: Partial): BalanceQuery {
+ return {
+ id: 'balance-1',
+ device_id: 'wwan0',
+ iccid: '',
+ rule_id: 'rule-1',
+ transport: 'sms',
+ state: 'sending',
+ parse_state: 'pending',
+ started_at: '2026-08-13T12:00:00Z',
+ expires_at: '2026-08-13T12:01:00Z',
+ created_at: '2026-08-13T12:00:00Z',
+ updated_at: '2026-08-13T12:00:00Z',
+ ...overrides
+ }
+}
+
+test('maps command events without turning running or failed work into success', () => {
+ const accepted = commandEvent({ kind: 'accepted', execution: {
+ id: 'exec-1', input: '/signal wwan0', command: 'signal', state: 'running',
+ created_at: '', updated_at: ''
+ } })
+ assert.deepEqual(presentCommandEvent(accepted), {
+ title: '已发送', detail: '/signal wwan0', tone: 'sent'
+ })
+ assert.equal(presentCommandEvent(commandEvent({ kind: 'progress', text: '正在读取信号' })).tone, 'running')
+ assert.deepEqual(presentCommandEvent(commandEvent({ kind: 'error', text: '设备离线' })), {
+ title: '执行失败', detail: '设备离线', tone: 'danger'
+ })
+ assert.equal(presentCommandEvent(commandEvent({ kind: 'result', text: '查询完成' })).tone, 'success')
+})
+
+test('keeps all balance lifecycle states explicit', () => {
+ assert.deepEqual(presentBalanceState(balanceQuery({ state: 'sending' })), {
+ label: '正在发送', tone: 'running'
+ })
+ assert.equal(presentBalanceState(balanceQuery({ state: 'awaiting_reply' })).label, '等待回复')
+ assert.equal(presentBalanceState(balanceQuery({ state: 'completed', parse_state: 'parsed' })).tone, 'parsed')
+ assert.equal(presentBalanceState(balanceQuery({ state: 'completed', parse_state: 'unparsed' })).tone, 'success')
+ assert.equal(presentBalanceState(balanceQuery({ state: 'timed_out' })).tone, 'danger')
+ assert.equal(presentBalanceState(balanceQuery({ state: 'failed' })).label, '查询失败')
+})
+
+test('renders only values and transport facts returned by the backend', () => {
+ assert.equal(balanceResultText(balanceQuery({ amount: '12.89', currency: 'GBP' })), '12.89 GBP')
+ assert.equal(balanceResultText(balanceQuery({ summary: '剩余 300MB' })), '剩余 300MB')
+ assert.equal(balanceResultText(balanceQuery({ state: 'failed', error: '未匹配规则' })), '未匹配规则')
+ assert.equal(balanceTransportLabel(balanceQuery({ transport: 'sms' })), 'SMS')
+ assert.equal(balanceTransportLabel(balanceQuery({ transport: 'ussd' })), 'USSD')
+})
- {{ item.kind === 'command' ? eventLabel(item.event) : 'VoHive' }}
+ {{ item.kind === 'command' ? presentCommandEvent(item.event).title : 'VoHive' }}
diff --git a/web/src/utils/commandPresentation.ts b/web/src/utils/commandPresentation.ts
new file mode 100644
index 00000000..70fac8f5
--- /dev/null
+++ b/web/src/utils/commandPresentation.ts
@@ -0,0 +1,58 @@
+import type { BalanceQuery, CommandEvent } from '../types/commands'
+
+export type CommandEventTone = 'sent' | 'running' | 'success' | 'danger'
+export type BalanceTone = 'running' | 'waiting' | 'parsed' | 'success' | 'danger'
+
+export type CommandEventPresentation = Readonly<{
+ title: string
+ detail: string
+ tone: CommandEventTone
+}>
+
+export type BalanceStatePresentation = Readonly<{
+ label: string
+ tone: BalanceTone
+}>
+
+export function presentCommandEvent(event: CommandEvent): CommandEventPresentation {
+ if (event.kind === 'error' || event.execution?.state === 'failed') {
+ return {
+ title: '执行失败',
+ detail: event.text || event.execution?.error || '命令执行失败',
+ tone: 'danger'
+ }
+ }
+ if (event.kind === 'accepted') {
+ return {
+ title: '已发送',
+ detail: event.execution?.input || event.text || '命令已提交',
+ tone: 'sent'
+ }
+ }
+ if (event.kind === 'progress' || event.execution?.state === 'running') {
+ return { title: '执行中', detail: event.text || '命令正在执行', tone: 'running' }
+ }
+ return { title: '执行成功', detail: event.text || '命令执行完成', tone: 'success' }
+}
+
+export function presentBalanceState(query: BalanceQuery): BalanceStatePresentation {
+ if (query.state === 'failed') return { label: '查询失败', tone: 'danger' }
+ if (query.state === 'timed_out') return { label: '等待超时', tone: 'danger' }
+ if (query.state === 'awaiting_reply') return { label: '等待回复', tone: 'waiting' }
+ if (query.state === 'sending') return { label: '正在发送', tone: 'running' }
+ if (query.parse_state === 'parsed') return { label: '已解析', tone: 'parsed' }
+ return { label: '已收到', tone: 'success' }
+}
+
+export function balanceResultText(query: BalanceQuery): string {
+ if (query.amount) return [query.amount, query.currency].filter(Boolean).join(' ')
+ if (query.summary) return query.summary
+ if (query.state === 'completed') return '已收到运营商回复'
+ if (query.state === 'failed') return query.error || '余额查询失败'
+ if (query.state === 'timed_out') return query.error || '未在有效期内收到回复'
+ return '等待运营商回复'
+}
+
+export function balanceTransportLabel(query: BalanceQuery): string {
+ return query.transport === 'ussd' ? 'USSD' : 'SMS'
+}
diff --git a/web/tests/commandPresentation.test.ts b/web/tests/commandPresentation.test.ts
new file mode 100644
index 00000000..ec9335af
--- /dev/null
+++ b/web/tests/commandPresentation.test.ts
@@ -0,0 +1,71 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import type { BalanceQuery, CommandEvent } from '../src/types/commands'
+import {
+ balanceResultText,
+ balanceTransportLabel,
+ presentBalanceState,
+ presentCommandEvent
+} from '../src/utils/commandPresentation'
+
+function commandEvent(overrides: Partial