ATlast — you'll never need to find your favorites on another platform again. Find your favs in the ATmosphere.
atproto
16
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(api): correct middleware test expectations

- add errorHandler for proper 401s to auth
- fix null throw & erorr-handler-throws edge cases in error
- match test env limits in rateLimit

byarielm.fyi 9f36b567 9e341e4f

verified
+27 -15
+2
packages/api/src/middleware/auth.test.ts
··· 6 6 import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; 7 7 import { Hono } from 'hono'; 8 8 import { authMiddleware, extractSessionId } from './auth'; 9 + import { errorHandler } from './error'; 9 10 import { createTestSession, deleteTestSession } from '../../__tests__/fixtures'; 10 11 11 12 describe('Auth Middleware', () => { ··· 13 14 14 15 beforeEach(() => { 15 16 app = new Hono(); 17 + app.onError(errorHandler); 16 18 app.use('/protected/*', authMiddleware); 17 19 app.get('/protected/test', (c) => { 18 20 // Access context variables added by authMiddleware
+17 -9
packages/api/src/middleware/error.test.ts
··· 379 379 describe('Edge Cases', () => { 380 380 it('handles null errors', async () => { 381 381 app.get('/test', () => { 382 - // Testing null throw 382 + // Testing null throw - Hono propagates non-Error values as-is 383 383 throw null; // eslint-disable-line @typescript-eslint/no-throw-literal 384 384 }); 385 385 386 - const res = await app.request('/test'); 387 - 388 - expect(res.status).toBe(500); 386 + // Hono doesn't catch non-Error throws, so request() rejects 387 + try { 388 + await app.request('/test'); 389 + expect.fail('should have thrown'); 390 + } catch (e) { 391 + expect(e).toBeNull(); 392 + } 389 393 }); 390 394 391 395 it('handles errors thrown during error handling', async () => { 392 - // This is a meta-test: what if error handler itself throws? 393 - // The framework should catch it 394 - 396 + // Meta-test: what if error handler itself throws? 395 397 const badErrorHandler = (err: Error, c: Context) => { 396 398 throw new Error('Error handler error'); 397 399 }; ··· 402 404 throw new Error('Original error'); 403 405 }); 404 406 405 - // This should not hang or crash 406 - await expect(testApp.request('/test')).resolves.toBeDefined(); 407 + // Hono propagates error handler failures, so request() rejects 408 + try { 409 + await testApp.request('/test'); 410 + expect.fail('should have thrown'); 411 + } catch (e) { 412 + expect(e).toBeInstanceOf(Error); 413 + expect((e as Error).message).toBe('Error handler error'); 414 + } 407 415 }); 408 416 409 417 it('handles circular references in error details', async () => {
+8 -6
packages/api/src/middleware/rateLimit.test.ts
··· 388 388 }); 389 389 390 390 describe('Predefined Rate Limiters', () => { 391 - it('apiRateLimit: 60 requests per minute', async () => { 391 + // In test environment, limits are elevated to avoid flaky tests 392 + // Production: 60/10/100, Test: 1000/100/1000 393 + it('apiRateLimit: elevated limit in test env (1000 req/min)', async () => { 392 394 app.use('/api/*', apiRateLimit); 393 395 app.get('/api/test', (c) => c.json({ success: true })); 394 396 ··· 397 399 }); 398 400 399 401 expect(res.status).toBe(200); 400 - expect(res.headers.get('X-RateLimit-Limit')).toBe('60'); 402 + expect(res.headers.get('X-RateLimit-Limit')).toBe('1000'); 401 403 }); 402 404 403 - it('searchRateLimit: 10 requests per minute', async () => { 405 + it('searchRateLimit: elevated limit in test env (100 req/min)', async () => { 404 406 app.use('/search', searchRateLimit); 405 407 app.post('/search', (c) => c.json({ success: true })); 406 408 ··· 410 412 }); 411 413 412 414 expect(res.status).toBe(200); 413 - expect(res.headers.get('X-RateLimit-Limit')).toBe('10'); 415 + expect(res.headers.get('X-RateLimit-Limit')).toBe('100'); 414 416 }); 415 417 416 - it('followRateLimit: 100 requests per hour', async () => { 418 + it('followRateLimit: elevated limit in test env (1000 req/hr)', async () => { 417 419 app.use('/follow', followRateLimit); 418 420 app.post('/follow', (c) => c.json({ success: true })); 419 421 ··· 423 425 }); 424 426 425 427 expect(res.status).toBe(200); 426 - expect(res.headers.get('X-RateLimit-Limit')).toBe('100'); 428 + expect(res.headers.get('X-RateLimit-Limit')).toBe('1000'); 427 429 }); 428 430 }); 429 431