spikard 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,220 +1,221 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
-
5
- module Spikard
6
- # Testing helpers that wrap the native Ruby extension.
7
- module Testing
8
- module_function
9
-
10
- def create_test_client(app, config: nil)
11
- unless defined?(Spikard::Native::TestClient)
12
- raise LoadError, 'Spikard native test client is not available. Build the native extension before running tests.'
13
- end
14
-
15
- # Allow generated apps to stash a test config
16
- if config.nil? && app.instance_variable_defined?(:@__spikard_test_config)
17
- config = app.instance_variable_get(:@__spikard_test_config)
18
- end
19
-
20
- # Use default config if none provided
21
- config ||= Spikard::ServerConfig.new
22
-
23
- routes_json = JSON.generate(app.route_metadata)
24
- handlers = app.handler_map.transform_keys(&:to_sym)
25
- ws_handlers = app.websocket_handlers || {}
26
- sse_producers = app.sse_producers || {}
27
- native = Spikard::Native::TestClient.new(routes_json, handlers, config, ws_handlers, sse_producers)
28
- TestClient.new(native)
29
- end
30
-
31
- # High level wrapper around the native test client.
32
- class TestClient
33
- def initialize(native)
34
- @native = native
35
- end
36
-
37
- # Factory method for creating test client from an app
38
- def self.new(app_or_native, config: nil)
39
- # If passed a native client directly, use it
40
- return super(app_or_native) if app_or_native.is_a?(Spikard::Native::TestClient)
41
-
42
- # Otherwise, create test client from app
43
- Spikard::Testing.create_test_client(app_or_native, config: config)
44
- end
45
-
46
- def request(method, path, **options)
47
- payload = @native.request(method.to_s.upcase, path, options)
48
- Response.new(payload)
49
- end
50
-
51
- def websocket(path)
52
- native_ws = @native.websocket(path)
53
- WebSocketTestConnection.new(native_ws)
54
- end
55
-
56
- def sse(path)
57
- native_sse = @native.sse(path)
58
- SseStream.new(native_sse)
59
- end
60
-
61
- def close
62
- @native.close
63
- end
64
-
65
- %w[get post put patch delete head options trace].each do |verb|
66
- define_method(verb) do |path, **options|
67
- request(verb.upcase, path, **options)
68
- end
69
- end
70
- end
71
-
72
- # WebSocket test connection wrapper
73
- class WebSocketTestConnection
74
- def initialize(native_ws)
75
- @native_ws = native_ws
76
- end
77
-
78
- def send_text(text)
79
- @native_ws.send_text(JSON.generate(text))
80
- end
81
-
82
- def send_json(obj)
83
- @native_ws.send_json(obj)
84
- end
85
-
86
- def receive_text
87
- raw = @native_ws.receive_text
88
- JSON.parse(raw)
89
- rescue JSON::ParserError
90
- raw
91
- end
92
-
93
- def receive_json
94
- @native_ws.receive_json
95
- end
96
-
97
- def receive_bytes
98
- receive_text
99
- end
100
-
101
- def receive_message
102
- native_msg = @native_ws.receive_message
103
- WebSocketMessage.new(native_msg)
104
- end
105
-
106
- def close
107
- @native_ws.close
108
- end
109
- end
110
-
111
- # WebSocket message wrapper
112
- class WebSocketMessage
113
- def initialize(native_msg)
114
- @native_msg = native_msg
115
- end
116
-
117
- def as_text
118
- raw = @native_msg.as_text
119
- return unless raw
120
-
121
- JSON.parse(raw)
122
- rescue JSON::ParserError
123
- raw
124
- end
125
-
126
- def as_json
127
- @native_msg.as_json
128
- end
129
-
130
- def as_binary
131
- @native_msg.as_binary
132
- end
133
-
134
- def close?
135
- @native_msg.is_close
136
- end
137
- end
138
-
139
- # SSE stream wrapper
140
- class SseStream
141
- def initialize(native_sse)
142
- @native_sse = native_sse
143
- end
144
-
145
- def body
146
- @native_sse.body
147
- end
148
-
149
- def events
150
- parsed_chunks.map { |chunk| InlineSseEvent.new(chunk) }
151
- end
152
-
153
- def events_as_json
154
- parsed_chunks.filter_map do |chunk|
155
- JSON.parse(chunk)
156
- rescue JSON::ParserError
157
- nil
158
- end
159
- end
160
-
161
- private
162
-
163
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
164
- def parsed_chunks
165
- raw = body.to_s.gsub("\r\n", "\n")
166
- events = []
167
- current = []
168
-
169
- raw.each_line do |line|
170
- stripped = line.chomp
171
- if stripped.start_with?('data:')
172
- current << stripped[5..].strip
173
- elsif stripped.empty?
174
- unless current.empty?
175
- data = current.join("\n").strip
176
- events << data unless data.empty?
177
- current = []
178
- end
179
- end
180
- end
181
-
182
- unless current.empty?
183
- data = current.join("\n").strip
184
- events << data unless data.empty?
185
- end
186
-
187
- events
188
- end
189
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
190
- end
191
-
192
- # SSE event wrapper
193
- class SseEvent
194
- def initialize(native_event)
195
- @native_event = native_event
196
- end
197
-
198
- def data
199
- @native_event.data
200
- end
201
-
202
- def as_json
203
- @native_event.as_json
204
- end
205
- end
206
-
207
- # Lightweight wrapper for parsed SSE events backed by strings.
208
- class InlineSseEvent
209
- attr_reader :data
210
-
211
- def initialize(data)
212
- @data = data
213
- end
214
-
215
- def as_json
216
- JSON.parse(@data)
217
- end
218
- end
219
- end
220
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Spikard
6
+ # Testing helpers that wrap the native Ruby extension.
7
+ module Testing
8
+ module_function
9
+
10
+ def create_test_client(app, config: nil)
11
+ unless defined?(Spikard::Native::TestClient)
12
+ raise LoadError, 'Spikard native test client is not available. Build the native extension before running tests.'
13
+ end
14
+
15
+ # Allow generated apps to stash a test config
16
+ if config.nil? && app.instance_variable_defined?(:@__spikard_test_config)
17
+ config = app.instance_variable_get(:@__spikard_test_config)
18
+ end
19
+
20
+ # Use default config if none provided
21
+ config ||= Spikard::ServerConfig.new
22
+
23
+ routes_json = JSON.generate(app.route_metadata)
24
+ handlers = app.handler_map.transform_keys(&:to_sym)
25
+ ws_handlers = app.websocket_handlers || {}
26
+ sse_producers = app.sse_producers || {}
27
+ dependencies = app.dependencies || {}
28
+ native = Spikard::Native::TestClient.new(routes_json, handlers, config, ws_handlers, sse_producers, dependencies)
29
+ TestClient.new(native)
30
+ end
31
+
32
+ # High level wrapper around the native test client.
33
+ class TestClient
34
+ def initialize(native)
35
+ @native = native
36
+ end
37
+
38
+ # Factory method for creating test client from an app
39
+ def self.new(app_or_native, config: nil)
40
+ # If passed a native client directly, use it
41
+ return super(app_or_native) if app_or_native.is_a?(Spikard::Native::TestClient)
42
+
43
+ # Otherwise, create test client from app
44
+ Spikard::Testing.create_test_client(app_or_native, config: config)
45
+ end
46
+
47
+ def request(method, path, **options)
48
+ payload = @native.request(method.to_s.upcase, path, options)
49
+ Response.new(payload)
50
+ end
51
+
52
+ def websocket(path)
53
+ native_ws = @native.websocket(path)
54
+ WebSocketTestConnection.new(native_ws)
55
+ end
56
+
57
+ def sse(path)
58
+ native_sse = @native.sse(path)
59
+ SseStream.new(native_sse)
60
+ end
61
+
62
+ def close
63
+ @native.close
64
+ end
65
+
66
+ %w[get post put patch delete head options trace].each do |verb|
67
+ define_method(verb) do |path, **options|
68
+ request(verb.upcase, path, **options)
69
+ end
70
+ end
71
+ end
72
+
73
+ # WebSocket test connection wrapper
74
+ class WebSocketTestConnection
75
+ def initialize(native_ws)
76
+ @native_ws = native_ws
77
+ end
78
+
79
+ def send_text(text)
80
+ @native_ws.send_text(JSON.generate(text))
81
+ end
82
+
83
+ def send_json(obj)
84
+ @native_ws.send_json(obj)
85
+ end
86
+
87
+ def receive_text
88
+ raw = @native_ws.receive_text
89
+ JSON.parse(raw)
90
+ rescue JSON::ParserError
91
+ raw
92
+ end
93
+
94
+ def receive_json
95
+ @native_ws.receive_json
96
+ end
97
+
98
+ def receive_bytes
99
+ receive_text
100
+ end
101
+
102
+ def receive_message
103
+ native_msg = @native_ws.receive_message
104
+ WebSocketMessage.new(native_msg)
105
+ end
106
+
107
+ def close
108
+ @native_ws.close
109
+ end
110
+ end
111
+
112
+ # WebSocket message wrapper
113
+ class WebSocketMessage
114
+ def initialize(native_msg)
115
+ @native_msg = native_msg
116
+ end
117
+
118
+ def as_text
119
+ raw = @native_msg.as_text
120
+ return unless raw
121
+
122
+ JSON.parse(raw)
123
+ rescue JSON::ParserError
124
+ raw
125
+ end
126
+
127
+ def as_json
128
+ @native_msg.as_json
129
+ end
130
+
131
+ def as_binary
132
+ @native_msg.as_binary
133
+ end
134
+
135
+ def close?
136
+ @native_msg.is_close
137
+ end
138
+ end
139
+
140
+ # SSE stream wrapper
141
+ class SseStream
142
+ def initialize(native_sse)
143
+ @native_sse = native_sse
144
+ end
145
+
146
+ def body
147
+ @native_sse.body
148
+ end
149
+
150
+ def events
151
+ parsed_chunks.map { |chunk| InlineSseEvent.new(chunk) }
152
+ end
153
+
154
+ def events_as_json
155
+ parsed_chunks.filter_map do |chunk|
156
+ JSON.parse(chunk)
157
+ rescue JSON::ParserError
158
+ nil
159
+ end
160
+ end
161
+
162
+ private
163
+
164
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
165
+ def parsed_chunks
166
+ raw = body.to_s.gsub("\r\n", "\n")
167
+ events = []
168
+ current = []
169
+
170
+ raw.each_line do |line|
171
+ stripped = line.chomp
172
+ if stripped.start_with?('data:')
173
+ current << stripped[5..].strip
174
+ elsif stripped.empty?
175
+ unless current.empty?
176
+ data = current.join("\n").strip
177
+ events << data unless data.empty?
178
+ current = []
179
+ end
180
+ end
181
+ end
182
+
183
+ unless current.empty?
184
+ data = current.join("\n").strip
185
+ events << data unless data.empty?
186
+ end
187
+
188
+ events
189
+ end
190
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
191
+ end
192
+
193
+ # SSE event wrapper
194
+ class SseEvent
195
+ def initialize(native_event)
196
+ @native_event = native_event
197
+ end
198
+
199
+ def data
200
+ @native_event.data
201
+ end
202
+
203
+ def as_json
204
+ @native_event.as_json
205
+ end
206
+ end
207
+
208
+ # Lightweight wrapper for parsed SSE events backed by strings.
209
+ class InlineSseEvent
210
+ attr_reader :data
211
+
212
+ def initialize(data)
213
+ @data = data
214
+ end
215
+
216
+ def as_json
217
+ JSON.parse(@data)
218
+ end
219
+ end
220
+ end
221
+ end