diff --git a/server/src/engine/orchestrator.test.ts b/server/src/engine/orchestrator.test.ts index 9aac794..98d9ffd 100644 --- a/server/src/engine/orchestrator.test.ts +++ b/server/src/engine/orchestrator.test.ts @@ -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).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).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).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)