feat: add instance management to dashboard

This commit is contained in:
chick
2026-07-07 23:04:32 +08:00
parent 182bd19c95
commit 2505ee561a
7 changed files with 267 additions and 12 deletions
+29 -1
View File
@@ -1,7 +1,10 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { createSimAdminClient, normalizeInstance, summarizeInstanceSnapshot, redactInstance } from '../server/core.js'
import { mkdtemp, readFile, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { createSimAdminClient, normalizeInstance, summarizeInstanceSnapshot, redactInstance, loadConfig, addInstanceToConfig, updateInstanceInConfig, deleteInstanceFromConfig } 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)
@@ -54,3 +57,28 @@ test('snapshot summary merges status from device, sim, network, sms and system e
assert.equal(snapshot.data.connected, true)
assert.equal(snapshot.ota.currentVersion, '1.2.3')
})
test('config store can add update and delete instances while persisting json', async () => {
const dir = await mkdtemp(path.join(tmpdir(), 'multi-simadmin-'))
const configPath = path.join(dir, 'config.json')
await writeFile(configPath, JSON.stringify({ server: { host: '127.0.0.1', port: 9999 }, instances: [] }, null, 2))
const config = await loadConfig({ configPath, defaultConfigPath: configPath })
const added = await addInstanceToConfig(config, { id: 'home-cpe', name: '家里 CPE', url: 'http://192.168.1.1/', auth: { password: 'pw' }, tags: ['home'] })
assert.equal(added.id, 'home-cpe')
assert.equal(added.url, 'http://192.168.1.1')
assert.equal(config.instances.length, 1)
const updated = await updateInstanceInConfig(config, 'home-cpe', { name: '客厅 CPE', url: 'http://192.168.1.2', auth: { mode: 'none' }, description: 'main router' })
assert.equal(updated.name, '客厅 CPE')
assert.equal(updated.auth.password, '')
const persisted = JSON.parse(await readFile(configPath, 'utf8'))
assert.equal(persisted.instances[0].name, '客厅 CPE')
assert.equal(persisted.instances[0].url, 'http://192.168.1.2')
const removed = await deleteInstanceFromConfig(config, 'home-cpe')
assert.equal(removed.id, 'home-cpe')
assert.equal(config.instances.length, 0)
assert.deepEqual(JSON.parse(await readFile(configPath, 'utf8')).instances, [])
})