An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Add tests for guest limits and ephemeral streaming
Yoshiki Miura4a881385608e
diff --git a/lib/rate-limit/__tests__/guest-limit.test.ts b/lib/rate-limit/__tests__/guest-limit.test.tsnew file mode 100644index 00000000..a1616d94--- /dev/null+++ b/lib/rate-limit/__tests__/guest-limit.test.ts@@ -0,0 +1,61 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'++import { checkAndEnforceGuestLimit } from '@/lib/rate-limit/guest-limit'++const mockRedisIncr = vi.fn()+const mockRedisExpire = vi.fn()++vi.mock('@upstash/redis', () => ({+ Redis: vi.fn().mockImplementation(() => ({+ incr: mockRedisIncr,+ expire: mockRedisExpire+ }))+}))++describe('checkAndEnforceGuestLimit', () => {+ beforeEach(() => {+ mockRedisIncr.mockReset()+ mockRedisExpire.mockReset()+ process.env.MORPHIC_CLOUD_DEPLOYMENT = 'true'+ process.env.UPSTASH_REDIS_REST_URL = 'https://example.com'+ process.env.UPSTASH_REDIS_REST_TOKEN = 'token'+ delete process.env.GUEST_CHAT_DAILY_LIMIT+ })++ it('returns null when ip is missing', async () => {+ const response = await checkAndEnforceGuestLimit(null)+ expect(response).toBeNull()+ })++ it('returns 401 when over the default limit', async () => {+ mockRedisIncr.mockResolvedValue(11)+ mockRedisExpire.mockResolvedValue(1)++ const response = await checkAndEnforceGuestLimit('1.2.3.4')+ expect(response).not.toBeNull()+ expect(response?.status).toBe(401)+ const body = await response!.json()+ expect(body.error).toBe('Please sign in to continue.')+ expect(body.limit).toBe(10)+ })++ it('uses configured limit when set', async () => {+ process.env.GUEST_CHAT_DAILY_LIMIT = '5'+ mockRedisIncr.mockResolvedValue(6)+ mockRedisExpire.mockResolvedValue(1)++ const response = await checkAndEnforceGuestLimit('5.6.7.8')+ expect(response).not.toBeNull()+ expect(response?.status).toBe(401)+ const body = await response!.json()+ expect(body.limit).toBe(5)+ })++ it('allows request under the limit', async () => {+ mockRedisIncr.mockResolvedValue(3)+ mockRedisExpire.mockResolvedValue(1)++ const response = await checkAndEnforceGuestLimit('9.9.9.9')+ expect(response).toBeNull()+ })+})diff --git a/lib/streaming/__tests__/create-ephemeral-chat-stream-response.test.ts b/lib/streaming/__tests__/create-ephemeral-chat-stream-response.test.tsnew file mode 100644index 00000000..8f3a6aa5--- /dev/null+++ b/lib/streaming/__tests__/create-ephemeral-chat-stream-response.test.ts@@ -0,0 +1,19 @@+import { describe, expect, it } from 'vitest'++import { createEphemeralChatStreamResponse } from '@/lib/streaming/create-ephemeral-chat-stream-response'++describe('createEphemeralChatStreamResponse', () => {+ it('returns 400 when messages are missing', async () => {+ const response = await createEphemeralChatStreamResponse({+ messages: [],+ model: { providerId: 'openai', id: 'gpt-4o-mini' } as any,+ abortSignal: new AbortController().signal,+ searchMode: 'quick',+ modelType: 'speed'+ })++ expect(response.status).toBe(400)+ const text = await response.text()+ expect(text).toBe('messages are required')+ })+})