refine Semi UI vehicle evidence

This commit is contained in:
lingniu
2026-07-18 18:07:15 +08:00
parent 801a9851ea
commit 5bfdc7f637
13 changed files with 110 additions and 47 deletions

View File

@@ -0,0 +1,16 @@
import { describe, expect, test } from 'vitest';
import { protocolDisplayLabel, protocolSourceLabel } from './protocols';
describe('protocol display vocabulary', () => {
test('uses one user-facing label for every supported vehicle protocol', () => {
expect(protocolDisplayLabel('GB32960')).toBe('GB/T 32960');
expect(protocolDisplayLabel('JT808')).toBe('JT/T 808');
expect(protocolDisplayLabel('YUTONG_MQTT')).toBe('宇通 MQTT');
});
test('keeps unknown protocols intact and explains qualified sources', () => {
expect(protocolDisplayLabel('CUSTOM')).toBe('CUSTOM');
expect(protocolSourceLabel('YUTONG_MQTT:canonical')).toBe('宇通 MQTT · canonical');
expect(protocolSourceLabel('CUSTOM:gateway')).toBe('CUSTOM:gateway');
});
});

View File

@@ -0,0 +1,21 @@
const PROTOCOL_LABELS: Record<string, string> = {
GB32960: 'GB/T 32960',
JT808: 'JT/T 808',
YUTONG_MQTT: '宇通 MQTT'
};
export function protocolDisplayLabel(protocol?: string) {
const normalized = protocol?.trim() ?? '';
return normalized ? PROTOCOL_LABELS[normalized] ?? normalized : '—';
}
export function protocolSourceLabel(source?: string) {
const normalized = source?.trim() ?? '';
if (!normalized) return '';
const separator = normalized.indexOf(':');
if (separator < 0) return protocolDisplayLabel(normalized);
const protocol = normalized.slice(0, separator);
const qualifier = normalized.slice(separator + 1);
const label = protocolDisplayLabel(protocol);
return label === protocol ? normalized : qualifier ? `${label} · ${qualifier}` : label;
}