59 lines
2.0 KiB
TypeScript
59 lines
2.0 KiB
TypeScript
import { test } from 'node:test';
|
|||
|
|
import assert from 'node:assert/strict';
|
||
|
|
import { buildReplyContent, replySendUrl, sendNotificationReply } from './swReply';
|
||
|
|
|
||
|
|
test('thread replies carry the m.thread relation, room replies do not', () => {
|
||
|
|
assert.deepEqual(buildReplyContent('hi'), { msgtype: 'm.text', body: 'hi', 'm.mentions': {} });
|
||
|
|
assert.deepEqual(buildReplyContent('hi', '$root')['m.relates_to'], {
|
||
|
|
rel_type: 'm.thread',
|
||
|
|
event_id: '$root',
|
||
|
|
is_falling_back: true,
|
||
|
|
'm.in_reply_to': { event_id: '$root' },
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
test('send URL encodes the room id against the session base URL', () => {
|
||
|
|
assert.equal(
|
||
|
|
replySendUrl('https://matrix.example.org/', '!r:example.org', 'tx1'),
|
||
|
|
'https://matrix.example.org/_matrix/client/v3/rooms/!r%3Aexample.org/send/m.room.message/tx1',
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('sendNotificationReply PUTs with the bearer token and reports ok / failure', async () => {
|
||
|
|
const calls: { url: string; init: RequestInit }[] = [];
|
||
|
|
const fetchOk = (async (url: string, init: RequestInit) => {
|
||
|
|
calls.push({ url, init });
|
||
|
|
return { ok: true } as Response;
|
||
|
|
}) as unknown as typeof fetch;
|
||
|
|
assert.equal(
|
||
|
|
await sendNotificationReply(
|
||
|
|
{ accessToken: 'tok', baseUrl: 'https://hs' },
|
||
|
|
'!r:hs',
|
||
|
|
'$t',
|
||
|
|
'yo',
|
||
|
|
fetchOk,
|
||
|
|
),
|
||
|
|
true,
|
||
|
|
);
|
||
|
|
assert.equal(calls[0].init.method, 'PUT');
|
||
|
|
assert.equal((calls[0].init.headers as Record<string, string>).Authorization, 'Bearer tok');
|
||
|
|
assert.match(
|
||
|
|
calls[0].url,
|
||
|
|
/^https:\/\/hs\/_matrix\/client\/v3\/rooms\/!r%3Ahs\/send\/m\.room\.message\/swreply-/,
|
||
|
|
);
|
||
|
|
assert.equal(JSON.parse(calls[0].init.body as string)['m.relates_to'].event_id, '$t');
|
||
|
|
|
||
|
|
const fetchFail = (async () => ({ ok: false }) as Response) as unknown as typeof fetch;
|
||
|
|
assert.equal(
|
||
|
|
await sendNotificationReply(
|
||
|
|
{ accessToken: 'tok', baseUrl: 'https://hs' },
|
||
|
|
'!r:hs',
|
||
|
|
undefined,
|
||
|
|
'yo',
|
||
|
|
fetchFail,
|
||
|
|
),
|
||
|
|
false,
|
||
|
|
);
|
||
|
|
assert.equal(await sendNotificationReply(undefined, '!r:hs', undefined, 'yo', fetchOk), false);
|
||
|
|
});
|