94 lines
3.8 KiB
TypeScript
94 lines
3.8 KiB
TypeScript
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { availableThemes, resolveTheme } from './resolveTheme';
|
|
|
|
describe('resolveTheme', () => {
|
|
it.each([undefined, '', ' '])('maps %j to the empty module (the default admin)', (name) => {
|
|
const theme = resolveTheme(name);
|
|
|
|
expect(path.basename(theme.css)).toBe('none.css');
|
|
expect(theme.favicon).toBeNull();
|
|
expect(theme.logo).toBeNull();
|
|
expect(fs.readFileSync(theme.css, 'utf-8')).not.toMatch(/--[a-z-]+\s*:/);
|
|
});
|
|
|
|
it('resolves the asf theme with its stylesheet and favicon', () => {
|
|
const theme = resolveTheme('asf');
|
|
|
|
expect(fs.existsSync(theme.css)).toBe(true);
|
|
expect(theme.css.replaceAll('\\', '/')).toMatch(/themes\/asf\/theme\.css$/);
|
|
expect(theme.favicon !== null && fs.existsSync(theme.favicon)).toBe(true);
|
|
});
|
|
|
|
it('ships the asf brand logo for both colour modes', () => {
|
|
const { logo } = resolveTheme('asf');
|
|
|
|
expect(logo).not.toBeNull();
|
|
expect(fs.existsSync(logo!.onLight) && fs.existsSync(logo!.onDark)).toBe(true);
|
|
});
|
|
|
|
it('treats a theme with only one logo variant as having no logo', () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'themes-'));
|
|
try {
|
|
fs.mkdirSync(path.join(dir, 'half'));
|
|
fs.writeFileSync(path.join(dir, 'half', 'theme.css'), '');
|
|
fs.writeFileSync(path.join(dir, 'half', 'logo-on-light.png'), '');
|
|
expect(resolveTheme('half', dir).logo).toBeNull();
|
|
} finally {
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('lists the available themes', () => {
|
|
expect(availableThemes()).toContain('asf');
|
|
});
|
|
|
|
it.each(['nope', '../asf', 'asf/../asf', '..', 'ASF', 'asf asf'])('rejects %j loudly, naming the real themes', (name) => {
|
|
expect(() => resolveTheme(name)).toThrow(/Unknown VITE_THEME .*Available themes: .*asf/);
|
|
});
|
|
|
|
it('reports (none) when no theme exists', () => {
|
|
const empty = fs.mkdtempSync(path.join(os.tmpdir(), 'themes-'));
|
|
try {
|
|
expect(() => resolveTheme('x', empty)).toThrow(/\(none\)/);
|
|
} finally {
|
|
fs.rmSync(empty, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('asf theme tokens', () => {
|
|
const tokens = (css: string, block: string) => {
|
|
const m = css.match(new RegExp(`${block}\\s*\\{([^}]*)\\}`));
|
|
return new Set([...(m?.[1] ?? '').matchAll(/(--[a-z-]+)\s*:/g)].map((x) => x[1]));
|
|
};
|
|
const base = fs.readFileSync(path.resolve(__dirname, '../src/index.css'), 'utf-8');
|
|
const theme = fs.readFileSync(path.resolve(__dirname, 'asf/theme.css'), 'utf-8');
|
|
|
|
it('overrides every existing light and dark design token and invents no structural one', () => {
|
|
for (const block of ['\\:root', '\\.dark']) {
|
|
const existing = tokens(base, block);
|
|
const themed = tokens(theme, block);
|
|
for (const t of existing) expect(themed, `${block} ${t}`).toContain(t);
|
|
// Anything beyond the existing tokens must be the theme's own --asf-* palette or the font token.
|
|
for (const t of themed) {
|
|
if (!existing.has(t)) expect(t).toMatch(/^--(asf-|font-sans$)/);
|
|
}
|
|
}
|
|
});
|
|
|
|
it('uses the website palette colours exactly', () => {
|
|
for (const hex of ['#faf7f2', '#f1ebe2', '#e8e0d5', '#232120', '#55504b', '#8a827a', '#e2dad0', '#7f8b76', '#5f6a58', '#475042', '#d9cdbc', '#a4443a']) {
|
|
expect(theme.toLowerCase()).toContain(hex);
|
|
}
|
|
});
|
|
|
|
it('self-hosts its fonts (no remote url)', () => {
|
|
expect(theme).not.toMatch(/https?:\/\//);
|
|
expect(theme).toContain('@fontsource/jost');
|
|
expect(theme).toContain('@fontsource/marcellus');
|
|
});
|
|
});
|