|
| 1 | +// @ts-check |
| 2 | +// The "@" of a mention when a letter of another script stands in front of it. |
| 3 | +// |
| 4 | +// Chinese, Japanese kana and Korean write a word right up against the "@" ("请@某人"), so a |
| 5 | +// character of those scripts does not swallow the mention. A letter of any other script is |
| 6 | +// still the local part of an address: "josé@alice" is an address and calls nobody, the same |
| 7 | +// way "mail@alice.com" does not. The name itself still has to end at the "@", so "@alicex" is |
| 8 | +// not Alice either. |
| 9 | + |
| 10 | +import assert from 'node:assert/strict'; |
| 11 | +import { test } from 'node:test'; |
| 12 | +import { mkdtemp, rm } from 'node:fs/promises'; |
| 13 | +import { tmpdir } from 'node:os'; |
| 14 | +import { join } from 'node:path'; |
| 15 | + |
| 16 | +import { Store } from '../miniapp/node/store.mjs'; |
| 17 | +import { MemoryStore } from '../miniapp/node/memory.mjs'; |
| 18 | +import { Engine } from '../miniapp/node/engine.mjs'; |
| 19 | + |
| 20 | +async function fixture(t) { |
| 21 | + const dir = await mkdtemp(join(tmpdir(), 'bot-mention-nonascii-')); |
| 22 | + const store = new Store(dir); |
| 23 | + const memory = new MemoryStore(dir); |
| 24 | + await store.init(); |
| 25 | + await memory.init(); |
| 26 | + const engine = new Engine({ |
| 27 | + store, memory, logger: { warn() {} }, broadcast: () => {}, retryDelaysMs: [], |
| 28 | + }); |
| 29 | + t.after(async () => { |
| 30 | + engine.dispose(); |
| 31 | + await rm(dir, { recursive: true, force: true }); |
| 32 | + }); |
| 33 | + store.state.bots = [ |
| 34 | + { id: 'alice', name: 'Alice' }, |
| 35 | + { id: 'bob', name: 'Bob' }, |
| 36 | + ]; |
| 37 | + return { store, engine, group: { memberIds: ['alice', 'bob'] } }; |
| 38 | +} |
| 39 | + |
| 40 | +test('a letter of a script that writes with spaces makes an address, not a mention', async (t) => { |
| 41 | + const { engine, group } = await fixture(t); |
| 42 | + // Accented Latin letters, in both compositions. |
| 43 | + assert.deepEqual(engine.mentionsIn('josé@alice', group), []); |
| 44 | + assert.deepEqual(engine.mentionsIn('José@alice', group), []); |
| 45 | + assert.deepEqual(engine.mentionsIn('jose\u0301@alice', group), []); |
| 46 | + // Greek and Cyrillic letters. |
| 47 | + assert.deepEqual(engine.mentionsIn('Παύλος@alice', group), []); |
| 48 | + assert.deepEqual(engine.mentionsIn('Привет@alice', group), []); |
| 49 | + // A mention later in the same text is still found. |
| 50 | + assert.deepEqual(engine.mentionsIn('josé@alice @bob', group), ['bob']); |
| 51 | +}); |
| 52 | + |
| 53 | +test('Chinese, Japanese and Korean write up against the "@" and still reach the member', async (t) => { |
| 54 | + const { engine, group } = await fixture(t); |
| 55 | + assert.deepEqual(engine.mentionsIn('请@alice', group), ['alice']); |
| 56 | + assert.deepEqual(engine.mentionsIn('(@alice)', group), ['alice']); |
| 57 | + assert.deepEqual(engine.mentionsIn(',@alice', group), ['alice']); |
| 58 | + assert.deepEqual(engine.mentionsIn('@alice', group), ['alice']); |
| 59 | + assert.deepEqual(engine.mentionsIn('日本語@alice', group), ['alice']); |
| 60 | + assert.deepEqual(engine.mentionsIn('カタカナ@alice', group), ['alice']); |
| 61 | + assert.deepEqual(engine.mentionsIn('한국어@alice', group), ['alice']); |
| 62 | +}); |
| 63 | + |
| 64 | +test('the address forms stay silent', async (t) => { |
| 65 | + const { engine, group } = await fixture(t); |
| 66 | + assert.deepEqual(engine.mentionsIn('mail@alice.com', group), []); |
| 67 | + assert.deepEqual(engine.mentionsIn('@alicex', group), []); |
| 68 | + assert.deepEqual(engine.mentionsIn('1@alice', group), []); |
| 69 | + assert.deepEqual(engine.mentionsIn('_@alice', group), []); |
| 70 | +}); |
0 commit comments