Files
sigil/test/s13-deploy-cooldown.test.ts
T
xiaoju ce4c2b7b36 refactor: migrate from CF API worker scripts to Dynamic Workers — 小橘 🍊
- Added worker_loaders binding (LOADER) to wrangler.toml
- Updated WorkerPool to use LOADER.get() instead of CF API deploy/delete
- Removed subdomain-based invoke; now uses Dynamic Workers directly
- Cleaned up config.ts (removed SUBDOMAIN_SUFFIX, PAGE_RATE_LIMIT)
- Simplified cf-api.ts to legacy cleanup only (LegacyCfApi)
- Updated all tests to use createMockLoader instead of createMockCfApi
- Removed PageRateLimitError (no longer needed)
- All API endpoints unchanged; migration is internal implementation only
2026-04-03 09:41:13 +00:00

86 lines
2.8 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest'
import { createMockKv, createMockLoader, 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('S13: deploy_cooldown', () => {
let mockKv: KVNamespace
let mockLoader: ReturnType<typeof createMockLoader>
let mockEmbed: MockEmbeddingService
let pool: WorkerPool
let auth: AuthModule
let kv: KvStore
beforeEach(async () => {
mockKv = createMockKv()
mockLoader = createMockLoader()
mockEmbed = new MockEmbeddingService()
pool = new WorkerPool(mockKv, mockLoader.cfApi, mockEmbed as any)
kv = new KvStore(mockKv)
auth = new AuthModule(kv)
await auth.setToken('deploy-token')
})
it('should reject rapid second deploy with 429', async () => {
// First deploy
const req1 = makeRequest('POST', '/_api/deploy', {
token: 'deploy-token',
body: {
name: 'ping',
code: '// ping',
type: 'normal',
},
})
const resp1 = await handleRequest(req1, { SIGIL_KV: mockKv, backend: pool, auth, kv })
expect(resp1.status).toBe(201)
// Immediate second deploy (< 5s cooldown)
const req2 = makeRequest('POST', '/_api/deploy', {
token: 'deploy-token',
body: {
name: 'ping2',
code: '// ping2',
type: 'normal',
},
})
const resp2 = await handleRequest(req2, { SIGIL_KV: mockKv, backend: pool, auth, kv })
expect(resp2.status).toBe(429)
})
it('should include retry_after in 429 response', async () => {
// First deploy
const req1 = makeRequest('POST', '/_api/deploy', {
token: 'deploy-token',
body: { name: 'ping', code: '// ping', type: 'normal' },
})
await handleRequest(req1, { SIGIL_KV: mockKv, backend: pool, auth, kv })
// Immediate second
const req2 = makeRequest('POST', '/_api/deploy', {
token: 'deploy-token',
body: { name: 'ping2', code: '// ping2', type: 'normal' },
})
const resp2 = await handleRequest(req2, { SIGIL_KV: mockKv, backend: pool, auth, kv })
const body = await resp2.json() as { error: string; retry_after: number }
expect(body.retry_after).toBeGreaterThan(0)
expect(body.retry_after).toBeLessThanOrEqual(5)
})
it('should allow deploy after cooldown expires', async () => {
// Manually set last deploy time as already expired
await kv.setLastDeployTime(Date.now() - 10000) // 10s ago, past 5s cooldown
const req = makeRequest('POST', '/_api/deploy', {
token: 'deploy-token',
body: { name: 'ping', code: '// ping', type: 'normal' },
})
const resp = await handleRequest(req, { SIGIL_KV: mockKv, backend: pool, auth, kv })
expect(resp.status).toBe(201)
})
})