203 lines
9.8 KiB
TypeScript
203 lines
9.8 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
||
import { foldReactions, parseReaction, parseReactions, reactionPreview, reactionText, sameEmoji } from '@shared/reactions'
|
||
|
||
let nextId = 1
|
||
const msg = (body: string, opts: { out?: boolean; sender?: string; media?: boolean; ts?: number } = {}) => ({
|
||
id: nextId++,
|
||
body,
|
||
ts: opts.ts ?? nextId * 1000,
|
||
direction: (opts.out ? 'out' : 'in') as 'in' | 'out',
|
||
contact: '5550001111,5550002222',
|
||
...(opts.sender ? { sender: opts.sender } : {}),
|
||
...(opts.media ? { media: [{ file: 'x.jpg' }] } : {})
|
||
})
|
||
|
||
describe('parseReaction', () => {
|
||
it('reads the classic tapbacks', () => {
|
||
expect(parseReaction('Loved “see you at 9”')).toEqual({ emoji: '❤️', removed: false, target: { text: 'see you at 9' } })
|
||
expect(parseReaction('Laughed at "lol ok"')?.emoji).toBe('😂')
|
||
expect(parseReaction('Emphasized “Bring a jacket”')?.emoji).toBe('‼️')
|
||
expect(parseReaction('Questioned “what time”')?.emoji).toBe('❓')
|
||
expect(parseReaction('Disliked “Monday”')?.emoji).toBe('👎')
|
||
expect(parseReaction('Liked “a quote with “nested” quotes”')?.target).toEqual({ text: 'a quote with “nested” quotes' })
|
||
})
|
||
|
||
it('reads emoji reactions, removals and media', () => {
|
||
expect(parseReaction('Reacted 🎉 to “we won”')).toEqual({ emoji: '🎉', removed: false, target: { text: 'we won' } })
|
||
expect(parseReaction('Removed a heart from “see you at 9”')).toEqual({ emoji: '❤️', removed: true, target: { text: 'see you at 9' } })
|
||
expect(parseReaction('Liked an image')).toEqual({ emoji: '👍', removed: false, target: { media: true } })
|
||
expect(parseReaction('Loved a photo')?.target).toEqual({ media: true })
|
||
})
|
||
|
||
it('leaves ordinary texts alone', () => {
|
||
expect(parseReaction('Loved it, thanks!')).toBeNull()
|
||
expect(parseReaction('Liked the movie a lot')).toBeNull()
|
||
expect(parseReaction('see you at 9')).toBeNull()
|
||
})
|
||
})
|
||
|
||
describe('foldReactions', () => {
|
||
it('moves reactions onto the quoted message and hides their texts', () => {
|
||
const a = msg('see you at 9', { out: true })
|
||
const b = msg('Loved “see you at 9”', { sender: '5550001111' })
|
||
const c = msg('Laughed at “see you at 9”', { sender: '5550002222' })
|
||
const { shown, reactions } = foldReactions([a, b, c])
|
||
expect(shown).toEqual([a])
|
||
expect(reactions.get(a.id)?.map((r) => [r.emoji, r.from])).toEqual([
|
||
['❤️', '5550001111'],
|
||
['😂', '5550002222']
|
||
])
|
||
})
|
||
|
||
it('keeps one reaction per person; a newer one replaces it, a removal clears it', () => {
|
||
const a = msg('dinner?')
|
||
const b = msg('Liked “dinner?”', { sender: '5550001111' })
|
||
const c = msg('Loved “dinner?”', { sender: '5550001111' })
|
||
const d = msg('Questioned “dinner?”', { sender: '5550002222' })
|
||
const e = msg('Removed a question mark from “dinner?”', { sender: '5550002222' })
|
||
const { shown, reactions } = foldReactions([a, b, c, d, e])
|
||
expect(shown).toEqual([a])
|
||
expect(reactions.get(a.id)?.map((r) => r.emoji)).toEqual(['❤️'])
|
||
})
|
||
|
||
it('finds the nearest earlier match, photos, and cut-off quotes', () => {
|
||
const old = msg('ok')
|
||
const photo = msg('', { media: true, out: true })
|
||
const recent = msg('ok')
|
||
const long = msg('This is a long message that the other phone will only quote the start of')
|
||
const r1 = msg('Liked “ok”', { sender: '5550001111' })
|
||
const r2 = msg('Loved an image', { sender: '5550001111' })
|
||
const r3 = msg('Emphasized “This is a long message that the other…”', { sender: '5550002222' })
|
||
const { reactions } = foldReactions([old, photo, recent, long, r1, r2, r3])
|
||
expect(reactions.has(old.id)).toBe(false)
|
||
expect(reactions.get(recent.id)?.[0].emoji).toBe('👍')
|
||
expect(reactions.get(photo.id)?.[0].emoji).toBe('❤️')
|
||
expect(reactions.get(long.id)?.[0].emoji).toBe('‼️')
|
||
})
|
||
|
||
it('ignores straight vs curly apostrophes', () => {
|
||
const a = msg("I'll be there", { out: true })
|
||
const r = msg('Loved “I’ll be there”', { sender: '5550001111' })
|
||
expect(foldReactions([a, r]).reactions.get(a.id)?.[0].emoji).toBe('❤️')
|
||
})
|
||
|
||
it("leaves a reaction it can't place as a normal message", () => {
|
||
const a = msg('hello')
|
||
const r = msg('Loved “something from last year”')
|
||
const { shown, reactions } = foldReactions([a, r])
|
||
expect(shown).toEqual([a, r])
|
||
expect(reactions.size).toBe(0)
|
||
})
|
||
})
|
||
|
||
describe('reactionPreview', () => {
|
||
it('summarises for the chat list', () => {
|
||
expect(reactionPreview('Loved “see you at 9”')).toBe('❤️ to “see you at 9”')
|
||
expect(reactionPreview('Liked an image')).toBe('👍 to a photo')
|
||
expect(reactionPreview('just text')).toBeNull()
|
||
})
|
||
})
|
||
|
||
describe('other languages', () => {
|
||
const emoji = (body: string) => parseReaction(body)?.emoji
|
||
it('reads Spanish, Portuguese, French, German, Italian tapbacks', () => {
|
||
expect(emoji('Le encanta “nos vemos”')).toBe('❤️')
|
||
expect(emoji('Le encantó “nos vemos”')).toBe('❤️')
|
||
expect(emoji('Le gusta “nos vemos”')).toBe('👍')
|
||
expect(emoji('No le gustó “nos vemos”')).toBe('👎')
|
||
expect(emoji('Le hace gracia “nos vemos”')).toBe('😂')
|
||
expect(emoji('Se rió de “nos vemos”')).toBe('😂')
|
||
expect(emoji('Enfatizó “nos vemos”')).toBe('‼️')
|
||
expect(emoji('Cuestionó “nos vemos”')).toBe('❓')
|
||
expect(emoji('Reaccionó con 🎉 a “nos vemos”')).toBe('🎉')
|
||
expect(emoji('Amou “até já”')).toBe('❤️')
|
||
expect(emoji('Curtiu “até já”')).toBe('👍')
|
||
expect(emoji('Não curtiu “até já”')).toBe('👎')
|
||
expect(emoji('Riu de “até já”')).toBe('😂')
|
||
expect(emoji('A adoré « à plus »')).toBe('❤️')
|
||
expect(emoji('A aimé « à plus »')).toBe('👍')
|
||
expect(emoji('N’a pas aimé « à plus »')).toBe('👎')
|
||
expect(parseReaction('A aimé « à plus »')?.target).toEqual({ text: 'à plus' })
|
||
expect(emoji('Hat „bis dann“ geliebt')).toBe('❤️')
|
||
expect(emoji('Hat über „bis dann“ gelacht')).toBe('😂')
|
||
expect(emoji('Gefällt „bis dann“ nicht')).toBe('👎')
|
||
expect(emoji('Ha messo Mi piace a “a dopo”')).toBe('👍')
|
||
expect(emoji('Le encantó una imagen')).toBe('❤️')
|
||
expect(parseReaction('Le encantó una imagen')?.target).toEqual({ media: true })
|
||
})
|
||
|
||
it('reads removals, even with the reaction quoted too', () => {
|
||
const r = parseReactions('Quitó un “me gusta” de “nos vemos”')
|
||
expect(r.some((x) => x.removed && x.emoji === '👍' && 'text' in x.target && x.target.text === 'nos vemos')).toBe(true)
|
||
expect(parseReaction('Quitó un corazón de “nos vemos”')).toMatchObject({ emoji: '❤️', removed: true })
|
||
})
|
||
|
||
it('leaves people writing alone', () => {
|
||
expect(parseReaction('Me encanta “Bad Bunny”')).toBeNull()
|
||
expect(parseReaction('I love “Bad Bunny”')).toBeNull()
|
||
expect(parseReaction('Mi canción favorita es “Despacito”, ¿y la tuya?')).toBeNull()
|
||
expect(parseReaction('nos vemos')).toBeNull()
|
||
})
|
||
|
||
it('folds only onto a message it quotes', () => {
|
||
const a = msg('nos vemos a las 9', { out: true })
|
||
const b = msg('Le encanta “nos vemos a las 9”', { sender: '5550001111' })
|
||
const c = msg('Le encanta “Bad Bunny”', { sender: '5550002222' })
|
||
const { shown, reactions } = foldReactions([a, b, c])
|
||
expect(shown).toEqual([a, c])
|
||
expect(reactions.get(a.id)?.[0].emoji).toBe('❤️')
|
||
})
|
||
|
||
it('keeps a failed reaction in view', () => {
|
||
const a = msg('hi')
|
||
const r = { ...msg('Loved “hi”', { out: true }), status: 'failed' }
|
||
expect(foldReactions([a, r]).shown).toEqual([a, r])
|
||
})
|
||
|
||
it('shows loose readings in previews only once placed', () => {
|
||
expect(reactionPreview('Le encanta “nos vemos”')).toBeNull()
|
||
})
|
||
})
|
||
|
||
describe('reactionText', () => {
|
||
it('words a reaction like an iPhone', () => {
|
||
expect(reactionText('❤️', { body: 'see you at 9' })).toBe('Loved “see you at 9”')
|
||
expect(reactionText('❤', { body: 'x' })).toBe('Loved “x”')
|
||
expect(reactionText('😂', { body: 'lol' })).toBe('Laughed at “lol”')
|
||
expect(reactionText('🎉', { body: 'we won' })).toBe('Reacted 🎉 to “we won”')
|
||
expect(reactionText('👍', { body: '', media: [{}] })).toBe('Liked an image')
|
||
expect(reactionText('❤️', { body: 'hi' }, true)).toBe('Removed a heart from “hi”')
|
||
expect(reactionText('🎉', { body: 'hi' }, true)).toBe('Removed a 🎉 from “hi”')
|
||
})
|
||
|
||
it('quotes their text exactly, so their phone can find it', () => {
|
||
expect(reactionText('👍', { body: ' I’m home\nsee you ' })).toBe('Liked “I’m home\nsee you”')
|
||
const a = msg('I’m home\nsee you')
|
||
const r = msg(reactionText('👍', a), { out: true })
|
||
expect(foldReactions([a, r]).reactions.get(a.id)?.[0].emoji).toBe('👍')
|
||
})
|
||
|
||
it('round-trips through the parser, long messages cut short', () => {
|
||
const long = 'word '.repeat(60).trim()
|
||
for (const e of ['❤️', '👍', '👎', '😂', '‼️', '❓', '🎉', '👨👩👧']) {
|
||
for (const removed of [false, true]) {
|
||
const a = msg(long)
|
||
const r = msg(reactionText(e, a, removed), { out: true })
|
||
const p = parseReaction(r.body)
|
||
expect(p?.removed).toBe(removed)
|
||
expect(sameEmoji(p?.emoji, e)).toBe(true)
|
||
if (!removed) expect(foldReactions([a, r]).reactions.get(a.id)?.[0].emoji).toBe(p?.emoji)
|
||
}
|
||
}
|
||
})
|
||
|
||
it('a second reaction replaces mine; the same one again takes it back', () => {
|
||
const a = msg('dinner?')
|
||
const r1 = msg(reactionText('👍', a), { out: true })
|
||
const r2 = msg(reactionText('❤️', a), { out: true })
|
||
expect(foldReactions([a, r1, r2]).reactions.get(a.id)?.map((r) => r.emoji)).toEqual(['❤️'])
|
||
const r3 = msg(reactionText('❤️', a, true), { out: true })
|
||
expect(foldReactions([a, r1, r2, r3]).reactions.has(a.id)).toBe(false)
|
||
})
|
||
})
|