93 lines
2.8 KiB
TypeScript
93 lines
2.8 KiB
TypeScript
|
|
import { describe, expect, it } from 'vitest'
|
||
|
|
import {
|
||
|
|
INITIAL_GUANGHU_ROUTER_STATE,
|
||
|
|
markGuanghuAuthorizationApproved,
|
||
|
|
reduceGuanghuRouterEvent,
|
||
|
|
} from './guanghuRouter'
|
||
|
|
|
||
|
|
describe('Guanghu Router event state', () => {
|
||
|
|
it('becomes online only from a server-connected receipt', () => {
|
||
|
|
const state = reduceGuanghuRouterEvent(INITIAL_GUANGHU_ROUTER_STATE, {
|
||
|
|
type: 'router.connected',
|
||
|
|
receipt: {
|
||
|
|
connection_id: 'connection-001',
|
||
|
|
node_id: 'JD-FD-PRIMARY',
|
||
|
|
receipt_id: 'receipt-001',
|
||
|
|
state: 'online',
|
||
|
|
},
|
||
|
|
})
|
||
|
|
expect(state.status).toBe('online')
|
||
|
|
expect(state.latestReceipt?.receipt_id).toBe('receipt-001')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('keeps valid authorization cards and rejects malformed placeholders', () => {
|
||
|
|
const online = {
|
||
|
|
...INITIAL_GUANGHU_ROUTER_STATE,
|
||
|
|
status: 'online' as const,
|
||
|
|
}
|
||
|
|
const malformed = reduceGuanghuRouterEvent(online, {
|
||
|
|
type: 'authorization.requested',
|
||
|
|
digest: 'placeholder',
|
||
|
|
workorder: { id: 'workorder' },
|
||
|
|
})
|
||
|
|
expect(malformed.cards).toEqual([])
|
||
|
|
|
||
|
|
const requested = reduceGuanghuRouterEvent(online, {
|
||
|
|
type: 'authorization.requested',
|
||
|
|
digest: 'a'.repeat(64),
|
||
|
|
workorder: {
|
||
|
|
action: 'read-navigation-map',
|
||
|
|
description: '进入第五域',
|
||
|
|
expiresAt: 2_000_000_000,
|
||
|
|
id: '203e12af-f821-4b62-b80f-b3d73df05161',
|
||
|
|
persona: { name: '铸渊', pid: 'ICE-GL-ZY001' },
|
||
|
|
scope: 'server-login',
|
||
|
|
target: 'JD-FD-PRIMARY',
|
||
|
|
},
|
||
|
|
})
|
||
|
|
expect(requested.cards).toHaveLength(1)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('turns transport failures and close events into visible non-online states', () => {
|
||
|
|
const failed = reduceGuanghuRouterEvent(INITIAL_GUANGHU_ROUTER_STATE, {
|
||
|
|
type: 'router.error',
|
||
|
|
error: 'device_not_registered',
|
||
|
|
})
|
||
|
|
expect(failed.status).toBe('error')
|
||
|
|
expect(failed.error).toBe('device_not_registered')
|
||
|
|
|
||
|
|
const closed = reduceGuanghuRouterEvent(failed, {
|
||
|
|
type: 'router.closed',
|
||
|
|
receipt: {
|
||
|
|
connection_id: 'connection-001',
|
||
|
|
node_id: 'JD-FD-PRIMARY',
|
||
|
|
receipt_id: 'receipt-002',
|
||
|
|
state: 'offline',
|
||
|
|
},
|
||
|
|
})
|
||
|
|
expect(closed.status).toBe('offline')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('surfaces the real repository upload receipt returned after approval', () => {
|
||
|
|
const state = markGuanghuAuthorizationApproved({
|
||
|
|
...INITIAL_GUANGHU_ROUTER_STATE,
|
||
|
|
cards: [],
|
||
|
|
status: 'online',
|
||
|
|
}, 'workorder', {
|
||
|
|
receipt: {
|
||
|
|
diagnostic_code: 'broadcast_console_approved',
|
||
|
|
state: 'approved',
|
||
|
|
},
|
||
|
|
repository_transfer: {
|
||
|
|
ok: true,
|
||
|
|
receipt: {
|
||
|
|
action: 'push-repository',
|
||
|
|
diagnostic_code: 'repo_push_succeeded',
|
||
|
|
state: 'succeeded',
|
||
|
|
},
|
||
|
|
},
|
||
|
|
})
|
||
|
|
expect(state.latestReceipt?.diagnostic_code).toBe('repo_push_succeeded')
|
||
|
|
expect(state.error).toBeNull()
|
||
|
|
})
|
||
|
|
})
|