| 1 | import assert from 'node:assert/strict'; |
| 2 | import { test } from 'node:test'; |
| 3 | import { clonePackDestinations, editableChoices, packSlug, shortcodeFor, stickersOf } from '../src/lib/packs.ts'; |
| 4 | |
| 5 | const image = (shortcode: string, usage: ('Emoticon' | 'Sticker')[]) => ({ |
| 6 | shortcode, |
| 7 | media_id: `mxc://x/${shortcode}`, |
| 8 | reaction_key: `mxc://x/${shortcode}`, |
| 9 | body: null, |
| 10 | mimetype: null, |
| 11 | width: null, |
| 12 | height: null, |
| 13 | usage |
| 14 | }); |
| 15 | |
| 16 | const roomPack = (state_key: string, editable: boolean) => ({ |
| 17 | id: `room:!r:x:${state_key}`, |
| 18 | display_name: state_key, |
| 19 | avatar: null, |
| 20 | attribution: null, |
| 21 | source: { Room: { room_id: '!r:x', state_key } }, |
| 22 | images: [image('a', ['Sticker']), image('b', ['Emoticon']), image('c', [])], |
| 23 | editable, |
| 24 | enabled_everywhere: false |
| 25 | }); |
| 26 | |
| 27 | test('shortcodes come from file names and never clash', () => { |
| 28 | const taken = new Set(['cat']); |
| 29 | assert.equal(shortcodeFor('Cat.PNG', taken), 'cat_2'); |
| 30 | assert.equal(shortcodeFor('my cool sticker!.webp', taken), 'my_cool_sticker'); |
| 31 | assert.equal(shortcodeFor('.png', taken), 'image'); |
| 32 | assert.equal(packSlug('Cats & Dogs'), 'cats-dogs'); |
| 33 | }); |
| 34 | |
| 35 | test('stickers include images with no usage of their own', () => { |
| 36 | assert.deepEqual(stickersOf(roomPack('s', true)).map((i) => i.shortcode), ['a', 'c']); |
| 37 | }); |
| 38 | |
| 39 | test('your pack is always a destination, then editable room packs only', () => { |
| 40 | const packs = { global: [], rooms: { '!r:x': [roomPack('mine', true), roomPack('theirs', false)] } }; |
| 41 | const choices = editableChoices(packs, () => 'Room'); |
| 42 | assert.deepEqual(choices.map((c) => c.target), [{ kind: 'user' }, { kind: 'room', room_id: '!r:x', state_key: 'mine' }]); |
| 43 | const dests = clonePackDestinations(roomPack('Fancy Pack', false), [{ room_id: '!d:x', name: 'Dest' }], () => 'Room'); |
| 44 | assert.deepEqual(dests[1].target, { kind: 'room', room_id: '!d:x', state_key: 'fancy-pack' }); |
| 45 | }); |