Add consent-gated media exchange library

This commit is contained in:
jjbrunton committed 2026-08-09 19:24:38 +01:00
1 parent b8e4a1bb88
commit bf474dfe10
42 files changed
+2533 -9

No files matched your search

+32
View File
@@ -14,6 +14,38 @@ const creds = {
capturedAt: Date.now(),
};
describe('MatrixClient outbound image media', () => {
it('uploads raw bytes and sends an m.image event with a stable transaction ID', async () => {
const calls: Array<{ url: string; init?: RequestInit }> = [];
const fetchImpl = ((url: string | URL | Request, init?: RequestInit) => {
calls.push({ url: String(url), init });
if (String(url).includes('/upload')) {
return Promise.resolve(new Response(JSON.stringify({ content_uri: 'mxc://reddit.com/image-one' }), {
status: 200, headers: { 'content-type': 'application/json' },
}));
}
return Promise.resolve(new Response(JSON.stringify({ event_id: '$image-event' }), {
status: 200, headers: { 'content-type': 'application/json' },
}));
}) as typeof fetch;
const client = new MatrixClient(creds, createMockLogger(), { minRequestIntervalMs: 0, fetchImpl });
const mxc = await client.uploadImage(Buffer.from([1, 2, 3]), 'image/jpeg', 'portrait.jpg');
await expect(client.sendImageMessage(
'!room:reddit.com', 'portrait.jpg', mxc,
{ mimeType: 'image/jpeg', size: 3, width: 10, height: 20 }, 'stable-image-txn',
)).resolves.toBe('$image-event');
expect(calls[0].url).toContain('/_matrix/media/v3/upload?filename=portrait.jpg');
expect(new Headers(calls[0].init?.headers).get('content-type')).toBe('image/jpeg');
expect(calls[1].url).toContain('/stable-image-txn');
expect(JSON.parse(String(calls[1].init?.body))).toEqual({
msgtype: 'm.image', body: 'portrait.jpg', url: 'mxc://reddit.com/image-one',
info: { mimetype: 'image/jpeg', size: 3, w: 10, h: 20 },
});
});
});
describe('MatrixClient HTTP timeout', () => {
it('loads an encoded Matrix user profile with bearer authentication', async () => {
let requestedUrl = '';