|
| 1 | +import { assertEquals, assertExists } from 'https://deno.land/[email protected]/assert/mod.ts' |
| 2 | +import { createClient, RealtimeChannel, SupabaseClient } from '@supabase/supabase-js' |
| 3 | + |
| 4 | +// These tests assume that a local Supabase server is already running |
| 5 | +// Start a local Supabase instance with 'supabase start' before running these tests |
| 6 | +Deno.test('Supabase Integration Tests', async (t) => { |
| 7 | + // Default local dev credentials from Supabase CLI |
| 8 | + const SUPABASE_URL = 'http://127.0.0.1:54321' |
| 9 | + const ANON_KEY = |
| 10 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' |
| 11 | + |
| 12 | + const supabase = createClient(SUPABASE_URL, ANON_KEY, { |
| 13 | + realtime: { heartbeatIntervalMs: 500 }, |
| 14 | + }) |
| 15 | + |
| 16 | + await t.step('should connect to Supabase instance', () => { |
| 17 | + assertExists(supabase) |
| 18 | + assertEquals(supabase instanceof SupabaseClient, true) |
| 19 | + }) |
| 20 | + |
| 21 | + await t.step('PostgREST - should query data from public schema', async () => { |
| 22 | + const { data, error } = await supabase.from('todos').select('*').limit(5) |
| 23 | + |
| 24 | + // The default schema includes a 'todos' table, but it might be empty |
| 25 | + assertEquals(error, null) |
| 26 | + assertEquals(Array.isArray(data), true) |
| 27 | + }) |
| 28 | + |
| 29 | + await t.step('PostgREST - should create and delete a todo', async () => { |
| 30 | + // Create a new todo |
| 31 | + const { data: createdTodo, error: createError } = await supabase |
| 32 | + .from('todos') |
| 33 | + .insert({ task: 'Integration Test Todo', is_complete: false }) |
| 34 | + .select() |
| 35 | + .single() |
| 36 | + |
| 37 | + assertEquals(createError, null) |
| 38 | + assertExists(createdTodo) |
| 39 | + assertEquals(createdTodo!.task, 'Integration Test Todo') |
| 40 | + assertEquals(createdTodo!.is_complete, false) |
| 41 | + |
| 42 | + // Delete the created todo |
| 43 | + const { error: deleteError } = await supabase.from('todos').delete().eq('id', createdTodo!.id) |
| 44 | + assertEquals(deleteError, null) |
| 45 | + |
| 46 | + // Verify the todo was deleted |
| 47 | + const { data: fetchedTodo, error: fetchError } = await supabase |
| 48 | + .from('todos') |
| 49 | + .select('*') |
| 50 | + .eq('id', createdTodo!.id) |
| 51 | + .single() |
| 52 | + |
| 53 | + assertExists(fetchError) |
| 54 | + assertEquals(fetchedTodo, null) |
| 55 | + }) |
| 56 | + |
| 57 | + await t.step('Authentication - should sign up a user', async () => { |
| 58 | + const email = `test-${Date.now()}@example.com` |
| 59 | + const password = 'password123' |
| 60 | + |
| 61 | + const { data, error } = await supabase.auth.signUp({ |
| 62 | + email, |
| 63 | + password, |
| 64 | + }) |
| 65 | + |
| 66 | + assertEquals(error, null) |
| 67 | + assertExists(data.user) |
| 68 | + assertEquals(data.user!.email, email) |
| 69 | + |
| 70 | + // Clean up by signing out the user |
| 71 | + await supabase.auth.signOut() |
| 72 | + }) |
| 73 | + |
| 74 | + await t.step('Realtime - is able to connect and broadcast', async () => { |
| 75 | + const channelName = `channel-${crypto.randomUUID()}` |
| 76 | + let channel: RealtimeChannel |
| 77 | + const email = `test-${Date.now()}@example.com` |
| 78 | + const password = 'password123' |
| 79 | + |
| 80 | + await supabase.auth.signUp({ email, password }) |
| 81 | + const config = { broadcast: { self: true }, private: true } |
| 82 | + channel = supabase.channel(channelName, { config }) |
| 83 | + await supabase.realtime.setAuth() |
| 84 | + |
| 85 | + const testMessage = { message: 'test' } |
| 86 | + let receivedMessage: any |
| 87 | + let subscribed = false |
| 88 | + let attempts = 0 |
| 89 | + |
| 90 | + channel |
| 91 | + .on('broadcast', { event: '*' }, (payload) => (receivedMessage = payload)) |
| 92 | + .subscribe((status) => { |
| 93 | + if (status == 'SUBSCRIBED') subscribed = true |
| 94 | + }) |
| 95 | + |
| 96 | + // Wait for subscription |
| 97 | + while (!subscribed) { |
| 98 | + if (attempts > 50) throw new Error('Timeout waiting for subscription') |
| 99 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 100 | + attempts++ |
| 101 | + } |
| 102 | + |
| 103 | + attempts = 0 |
| 104 | + |
| 105 | + channel.send({ type: 'broadcast', event: 'test-event', payload: testMessage }) |
| 106 | + |
| 107 | + // Wait on message |
| 108 | + while (!receivedMessage) { |
| 109 | + if (attempts > 50) throw new Error('Timeout waiting for message') |
| 110 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 111 | + attempts++ |
| 112 | + } |
| 113 | + |
| 114 | + assertExists(receivedMessage) |
| 115 | + assertEquals(supabase.realtime.getChannels().length, 1) |
| 116 | + |
| 117 | + await supabase.removeAllChannels() |
| 118 | + }) |
| 119 | +}) |
0 commit comments