Files
sigil/test/s07-list.test.ts
T
xiaoju c3f3b822f1 feat: embedding semantic search + MMR for explore
- Use CF Workers AI bge-base-en-v1.5 for embeddings
- Deploy stores capability embedding in KV
- Query uses cosine similarity (find) and MMR (explore)
- Query embedding cached in KV (1h TTL)
- Fallback to string matching for capabilities without embeddings
- Mock embedding service for unit tests
2026-04-03 08:16:27 +00:00

70 lines
2.2 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest'
import { createMockKv, createMockCfApi, makeRequest, MockEmbeddingService } 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('S7: 列出能力(已迁移至 query 接口)', () => {
let mockKv: KVNamespace
let mockCf: ReturnType<typeof createMockCfApi>
let mockEmbed: MockEmbeddingService
let pool: WorkerPool
let auth: AuthModule
let kv: KvStore
beforeEach(async () => {
mockKv = createMockKv()
mockCf = createMockCfApi()
mockEmbed = new MockEmbeddingService()
pool = new WorkerPool(mockKv, mockCf.cfApi, mockEmbed as any)
kv = new KvStore(mockKv)
auth = new AuthModule(kv)
await auth.setToken('deploy-token')
// Deploy some capabilities (keep <= MAX_SLOTS=3 to avoid eviction)
for (const name of ['ping', 'echo', 'hello']) {
await pool.deploy({
name,
code: `// ${name}`,
type: 'normal',
})
}
})
it('/_api/list should return 404 (removed)', async () => {
const req = makeRequest('GET', '/_api/list', {
token: 'deploy-token',
})
const resp = await handleRequest(req, { SIGIL_KV: mockKv, backend: pool, auth, kv })
expect(resp.status).toBe(404)
})
it('/_api/query should return all capabilities (explore mode)', async () => {
const req = makeRequest('GET', '/_api/query')
const resp = await handleRequest(req, { SIGIL_KV: mockKv, backend: pool, auth, kv })
expect(resp.status).toBe(200)
const body = await resp.json() as { total: number; items: Array<{ capability: string }> }
expect(body.total).toBe(3)
expect(body.items).toHaveLength(3)
const names = body.items.map((c: { capability: string }) => c.capability)
expect(names).toContain('ping')
expect(names).toContain('echo')
expect(names).toContain('hello')
})
it('should include capability metadata in query results', async () => {
const result = await pool.query({})
expect(result.total).toBe(3)
for (const item of result.items) {
expect(item.type).toBe('normal')
expect(item.score).toBeGreaterThan(0)
}
})
})