Skip to content

Commit 604b0f2

Browse files
committed
add tests
Signed-off-by: Timo K <[email protected]>
1 parent 5358215 commit 604b0f2

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

spec/unit/matrixrtc/types.spec.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
Copyright 2025 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { type CallMembership } from "../../../src/matrixrtc";
18+
import { isMyMembership, parseCallNotificationContent } from "../../../src/matrixrtc/types";
19+
20+
describe("types", () => {
21+
describe("isMyMembership", () => {
22+
it("returns false if userId is different", () => {
23+
expect(
24+
isMyMembership(
25+
{ sender: "@alice:example.org", deviceId: "DEVICE" } as CallMembership,
26+
"@bob:example.org",
27+
"DEVICE",
28+
),
29+
).toBe(false);
30+
});
31+
it("returns true if userId and device is the same", () => {
32+
expect(
33+
isMyMembership(
34+
{ sender: "@alice:example.org", deviceId: "DEVICE" } as CallMembership,
35+
"@alice:example.org",
36+
"DEVICE",
37+
),
38+
).toBe(true);
39+
});
40+
});
41+
});
42+
43+
describe("IRTCNotificationContent", () => {
44+
const validBase = Object.freeze({
45+
"m.mentions": { user_ids: [], room: true },
46+
"notification_type": "notification",
47+
"sender_ts": 123,
48+
"lifetime": 1000,
49+
});
50+
51+
it("parses valid content", () => {
52+
const res = parseCallNotificationContent({ ...validBase });
53+
expect(res).toMatchObject(validBase);
54+
});
55+
56+
it("caps lifetime to 120000ms", () => {
57+
const res = parseCallNotificationContent({ ...validBase, lifetime: 130000 });
58+
expect(res.lifetime).toBe(120000);
59+
});
60+
61+
it("throws on missing m.mentions", () => {
62+
expect(() =>
63+
parseCallNotificationContent({
64+
notification_type: "notification",
65+
sender_ts: 123,
66+
lifetime: 1000,
67+
} as any),
68+
).toThrow("Missing m.mentions");
69+
});
70+
71+
it("throws on missing or invalid notification_type", () => {
72+
expect(() =>
73+
parseCallNotificationContent({
74+
...validBase,
75+
notification_type: undefined,
76+
} as any),
77+
).toThrow("Missing or invalid notification_type");
78+
79+
expect(() =>
80+
parseCallNotificationContent({
81+
...validBase,
82+
notification_type: 123 as any,
83+
} as any),
84+
).toThrow("Missing or invalid notification_type");
85+
});
86+
87+
it("throws on missing or invalid sender_ts", () => {
88+
expect(() =>
89+
parseCallNotificationContent({
90+
...validBase,
91+
sender_ts: undefined,
92+
} as any),
93+
).toThrow("Missing or invalid sender_ts");
94+
95+
expect(() =>
96+
parseCallNotificationContent({
97+
...validBase,
98+
sender_ts: "123" as any,
99+
} as any),
100+
).toThrow("Missing or invalid sender_ts");
101+
});
102+
103+
it("throws on missing or invalid lifetime", () => {
104+
expect(() =>
105+
parseCallNotificationContent({
106+
...validBase,
107+
lifetime: undefined,
108+
} as any),
109+
).toThrow("Missing or invalid lifetime");
110+
111+
expect(() =>
112+
parseCallNotificationContent({
113+
...validBase,
114+
lifetime: "1000" as any,
115+
} as any),
116+
).toThrow("Missing or invalid lifetime");
117+
});
118+
119+
it("throws on invalid decline_reason type", () => {
120+
expect(() =>
121+
parseCallNotificationContent({
122+
...validBase,
123+
decline_reason: 42 as any,
124+
} as any),
125+
).toThrow("Invalid decline_reason");
126+
});
127+
128+
it("accepts valid relation (m.reference)", () => {
129+
// Note: parseCallNotificationContent currently checks `relation.rel_type` rather than `m.relates_to`.
130+
const res = parseCallNotificationContent({
131+
...validBase,
132+
relation: { rel_type: "m.reference", event_id: "$ev" },
133+
} as any);
134+
expect(res).toBeTruthy();
135+
});
136+
137+
it("throws on invalid relation rel_type", () => {
138+
expect(() =>
139+
parseCallNotificationContent({
140+
...validBase,
141+
relation: { rel_type: "m.annotation", event_id: "$ev" },
142+
} as any),
143+
).toThrow("Invalid relation");
144+
});
145+
});

src/matrixrtc/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export function parseCallNotificationContent(content: IContent): IRTCNotificatio
135135
}
136136

137137
const cappedLifetime = content["lifetime"] >= 120000 ? 120000 : content["lifetime"];
138-
return { lifetime: cappedLifetime, ...content } as IRTCNotificationContent;
138+
return { ...content, lifetime: cappedLifetime } as IRTCNotificationContent;
139139
}
140140

141141
/**

0 commit comments

Comments
 (0)