Class: Supabase::Realtime::TestSocket

Inherits:
Object
  • Object
show all
Includes:
Socket
Defined in:
lib/supabase/realtime/test_socket.rb

Overview

In-memory Socket implementation for specs and local prototyping. Captures every frame the client sends in ‘sent_frames`, and exposes `inject(frame)` so a test can simulate a server response.

Not intended for production use — bring a real WebSocket adapter for that.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Socket

#close_callbacks, #error_callbacks, #message_callbacks, #on_close, #on_error, #on_message, #on_open, #open_callbacks

Constructor Details

#initializeTestSocket

Returns a new instance of TestSocket.



17
18
19
20
# File 'lib/supabase/realtime/test_socket.rb', line 17

def initialize
  @connected   = false
  @sent_frames = []
end

Instance Attribute Details

#sent_framesObject (readonly)

Returns the value of attribute sent_frames.



15
16
17
# File 'lib/supabase/realtime/test_socket.rb', line 15

def sent_frames
  @sent_frames
end

Instance Method Details

#closeObject



27
28
29
30
# File 'lib/supabase/realtime/test_socket.rb', line 27

def close
  @connected = false
  close_callbacks.each(&:call)
end

#connectObject



22
23
24
25
# File 'lib/supabase/realtime/test_socket.rb', line 22

def connect
  @connected = true
  open_callbacks.each(&:call)
end

#connected?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/supabase/realtime/test_socket.rb', line 36

def connected?
  @connected
end

#inject(frame) ⇒ Object

Push a JSON frame as if it came from the server. Accepts a JSON String or a Hash (which gets JSON-encoded for you).



44
45
46
47
# File 'lib/supabase/realtime/test_socket.rb', line 44

def inject(frame)
  raw = frame.is_a?(String) ? frame : JSON.generate(frame)
  message_callbacks.each { |cb| cb.call(raw) }
end

#last_sent_frameObject

Convenience: the last frame the client pushed, parsed back to a Hash.



50
51
52
53
54
# File 'lib/supabase/realtime/test_socket.rb', line 50

def last_sent_frame
  return nil if @sent_frames.empty?

  JSON.parse(@sent_frames.last)
end

#reset_sent_framesObject



60
61
62
# File 'lib/supabase/realtime/test_socket.rb', line 60

def reset_sent_frames
  @sent_frames = []
end

#send(payload) ⇒ Object



32
33
34
# File 'lib/supabase/realtime/test_socket.rb', line 32

def send(payload)
  @sent_frames << payload
end

#sent_eventsObject



56
57
58
# File 'lib/supabase/realtime/test_socket.rb', line 56

def sent_events
  @sent_frames.map { |f| JSON.parse(f)["event"] }
end