Skip to content

Commit b71aec2

Browse files
rsashankLucasC-B
authored andcommitted
refactor: model/api_types/views/version: Simplify feature-level typing.
When the server originally did not provide `zulip_feature_level`, this was previously represented by a `None` value in `model.server_feature_level`. This replaces the current potential `None` value with a zero value, allowing a pure integer representation in the model. This enables simplification of conditional statements when handling different server versions. The feature-level is represented by various strings in the repository, including: - server_feature_level - zulip_feature_level - ZFL - feature_level Tests updated, including test ids. Additional test case added for realm retention. Fixes #1364. Co-authored-by: Lucas.C.B <[email protected]>
1 parent ba5e27a commit b71aec2

File tree

7 files changed

+41
-35
lines changed

7 files changed

+41
-35
lines changed

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def messages_successful_response(
559559
params=SUPPORTED_SERVER_VERSIONS,
560560
ids=(lambda param: "server_version:{}-server_feature_level:{}".format(*param)),
561561
)
562-
def zulip_version(request: Any) -> Tuple[str, Optional[int]]:
562+
def zulip_version(request: Any) -> Tuple[str, int]:
563563
"""
564564
Fixture to test different components based on the server version and the
565565
feature level.
@@ -1449,7 +1449,7 @@ def stream_dict(streams_fixture: List[Dict[str, Any]]) -> Dict[int, Any]:
14491449
},
14501450
],
14511451
ids=[
1452-
"zulip_feature_level:None",
1452+
"zulip_feature_level:0",
14531453
"zulip_feature_level:1",
14541454
],
14551455
)

tests/model/test_model.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_init(
8181
)
8282
assert model.initial_data == initial_data
8383
assert model.server_version == initial_data["zulip_version"]
84-
assert model.server_feature_level == initial_data.get("zulip_feature_level")
84+
assert model.server_feature_level == initial_data.get("zulip_feature_level", 0)
8585
assert model.user_id == user_profile["user_id"]
8686
assert model.user_full_name == user_profile["full_name"]
8787
assert model.user_email == user_profile["email"]
@@ -157,7 +157,7 @@ def test_user_settings_expected_contents(self, model):
157157
(
158158
[["Stream 1", "muted stream muted topic"]],
159159
{("Stream 1", "muted stream muted topic"): None},
160-
None,
160+
0,
161161
),
162162
(
163163
[["Stream 2", "muted topic", 1530129122]],
@@ -166,7 +166,7 @@ def test_user_settings_expected_contents(self, model):
166166
),
167167
],
168168
ids=[
169-
"zulip_feature_level:None",
169+
"zulip_feature_level:0",
170170
"zulip_feature_level:1",
171171
],
172172
)
@@ -276,12 +276,19 @@ def test_register_initial_desired_events(self, mocker, initial_data):
276276
"expect_msg_retention_text",
277277
],
278278
[
279+
case(
280+
{1: {}},
281+
None,
282+
0,
283+
{1: "Indefinite [Organization default]"},
284+
id="ZFL=0_no_stream_retention_realm_retention=None",
285+
),
279286
case(
280287
{1: {}},
281288
None,
282289
10,
283290
{1: "Indefinite [Organization default]"},
284-
id="ZFL=None_no_stream_retention_realm_retention=None",
291+
id="ZFL=10_no_stream_retention_realm_retention=None",
285292
),
286293
case(
287294
{1: {}, 2: {}},
@@ -973,7 +980,7 @@ def test_update_stream_message(
973980
@pytest.mark.parametrize(
974981
"ZFL, expect_API_notify_args",
975982
[
976-
(None, False),
983+
(0, False),
977984
(8, False),
978985
(9, True),
979986
(152, True),
@@ -1351,7 +1358,7 @@ def test_modernize_message_response(
13511358
@pytest.mark.parametrize(
13521359
"feature_level, to_vary_in_initial_data",
13531360
[
1354-
(None, {}),
1361+
(0, {}),
13551362
(27, {}),
13561363
(52, {}),
13571364
(
@@ -1364,7 +1371,7 @@ def test_modernize_message_response(
13641371
),
13651372
],
13661373
ids=[
1367-
"Zulip_2.1.x_ZFL_None_no_restrictions",
1374+
"Zulip_2.1.x_ZFL_0_no_restrictions",
13681375
"Zulip_3.1.x_ZFL_27_no_restrictions",
13691376
"Zulip_4.0.x_ZFL_52_no_restrictions",
13701377
"Zulip_4.0.x_ZFL_53_with_restrictions",
@@ -2777,7 +2784,7 @@ def test__handle_reaction_event_for_msg_in_index(
27772784
params=[
27782785
("op", 32), # At server feature level 32, event uses standard field
27792786
("operation", 31),
2780-
("operation", None),
2787+
("operation", 0),
27812788
]
27822789
)
27832790
def update_message_flags_operation(self, request):
@@ -3297,11 +3304,11 @@ def test__handle_typing_event(
32973304
),
32983305
],
32993306
ids=[
3300-
"remove_18_in_home_view:already_unmuted:ZFLNone",
3301-
"remove_19_in_home_view:muted:ZFLNone",
3302-
"add_19_in_home_view:already_muted:ZFLNone",
3303-
"add_30_in_home_view:unmuted:ZFLNone",
3304-
"remove_30_is_muted:already_unmutedZFL139",
3307+
"remove_18_in_home_view:already_unmuted:ZFL0",
3308+
"remove_19_in_home_view:muted:ZFL0",
3309+
"add_19_in_home_view:already_muted:ZFL0",
3310+
"add_30_in_home_view:unmuted:ZFL0",
3311+
"remove_30_is_muted:already_unmuted:ZFL139",
33053312
"remove_19_is_muted:muted:ZFL139",
33063313
"add_15_is_muted:already_muted:ZFL139",
33073314
"add_30_is_muted:unmuted:ZFL139",
@@ -3469,7 +3476,7 @@ def test__handle_subscription_event_visual_notifications(
34693476
[
34703477
(
34713478
{"op": "peer_add", "stream_id": 99, "user_id": 12},
3472-
None,
3479+
0,
34733480
99,
34743481
[1001, 11, 12],
34753482
),
@@ -3491,7 +3498,7 @@ def test__handle_subscription_event_visual_notifications(
34913498
99,
34923499
[1001, 11, 12],
34933500
),
3494-
({"op": "peer_remove", "stream_id": 2, "user_id": 12}, None, 2, [1001, 11]),
3501+
({"op": "peer_remove", "stream_id": 2, "user_id": 12}, 0, 2, [1001, 11]),
34953502
({"op": "peer_remove", "stream_id": 2, "user_id": 12}, 34, 2, [1001, 11]),
34963503
(
34973504
{"op": "peer_remove", "stream_ids": [2], "user_ids": [12]},
@@ -3507,11 +3514,11 @@ def test__handle_subscription_event_visual_notifications(
35073514
),
35083515
],
35093516
ids=[
3510-
"user_subscribed_to_stream:ZFLNone",
3517+
"user_subscribed_to_stream:ZFL0",
35113518
"user_subscribed_to_stream:ZFL34",
35123519
"user_subscribed_to_stream:ZFL34_should_be_35",
35133520
"user_subscribed_to_stream:ZFL35",
3514-
"user_unsubscribed_from_stream:ZFLNone",
3521+
"user_unsubscribed_from_stream:ZFL0",
35153522
"user_unsubscribed_from_stream:ZFL34",
35163523
"user_unsubscribed_from_stream:ZFL34_should_be_35",
35173524
"user_unsubscribed_from_stream:ZFL35",

tests/ui_tools/test_popups.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def test_keypress_exit_popup_invalid_key(
222222
assert not self.controller.exit_popup.called
223223

224224
def test_feature_level_content(
225-
self, mocker: MockerFixture, zulip_version: Tuple[str, Optional[int]]
225+
self, mocker: MockerFixture, zulip_version: Tuple[str, int]
226226
) -> None:
227227
self.controller = mocker.Mock()
228228
mocker.patch.object(
@@ -1282,16 +1282,16 @@ def test_keypress_stream_members(
12821282
case(
12831283
{"date_created": None, "is_announcement_only": True},
12841284
"74 [Organization default]",
1285-
None,
1285+
0,
12861286
17,
1287-
id="ZFL=None_no_date_created__no_retention_days__admins_only",
1287+
id="ZFL=0_no_date_created__no_retention_days__admins_only",
12881288
),
12891289
case(
12901290
{"date_created": None, "is_announcement_only": False},
12911291
"74 [Organization default]",
1292-
None,
1292+
0,
12931293
16,
1294-
id="ZFL=None_no_date_created__no_retention_days__anyone_can_type",
1294+
id="ZFL=0_no_date_created__no_retention_days__anyone_can_type",
12951295
),
12961296
case(
12971297
{"date_created": None, "stream_post_policy": 1},
@@ -1363,7 +1363,7 @@ def test_popup_height(
13631363
general_stream: Dict[str, Any],
13641364
to_vary_in_stream_data: Dict[str, Optional[int]],
13651365
cached_message_retention_text: str,
1366-
server_feature_level: Optional[int],
1366+
server_feature_level: int,
13671367
expected_height: int,
13681368
) -> None:
13691369
model = self.controller.model

zulipterminal/api_types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,6 @@ class ServerSettings(TypedDict):
655655
# Added in Zulip 2.1.0
656656
external_authentication_methods: List[ExternalAuthenticationMethod]
657657

658-
# TODO Refactor ZFL to default to zero
659658
zulip_feature_level: NotRequired[int] # New in Zulip 3.0, ZFL 1
660659
zulip_version: str
661660
zulip_merge_base: NotRequired[str] # New in Zulip 5.0, ZFL 88

zulipterminal/model.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def __init__(self, controller: Any) -> None:
170170
self._fetch_initial_data()
171171

172172
self.server_version = self.initial_data["zulip_version"]
173-
self.server_feature_level = self.initial_data.get("zulip_feature_level")
173+
self.server_feature_level: int = self.initial_data.get("zulip_feature_level", 0)
174174

175175
self.user_dict: Dict[str, MinimalUserData] = {}
176176
self.user_id_email_dict: Dict[int, str] = {}
@@ -189,8 +189,8 @@ def __init__(self, controller: Any) -> None:
189189

190190
# NOTE: The date_created field of stream has been added in feature
191191
# level 30, server version 4. For consistency we add this field
192-
# on server iterations even before this with value of None.
193-
if self.server_feature_level is None or self.server_feature_level < 30:
192+
# on earlier server iterations with the value of None.
193+
if self.server_feature_level < 30:
194194
for stream in self.stream_dict.values():
195195
stream["date_created"] = None
196196

@@ -203,7 +203,7 @@ def __init__(self, controller: Any) -> None:
203203
assert set(map(len, muted_topics)) in (set(), {2}, {3})
204204
self._muted_topics: Dict[Tuple[str, str], Optional[int]] = {
205205
(stream_name, topic): (
206-
None if self.server_feature_level is None else date_muted[0]
206+
None if self.server_feature_level == 0 else date_muted[0]
207207
)
208208
for stream_name, topic, *date_muted in muted_topics
209209
}
@@ -259,7 +259,7 @@ def normalize_and_cache_message_retention_text(self) -> None:
259259
# sream_id in model.cached_retention_text. This will be displayed in the UI.
260260
self.cached_retention_text: Dict[int, str] = {}
261261
realm_message_retention_days = self.initial_data["realm_message_retention_days"]
262-
if self.server_feature_level is None or self.server_feature_level < 17:
262+
if self.server_feature_level < 17:
263263
for stream in self.stream_dict.values():
264264
stream["message_retention_days"] = None
265265

@@ -590,7 +590,7 @@ def update_stream_message(
590590
if content is not None:
591591
request["content"] = content
592592

593-
if self.server_feature_level is not None and self.server_feature_level >= 9:
593+
if self.server_feature_level >= 9:
594594
request["send_notification_to_old_thread"] = notify_old
595595
request["send_notification_to_new_thread"] = notify_new
596596

@@ -1822,7 +1822,7 @@ def _handle_update_message_flags_event(self, event: Event) -> None:
18221822
Handle change to message flags (eg. starred, read)
18231823
"""
18241824
assert event["type"] == "update_message_flags"
1825-
if self.server_feature_level is None or self.server_feature_level < 32:
1825+
if self.server_feature_level < 32:
18261826
operation = event["operation"]
18271827
else:
18281828
operation = event["op"]

zulipterminal/ui_tools/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ def __init__(
10831083
*,
10841084
zt_version: str,
10851085
server_version: str,
1086-
server_feature_level: Optional[int],
1086+
server_feature_level: int,
10871087
theme_name: str,
10881088
color_depth: int,
10891089
autohide_enabled: bool,

zulipterminal/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
ZT_VERSION = "0.7.0+git"
66

77
SUPPORTED_SERVER_VERSIONS = [
8-
("2.1", None),
8+
("2.1", 0),
99
("3.0", 25),
1010
]
1111

0 commit comments

Comments
 (0)