|
1 | | -import { describe, expect, it, vi } from 'vitest'; |
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type * as MatrixUtils from '$utils/matrix'; |
2 | 3 | import { MsgType, type MatrixClient } from '$types/matrix-sdk'; |
3 | 4 | import type { TUploadItem } from '$state/room/roomInputDrafts'; |
4 | 5 | import { TGS_MIMETYPE } from '$utils/mimeTypes'; |
| 6 | +import { MATRIX_UNSTABLE_SPOILER_PROPERTY_NAME } from '$unstable/prefixes'; |
5 | 7 | import { getGalleryItemContent, getGifMsgContent, getImageMsgContent } from './msgContent'; |
6 | 8 |
|
| 9 | +const { fetchMock, uploadMock, encryptFileMock } = vi.hoisted(() => ({ |
| 10 | + fetchMock: vi.fn<(url: string) => Promise<Response>>(), |
| 11 | + uploadMock: vi.fn<(mx: unknown, file: File) => Promise<{ content_uri?: string }>>(), |
| 12 | + encryptFileMock: vi.fn<(file: File) => Promise<{ file: File; encInfo: object }>>(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock('$utils/fetch', () => ({ fetch: fetchMock })); |
| 16 | +vi.mock('$utils/matrix', async (importOriginal) => ({ |
| 17 | + ...(await importOriginal<typeof MatrixUtils>()), |
| 18 | + uploadContentToServer: uploadMock, |
| 19 | + encryptFile: encryptFileMock, |
| 20 | +})); |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + fetchMock.mockReset(); |
| 24 | + uploadMock.mockReset(); |
| 25 | + encryptFileMock.mockReset(); |
| 26 | +}); |
| 27 | + |
7 | 28 | vi.mock('$utils/dom', () => ({ |
8 | 29 | getImageFileUrl: vi.fn<(file: File | Blob) => string>(() => 'blob:test'), |
9 | 30 | loadImageElement: vi |
@@ -56,54 +77,75 @@ describe('TGS message content', () => { |
56 | 77 | }); |
57 | 78 | }); |
58 | 79 |
|
59 | | -describe('KLIPY message content', () => { |
60 | | - it('sends a direct-link text event without fetching media', () => { |
61 | | - expect( |
62 | | - getGifMsgContent({ |
63 | | - id: 'gif-id', |
64 | | - title: 'Reaction', |
65 | | - shareUrl: 'https://klipy.com/gif/gif-id', |
66 | | - mediaUrl: 'https://static.klipy.com/ii/reaction.gif', |
67 | | - width: 480, |
68 | | - height: 270, |
69 | | - mimetype: 'image/gif', |
70 | | - }) |
71 | | - ).toEqual({ |
72 | | - msgtype: MsgType.Text, |
73 | | - body: 'https://klipy.com/gif/gif-id', |
74 | | - 'pet.plz.gif': { |
75 | | - v: 1, |
76 | | - provider: 'klipy', |
77 | | - media_url: 'https://static.klipy.com/ii/reaction.gif', |
78 | | - w: 480, |
79 | | - h: 270, |
80 | | - mimetype: 'image/gif', |
81 | | - id: 'gif-id', |
82 | | - title: 'Reaction', |
83 | | - }, |
| 80 | +describe('GIF message content', () => { |
| 81 | + const searchResult = { |
| 82 | + id: 'gif-id', |
| 83 | + title: 'Reaction', |
| 84 | + shareUrl: 'https://tenor.com/view/gif-id', |
| 85 | + mediaUrl: 'https://media.tenor.com/gif-id/reaction.gif', |
| 86 | + width: 480, |
| 87 | + height: 270, |
| 88 | + mimetype: 'image/gif', |
| 89 | + }; |
| 90 | + |
| 91 | + it('uploads the gif and sends it as an image event', async () => { |
| 92 | + fetchMock.mockResolvedValue(new Response('gif-bytes', { status: 200 })); |
| 93 | + uploadMock.mockResolvedValue({ content_uri: 'mxc://server/uploaded' }); |
| 94 | + |
| 95 | + const content = await getGifMsgContent({} as MatrixClient, searchResult, { encrypt: false }); |
| 96 | + |
| 97 | + expect(fetchMock).toHaveBeenCalledWith(searchResult.mediaUrl); |
| 98 | + expect(encryptFileMock).not.toHaveBeenCalled(); |
| 99 | + expect(content).toEqual({ |
| 100 | + msgtype: MsgType.Image, |
| 101 | + body: 'Reaction.gif', |
| 102 | + url: 'mxc://server/uploaded', |
| 103 | + info: { w: 480, h: 270, mimetype: 'image/gif', size: 9 }, |
84 | 104 | }); |
85 | 105 | }); |
86 | 106 |
|
87 | | - it('keeps Matrix MXC favorites on the standard image path', () => { |
88 | | - expect( |
89 | | - getGifMsgContent({ |
90 | | - id: 'matrix-gif', |
91 | | - title: 'Favorite', |
92 | | - shareUrl: 'mxc://matrix.example/media-id', |
93 | | - mediaUrl: 'mxc://matrix.example/media-id', |
94 | | - width: 320, |
95 | | - height: 240, |
96 | | - mimetype: 'image/gif', |
97 | | - }) |
98 | | - ).toMatchObject({ |
| 107 | + it('encrypts the upload for encrypted rooms', async () => { |
| 108 | + fetchMock.mockResolvedValue(new Response('gif-bytes', { status: 200 })); |
| 109 | + uploadMock.mockResolvedValue({ content_uri: 'mxc://server/encrypted' }); |
| 110 | + encryptFileMock.mockImplementation(async (file: File) => ({ |
| 111 | + file, |
| 112 | + encInfo: { key: { k: 'secret' } }, |
| 113 | + })); |
| 114 | + |
| 115 | + const content = await getGifMsgContent({} as MatrixClient, searchResult, { |
| 116 | + encrypt: true, |
| 117 | + spoiler: true, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(content?.url).toBeUndefined(); |
| 121 | + expect(content?.file).toEqual({ key: { k: 'secret' }, url: 'mxc://server/encrypted' }); |
| 122 | + expect(content?.[MATRIX_UNSTABLE_SPOILER_PROPERTY_NAME]).toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + it('sends favorited homeserver gifs without re-uploading', async () => { |
| 126 | + const content = await getGifMsgContent( |
| 127 | + {} as MatrixClient, |
| 128 | + { ...searchResult, mediaUrl: 'mxc://matrix.example/media-id' }, |
| 129 | + { encrypt: false } |
| 130 | + ); |
| 131 | + |
| 132 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 133 | + expect(content).toMatchObject({ |
99 | 134 | msgtype: MsgType.Image, |
100 | | - body: 'Favorite', |
| 135 | + body: 'Reaction.gif', |
101 | 136 | url: 'mxc://matrix.example/media-id', |
102 | | - info: { |
103 | | - w: 320, |
104 | | - h: 240, |
105 | | - mimetype: 'image/gif', |
106 | | - }, |
| 137 | + info: { w: 480, h: 270, mimetype: 'image/gif' }, |
107 | 138 | }); |
108 | 139 | }); |
| 140 | + |
| 141 | + it('refuses media URLs outside the configured providers', async () => { |
| 142 | + await expect( |
| 143 | + getGifMsgContent( |
| 144 | + {} as MatrixClient, |
| 145 | + { ...searchResult, mediaUrl: 'https://media.tenor.com.attacker.example/a.gif' }, |
| 146 | + { encrypt: false } |
| 147 | + ) |
| 148 | + ).resolves.toBeUndefined(); |
| 149 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 150 | + }); |
109 | 151 | }); |
0 commit comments