[verified] refactor: harden operations and redesign device console

This commit is contained in:
chick
2026-07-15 16:33:16 +08:00
parent e04a16e817
commit 4977afdc20
24 changed files with 769 additions and 779 deletions
+7 -6
View File
@@ -39,7 +39,7 @@ test('FileConfigStore uses example only as template and commits immutable snapsh
const store = await FileConfigStore.open({ configPath, examplePath })
assert.equal(store.snapshot.server.host, '127.0.0.1')
assert.ok(Object.isFrozen(store.snapshot.instances))
await store.add({ id: 'one', url: 'http://127.0.0.1:3000' })
await store.add({ id: 'one', url: 'http://192.0.2.10:3000' })
assert.equal(JSON.parse(await readFile(configPath, 'utf8')).instances[0].id, 'one')
assert.equal(await readFile(examplePath, 'utf8'), before)
})
@@ -48,7 +48,7 @@ test('failed persistence does not publish memory and transactions serialize', as
const { configPath, examplePath } = await fixtureConfig()
let writes = 0
const store = await FileConfigStore.open({ configPath, examplePath, atomicWrite: async () => { writes += 1; throw new Error('disk full') } })
await assert.rejects(store.add({ id: 'x', url: 'http://127.0.0.1' }), /disk full/)
await assert.rejects(store.add({ id: 'x', url: 'http://192.0.2.11' }), /disk full/)
assert.equal(store.snapshot.instances.length, 0)
assert.equal(writes, 1)
await assert.rejects(access(configPath))
@@ -76,16 +76,17 @@ test('proxy path and target URL contracts reject origin escape vectors', () => {
})
test('buildApp supports inject without listening and covers health/readiness/config/auth/proxy/404', async t => {
const { configPath, examplePath } = await fixtureConfig([{ id: 'one', url: 'http://127.0.0.1:3000' }])
const { configPath, examplePath } = await fixtureConfig([{ id: 'one', url: 'http://192.0.2.10:3000' }])
const store = await FileConfigStore.open({ configPath, examplePath })
const calls = []
const registry = new ClientRegistry({ createClient: instance => fakeClient(instance, calls) }).reconcile(store.snapshot.instances)
const app = await buildApp({ configStore: store, clientRegistry: registry, staticFiles: false })
const testPolicy = { authorize: (_method, path) => path === '/api/test' ? { allowed: true, dangerous: false } : { allowed: false, statusCode: 403, reason: 'test policy' } }
const app = await buildApp({ configStore: store, clientRegistry: registry, staticFiles: false, proxyPolicy: testPolicy })
t.after(() => app.close())
assert.deepEqual((await app.inject('/api/health')).json(), { ok: true })
assert.deepEqual((await app.inject('/api/ready')).json(), { ready: true })
assert.equal((await app.inject('/api/config')).statusCode, 200)
const created = await app.inject({ method: 'POST', url: '/api/instances', payload: { id: 'two', url: 'https://example.test' } })
const created = await app.inject({ method: 'POST', url: '/api/instances', payload: { id: 'two', url: 'https://192.0.2.15' } })
assert.equal(created.statusCode, 200)
const renamed = await app.inject({ method: 'PUT', url: '/api/instances/two', payload: { id: 'renamed' } })
assert.equal(renamed.json().instance.id, 'renamed')
@@ -104,7 +105,7 @@ test('buildApp supports inject without listening and covers health/readiness/con
assert.equal(calls[0].options.headers.has('authorization'), false)
assert.equal(calls[0].options.headers.has('forwarded'), false)
assert.equal(calls[0].options.body, JSON.stringify({ hello: 'world' }))
assert.equal(new URL(calls[0].url).origin, 'http://127.0.0.1:3000')
assert.equal(new URL(calls[0].url).origin, 'http://192.0.2.10:3000')
assert.deepEqual((await app.inject('/api/missing')).json(), { error: 'not found' })
})