166 lines
6.7 KiB
TypeScript
166 lines
6.7 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
import {
|
|
buildLayerShareItems,
|
|
buildTaskTrendBuckets,
|
|
computeBentoPreviewLimit,
|
|
computeDatasourceHealth,
|
|
extractArrayPayload,
|
|
formatDataTypeLabel,
|
|
formatDurationShort,
|
|
formatRelativeAge,
|
|
normalizeCollectorRows,
|
|
} from '../src/admin/pages/dashboardHelpers'
|
|
|
|
describe('dashboardHelpers', () => {
|
|
test('extractArrayPayload supports enveloped data, items, and bare arrays', () => {
|
|
expect(extractArrayPayload({ data: [1, 2] })).toEqual([1, 2])
|
|
expect(extractArrayPayload({ items: ['a'] })).toEqual(['a'])
|
|
expect(extractArrayPayload([3, 4])).toEqual([3, 4])
|
|
expect(extractArrayPayload(null)).toEqual([])
|
|
})
|
|
|
|
test('computeDatasourceHealth classifies disabled, running, error, stale, and healthy', () => {
|
|
const now = Date.parse('2026-12-03T12:00:00Z')
|
|
expect(computeDatasourceHealth({ is_active: false }, now)).toBe('disabled')
|
|
expect(computeDatasourceHealth({ is_active: true, is_running: true }, now)).toBe('running')
|
|
expect(computeDatasourceHealth({ is_active: true, last_status: 'failed' }, now)).toBe('error')
|
|
expect(
|
|
computeDatasourceHealth(
|
|
{ is_active: true, last_status: 'success', frequency_minutes: 60, last_run_at: '2026-12-03T09:00:00Z' },
|
|
now,
|
|
),
|
|
).toBe('stale')
|
|
expect(
|
|
computeDatasourceHealth(
|
|
{ is_active: true, last_status: 'success', frequency_minutes: 60, last_run_at: '2026-12-03T11:30:00Z' },
|
|
now,
|
|
),
|
|
).toBe('healthy')
|
|
})
|
|
|
|
test('normalizeCollectorRows merges built-in and custom collectors without duplicating overrides', () => {
|
|
const builtin = {
|
|
data: [
|
|
{
|
|
name: 'spacetrack_tle',
|
|
display_name: 'Space-Track GP',
|
|
product: 'satellites',
|
|
source_type: 'rest',
|
|
endpoint: 'https://www.space-track.org',
|
|
is_active: true,
|
|
requires_credentials: true,
|
|
has_credentials: false,
|
|
auth_type: 'basic',
|
|
credential_preview: '',
|
|
connectivity_status: 'missing_credentials',
|
|
},
|
|
{
|
|
name: 'celestrak_tle',
|
|
display_name: 'CelesTrak TLE',
|
|
product: 'satellites',
|
|
source_type: 'http',
|
|
endpoint: 'https://celestrak.org',
|
|
is_active: true,
|
|
requires_credentials: false,
|
|
has_credentials: true,
|
|
auth_type: 'none',
|
|
credential_preview: '',
|
|
connectivity_status: 'connected',
|
|
},
|
|
],
|
|
}
|
|
const custom = {
|
|
data: [
|
|
{
|
|
id: 11,
|
|
name: 'spacetrack_tle',
|
|
product: 'satellites',
|
|
source_type: 'rest',
|
|
endpoint: 'https://override.example',
|
|
auth_type: 'basic',
|
|
is_active: true,
|
|
},
|
|
{
|
|
id: 12,
|
|
name: 'custom_idc_api',
|
|
product: 'compute',
|
|
source_type: 'rest',
|
|
endpoint: 'https://cmdb.example/api',
|
|
auth_type: 'api_key',
|
|
requires_credentials: true,
|
|
has_credentials: true,
|
|
credential_preview: 'idc••••99ab',
|
|
is_active: true,
|
|
},
|
|
],
|
|
}
|
|
|
|
const rows = normalizeCollectorRows(builtin, custom)
|
|
expect(rows).toHaveLength(3)
|
|
expect(rows[0].health).toBe('missing_credentials')
|
|
expect(rows[1].health).toBe('connected')
|
|
expect(rows[2].kind).toBe('custom')
|
|
expect(rows[2].credentialPreview).toBe('idc••••99ab')
|
|
})
|
|
|
|
test('formatRelativeAge and formatDurationShort return concise strings', () => {
|
|
const now = Date.parse('2026-12-03T12:00:00Z')
|
|
expect(formatRelativeAge('2026-12-03T11:50:00Z', false, now)).toBe('10 分钟前')
|
|
expect(formatRelativeAge('2026-12-03T11:50:00Z', true, now)).toBe('10m ago')
|
|
expect(formatDurationShort('2026-12-03T12:00:00Z', '2026-12-03T12:00:03.5Z')).toBe('3.5s')
|
|
})
|
|
|
|
test('computeBentoPreviewLimit fills available height and reserves show-more row on overflow', () => {
|
|
expect(computeBentoPreviewLimit(240, 0)).toEqual({ visibleCount: 0, hiddenCount: 0 })
|
|
// Fits all 3 items without needing show-more row (3 * 32 + 2 * 6 = 108px <= 150px)
|
|
expect(computeBentoPreviewLimit(150, 3)).toEqual({ visibleCount: 3, hiddenCount: 0 })
|
|
// Overflowing 31 items inside 220px reserves 28px for "显示更多" row: floor((220-28)/38) = 5
|
|
expect(computeBentoPreviewLimit(220, 31)).toEqual({ visibleCount: 5, hiddenCount: 26 })
|
|
// Unmeasured container height falls back cleanly
|
|
expect(computeBentoPreviewLimit(0, 12)).toEqual({ visibleCount: 4, hiddenCount: 8 })
|
|
})
|
|
|
|
test('formatDataTypeLabel translates known types and humanizes fallback tokens', () => {
|
|
expect(formatDataTypeLabel('satellite_tle', false)).toBe('卫星轨道根数')
|
|
expect(formatDataTypeLabel('satellite_tle', true)).toBe('Satellite TLE')
|
|
expect(formatDataTypeLabel('network', false)).toBe('网络')
|
|
expect(formatDataTypeLabel('bgp_update', true)).toBe('BGP Updates')
|
|
expect(formatDataTypeLabel('compute_center', false)).toBe('算力中心')
|
|
expect(formatDataTypeLabel('custom_signal_feed', true)).toBe('Custom Signal Feed')
|
|
expect(formatDataTypeLabel('', true)).toBe('-')
|
|
})
|
|
|
|
test('buildTaskTrendBuckets aggregates 24h and 7d task counts without NaN', () => {
|
|
const now = Date.parse('2026-12-03T12:15:00Z')
|
|
const tasks = [
|
|
{ id: 1, datasource_id: 1, datasource_name: 'BGP', status: 'success', started_at: '2026-12-03T11:30:00Z' },
|
|
{ id: 2, datasource_id: 1, datasource_name: 'BGP', status: 'failed', started_at: '2026-12-03T11:40:00Z' },
|
|
{ id: 3, datasource_id: 2, datasource_name: 'TLE', status: 'completed', started_at: '2026-12-01T08:00:00Z' },
|
|
{ id: 4, datasource_id: 3, datasource_name: 'AIS', status: 'error', started_at: '2026-11-20T00:00:00Z' },
|
|
]
|
|
const buckets24h = buildTaskTrendBuckets(tasks, '24h', now)
|
|
expect(buckets24h).toHaveLength(6)
|
|
expect(buckets24h[5].ok).toBe(1)
|
|
expect(buckets24h[5].fail).toBe(1)
|
|
|
|
const buckets7d = buildTaskTrendBuckets(tasks, '7d', now)
|
|
expect(buckets7d).toHaveLength(7)
|
|
expect(buckets7d.reduce((sum, item) => sum + item.ok, 0)).toBe(2)
|
|
expect(buckets7d.reduce((sum, item) => sum + item.fail, 0)).toBe(1)
|
|
})
|
|
|
|
test('buildLayerShareItems sorts descending and collapses excess tail into other slice', () => {
|
|
const layers = Array.from({ length: 8 }, (_, idx) => ({
|
|
id: `layer_${idx}`,
|
|
name: `Layer ${idx}`,
|
|
count: (idx + 1) * 10,
|
|
}))
|
|
layers.push({ id: 'empty', name: 'Empty', count: 0 })
|
|
|
|
const shares = buildLayerShareItems(layers, 6, 'Other')
|
|
expect(shares).toHaveLength(6)
|
|
expect(shares[0]).toMatchObject({ id: 'layer_7', count: 80 })
|
|
expect(shares[5]).toMatchObject({ id: '__other__', name: 'Other', count: 60 })
|
|
})
|
|
})
|