|
| 1 | +import dataclasses |
| 2 | + |
| 3 | +from fishjam import events |
| 4 | +from fishjam.events import _protos |
| 5 | +from fishjam.events._protos.fishjam import ServerMessage |
| 6 | +from fishjam.events.allowed_notifications import ALLOWED_NOTIFICATIONS |
| 7 | + |
| 8 | +# Types intentionally NOT published as SDK notifications. |
| 9 | +# Adding a new oneof member to ServerMessage.content forces a maintainer |
| 10 | +# decision: either add it to ALLOWED_NOTIFICATIONS (and re-export from |
| 11 | +# fishjam.events), or add it here with a comment. |
| 12 | +IGNORED_NOTIFICATIONS = { |
| 13 | + # Auth / subscribe handshake — transport-level, not user-facing events. |
| 14 | + "ServerMessageAuthenticated", |
| 15 | + "ServerMessageAuthRequest", |
| 16 | + "ServerMessageSubscribeRequest", |
| 17 | + "ServerMessageSubscribeResponse", |
| 18 | + # Not surfaced to SDK users - support for compositions is REST api only |
| 19 | + # and not supported in SDKs |
| 20 | + "ServerMessageTrackForwarding", |
| 21 | + "ServerMessageTrackForwardingRemoved", |
| 22 | + "ServerMessageVadNotification", |
| 23 | + # Deprecated in the proto. |
| 24 | + "ServerMessageStreamConnected", |
| 25 | + "ServerMessageStreamDisconnected", |
| 26 | + "ServerMessageHlsPlayable", |
| 27 | + "ServerMessageHlsUploaded", |
| 28 | + "ServerMessageHlsUploadCrashed", |
| 29 | + "ServerMessageComponentCrashed", |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +def _oneof_content_types() -> list[type]: |
| 34 | + types = [] |
| 35 | + for field in dataclasses.fields(ServerMessage): |
| 36 | + meta = field.metadata.get("betterproto") |
| 37 | + if meta is not None and getattr(meta, "group", None) == "content": |
| 38 | + types.append(getattr(_protos.fishjam, field.type)) |
| 39 | + return types |
| 40 | + |
| 41 | + |
| 42 | +def test_every_content_oneof_is_allowed_or_explicitly_ignored(): |
| 43 | + allowed_names = {cls.__name__ for cls in ALLOWED_NOTIFICATIONS} |
| 44 | + undecided = [ |
| 45 | + cls.__name__ |
| 46 | + for cls in _oneof_content_types() |
| 47 | + if cls.__name__ not in allowed_names |
| 48 | + and cls.__name__ not in IGNORED_NOTIFICATIONS |
| 49 | + ] |
| 50 | + assert not undecided, ( |
| 51 | + "New ServerMessage.content oneof members found without a maintainer " |
| 52 | + "decision. Add each to ALLOWED_NOTIFICATIONS (and re-export from " |
| 53 | + "fishjam.events) or to IGNORED_NOTIFICATIONS in this test:\n - " |
| 54 | + + "\n - ".join(sorted(undecided)) |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def test_allowed_and_ignored_are_disjoint(): |
| 59 | + overlap = {cls.__name__ for cls in ALLOWED_NOTIFICATIONS} & IGNORED_NOTIFICATIONS |
| 60 | + assert not overlap, f"Types cannot be both allowed and ignored: {overlap}" |
| 61 | + |
| 62 | + |
| 63 | +def test_allowed_notifications_are_reexported_from_package(): |
| 64 | + missing = [ |
| 65 | + cls.__name__ |
| 66 | + for cls in ALLOWED_NOTIFICATIONS |
| 67 | + if cls.__name__ not in events.__all__ or not hasattr(events, cls.__name__) |
| 68 | + ] |
| 69 | + assert not missing, ( |
| 70 | + "Every ALLOWED_NOTIFICATIONS type must be re-exported from " |
| 71 | + f"fishjam.events. Missing: {missing}" |
| 72 | + ) |
0 commit comments