|
| 1 | +import { assertEquals, assertExists } from 'https://deno.land/[email protected]/assert/mod.ts' |
| 2 | +import { createClient } from '../../dist/module/index.js' |
| 3 | + |
| 4 | +// These tests are for integration testing with actual deployed edge functions |
| 5 | +// To run these tests, you need to: |
| 6 | +// 1. Deploy the edge functions to a Supabase project |
| 7 | +// 2. Set the SUPABASE_URL and SUPABASE_ANON_KEY environment variables |
| 8 | +// 3. Or use the local development credentials below |
| 9 | + |
| 10 | +Deno.test( |
| 11 | + 'Edge Functions Integration Tests', |
| 12 | + { sanitizeOps: false, sanitizeResources: false }, |
| 13 | + async (t) => { |
| 14 | + // Use environment variables or fall back to local development |
| 15 | + const SUPABASE_URL = Deno.env.get('SUPABASE_URL') || 'http://127.0.0.1:54321' |
| 16 | + const ANON_KEY = |
| 17 | + Deno.env.get('SUPABASE_ANON_KEY') || |
| 18 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' |
| 19 | + |
| 20 | + const supabase = createClient(SUPABASE_URL, ANON_KEY, { |
| 21 | + realtime: { heartbeatIntervalMs: 500 }, |
| 22 | + }) |
| 23 | + |
| 24 | + try { |
| 25 | + await t.step('hello function - should return greeting with name', async () => { |
| 26 | + const { data, error } = await supabase.functions.invoke('hello', { |
| 27 | + body: { name: 'Test User' }, |
| 28 | + }) |
| 29 | + |
| 30 | + assertEquals(error, null) |
| 31 | + assertExists(data) |
| 32 | + assertEquals(typeof data.message, 'string') |
| 33 | + assertEquals(data.message, 'Hello Test User!') |
| 34 | + assertEquals(typeof data.timestamp, 'string') |
| 35 | + }) |
| 36 | + |
| 37 | + await t.step('hello function - should return default greeting without name', async () => { |
| 38 | + const { data, error } = await supabase.functions.invoke('hello', { |
| 39 | + body: {}, |
| 40 | + }) |
| 41 | + |
| 42 | + assertEquals(error, null) |
| 43 | + assertExists(data) |
| 44 | + assertEquals(typeof data.message, 'string') |
| 45 | + assertEquals(data.message, 'Hello World!') |
| 46 | + assertEquals(typeof data.timestamp, 'string') |
| 47 | + }) |
| 48 | + |
| 49 | + await t.step('echo function - should echo request body', async () => { |
| 50 | + const testData = { |
| 51 | + message: 'Hello Echo!', |
| 52 | + number: 42, |
| 53 | + array: [1, 2, 3], |
| 54 | + nested: { key: 'value' }, |
| 55 | + } |
| 56 | + |
| 57 | + const { data, error } = await supabase.functions.invoke('echo', { |
| 58 | + body: testData, |
| 59 | + }) |
| 60 | + |
| 61 | + assertEquals(error, null) |
| 62 | + assertExists(data) |
| 63 | + assertEquals(data.echo, testData) |
| 64 | + assertEquals(typeof data.method, 'string') |
| 65 | + assertEquals(typeof data.url, 'string') |
| 66 | + assertEquals(typeof data.timestamp, 'string') |
| 67 | + }) |
| 68 | + |
| 69 | + await t.step('status function - should return system status', async () => { |
| 70 | + const { data, error } = await supabase.functions.invoke('status', { |
| 71 | + body: {}, |
| 72 | + }) |
| 73 | + |
| 74 | + assertEquals(error, null) |
| 75 | + assertExists(data) |
| 76 | + assertEquals(data.status, 'ok') |
| 77 | + assertEquals(typeof data.timestamp, 'string') |
| 78 | + assertEquals(typeof data.environment, 'string') |
| 79 | + assertEquals(data.version, '1.0.0') |
| 80 | + assertEquals(typeof data.uptime, 'number') |
| 81 | + }) |
| 82 | + |
| 83 | + await t.step('should handle non-existent function', async () => { |
| 84 | + const { data, error } = await supabase.functions.invoke('non-existent-function', { |
| 85 | + body: {}, |
| 86 | + }) |
| 87 | + |
| 88 | + assertExists(error) |
| 89 | + assertEquals(data, null) |
| 90 | + }) |
| 91 | + |
| 92 | + await t.step('should handle concurrent function calls', async () => { |
| 93 | + const promises = Array.from({ length: 5 }, (_, i) => |
| 94 | + supabase.functions.invoke('hello', { |
| 95 | + body: { name: `Concurrent Test ${i}` }, |
| 96 | + }) |
| 97 | + ) |
| 98 | + |
| 99 | + const results = await Promise.all(promises) |
| 100 | + |
| 101 | + // Check if any functions are deployed |
| 102 | + const hasDeployedFunctions = results.some(({ error }) => !error) |
| 103 | + |
| 104 | + if (!hasDeployedFunctions) { |
| 105 | + console.log('No functions deployed, skipping concurrent execution test') |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + results.forEach(({ data, error }) => { |
| 110 | + if (!error) { |
| 111 | + assertEquals(error, null) |
| 112 | + assertExists(data) |
| 113 | + assertEquals(typeof data.message, 'string') |
| 114 | + assertEquals(typeof data.timestamp, 'string') |
| 115 | + } |
| 116 | + }) |
| 117 | + }) |
| 118 | + |
| 119 | + await t.step('should handle function errors gracefully', async () => { |
| 120 | + const { data, error } = await supabase.functions.invoke('hello', { |
| 121 | + body: 'invalid json', |
| 122 | + }) |
| 123 | + |
| 124 | + assertExists(error) |
| 125 | + assertEquals(data, null) |
| 126 | + }) |
| 127 | + } catch (error) { |
| 128 | + console.error('Test error:', error) |
| 129 | + throw error |
| 130 | + } |
| 131 | + } |
| 132 | +) |
0 commit comments