Files
sigil/test/s01-deploy.test.ts
T
xiaoju f20b19a71e feat: implement Sigil Phase 1 MVP 🔮
- Wrangler project setup (TypeScript + Vitest)
- SigilBackend interface + WorkerPool implementation
- KV store with layered key schema (code/meta/lru/route/auth/stats)
- LRU scheduler with eviction priority (ephemeral_expired > ephemeral > normal > persistent)
- AuthModule: Bearer token validation, agent isolation, deploy cooldown
- Router: /_health, /_api/deploy, /_api/remove, /_api/list, /_api/inspect, /{agent}/{capability}
- 13 test scenarios, all passing (38 tests)
- MockKV + MockCfApi for isolated testing

Tests: 38/38  | Build: 22KB gzip:5KB 

小橘 🍊(NEKO Team)
2026-04-03 04:17:43 +00:00

85 lines
2.5 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest'
import { createMockKv, createMockCfApi, makeRequest } from './setup.js'
import { WorkerPool } from '../src/backend/worker-pool.js'
import { AuthModule } from '../src/auth.js'
import { KvStore } from '../src/kv.js'
import { handleRequest } from '../src/router.js'
describe('S1: 部署能力', () => {
let mockKv: KVNamespace
let mockCf: ReturnType<typeof createMockCfApi>
let pool: WorkerPool
let auth: AuthModule
let kv: KvStore
beforeEach(async () => {
mockKv = createMockKv()
mockCf = createMockCfApi()
pool = new WorkerPool(mockKv, mockCf.cfApi)
kv = new KvStore(mockKv)
auth = new AuthModule(kv)
// Register agent
await auth.registerAgent('xiaoju', 'token-xiaoju')
})
it('should deploy a capability via API', async () => {
const req = makeRequest('POST', '/_api/deploy', {
token: 'token-xiaoju',
body: {
agent: 'xiaoju',
name: 'ping',
code: "export default { fetch() { return new Response('pong') } }",
type: 'normal',
},
})
const resp = await handleRequest(req, { SIGIL_KV: mockKv, backend: pool, auth, kv })
expect(resp.status).toBe(201)
const body = await resp.json() as {
capability: string
url: string
cold_start: boolean
}
expect(body.capability).toBe('xiaoju--ping')
expect(body.url).toBe('https://sigil.shazhou.workers.dev/xiaoju/ping')
expect(body.cold_start).toBe(false)
})
it('should call CfApi.deployWorker', async () => {
await pool.deploy({
agent: 'xiaoju',
name: 'ping',
code: "export default { fetch() { return new Response('pong') } }",
type: 'normal',
})
expect(mockCf.deployCalls()).toContain('s-xiaoju-ping')
})
it('should write KV entries (code, meta, lru, route)', async () => {
await pool.deploy({
agent: 'xiaoju',
name: 'ping',
code: "export default { fetch() { return new Response('pong') } }",
type: 'normal',
})
const code = await kv.getCode('xiaoju--ping')
expect(code).toBeTruthy()
const meta = await kv.getMeta('xiaoju--ping')
expect(meta?.agent).toBe('xiaoju')
expect(meta?.name).toBe('ping')
expect(meta?.type).toBe('normal')
const lru = await kv.getLru('xiaoju--ping')
expect(lru?.deployed).toBe(true)
expect(lru?.access_count).toBe(0)
const route = await kv.getRoute('xiaoju--ping')
expect(route?.worker_name).toBe('s-xiaoju-ping')
})
})