| import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| import { supabase } from '../supabase'; |
|
|
| vi.mock('../supabase', () => { |
| return { |
| supabase: { |
| functions: { |
| invoke: vi.fn(), |
| }, |
| }, |
| }; |
| }); |
|
|
| |
| const mockFetch = vi.fn(); |
| global.fetch = mockFetch; |
|
|
| |
| import { invokeEdgeFunction, setUseGoGateway, setGoGatewayUrl } from '../apiAdapter'; |
|
|
| describe('apiAdapter - Go Gateway Strangler Pattern', () => { |
| beforeEach(() => { |
| vi.clearAllMocks(); |
| setUseGoGateway('verify-receipt', false); |
| setGoGatewayUrl('http://localhost:8080'); |
| }); |
|
|
| it('routes to Supabase Deno Edge Function when USE_GO_GATEWAY is false', async () => { |
| |
| vi.mocked(supabase.functions.invoke).mockResolvedValue({ |
| data: { |
| success: true, |
| co2Reduction: 21, |
| confidence: 0.95, |
| }, |
| error: null, |
| } as any); |
|
|
| |
| const res = await invokeEdgeFunction<{ text: string }, { success: boolean; co2Reduction: number }>('verify-receipt', { |
| text: 'receipt-data', |
| }); |
|
|
| |
| expect(supabase.functions.invoke).toHaveBeenCalledWith('verify-receipt', { |
| body: { text: 'receipt-data' }, |
| }); |
| expect(mockFetch).not.toHaveBeenCalled(); |
| expect(res.success).toBe(true); |
| expect(res.co2Reduction).toBe(21); |
| }); |
|
|
| it('routes to Go Gateway via fetch when USE_GO_GATEWAY is true', async () => { |
| |
| setUseGoGateway('verify-receipt', true); |
| setGoGatewayUrl('https://gateway.carboany.com'); |
|
|
| |
| mockFetch.mockResolvedValue({ |
| ok: true, |
| json: vi.fn().mockResolvedValue({ |
| success: true, |
| co2Reduction: 33, |
| confidence: 0.88, |
| }), |
| } as any); |
|
|
| |
| const res = await invokeEdgeFunction<{ text: string }, { success: boolean; co2Reduction: number }>('verify-receipt', { |
| text: 'receipt-data', |
| }); |
|
|
| |
| expect(supabase.functions.invoke).not.toHaveBeenCalled(); |
| expect(mockFetch).toHaveBeenCalledWith('https://gateway.carboany.com/api/v1/verify-receipt', expect.objectContaining({ |
| method: 'POST', |
| body: JSON.stringify({ text: 'receipt-data' }), |
| })); |
| expect(res.success).toBe(true); |
| expect(res.co2Reduction).toBe(33); |
| }); |
|
|
| it('throws structured exception when response data contract is broken', async () => { |
| |
| setUseGoGateway('verify-receipt', true); |
|
|
| |
| mockFetch.mockResolvedValue({ |
| ok: true, |
| json: vi.fn().mockResolvedValue({ |
| success: true, |
| |
| confidence: 0.99, |
| }), |
| } as any); |
|
|
| |
| await expect( |
| invokeEdgeFunction<{ text: string }, { success: boolean; co2Reduction: number }>('verify-receipt', { |
| text: 'receipt-data', |
| }) |
| ).rejects.toThrow( |
| '[apiAdapter] API κ³μ½ κ²μ¦ μ€ν¨: verify-receipt μλ΅μ νμ νλ(co2Reduction)κ° λλ½λμμ΅λλ€.' |
| ); |
| }); |
| }); |
|
|