|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { test } from "node:test"; |
| 3 | + |
| 4 | +import { buildGroupedMembershipPayload } from "./membershipGroupPayload.ts"; |
| 5 | +import { buildTimelineItems } from "./timelineItems.ts"; |
| 6 | +import { KIND_SYSTEM_MESSAGE } from "../../../shared/constants/kinds.ts"; |
| 7 | + |
| 8 | +const DAY_START = Math.floor(Date.UTC(2026, 5, 14, 12, 0, 0) / 1000); |
| 9 | + |
| 10 | +function systemEntry(id, createdAt, body) { |
| 11 | + return { |
| 12 | + message: { |
| 13 | + author: "System", |
| 14 | + body: JSON.stringify(body), |
| 15 | + createdAt, |
| 16 | + depth: 0, |
| 17 | + id, |
| 18 | + kind: KIND_SYSTEM_MESSAGE, |
| 19 | + pubkey: "aa".repeat(32), |
| 20 | + reactions: [], |
| 21 | + time: "12:00 PM", |
| 22 | + }, |
| 23 | + summary: null, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +// One symbol per membership mechanism the relay can emit, so the matrix covers |
| 28 | +// self-arrival, addition (same and different actor), and departure — for two |
| 29 | +// distinct targets, which is what makes same-target vs cross-target grouping |
| 30 | +// observable. |
| 31 | +const SYMBOLS = { |
| 32 | + "self-join(a)": { type: "member_joined", actor: "aaa", target: "aaa" }, |
| 33 | + "self-join(b)": { type: "member_joined", actor: "bbb", target: "bbb" }, |
| 34 | + "add(a by x)": { type: "member_joined", actor: "xxx", target: "aaa" }, |
| 35 | + "add(b by x)": { type: "member_joined", actor: "xxx", target: "bbb" }, |
| 36 | + "add(b by y)": { type: "member_joined", actor: "yyy", target: "bbb" }, |
| 37 | + "leave(a)": { type: "member_left", actor: "aaa" }, |
| 38 | + "leave(b)": { type: "member_left", actor: "bbb" }, |
| 39 | +}; |
| 40 | + |
| 41 | +const SYMBOL_NAMES = Object.keys(SYMBOLS); |
| 42 | + |
| 43 | +function sequencesOfLength(length) { |
| 44 | + if (length === 0) return [[]]; |
| 45 | + return sequencesOfLength(length - 1).flatMap((prefix) => |
| 46 | + SYMBOL_NAMES.map((name) => [...prefix, name]), |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +function entriesFor(sequence) { |
| 51 | + return sequence.map((name, index) => |
| 52 | + systemEntry(`e${index}`, DAY_START + index * 30, SYMBOLS[name]), |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * The seam this guards: `buildTimelineItems` owns *which* contiguous membership |
| 58 | + * events collapse into one `system-group`, and `buildGroupedMembershipPayload` |
| 59 | + * owns *how* that group is described. They are two statements of one rule. |
| 60 | + * |
| 61 | + * When the builder emits a group the payload builder returns null for, |
| 62 | + * `SystemMessageRow` falls back to the group's oldest message and every other |
| 63 | + * event in the group vanishes from the timeline — a member who left still |
| 64 | + * renders as present. That failure is silent, so it needs a standing guard |
| 65 | + * rather than one case-by-case regression per shape. |
| 66 | + */ |
| 67 | +test("every membership group buildTimelineItems emits is describable", () => { |
| 68 | + const undescribable = []; |
| 69 | + let groupsChecked = 0; |
| 70 | + |
| 71 | + for (const length of [2, 3, 4]) { |
| 72 | + for (const sequence of sequencesOfLength(length)) { |
| 73 | + const { items } = buildTimelineItems(entriesFor(sequence), null); |
| 74 | + for (const item of items) { |
| 75 | + if (item.kind !== "system-group") continue; |
| 76 | + groupsChecked += 1; |
| 77 | + const payload = buildGroupedMembershipPayload( |
| 78 | + item.entries.map((entry) => entry.message), |
| 79 | + ); |
| 80 | + if (!payload) { |
| 81 | + undescribable.push( |
| 82 | + `${sequence.join(" → ")} [group: ${item.entries |
| 83 | + .map((entry) => entry.message.id) |
| 84 | + .join(",")}]`, |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + assert.ok(groupsChecked > 0, "matrix produced no groups to check"); |
| 92 | + assert.deepEqual( |
| 93 | + undescribable.slice(0, 10), |
| 94 | + [], |
| 95 | + `${undescribable.length} grouped shape(s) render as the oldest event only`, |
| 96 | + ); |
| 97 | +}); |
| 98 | + |
| 99 | +test("a lifecycle group needs every arrival to be a self-join by the departing member", () => { |
| 100 | + const elrond = "11".repeat(32); |
| 101 | + const viewer = "10".repeat(32); |
| 102 | + const message = (id, body) => ({ |
| 103 | + author: "System", |
| 104 | + body: JSON.stringify(body), |
| 105 | + createdAt: 1, |
| 106 | + depth: 0, |
| 107 | + id, |
| 108 | + kind: KIND_SYSTEM_MESSAGE, |
| 109 | + reactions: [], |
| 110 | + time: "12:00 PM", |
| 111 | + }); |
| 112 | + const selfJoin = (id) => |
| 113 | + message(id, { type: "member_joined", actor: elrond, target: elrond }); |
| 114 | + const leave = (id) => message(id, { type: "member_left", actor: elrond }); |
| 115 | + |
| 116 | + // Accepted: one or more equivalent self-arrivals, then that member departing. |
| 117 | + for (const arrivals of [1, 2, 3]) { |
| 118 | + const messages = [ |
| 119 | + ...Array.from({ length: arrivals }, (_, index) => selfJoin(`j${index}`)), |
| 120 | + leave("l"), |
| 121 | + ]; |
| 122 | + assert.equal( |
| 123 | + buildGroupedMembershipPayload(messages)?.type, |
| 124 | + "member_joined_then_left", |
| 125 | + `${arrivals} self-arrival(s) + departure should be one lifecycle summary`, |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + // Rejected: anything else. Each of these would misattribute a self-join the |
| 130 | + // member never made, or fold in an unrelated member's activity. |
| 131 | + const rejected = { |
| 132 | + "addition, not a self-join": [ |
| 133 | + message("a", { type: "member_joined", actor: viewer, target: elrond }), |
| 134 | + leave("l"), |
| 135 | + ], |
| 136 | + "one arrival is an addition": [ |
| 137 | + selfJoin("j0"), |
| 138 | + message("a", { type: "member_joined", actor: viewer, target: elrond }), |
| 139 | + leave("l"), |
| 140 | + ], |
| 141 | + "a different member departs": [ |
| 142 | + selfJoin("j0"), |
| 143 | + message("l", { type: "member_left", actor: viewer }), |
| 144 | + ], |
| 145 | + "departure is not last": [leave("l"), selfJoin("j0")], |
| 146 | + "no departure at all": [selfJoin("j0"), selfJoin("j1")], |
| 147 | + }; |
| 148 | + for (const [name, messages] of Object.entries(rejected)) { |
| 149 | + assert.notEqual( |
| 150 | + buildGroupedMembershipPayload(messages)?.type, |
| 151 | + "member_joined_then_left", |
| 152 | + `${name} must not render as a lifecycle summary`, |
| 153 | + ); |
| 154 | + } |
| 155 | +}); |
0 commit comments