Files
scrabble-game/ui/src/lib/checkword.test.ts
T
Ilia Denisov 8e0d7f9e17
CI / changes (pull_request) Successful in 3s
CI / unit (pull_request) Has been skipped
CI / integration (pull_request) Has been skipped
CI / ui (pull_request) Successful in 48s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 56s
feat(ui): external dictionary lookup link on the word-check tool
When a checked word is found, show a 'look it up' text link beside the
complaint button that opens an external reference dictionary in a new tab:
gramota.ru for the Russian variants, scrabblewordfinder.org for English
(word lower-cased and percent-encoded). The link hides for a word that is
not found. Inside Telegram it routes through the Mini App SDK's openLink, so
Telegram opens it directly instead of the WebView's 'open this link?'
confirmation; in a browser the anchor's own target=_blank handles it.

Relabel the complaint button to 'Возражаю' (ru); English stays 'Disagree'.
2026-06-15 18:14:19 +02:00

58 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { canCheckWord, dictionaryLookupUrl, MAX_WORD_LEN, sanitizeCheckWord } from './checkword';
const EN = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
describe('sanitizeCheckWord', () => {
it('upper-cases and keeps only letters of the alphabet', () => {
expect(sanitizeCheckWord('ca7t!', EN)).toBe('CAT');
expect(sanitizeCheckWord(' Hi 9 ', EN)).toBe('HI');
});
it('drops characters outside the active alphabet', () => {
expect(sanitizeCheckWord('cat', ['C', 'A'])).toBe('CA'); // T not in this alphabet
const RU = 'КОТ'.split('');
expect(sanitizeCheckWord('коt', RU)).toBe('КО'); // cyrillic kept, latin "t" dropped
});
it('caps the length at MAX_WORD_LEN', () => {
expect(sanitizeCheckWord('A'.repeat(30), EN)).toHaveLength(MAX_WORD_LEN);
});
});
describe('canCheckWord', () => {
it('allows a fresh, in-range word', () => {
expect(canCheckWord('CAT', false, false)).toBe(true);
});
it('rejects an out-of-range length', () => {
expect(canCheckWord('A', false, false)).toBe(false); // too short
expect(canCheckWord('A'.repeat(MAX_WORD_LEN + 1), false, false)).toBe(false); // too long
});
it('rejects an already-checked word or a cooling-down state', () => {
expect(canCheckWord('CAT', true, false)).toBe(false);
expect(canCheckWord('CAT', false, true)).toBe(false);
});
it('trims surrounding whitespace before measuring length', () => {
expect(canCheckWord(' ok ', false, false)).toBe(true);
expect(canCheckWord(' a ', false, false)).toBe(false);
});
});
describe('dictionaryLookupUrl', () => {
it('points the Russian variants at gramota.ru with a lower-cased, encoded query', () => {
expect(dictionaryLookupUrl('КОТ', 'scrabble_ru')).toBe(
'https://gramota.ru/poisk?query=' + encodeURIComponent('кот'),
);
expect(dictionaryLookupUrl('ЭРА', 'erudit_ru')).toBe(
'https://gramota.ru/poisk?query=' + encodeURIComponent('эра'),
);
});
it('points English at scrabblewordfinder.org with a lower-cased path', () => {
expect(dictionaryLookupUrl('CAT', 'scrabble_en')).toBe('https://scrabblewordfinder.org/dictionary/cat');
});
});