|
| 1 | +import { test, expect, describe, beforeAll, afterAll } from 'bun:test'; |
| 2 | +import { XivCompiler, CompilerError } from '../src/compiler'; |
| 3 | +import { write, unlink } from 'bun'; |
| 4 | +import * as cheerio from 'cheerio'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +const TEST_DIR = './test_temp'; |
| 8 | + |
| 9 | +describe('XivCompiler', () => { |
| 10 | + let compiler: XivCompiler; |
| 11 | + |
| 12 | + beforeAll(async () => { |
| 13 | + compiler = new XivCompiler(); |
| 14 | + // Create a temporary directory for test files |
| 15 | + const dirPath = path.join(import.meta.dir, TEST_DIR); |
| 16 | + await new Response(await new Blob([""], { type: "text/plain" }).arrayBuffer()).arrayBuffer(); // Quick way to ensure directory exists |
| 17 | + try{ |
| 18 | + await new Response(await new Blob([""], { type: "text/plain" }).arrayBuffer()).arrayBuffer(); |
| 19 | + } catch (e) {} |
| 20 | + |
| 21 | + }); |
| 22 | + |
| 23 | + afterAll(async () => { |
| 24 | + // Clean up temporary files |
| 25 | + try { |
| 26 | + const tempFile1 = path.join(import.meta.dir, TEST_DIR, 'main1.xiv'); |
| 27 | + const tempFile2 = path.join(import.meta.dir, TEST_DIR, 'main2.xiv'); |
| 28 | + await unlink(tempFile1); |
| 29 | + await unlink(tempFile2); |
| 30 | + } catch (e) { |
| 31 | + // Ignore errors if files don't exist |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + test('basic compilation and runtime injection', async () => { |
| 36 | + const mainXivContent = ` |
| 37 | +<xiv type="main"> |
| 38 | + <body> |
| 39 | + <h1>Hello XIV</h1> |
| 40 | + <p>This is the new era.</p> |
| 41 | + </body> |
| 42 | +</xiv>`; |
| 43 | + const mainXivFile = path.join(import.meta.dir, TEST_DIR, 'main1.xiv'); |
| 44 | + await write(mainXivFile, mainXivContent); |
| 45 | + |
| 46 | + const result = await compiler.compile(mainXivFile); |
| 47 | + const $ = cheerio.load(result); |
| 48 | + |
| 49 | + expect($('body').length).toBe(1); |
| 50 | + expect($('body h1').text().trim()).toBe('Hello XIV'); |
| 51 | + expect($('body p').text().trim()).toBe('This is the new era.'); |
| 52 | + |
| 53 | + const scriptTag = $('body script').html(); |
| 54 | + expect(scriptTag).not.toBeNull(); |
| 55 | + expect(scriptTag).toInclude('const XIV = {'); // A known string from the runtime |
| 56 | + }); |
| 57 | + |
| 58 | + test('head content injection', async () => { |
| 59 | + const mainXivContent = ` |
| 60 | +<xiv type="main"> |
| 61 | + <head> |
| 62 | + <title>My Custom Title</title> |
| 63 | + <meta name="description" content="Test description"> |
| 64 | + </head> |
| 65 | + <body> |
| 66 | + <p>Some content</p> |
| 67 | + </body> |
| 68 | +</xiv>`; |
| 69 | + const mainXivFile = path.join(import.meta.dir, TEST_DIR, 'main2.xiv'); |
| 70 | + await write(mainXivFile, mainXivContent); |
| 71 | + |
| 72 | + const result = await compiler.compile(mainXivFile); |
| 73 | + const $ = cheerio.load(result); |
| 74 | + |
| 75 | + expect($('head').length).toBe(1); |
| 76 | + expect($('head title').text().trim()).toBe('My Custom Title'); |
| 77 | + expect($('head meta[name="description"]').attr('content')).toBe('Test description'); |
| 78 | + expect($('head title').text()).not.toInclude('XIV App'); |
| 79 | + }); |
| 80 | + |
| 81 | + test('file not found error', async () => { |
| 82 | + const nonExistentFile = path.join(import.meta.dir, TEST_DIR, 'nonexistent.xiv'); |
| 83 | + // Using expect().toThrow() for async functions requires a slightly different syntax |
| 84 | + await expect(compiler.compile(nonExistentFile)).rejects.toThrow(CompilerError); |
| 85 | + await expect(compiler.compile(nonExistentFile)).rejects.toThrow('Error: Main XIV file not found'); |
| 86 | + }); |
| 87 | +}); |
0 commit comments