test: verify orphaned fight cleanup cancels stale live fights

Add 3 tests for cleanupOrphanedFights: verifies db.update sets
status='cancelled' with endedAt on stale live fights, returns 0
on success, and propagates DB errors correctly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dorian
2026-03-13 09:49:27 +00:00
co-authored by Claude Opus 4.6
parent 9b0d251d1c
commit 023a1a58a9
+40
View File
@@ -52,6 +52,7 @@ const {
getActiveFighterCount,
isMockBot,
isAllowedWebhookUrl,
cleanupOrphanedFights,
} = await import('./orchestrator.js')
describe('orchestrator utility functions', () => {
@@ -75,6 +76,45 @@ describe('orchestrator utility functions', () => {
})
})
describe('cleanupOrphanedFights', () => {
it('calls db.update to cancel stale live fights', async () => {
const { db } = await import('../db/index.js')
const mockWhere = vi.fn().mockResolvedValue(undefined)
const mockSet = vi.fn().mockReturnValue({ where: mockWhere });
(db.update as ReturnType<typeof vi.fn>).mockReturnValue({ set: mockSet })
await cleanupOrphanedFights()
// Verify db.update was called
expect(db.update).toHaveBeenCalled()
// Verify set was called with cancelled status and endedAt
expect(mockSet).toHaveBeenCalledWith(
expect.objectContaining({ status: 'cancelled', endedAt: expect.any(String) }),
)
// Verify where clause was applied (filters live + old)
expect(mockWhere).toHaveBeenCalled()
})
it('returns 0 (placeholder) on success', async () => {
const { db } = await import('../db/index.js')
const mockWhere = vi.fn().mockResolvedValue(undefined)
const mockSet = vi.fn().mockReturnValue({ where: mockWhere });
(db.update as ReturnType<typeof vi.fn>).mockReturnValue({ set: mockSet })
const result = await cleanupOrphanedFights()
expect(result).toBe(0)
})
it('throws if db.update fails', async () => {
const { db } = await import('../db/index.js')
const mockWhere = vi.fn().mockRejectedValue(new Error('DB locked'));
const mockSet = vi.fn().mockReturnValue({ where: mockWhere });
(db.update as ReturnType<typeof vi.fn>).mockReturnValue({ set: mockSet })
await expect(cleanupOrphanedFights()).rejects.toThrow('DB locked')
})
})
describe('isAllowedWebhookUrl — SSRF protection', () => {
it('blocks localhost variants', () => {
expect(isAllowedWebhookUrl('http://localhost/webhook')).toBe(false)