import { readFileSync } from 'node:fs' import { test as base, expect } from 'vitest' import LoreML, { LoreMLOptions } from './index' const testDocument = readFileSync('test.xml', 'utf-8') class TestParser extends LoreML { constructor(opts?: LoreMLOptions) { super(opts) } public act() { return } } interface TestFixture { parser: TestParser, } const test = base.extend({ parser: new TestParser(), }) test('It should be initialized.', ({ parser }) => { expect(parser).toBeDefined() }) test('It should parse the test document.', async ({ parser }) => { const result = await parser.parse(testDocument) expect(result).toBeDefined() console.log(result) }) test('It should parse with default root attributes.', async ({ parser }) => { const doc = ` This is a test ` const result = await parser.parse(doc) expect(result.trim()).toBe('This is a test') }) test('It should honor a zero seed.', async () => { const doc = ` ` const first = new TestParser() const second = new TestParser() const [firstResult, secondResult] = await Promise.all([ first.parse(doc), second.parse(doc), ]) expect(firstResult.trim()).toBe(secondResult.trim()) }) test('It should parse with the debug header.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toMatch(/version: \d+\.\d+\.\d+ \| seed: -*\d+ \| locale: en/) }) test('It should parse with spanish localization.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toBe('Manuel Molina Betancourt') }) test('It should parse with german localization.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toBe('Cedric Schumacher') }) test('It should parse with italian localization.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toBe('Romolo Di Giovanni') }) test('It should parse with french localization.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toBe('Hildebert Cousin') }) test('It should parse with default attributes.', async ({ parser }) => { const doc = ` This is a test ` const result = await parser.parse(doc) expect(result.trim()).toBe('This is a test') }) test('It should parse with nested and no attributes.', async ({ parser }) => { const doc = ` This is a test ` const result = await parser.parse(doc) expect(result.trim()).toBe('This is a test') }) test('It should parse with repeat attributes.', async ({ parser }) => { const doc = ` This is a test ` const result = await parser.parse(doc) expect(result.trim()).toBe('This is a test\n\n\nThis is a test') }) test('It should parse with pick attributes.', async ({ parser }) => { const doc = ` 1. This is a test 2. This is a test 3. This is a test ` const result = await parser.parse(doc) expect(result).toBe('1. This is a test\n2. This is a test\n') }) test('It should clamp pick to the available children.', async ({ parser }) => { const doc = ` 1. This is a test 2. This is a test ` const result = await parser.parse(doc) expect(result).toContain('1. This is a test') expect(result).toContain('2. This is a test') }) test('It should not crash when a referenced person is missing.', async ({ parser }) => { const doc = ` BeforeAfter ` const result = await parser.parse(doc) expect(result).toBeTypeOf('string') }) test('It should parse with default attributes.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toBe('0') }) test('It should parse with default attributes.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toBe('8.92') }) test('It should parse with default attributes.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toBe('Suppellex a cognatus arca aliquam audentia.') }) test('It should parse with default attributes.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) const expected = 'A cognatus arca aliquam audentia coniuratio crux fugit. Stillicidium bardus utrimque acsi spargo cur. Aqua avaritia thesaurus volo combibo stultus utor. Ago adflicto assentator utrimque altus curiositas vita expedita stultus comedo. Trucido accusamus tandem voveo tamisium cicuta testimonium amet. Ver claudeo civis aperio accusantium spoliatio.' expect(result.trim()).toBe(expected) }) test('It should generate deterministic dates for the same seed.', async () => { const doc = ` ` const first = new TestParser() const second = new TestParser() const [firstResult, secondResult] = await Promise.all([ first.parse(doc), second.parse(doc), ]) expect(firstResult.trim()).toBe(secondResult.trim()) }) test('It should allow configuring refDate through parser options.', async () => { const doc = ` ` const early = new TestParser({ refDate: '2020-01-01T00:00:00.000Z' }) const late = new TestParser({ refDate: '2030-01-01T00:00:00.000Z' }) const [earlyResult, lateResult] = await Promise.all([ early.parse(doc), late.parse(doc), ]) expect(earlyResult.trim()).not.toBe(lateResult.trim()) }) test('It should allow overriding refDate on Root.', async () => { const defaultDoc = ` ` const overriddenDoc = ` ` const parser = new TestParser({ refDate: '2020-01-01T00:00:00.000Z' }) const defaultResult = await parser.parse(defaultDoc) const overriddenResult = await parser.parse(overriddenDoc) expect(defaultResult.trim()).not.toBe(overriddenResult.trim()) }) test('It should parse with default attributes.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toMatch(/\d{4}-\d{2}-\d{2}/) }) test('It should parse with default attributes.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toMatch(/\d{2}:\d{2}:\d{2}.\d{3}/) }) test('It should parse with format attributes.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toMatch(/\d{2}:\d{2}:\d{2}.\d{3}/) }) test('It should parse with when attributes.', async ({ parser }) => { const docPast = ` ` const resultPast = await parser.parse(docPast) expect(resultPast.trim()).toMatch(/\d{4}-\d{2}-\d{2}/) const docSoon = ` ` const resultSoon = await parser.parse(docSoon) expect(resultSoon.trim()).toMatch(/\d{4}-\d{2}-\d{2}/) const docFuture = ` ` const resultFuture = await parser.parse(docFuture) expect(resultFuture.trim()).toMatch(/\d{4}-\d{2}-\d{2}/) }) test('It should parse with default attributes.', async ({ parser }) => { const doc = ` ` const result = await parser.parse(doc) expect(result.trim()).toBe('Hello, Lorem!') }) test('It should parse with default attributes.', async ({ parser }) => { const doc = ` , ` const result = await parser.parse(doc) expect(result.trim()).toBe('Ms. Terry, Felicia Violet') }) test('It should parse localized with default attributes.', async ({ parser }) => { const doc = ` , ` const result = await parser.parse(doc) expect(result.trim()).toBe('Frau Schumacher, Cedric Kyle') }) test('It should parse with default attributes.', async ({ parser }) => { const doc = `
` const result = await parser.parse(doc) expect(result.trim()).toBe('7031 Christy Grove, New Heaven, Ohio 80645 Bulgaria') }) test('It should parse with locale root attributes.', async ({ parser }) => { const doc = ` , ` const result = await parser.parse(doc) expect(result.trim()).toBe('Hans-Schlehahn-Str. 13b Nord Darrendorf, Hessen 54628 Algerien') })