/* eslint-disable max-len */ import { htmlify } from './modules/htmlify'; import { humanize } from './modules/humanize'; import { tokenize } from './modules/tokenize'; import { SYNTAX, SYNTAX_MAP, TOKEN } from './utils/syntax'; import { tmText } from './index'; import type { TmTextOptions } from './utils/options'; import type { Syntax } from './utils/syntax'; const MAIN_TEST_STRING = '$oLorem $wipsum $p[dolor]dolor$p $<$f00sit$> $samet\n\tconsectetur'; type Suite = 'exported' | 'member'; const runTestsForSyntax = (suite: Suite, syntax: Syntax): void => { const options: Partial = { syntax, }; const text = tmText(MAIN_TEST_STRING, options); describe(`tokenization for syntax "${syntax}"`, () => { const tokens = suite === 'member' ? text.tokenize() : tokenize(MAIN_TEST_STRING, options); it('should provide an array of correctly shaped tokens', () => { expect(Array.isArray(tokens)).toBe(true); tokens.forEach((token) => { expect(token).toHaveProperty('kind'); expect(token).toHaveProperty('content'); expect(token).toHaveProperty('pos.start'); expect(token).toHaveProperty('pos.end'); expect(Object.values(TOKEN)).toContain(token.kind); expect(typeof token.content).toBe('string'); expect(token.content.length).toBeGreaterThan(0); }); }); it('should only contain tokens', () => { tokens.forEach((token) => { expect(Object.values(SYNTAX_MAP[syntax])).toContain(token.kind); }); }); it('should return the expected result', () => { if (syntax === SYNTAX.CLASSIC) { expect(tokens).toMatchObject([ { kind: TOKEN.WORD, content: 'Lorem ', pos: { start: 2, end: 8 } }, { kind: TOKEN.WIDTH_WIDE, content: '$w', pos: { start: 8, end: 10 } }, { kind: TOKEN.WORD, content: 'ipsum [dolor]dolor ', pos: { start: 10, end: 25 } }, { kind: TOKEN.COLOR, content: '$f00', pos: { start: 35, end: 39 } }, { kind: TOKEN.WORD, content: 'sit ', pos: { start: 39, end: 43 } }, { kind: TOKEN.SHADOW, content: '$s', pos: { start: 45, end: 47 } }, { kind: TOKEN.WORD, content: 'amet', pos: { start: 47, end: 51 } }, { kind: TOKEN.NEWLINE, content: ' ', pos: { start: 51, end: 52 } }, { kind: TOKEN.TAB, content: ' ', pos: { start: 52, end: 53 } }, { kind: TOKEN.WORD, content: 'consectetur', pos: { start: 53, end: 64 } }, ]); } else if (syntax === SYNTAX.UNITED) { expect(tokens).toMatchObject([ { kind: TOKEN.BOLD, content: '$o', pos: { start: 0, end: 2 } }, { kind: TOKEN.WORD, content: 'Lorem ', pos: { start: 2, end: 8 } }, { kind: TOKEN.WIDTH_WIDE, content: '$w', pos: { start: 8, end: 10 } }, { kind: TOKEN.WORD, content: 'ipsum [dolor]dolor ', pos: { start: 10, end: 25 } }, { kind: TOKEN.COLOR, content: '$f00', pos: { start: 35, end: 39 } }, { kind: TOKEN.WORD, content: 'sit ', pos: { start: 39, end: 43 } }, { kind: TOKEN.SHADOW, content: '$s', pos: { start: 45, end: 47 } }, { kind: TOKEN.WORD, content: 'amet', pos: { start: 47, end: 51 } }, { kind: TOKEN.NEWLINE, content: ' ', pos: { start: 51, end: 52 } }, { kind: TOKEN.TAB, content: ' ', pos: { start: 52, end: 53 } }, { kind: TOKEN.WORD, content: 'consectetur', pos: { start: 53, end: 64 } }, ]); } else if (syntax === SYNTAX.FOREVER) { expect(tokens).toMatchObject([ { kind: TOKEN.BOLD, content: '$o', pos: { start: 0, end: 2 } }, { kind: TOKEN.WORD, content: 'Lorem ', pos: { start: 2, end: 8 } }, { kind: TOKEN.WIDTH_WIDE, content: '$w', pos: { start: 8, end: 10 } }, { kind: TOKEN.WORD, content: 'ipsum ', pos: { start: 10, end: 16 } }, { kind: TOKEN.LINK_INTERNAL_WITH_PARAMS, content: '$p', pos: { start: 16, end: 18 } }, { kind: TOKEN.HREF_START, content: '[', pos: { start: 18, end: 19 } }, { kind: TOKEN.HREF_CONTENT, content: 'dolor', pos: { start: 19, end: 24 } }, { kind: TOKEN.HREF_END, content: ']', pos: { start: 24, end: 25 } }, { kind: TOKEN.WORD, content: 'dolor', pos: { start: 25, end: 30 } }, { kind: TOKEN.LINK_INTERNAL_WITH_PARAMS, content: '$p', pos: { start: 30, end: 32 } }, { kind: TOKEN.WORD, content: ' ', pos: { start: 32, end: 33 } }, { kind: TOKEN.COLOR, content: '$f00', pos: { start: 35, end: 39 } }, { kind: TOKEN.WORD, content: 'sit ', pos: { start: 39, end: 43 } }, { kind: TOKEN.SHADOW, content: '$s', pos: { start: 45, end: 47 } }, { kind: TOKEN.WORD, content: 'amet', pos: { start: 47, end: 51 } }, { kind: TOKEN.NEWLINE, content: ' ', pos: { start: 51, end: 52 } }, { kind: TOKEN.TAB, content: ' ', pos: { start: 52, end: 53 } }, { kind: TOKEN.WORD, content: 'consectetur', pos: { start: 53, end: 64 } }, ]); } else { expect(tokens).toMatchObject([ { kind: TOKEN.BOLD, content: '$o', pos: { start: 0, end: 2 } }, { kind: TOKEN.WORD, content: 'Lorem ', pos: { start: 2, end: 8 } }, { kind: TOKEN.WIDTH_WIDE, content: '$w', pos: { start: 8, end: 10 } }, { kind: TOKEN.WORD, content: 'ipsum ', pos: { start: 10, end: 16 } }, { kind: TOKEN.LINK_INTERNAL_WITH_PARAMS, content: '$p', pos: { start: 16, end: 18 } }, { kind: TOKEN.HREF_START, content: '[', pos: { start: 18, end: 19 } }, { kind: TOKEN.HREF_CONTENT, content: 'dolor', pos: { start: 19, end: 24 } }, { kind: TOKEN.HREF_END, content: ']', pos: { start: 24, end: 25 } }, { kind: TOKEN.WORD, content: 'dolor', pos: { start: 25, end: 30 } }, { kind: TOKEN.LINK_INTERNAL_WITH_PARAMS, content: '$p', pos: { start: 30, end: 32 } }, { kind: TOKEN.WORD, content: ' ', pos: { start: 32, end: 33 } }, { kind: TOKEN.BLOCK_START, content: '$<', pos: { start: 33, end: 35 } }, { kind: TOKEN.COLOR, content: '$f00', pos: { start: 35, end: 39 } }, { kind: TOKEN.WORD, content: 'sit', pos: { start: 39, end: 42 } }, { kind: TOKEN.BLOCK_END, content: '$>', pos: { start: 42, end: 44 } }, { kind: TOKEN.WORD, content: ' ', pos: { start: 44, end: 45 } }, { kind: TOKEN.SHADOW, content: '$s', pos: { start: 45, end: 47 } }, { kind: TOKEN.WORD, content: 'amet', pos: { start: 47, end: 51 } }, { kind: TOKEN.NEWLINE, content: ' ', pos: { start: 51, end: 52 } }, { kind: TOKEN.TAB, content: ' ', pos: { start: 52, end: 53 } }, { kind: TOKEN.WORD, content: 'consectetur', pos: { start: 53, end: 64 } }, ]); } }); }); describe(`humanization for syntax "${syntax}"`, () => { it('should remove all non-word tokens', () => { const value = suite === 'member' ? text.humanize() : humanize(MAIN_TEST_STRING, options); if (syntax === SYNTAX.CLASSIC || syntax === SYNTAX.UNITED) { expect(value).toBe('Lorem ipsum [dolor]dolor sit amet consectetur'); } else { expect(value).toBe('Lorem ipsum dolor sit amet consectetur'); } }); }); describe(`conversion to HTML for syntax "${syntax}"`, () => { it('should handle input with no text content or only spaces', () => { expect(htmlify('$s$i', options)).toBe(''); expect(htmlify(' $s$i', options)).toBe(' '); expect(htmlify(' $s$i', options)).toBe(' '); expect(htmlify('$s $i', options)).toBe(' '); expect(htmlify('$s$i ', options)).toBe(' '); expect(htmlify(' $s$i ', options)).toBe(' '); expect(htmlify(' $s $i ', options)).toBe(' '); }); it('should handle special escape characters', () => { expect(htmlify('\f\n\r\v\t\u00A0\u2028\u2029', options)).toBe(' '); expect(htmlify('$f00Lorem$\n0f0ipsum', options)).toBe('Lorem 0f0ipsum'); expect(htmlify('$f00Lorem$0f0\nipsum', options)).toBe('Lorem ipsum'); }); it('should ignore unknown tokens', () => { expect(htmlify('$a', options)).toBe(''); expect(htmlify('$bLorem', options)).toBe('Lorem'); expect(htmlify('$c$iLorem', options)).toBe('Lorem'); expect(htmlify('$def$aLorem', options)).toBe('Lorem'); expect(htmlify('$ghiLorem', options)).toBe('hiLorem'); expect(htmlify('$007$00Lorem', options)).toBe('0Lorem'); expect(htmlify('$007$00fLorem', options)).toBe('Lorem'); }); it('should be able to reset formatting', () => { expect(htmlify('$iLorem $zipsum', options)).toBe('Lorem ipsum'); expect(htmlify('$f00Lorem $gipsum', options)).toBe('Lorem ipsum'); expect(htmlify('$f00$iLorem $gipsum', options)).toBe('Lorem ipsum'); }); it('should handle css tokens without end tags correctly', () => { expect(htmlify('$mLorem', options)).toBe('Lorem'); expect(htmlify('$nLorem', options)).toBe('Lorem'); expect(htmlify('$wLorem', options)).toBe('Lorem'); expect(htmlify('$mLorem$m ipsum', options)).toBe('Lorem ipsum'); expect(htmlify('$nLorem$n ipsum', options)).toBe('Lorem ipsum'); expect(htmlify('$wLorem$w ipsum', options)).toBe('Lorem ipsum'); expect(htmlify('$wLorem $mipsum $ndolor', options)).toBe('Lorem ipsum dolor'); }); it('should handle css tokens with end tags correctly', () => { expect(htmlify('$iLorem$i ipsum', options)).toBe('Lorem ipsum'); expect(htmlify('$sLorem$s ipsum', options)).toBe('Lorem ipsum'); expect(htmlify('$tLorem$t ipsum', options)).toBe('Lorem ipsum'); expect(htmlify('$t$f00Lorem $f00ipsum', options)).toBe('Lorem ipsum'); expect(htmlify('$t$f00Lorem $0f0ipsum', options)).toBe('Lorem ipsum'); expect(htmlify('$t$f00Lorem $0f0ipsum $tdolor', options)).toBe('Lorem ipsum dolor'); if (syntax === SYNTAX.CLASSIC) { expect(htmlify('$oLorem$o ipsum', options)).toBe('Lorem ipsum'); } else { expect(htmlify('$oLorem$o ipsum', options)).toBe('Lorem ipsum'); } }); it('should handle links correctly', () => { if (syntax === SYNTAX.CLASSIC) { expect(htmlify('$hexample.com', options)).toBe('example.com'); expect(htmlify('$lexample.com', options)).toBe('example.com'); expect(htmlify('$hexample.com$hLorem', options)).toBe('example.comLorem'); expect(htmlify('$lexample.com$lLorem', options)).toBe('example.comLorem'); expect(htmlify('$h[example.com]Lorem', options)).toBe('[example.com]Lorem'); expect(htmlify('$l[example.com]Lorem', options)).toBe('[example.com]Lorem'); expect(htmlify('$h[example.com]Lor$hem', options)).toBe('[example.com]Lorem'); expect(htmlify('$l[example.com]Lor$lem', options)).toBe('[example.com]Lorem'); expect(htmlify('$h[example.com]L$ior$hem', options)).toBe('[example.com]Lorem'); expect(htmlify('$l[example.com]L$ior$lem', options)).toBe('[example.com]Lorem'); expect(htmlify('$hexa$omple.com', options)).toBe('example.com'); expect(htmlify('$lexa$omple.com', options)).toBe('example.com'); expect(htmlify('$pexa$omple.com', options)).toBe('example.com'); } else { expect(htmlify('$lexample.com', options)).toBe('example.com'); expect(htmlify('$lhttps://example.com', options)).toBe('https://example.com'); expect(htmlify('$lhttp://example.com', options)).toBe('http://example.com'); expect(htmlify('$lexample.com', { ...options, scheme: 'http' })).toBe('example.com'); expect(htmlify('$lexample.com$lLorem', options)).toBe('example.comLorem'); expect(htmlify('$l[example.com]Lorem', options)).toBe('Lorem'); expect(htmlify('$l[example.com]Lor$lem', options)).toBe('Lorem'); expect(htmlify('$l[example.com]L$ior$lem', options)).toBe('Lorem'); expect(htmlify('$lexa$omple.com', options)).toBe('example.com'); if (syntax === SYNTAX.MANIAPLANET) { expect(htmlify('$hexample.com', options)).toBe('example.com'); expect(htmlify('$hmaniaplanet://example.com', options)).toBe('maniaplanet://example.com'); expect(htmlify('$hexample.com$hLorem', options)).toBe('example.comLorem'); expect(htmlify('$h[example.com]Lorem', options)).toBe('Lorem'); expect(htmlify('$h[example.com]Lor$hem', options)).toBe('Lorem'); expect(htmlify('$h[example.com]L$ior$hem', options)).toBe('Lorem'); expect(htmlify('$hexa$omple.com', options)).toBe('example.com'); expect(htmlify('$hexa$omple$l.com', options)).toBe('example.com'); expect(htmlify('$pexample.com', options)).toBe('example.com'); expect(htmlify('$pexample.com$pLorem', options)).toBe('example.comLorem'); expect(htmlify('$p[example.com]Lorem', options)).toBe('Lorem'); expect(htmlify('$p[example.com]Lor$pem', options)).toBe('Lorem'); expect(htmlify('$p[example.com]L$ior$pem', options)).toBe('Lorem'); expect(htmlify('$pexa$omple.com', options)).toBe('example.com'); expect(htmlify('$hexa$omple$l.c$z$pom', options)).toBe('example.com'); expect(htmlify('$hexa$omple$l.c$p$zom', options)).toBe('example.com'); expect(htmlify('$hexa$omple$l.c$pom', options)).toBe('example.com'); } else { expect(htmlify('$hexample.com', options)).toBe('example.com'); expect(htmlify('$htmtp://example.com', options)).toBe('tmtp://example.com'); expect(htmlify('$hexample.com$hLorem', options)).toBe('example.comLorem'); expect(htmlify('$h[example.com]Lorem', options)).toBe('Lorem'); expect(htmlify('$h[example.com]Lor$hem', options)).toBe('Lorem'); expect(htmlify('$h[example.com]L$ior$hem', options)).toBe('Lorem'); expect(htmlify('$hexa$omple.com', options)).toBe('example.com'); expect(htmlify('$hexa$omple$l.com', options)).toBe('example.com'); if (syntax === SYNTAX.FOREVER) { expect(htmlify('$pexample.com', options)).toBe('example.com'); expect(htmlify('$pexample.com$pLorem', options)).toBe('example.comLorem'); expect(htmlify('$p[example.com]Lorem', options)).toBe('Lorem'); expect(htmlify('$p[example.com]Lor$pem', options)).toBe('Lorem'); expect(htmlify('$p[example.com]L$ior$pem', options)).toBe('Lorem'); expect(htmlify('$pexa$omple.com', options)).toBe('example.com'); expect(htmlify('$hexa$omple$l.c$z$pom', options)).toBe('example.com'); expect(htmlify('$hexa$omple$l.c$p$zom', options)).toBe('example.com'); expect(htmlify('$hexa$omple$l.c$pom', options)).toBe('example.com'); expect(htmlify('$pexample.com', { ...options, playerParameters: { playerlogin: 'jondoe', nickname: '$w$s$f00jon$0f0doe', lang: 'en', path: 'World|United States of America|California', }, })).toBe('example.com'); } else { expect(htmlify('$hexa$omple$l.c$z$pom', options)).toBe('example.com'); expect(htmlify('$hexa$omple$l.c$p$zom', options)).toBe('example.com'); expect(htmlify('$hexa$omple$l.c$pom', options)).toBe('example.com'); } } } if (syntax === SYNTAX.CLASSIC || syntax === SYNTAX.UNITED) { expect(htmlify('$pexample.com', options)).toBe('example.com'); expect(htmlify('$pexample.com$pLorem', options)).toBe('example.comLorem'); expect(htmlify('$p[example.com]Lorem', options)).toBe('[example.com]Lorem'); expect(htmlify('$p[example.com]Lor$pem', options)).toBe('[example.com]Lorem'); expect(htmlify('$p[example.com]L$ior$pem', options)).toBe('[example.com]Lorem'); if (syntax === SYNTAX.UNITED) { expect(htmlify('$pexa$omple.com', options)).toBe('example.com'); } } }); it('should handle formatting blocks correctly', () => { if (syntax === SYNTAX.MANIAPLANET) { expect(htmlify('$iLorem $<$tipsum$> dolor', options)).toBe('Lorem ipsum dolor'); expect(htmlify('$f00Red$<$0f0$tGreen$<$00fBlue$>Green$>Red', options)).toBe('RedGreenBlueGreenRed'); expect(htmlify('$<$tupper $ upper $tlower $t$> lower $tupper$t lower', options)).toBe('upper upper lower upper upper lower lower upper lower'); expect(htmlify('$<$tLorem$> ipsum$> dolor', options)).toBe('Lorem ipsum dolor'); } else { expect(htmlify('$iLorem $<$tipsum$> dolor', options)).toBe('Lorem ipsum dolor'); if (syntax === SYNTAX.CLASSIC) { expect(htmlify('$l[Lorem ipsum]dolor $<$0f0sit$l amet$> consectetur', options)).toBe('[Lorem ipsum]dolor sit amet consectetur'); } else { expect(htmlify('$l[Lorem ipsum]dolor $<$0f0sit$l amet$> consectetur', options)).toBe('dolor sit amet consectetur'); } } }); it('should handle formatting blocks in combination with links correctly', () => { if (syntax !== SYNTAX.MANIAPLANET) { return; } expect(htmlify('$l[Lorem ipsum]dolor $<$0f0sit$l amet$> consectetur', options)).toBe('dolor sit amet consectetur'); expect(htmlify('$p[exa$omple.com]Lorem', options)).toBe('Lorem'); expect(htmlify('$l[Lorem$<$f00 ipsum]$tdolor$l sit$> amet', options)).toBe('dolor sit amet'); expect(htmlify('$f00$<$tLorem$> $<$i$0f0ipsum$> dolor', options)).toBe('Lorem ipsum dolor'); expect(htmlify('Lorem $<$tip$l[sum$> do]lo$>r', options)).toBe('Lorem iplor'); // FIXME: These tests currently don't pass. // A more sophisticated solution has to be found to be able to accurately reproduce the behavior shown in the game. // // expect(htmlify('Lor$lem $<$tipsum$> do$llor', options)).toBe('Lorem ipsum dolor'); // expect(htmlify('Lorem $<$tip$lsum$> dolor', options)).toBe('Lorem ipsum dolor'); // expect(htmlify('$lLorem $<$tip$lsum$> dolor', options)).toBe('Lorem ipsum dolor'); // expect(htmlify('$lLorem $<$tip$lsu$lm$> dolor', options)).toBe('Lorem ipsum dolor'); }); }); }; const runSuite = (suite: Suite): void => { describe(`tm-text ${suite} functions`, () => { Object.values(SYNTAX).forEach((syntax) => runTestsForSyntax(suite, syntax)); }); }; runSuite('member'); runSuite('exported');