|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { HierarchyRoom } from "../../providers/types"; |
| 4 | +import { buildTree, isValidOrder, sortChildren } from "./RoomHierarchy"; |
| 5 | + |
| 6 | +const makeChild = ( |
| 7 | + state_key: string, |
| 8 | + origin_server_ts: number, |
| 9 | + order?: string |
| 10 | +): HierarchyRoom["children_state"][number] => ({ |
| 11 | + type: "m.space.child", |
| 12 | + state_key, |
| 13 | + content: { via: ["example.org"], ...(order !== undefined ? { order } : {}) }, |
| 14 | + sender: "@admin:example.org", |
| 15 | + origin_server_ts, |
| 16 | +}); |
| 17 | + |
| 18 | +const makeRoom = (room_id: string, children: HierarchyRoom["children_state"] = []): HierarchyRoom => ({ |
| 19 | + room_id, |
| 20 | + num_joined_members: 1, |
| 21 | + guest_can_join: false, |
| 22 | + world_readable: false, |
| 23 | + children_state: children, |
| 24 | +}); |
| 25 | + |
| 26 | +describe("isValidOrder", () => { |
| 27 | + it("accepts printable ASCII strings", () => { |
| 28 | + expect(isValidOrder("1")).toBe(true); |
| 29 | + expect(isValidOrder("abc")).toBe(true); |
| 30 | + expect(isValidOrder(" ")).toBe(true); // 0x20 |
| 31 | + expect(isValidOrder("~")).toBe(true); // 0x7E |
| 32 | + }); |
| 33 | + |
| 34 | + it("rejects empty string", () => { |
| 35 | + expect(isValidOrder("")).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + it("rejects non-strings", () => { |
| 39 | + expect(isValidOrder(undefined)).toBe(false); |
| 40 | + expect(isValidOrder(null)).toBe(false); |
| 41 | + expect(isValidOrder(1)).toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it("rejects strings with control characters", () => { |
| 45 | + expect(isValidOrder("\x1F")).toBe(false); |
| 46 | + expect(isValidOrder("\x7F")).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it("rejects non-ASCII Unicode that would pass naive string comparison", () => { |
| 50 | + expect(isValidOrder("\u0100")).toBe(false); // Ā — codepoint 256, above 0x7E |
| 51 | + expect(isValidOrder("café")).toBe(false); // é is codepoint 233, above 0x7E |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe("sortChildren", () => { |
| 56 | + it("sorts lexicographically by order field", () => { |
| 57 | + const children = [makeChild("!c", 1, "3"), makeChild("!a", 2, "1"), makeChild("!b", 3, "2")]; |
| 58 | + const sorted = sortChildren(children).map(c => c.state_key); |
| 59 | + expect(sorted).toEqual(["!a", "!b", "!c"]); |
| 60 | + }); |
| 61 | + |
| 62 | + it("sorts without order by origin_server_ts ascending", () => { |
| 63 | + const children = [makeChild("!c", 300), makeChild("!a", 100), makeChild("!b", 200)]; |
| 64 | + const sorted = sortChildren(children).map(c => c.state_key); |
| 65 | + expect(sorted).toEqual(["!a", "!b", "!c"]); |
| 66 | + }); |
| 67 | + |
| 68 | + it("places ordered children before unordered", () => { |
| 69 | + const children = [makeChild("!unordered", 1), makeChild("!ordered", 999, "1")]; |
| 70 | + const sorted = sortChildren(children).map(c => c.state_key); |
| 71 | + expect(sorted).toEqual(["!ordered", "!unordered"]); |
| 72 | + }); |
| 73 | + |
| 74 | + it("treats empty string order as no order", () => { |
| 75 | + const children = [makeChild("!empty-order", 1, ""), makeChild("!ordered", 999, "1")]; |
| 76 | + const sorted = sortChildren(children).map(c => c.state_key); |
| 77 | + expect(sorted).toEqual(["!ordered", "!empty-order"]); |
| 78 | + }); |
| 79 | + |
| 80 | + it("falls back to origin_server_ts then state_key when order values are equal", () => { |
| 81 | + const children = [makeChild("!z", 2, "1"), makeChild("!a", 1, "1")]; |
| 82 | + const sorted = sortChildren(children).map(c => c.state_key); |
| 83 | + expect(sorted).toEqual(["!a", "!z"]); |
| 84 | + }); |
| 85 | + |
| 86 | + it("uses state_key as final tiebreaker when origin_server_ts equal and no order", () => { |
| 87 | + const children = [makeChild("!z", 100), makeChild("!a", 100)]; |
| 88 | + const sorted = sortChildren(children).map(c => c.state_key); |
| 89 | + expect(sorted).toEqual(["!a", "!z"]); |
| 90 | + }); |
| 91 | + |
| 92 | + it("does not mutate the original array", () => { |
| 93 | + const children = [makeChild("!b", 1, "2"), makeChild("!a", 2, "1")]; |
| 94 | + const original = [...children]; |
| 95 | + sortChildren(children); |
| 96 | + expect(children).toEqual(original); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe("buildTree", () => { |
| 101 | + it("returns empty array for empty input", () => { |
| 102 | + expect(buildTree([])).toEqual([]); |
| 103 | + }); |
| 104 | + |
| 105 | + it("respects order field when building children", () => { |
| 106 | + const root = makeRoom("!root", [makeChild("!c", 1, "3"), makeChild("!a", 2, "1"), makeChild("!b", 3, "2")]); |
| 107 | + const roomA = makeRoom("!a"); |
| 108 | + const roomB = makeRoom("!b"); |
| 109 | + const roomC = makeRoom("!c"); |
| 110 | + const [rootNode] = buildTree([root, roomC, roomA, roomB]); |
| 111 | + expect(rootNode.children.map(n => n.room.room_id)).toEqual(["!a", "!b", "!c"]); |
| 112 | + }); |
| 113 | + |
| 114 | + it("handles cycle detection correctly after sort", () => { |
| 115 | + const roomA = makeRoom("!a", [makeChild("!b", 1)]); |
| 116 | + const roomB = makeRoom("!b", [makeChild("!a", 2)]); |
| 117 | + const [rootNode] = buildTree([roomA, roomB]); |
| 118 | + expect(rootNode.children).toHaveLength(1); |
| 119 | + expect(rootNode.children[0].children).toHaveLength(0); |
| 120 | + }); |
| 121 | + |
| 122 | + it("appends orphan rooms after root", () => { |
| 123 | + const root = makeRoom("!root"); |
| 124 | + const orphan = makeRoom("!orphan"); |
| 125 | + const nodes = buildTree([root, orphan]); |
| 126 | + expect(nodes).toHaveLength(2); |
| 127 | + expect(nodes[0].room.room_id).toBe("!root"); |
| 128 | + expect(nodes[1].room.room_id).toBe("!orphan"); |
| 129 | + }); |
| 130 | +}); |
0 commit comments