57 lines
3.0 KiB
JavaScript
57 lines
3.0 KiB
JavaScript
|
|
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { createSimAdminClient, normalizeInstance, summarizeInstanceSnapshot, redactInstance } from '../server/core.js'
|
|
|
|
test('normalizeInstance supports passwordless and password-protected instances without exposing password', () => {
|
|
const open = normalizeInstance({ id: 'open-1', name: '开放设备', url: 'http://127.0.0.1:3000/' }, 0)
|
|
assert.equal(open.auth.mode, 'none')
|
|
assert.equal(open.url, 'http://127.0.0.1:3000')
|
|
|
|
const protectedOne = normalizeInstance({ id: 'locked', url: 'http://cpe.local', auth: { password: 'secret' } }, 1)
|
|
assert.equal(protectedOne.auth.mode, 'password')
|
|
assert.equal(protectedOne.auth.password, 'secret')
|
|
assert.equal(redactInstance(protectedOne).auth.hasPassword, true)
|
|
assert.equal(redactInstance(protectedOne).auth.password, undefined)
|
|
})
|
|
|
|
test('client stores simadmin_session from login and sends it on later proxied requests', async () => {
|
|
const calls = []
|
|
const fetchImpl = async (url, options = {}) => {
|
|
calls.push({ url: String(url), options })
|
|
if (String(url).endsWith('/api/auth/status') && calls.length === 1) {
|
|
return new Response(JSON.stringify({ data: { configured: true, authenticated: false, settings: { password_protection_enabled: true } } }), { status: 200, headers: { 'content-type': 'application/json' } })
|
|
}
|
|
if (String(url).endsWith('/api/auth/login')) {
|
|
return new Response(JSON.stringify({ success: true }), { status: 200, headers: { 'set-cookie': 'simadmin_session=abc123; HttpOnly; Path=/; Max-Age=86400' } })
|
|
}
|
|
if (String(url).endsWith('/api/device')) {
|
|
return new Response(JSON.stringify({ data: { model: 'RM500Q' } }), { status: 200, headers: { 'content-type': 'application/json' } })
|
|
}
|
|
return new Response('{}', { status: 404 })
|
|
}
|
|
const client = createSimAdminClient(normalizeInstance({ id: 'locked', url: 'http://sim.local', auth: { password: 'secret' } }, 0), { fetchImpl })
|
|
const auth = await client.ensureAuthenticated()
|
|
assert.equal(auth.authenticated, true)
|
|
const proxied = await client.fetchJson('/api/device')
|
|
assert.equal(proxied.status, 200)
|
|
assert.match(calls.at(-1).options.headers.cookie, /simadmin_session=abc123/)
|
|
})
|
|
|
|
test('snapshot summary merges status from device, sim, network, sms and system endpoints', () => {
|
|
const snapshot = summarizeInstanceSnapshot({
|
|
device: { data: { model: 'CPE X', imei: '123' } },
|
|
sim: { data: { iccid: '8986', operator: 'CMCC' } },
|
|
network: { data: { accessTechnology: 'LTE', registration: 'registered' } },
|
|
smsStats: { data: { total: 12, unread: 2 } },
|
|
data: { data: { enabled: true, connected: true } },
|
|
ota: { data: { current_version: '1.2.3' } },
|
|
})
|
|
assert.equal(snapshot.device.model, 'CPE X')
|
|
assert.equal(snapshot.sim.iccid, '8986')
|
|
assert.equal(snapshot.network.accessTechnology, 'LTE')
|
|
assert.equal(snapshot.sms.total, 12)
|
|
assert.equal(snapshot.data.connected, true)
|
|
assert.equal(snapshot.ota.currentVersion, '1.2.3')
|
|
})
|