@@ -3,12 +3,11 @@ import { assertEquals } from 'https://deno.land/
[email protected] /testing/asserts.ts'
33import { describe , it , beforeAll , afterAll } from 'https://deno.land/[email protected] /testing/bdd.ts' 44import { Browser , Page , launch } from 'npm:[email protected] ' 55import { sleep } from 'https://deno.land/x/sleep/mod.ts'
6- // Run the UMD build before serving the page
6+
77const stderr = 'inherit'
88const ac = new AbortController ( )
99
1010let browser : Browser
11- let page : Page
1211
1312const port = 8000
1413const content = `<html>
@@ -22,23 +21,68 @@ const content = `<html>
2221 <script type="text/babel" data-presets="env,react">
2322 const SUPABASE_URL = 'http://127.0.0.1:54321'
2423 const ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0'
25- const supabase = supabase.createClient(SUPABASE_URL, ANON_KEY)
26- const App = (props) => {
24+
25+ // Get vsn from query params
26+ const urlParams = new URLSearchParams(window.location.search)
27+ const vsn = urlParams.get('vsn') || '1.0.0'
28+
29+ const supabase = window.supabase.createClient(SUPABASE_URL, ANON_KEY, {
30+ realtime: {
31+ heartbeatIntervalMs: 500,
32+ vsn: vsn
33+ }
34+ })
35+
36+ const App = () => {
2737 const [realtimeStatus, setRealtimeStatus] = React.useState(null)
28- const channel = supabase.channel('realtime:public:todos')
38+ const [receivedMessage, setReceivedMessage] = React.useState(null)
39+ const channelRef = React.useRef(null)
40+
2941 React.useEffect(() => {
30- channel.subscribe((status) => { if (status === 'SUBSCRIBED') setRealtimeStatus(status) })
42+ const channelName = \`test-broadcast-channel-\${vsn}\`
43+ const channel = supabase.channel(channelName, {
44+ config: { broadcast: { ack: true, self: true } }
45+ })
46+
47+ channelRef.current = channel
48+
49+ // Listen for broadcast messages
50+ channel.on('broadcast', { event: 'test-event' }, (payload) => {
51+ setReceivedMessage(payload.payload)
52+ })
53+
54+ // Subscribe to the channel
55+ channel.subscribe(async (status) => {
56+ if (status === 'SUBSCRIBED') {
57+ setRealtimeStatus(status)
58+
59+ // Send a test message after subscribing
60+ await channel.send({
61+ type: 'broadcast',
62+ event: 'test-event',
63+ payload: { message: 'Hello from browser!' }
64+ })
65+ }
66+ })
3167
3268 return () => {
3369 channel.unsubscribe()
3470 }
3571 }, [])
36- if (realtimeStatus) {
37- return <div id='realtime_status'>{realtimeStatus}</div>
38- } else {
39- return <div></div>
40- }
72+
73+ return (
74+ <div>
75+ <div id='vsn'>{vsn}</div>
76+ {realtimeStatus && (
77+ <div id='realtime_status'>{realtimeStatus}</div>
78+ )}
79+ {receivedMessage && (
80+ <div id='received_message'>{receivedMessage.message}</div>
81+ )}
82+ </div>
83+ )
4184 }
85+
4286 ReactDOM.render(<App />, document.getElementById('output'));
4387 </script>
4488</body>
@@ -80,8 +124,6 @@ beforeAll(async () => {
80124
81125afterAll ( async ( ) => {
82126 await ac . abort ( )
83- await page . close ( )
84- await browser . close ( )
85127 await sleep ( 1 )
86128} )
87129
@@ -90,13 +132,49 @@ describe('Realtime integration test', () => {
90132 browser = await launch ( {
91133 args : [ '--no-sandbox' , '--disable-setuid-sandbox' ] ,
92134 } )
93- page = await browser . newPage ( )
94135 } )
95136
96- it ( 'connects to realtime' , async ( ) => {
97- await page . goto ( 'http://localhost:8000' )
98- await page . waitForSelector ( '#realtime_status' , { timeout : 2000 } )
99- const realtimeStatus = await page . $eval ( '#realtime_status' , ( el ) => el . innerHTML )
100- assertEquals ( realtimeStatus , 'SUBSCRIBED' )
137+ afterAll ( async ( ) => {
138+ await browser . close ( )
139+ } )
140+
141+ const versions = [ { vsn : '1.0.0' } , { vsn : '2.0.0' } ]
142+
143+ versions . forEach ( ( { vsn } ) => {
144+ describe ( `Realtime with vsn: ${ vsn } ` , ( ) => {
145+ let page : Page
146+
147+ beforeAll ( async ( ) => {
148+ page = await browser . newPage ( )
149+ } )
150+
151+ afterAll ( async ( ) => {
152+ await page . close ( )
153+ } )
154+
155+ it ( 'connects to realtime' , async ( ) => {
156+ await page . goto ( `http://localhost:${ port } ?vsn=${ vsn } ` )
157+ await page . waitForSelector ( '#realtime_status' , { timeout : 2000 } )
158+ const realtimeStatus = await page . $eval ( '#realtime_status' , ( el ) => el . innerHTML )
159+ assertEquals ( realtimeStatus , 'SUBSCRIBED' )
160+
161+ // Verify correct version is being used
162+ const displayedVsn = await page . $eval ( '#vsn' , ( el ) => el . innerHTML )
163+ assertEquals ( displayedVsn , vsn )
164+ } )
165+
166+ it ( 'can broadcast and receive messages' , async ( ) => {
167+ await page . goto ( `http://localhost:${ port } ?vsn=${ vsn } ` )
168+
169+ // Wait for subscription
170+ await page . waitForSelector ( '#realtime_status' , { timeout : 2000 } )
171+
172+ // Wait for the broadcast message to be received
173+ await page . waitForSelector ( '#received_message' , { timeout : 5000 } )
174+ const receivedMessage = await page . $eval ( '#received_message' , ( el ) => el . innerHTML )
175+
176+ assertEquals ( receivedMessage , 'Hello from browser!' )
177+ } )
178+ } )
101179 } )
102180} )
0 commit comments