tests: expand coverage for poller, rate limiter, ombi decoration, downloads UI, and SSE streaming lifecycle (closes #60)
- Add tests/unit/utils/poller.test.js covering background polling lock, registry, error recovery, webhook bypasses, and global fallbacks - Add tests/integration/rateLimiter.test.js verifying 429 response rate-limiting in an isolated production environment - Add tests/integration/ombiDecoration.test.js covering deep links and admin role checks - Expand tests/frontend/ui/downloads.test.js covering createServiceIcons() and createClientLogo() fallbacks - Expand tests/integration/dashboard.test.js verifying SSE heartbeats, payload schema contract, and listener cleanup on client disconnect
This commit is contained in:
@@ -1134,5 +1134,88 @@ describe('GET /api/dashboard/stream — SSE with Ombi showAll filtering', () =>
|
||||
expect(data.ombiRequests.movie).toHaveLength(2);
|
||||
expect(data.ombiRequests.tv).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('verifies SSE payload structure contract against the frontend schema', async () => {
|
||||
const { cookies } = await loginAs(appInstance);
|
||||
const res = await request(appInstance)
|
||||
.get('/api/dashboard/stream')
|
||||
.query({ testClose: 'true' })
|
||||
.set('Cookie', cookies);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const text = res.text;
|
||||
expect(text).toContain('data:');
|
||||
|
||||
const dataStr = text.substring(text.indexOf('{'));
|
||||
const data = JSON.parse(dataStr.trim());
|
||||
|
||||
// Payload Contract Validation
|
||||
expect(data).toHaveProperty('user');
|
||||
expect(data).toHaveProperty('isAdmin');
|
||||
expect(data).toHaveProperty('downloads');
|
||||
expect(data).toHaveProperty('downloadClients');
|
||||
expect(data).toHaveProperty('ombiRequests');
|
||||
expect(data).toHaveProperty('ombiBaseUrl');
|
||||
|
||||
expect(Array.isArray(data.downloads)).toBe(true);
|
||||
expect(Array.isArray(data.downloadClients)).toBe(true);
|
||||
expect(Array.isArray(data.ombiRequests.movie)).toBe(true);
|
||||
expect(Array.isArray(data.ombiRequests.tv)).toBe(true);
|
||||
});
|
||||
|
||||
it('sends heartbeat comment over active stream and cleans up on close', async () => {
|
||||
vi.useFakeTimers();
|
||||
|
||||
// 1. Get the route handler from the dashboard router stack
|
||||
const dashboardRouter = require('../../server/routes/dashboard.js');
|
||||
const route = dashboardRouter.stack.find(layer => layer.route && layer.route.path === '/stream');
|
||||
// Get the final handler (after requireAuth middleware)
|
||||
const streamHandler = route.route.stack[route.route.stack.length - 1].handle;
|
||||
|
||||
// 2. Setup mock req and res
|
||||
const mockUser = { name: 'Alice', isAdmin: false };
|
||||
const reqOnCallbacks = {};
|
||||
const mockReq = {
|
||||
user: mockUser,
|
||||
query: { showAll: 'false', testClose: 'false' },
|
||||
on: vi.fn((event, cb) => {
|
||||
reqOnCallbacks[event] = cb;
|
||||
})
|
||||
};
|
||||
|
||||
const resWrites = [];
|
||||
const mockRes = {
|
||||
setHeader: vi.fn(),
|
||||
flushHeaders: vi.fn(),
|
||||
write: vi.fn((data) => {
|
||||
resWrites.push(data);
|
||||
}),
|
||||
end: vi.fn()
|
||||
};
|
||||
|
||||
// 3. Call the handler
|
||||
await streamHandler(mockReq, mockRes);
|
||||
|
||||
// Initial payload should be written
|
||||
expect(resWrites.length).toBeGreaterThan(0);
|
||||
expect(resWrites[0]).toContain('data:');
|
||||
|
||||
// 4. Advance time by 25s to trigger the heartbeat setInterval
|
||||
vi.advanceTimersByTime(25000);
|
||||
|
||||
// Check that heartbeat was written
|
||||
expect(resWrites).toContain(': heartbeat\n\n');
|
||||
|
||||
// 5. Simulate client disconnect by triggering the 'close' event callback
|
||||
expect(reqOnCallbacks['close']).toBeDefined();
|
||||
reqOnCallbacks['close']();
|
||||
|
||||
// Check that advancing time again does NOT write another heartbeat
|
||||
const beforeLength = resWrites.length;
|
||||
vi.advanceTimersByTime(25000);
|
||||
expect(resWrites.length).toBe(beforeLength); // No new writes!
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user