|
| 1 | +import { expect, test } from 'bun:test'; |
| 2 | +import { readFileSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | + |
| 5 | +const root = join(import.meta.dir, '..', '..'); |
| 6 | + |
| 7 | +function readPackageVersion(): string { |
| 8 | + try { |
| 9 | + const raw = readFileSync(join(root, 'package.json'), 'utf8'); |
| 10 | + const pkg = JSON.parse(raw) as { version?: string }; |
| 11 | + if (typeof pkg.version !== 'string' || !pkg.version) { |
| 12 | + throw new Error('package.json missing version'); |
| 13 | + } |
| 14 | + return pkg.version; |
| 15 | + } catch (e) { |
| 16 | + throw new Error('failed to read package.json', { cause: e }); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +test('Footer embeds site version from package.json', () => { |
| 21 | + const version = readPackageVersion(); |
| 22 | + const footer = readFileSync( |
| 23 | + join(root, 'src/components/Footer.astro'), |
| 24 | + 'utf8', |
| 25 | + ); |
| 26 | + expect(version).toMatch(/^\d+\.\d+\.\d+/); |
| 27 | + expect(footer).toContain("from '../../package.json'"); |
| 28 | + expect(footer).toContain('{appVersion}'); |
| 29 | + expect(footer.indexOf('Links:')).toBeLessThan( |
| 30 | + footer.indexOf('data-site-version'), |
| 31 | + ); |
| 32 | +}); |
| 33 | + |
| 34 | +test('built home page includes version in footer when dist exists', () => { |
| 35 | + const distIndex = join(root, 'dist', 'index.html'); |
| 36 | + let html: string; |
| 37 | + try { |
| 38 | + html = readFileSync(distIndex, 'utf8'); |
| 39 | + } catch { |
| 40 | + return; |
| 41 | + } |
| 42 | + const version = readPackageVersion(); |
| 43 | + expect(html).toContain(`v${version}`); |
| 44 | + expect(html).toContain('data-site-version'); |
| 45 | +}); |
0 commit comments