This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort child events according to MSC1772 rules.
- Loading branch information
Showing
3 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Update support for [MSC2946](https://github.com/matrix-org/matrix-doc/pull/2946): Spaces Summary. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Copyright 2021 The Matrix.org Foundation C.I.C. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import Any, Optional | ||
from unittest import mock | ||
|
||
from synapse.handlers.space_summary import _child_events_comparison_key | ||
|
||
from tests import unittest | ||
|
||
|
||
def _create_event(room_id: str, order: Optional[Any] = None): | ||
result = mock.Mock() | ||
result.room_id = room_id | ||
result.content = {} | ||
if order is not None: | ||
result.content["order"] = order | ||
return result | ||
|
||
|
||
def _order(*events): | ||
return sorted(events, key=_child_events_comparison_key) | ||
|
||
|
||
class TestSpaceSummarySort(unittest.TestCase): | ||
def test_no_order_last(self): | ||
"""An event with no ordering is placed behind those with an ordering.""" | ||
ev1 = _create_event("!abc:test") | ||
ev2 = _create_event("!xyz:test", "xyz") | ||
|
||
self.assertEqual([ev2, ev1], _order(ev1, ev2)) | ||
|
||
def test_order(self): | ||
"""The ordering should be used.""" | ||
ev1 = _create_event("!abc:test", "xyz") | ||
ev2 = _create_event("!xyz:test", "abc") | ||
|
||
self.assertEqual([ev2, ev1], _order(ev1, ev2)) | ||
|
||
def test_order_room_id(self): | ||
"""Room ID is a tie-breaker for ordering.""" | ||
ev1 = _create_event("!abc:test", "abc") | ||
ev2 = _create_event("!xyz:test", "abc") | ||
|
||
self.assertEqual([ev1, ev2], _order(ev1, ev2)) | ||
|
||
def test_invalid_ordering_type(self): | ||
"""Invalid orderings are considered the same as missing.""" | ||
ev1 = _create_event("!abc:test", 1) | ||
ev2 = _create_event("!xyz:test", "xyz") | ||
|
||
self.assertEqual([ev2, ev1], _order(ev1, ev2)) | ||
|
||
ev1 = _create_event("!abc:test", {}) | ||
self.assertEqual([ev2, ev1], _order(ev1, ev2)) | ||
|
||
ev1 = _create_event("!abc:test", []) | ||
self.assertEqual([ev2, ev1], _order(ev1, ev2)) | ||
|
||
ev1 = _create_event("!abc:test", True) | ||
self.assertEqual([ev2, ev1], _order(ev1, ev2)) | ||
|
||
def test_invalid_ordering_value(self): | ||
"""Invalid orderings are considered the same as missing.""" | ||
ev1 = _create_event("!abc:test", "\nfoo") | ||
ev2 = _create_event("!xyz:test", "xyz") | ||
|
||
self.assertEqual([ev2, ev1], _order(ev1, ev2)) | ||
|
||
ev1 = _create_event("!abc:test", "a" * 51) | ||
self.assertEqual([ev2, ev1], _order(ev1, ev2)) |