99 lines
4.3 KiB
TypeScript
99 lines
4.3 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { loadWaasConfigs, FALLBACK_SITE_CONFIG, FALLBACK_THEME_CONFIG, FALLBACK_SMTP_CONFIG } from '../src/utils/configLoader';
|
|
import { THEME_PRESETS } from '../src/utils/themePresets';
|
|
|
|
describe('WaaS Engine Core Unit Tests', () => {
|
|
const testDataDir = path.join(process.cwd(), 'app', 'test_data');
|
|
|
|
beforeEach(() => {
|
|
process.env.DATA_DIR = testDataDir;
|
|
if (fs.existsSync(testDataDir)) {
|
|
fs.rmSync(testDataDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (fs.existsSync(testDataDir)) {
|
|
fs.rmSync(testDataDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('1. Should return fallbacks when JSON files do not exist (Zero-Crash Guarantee)', () => {
|
|
const configs = loadWaasConfigs('');
|
|
expect(configs.siteConfig.siteName).toBe(FALLBACK_SITE_CONFIG.siteName);
|
|
expect(configs.themeConfig.activePreset).toBe(FALLBACK_THEME_CONFIG.activePreset);
|
|
expect(configs.smtpConfig.host).toBe(FALLBACK_SMTP_CONFIG.host);
|
|
});
|
|
|
|
it('2. Should dynamically merge custom site.config.json', () => {
|
|
fs.mkdirSync(testDataDir, { recursive: true });
|
|
const customSite = { siteName: 'Custom Test Agency', hero: { title: 'Custom Title' } };
|
|
fs.writeFileSync(path.join(testDataDir, 'site.config.json'), JSON.stringify(customSite), 'utf8');
|
|
|
|
const configs = loadWaasConfigs('');
|
|
expect(configs.siteConfig.siteName).toBe('Custom Test Agency');
|
|
expect(configs.siteConfig.hero.title).toBe('Custom Title');
|
|
expect(configs.siteConfig.hero.badge).toBe(FALLBACK_SITE_CONFIG.hero.badge);
|
|
});
|
|
|
|
it('3. Should have 10 valid Theme Presets with root CSS color tokens', () => {
|
|
const presetKeys = Object.keys(THEME_PRESETS);
|
|
expect(presetKeys.length).toBe(10);
|
|
expect(THEME_PRESETS.corporateDark.colors.bg).toBe('#0B0F19');
|
|
expect(THEME_PRESETS.corporateDark.colors.accent).toBe('#38BDF8');
|
|
});
|
|
|
|
it('4. Should support dynamic UIComponent elements in SectionConfig', () => {
|
|
const heroElements = FALLBACK_SITE_CONFIG.hero.elements || [];
|
|
expect(heroElements.length).toBeGreaterThan(0);
|
|
expect(heroElements[0].type).toBe('badge');
|
|
});
|
|
|
|
it('5. Should load device-specific configs for desktop, mobile, tablet with fallback', () => {
|
|
fs.mkdirSync(testDataDir, { recursive: true });
|
|
fs.writeFileSync(path.join(testDataDir, 'site.mobile.config.json'), JSON.stringify({ siteName: 'Mobile Site' }), 'utf8');
|
|
fs.writeFileSync(path.join(testDataDir, 'site.tablet.config.json'), JSON.stringify({ siteName: 'Tablet Site' }), 'utf8');
|
|
fs.writeFileSync(path.join(testDataDir, 'site.desktop.config.json'), JSON.stringify({ siteName: 'Desktop Site' }), 'utf8');
|
|
|
|
const mobileConfig = loadWaasConfigs('Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)');
|
|
expect(mobileConfig.siteConfig.siteName).toBe('Mobile Site');
|
|
|
|
const tabletConfig = loadWaasConfigs('Mozilla/5.0 (iPad; CPU OS 14_0 like Mac OS X)');
|
|
expect(tabletConfig.siteConfig.siteName).toBe('Tablet Site');
|
|
|
|
const desktopConfig = loadWaasConfigs('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
|
|
expect(desktopConfig.siteConfig.siteName).toBe('Desktop Site');
|
|
});
|
|
|
|
it('6. Should correctly resolve Theme Builder template based on Display Conditions', async () => {
|
|
const { resolveTemplate } = await import('../src/utils/conditionResolver');
|
|
const templates = [
|
|
{
|
|
id: 'header-global',
|
|
name: 'Global Header',
|
|
type: 'header' as const,
|
|
status: 'published' as const,
|
|
priority: 1,
|
|
conditions: [{ type: 'include' as const, scope: 'entire_site' as const }]
|
|
},
|
|
{
|
|
id: 'header-custom-page',
|
|
name: 'Custom Page Header',
|
|
type: 'header' as const,
|
|
status: 'published' as const,
|
|
priority: 1,
|
|
conditions: [{ type: 'include' as const, scope: 'singular' as const, entityId: 'landing_1' }]
|
|
}
|
|
];
|
|
|
|
// Global matching
|
|
const matchGlobal = resolveTemplate(templates, 'header', { url: '/about', routeType: 'singular', entityId: 'about_page' });
|
|
expect(matchGlobal?.id).toBe('header-global');
|
|
|
|
// Specific page matching
|
|
const matchSpecific = resolveTemplate(templates, 'header', { url: '/landing', routeType: 'singular', entityId: 'landing_1' });
|
|
expect(matchSpecific?.id).toBe('header-custom-page');
|
|
});
|
|
}); |