Skip to content

Commit d66171e

Browse files
committed
fix: resolve serializer test failures
- Fix JSON encoding to use compact format (no spaces) to match expected output - Fix metadata extraction to exclude special fields (type, event, payload) - Fix broadcast decoding offset calculation - Fix metadata start position calculation in test - Update metadata length validation test to use sufficient length
1 parent 4cd5d8e commit d66171e

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/realtime/src/realtime/serializer.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def encode(
8888
msg.get("topic"),
8989
msg.get("event"),
9090
payload,
91-
])
91+
], separators=(',', ':'))
9292

9393
def _binary_encode_push(self, message: Dict[str, Any]) -> bytes:
9494
"""
@@ -215,8 +215,10 @@ def _encode_user_broadcast_push_internal(
215215
payload = message.get("payload", {})
216216
user_event = payload.get("event", "")
217217

218-
# Filter metadata based on allowed keys
218+
# Filter metadata based on allowed keys (exclude special fields: type, event, payload)
219219
rest = self._pick(payload, self.allowed_metadata_keys)
220+
# Remove special fields that shouldn't be in metadata
221+
rest = {k: v for k, v in rest.items() if k not in ("type", "event", "payload")}
220222
metadata = json.dumps(rest) if rest else ""
221223

222224
# Validate lengths don't exceed uint8 max value (255)
@@ -384,13 +386,13 @@ def _decode_reply(self, buffer: bytes) -> Dict[str, Any]:
384386

385387
def _decode_broadcast(self, buffer: bytes) -> Dict[str, Any]:
386388
"""Decode a broadcast message."""
387-
if len(buffer) < self.HEADER_LENGTH + 3:
389+
if len(buffer) < self.HEADER_LENGTH + 2:
388390
return {}
389391

390392
topic_size = buffer[1]
391393
event_size = buffer[2]
392394

393-
offset = self.HEADER_LENGTH + 3
395+
offset = self.HEADER_LENGTH + 2 # kind(1) + topic_len(1) + event_len(1) = 3, but offset is after reading lengths
394396

395397
topic = buffer[offset:offset + topic_size].decode("utf-8")
396398
offset += topic_size

src/realtime/tests/test_serializer.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,15 @@ def test_encode_user_broadcast_push_json_with_metadata():
127127
assert result[0] == 3 # userBroadcastPush kind
128128
assert result[5] > 0 # metadata length (should have metadata)
129129
# Verify metadata contains "extra" but not "store"
130-
metadata_start = 1 + 5 + 2 + 1 + 3 + 10 # header + lengths
130+
# Format: kind(1) + join_ref_len(1) + ref_len(1) + topic_len(1) + user_event_len(1) + metadata_len(1) + encoding(1) = 7 bytes header
131+
# Then: join_ref(2) + ref(1) + topic(3) + user_event(10) = 16 bytes
132+
# So metadata starts at: 7 + 16 = 23
133+
header_len = 7
134+
join_ref_len = result[1]
135+
ref_len = result[2]
136+
topic_len = result[3]
137+
user_event_len = result[4]
138+
metadata_start = header_len + join_ref_len + ref_len + topic_len + user_event_len
131139
metadata_len = result[5]
132140
metadata_bytes = result[metadata_start:metadata_start + metadata_len]
133141
metadata = json.loads(metadata_bytes.decode("utf-8"))
@@ -323,6 +331,9 @@ def test_encode_validation_errors():
323331

324332
# Test metadata too long
325333
serializer_with_meta = Serializer(allowed_metadata_keys=["extra"])
334+
# Create metadata that will exceed 255 chars when JSON stringified
335+
# JSON format: {"extra":"aaa..."} = 2 + 9 + 1 + 1 + N + 1 = 14 + N
336+
# So we need N > 241 to exceed 255 total
326337
with pytest.raises(ValueError, match="metadata length"):
327338
serializer_with_meta._encode_user_broadcast_push_internal(
328339
{
@@ -332,7 +343,7 @@ def test_encode_validation_errors():
332343
"payload": {
333344
"event": "user-event",
334345
"payload": {},
335-
"extra": "a" * 240, # Will exceed 255 when JSON stringified
346+
"extra": "a" * 260, # Will exceed 255 when JSON stringified
336347
},
337348
},
338349
1,

0 commit comments

Comments
 (0)