UNPKG

@mdfriday/foundry

Version:

The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.

235 lines 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const root_1 = require("../entity/root"); describe('Root Entity', () => { let sampleRootConfig; let sampleParams; beforeEach(() => { sampleRootConfig = { baseURL: 'https://example.com', title: 'Test Blog', theme: ['mytheme', 'fallback'], timeout: '30s', contentDir: 'content', dataDir: 'data', layoutDir: 'layouts', staticDir: 'static', archetypeDir: 'archetypes', assetDir: 'assets', publishDir: 'public', buildDrafts: false, buildExpired: false, buildFuture: false, copyright: '© 2024 Test Blog', defaultContentLanguage: 'en', defaultContentLanguageInSubdir: false, disableAliases: false, disablePathToLower: false, disableKinds: [], disableLanguages: [], renderSegments: [], disableHugoGeneratorInject: false, disableLiveReload: false, enableEmoji: false }; sampleParams = { author: 'John Doe', description: 'A test blog' }; }); describe('constructor', () => { it('should create Root instance with config and params', () => { const root = new root_1.Root(sampleRootConfig, sampleParams); expect(root.getRootConfig()).toEqual(sampleRootConfig); expect(root.configParams()).toEqual(sampleParams); }); it('should create Root instance with default empty params', () => { const root = new root_1.Root(sampleRootConfig); expect(root.getRootConfig()).toEqual(sampleRootConfig); expect(root.configParams()).toEqual({}); }); }); describe('defaultTheme', () => { it('should return first theme from theme array', () => { const root = new root_1.Root(sampleRootConfig); expect(root.defaultTheme()).toBe('mytheme'); }); it('should return empty string when no themes', () => { const configWithoutThemes = { ...sampleRootConfig, theme: [] }; const root = new root_1.Root(configWithoutThemes); expect(root.defaultTheme()).toBe(''); }); }); describe('compiledTimeout', () => { it('should parse plain number as seconds', () => { const configWithNumberTimeout = { ...sampleRootConfig, timeout: '45' }; const root = new root_1.Root(configWithNumberTimeout); expect(root.compiledTimeout()).toBe(45000); // 45 seconds in milliseconds }); it('should parse seconds format', () => { const configWithSecondsTimeout = { ...sampleRootConfig, timeout: '60s' }; const root = new root_1.Root(configWithSecondsTimeout); expect(root.compiledTimeout()).toBe(60000); // 60 seconds in milliseconds }); it('should parse minutes format', () => { const configWithMinutesTimeout = { ...sampleRootConfig, timeout: '5m' }; const root = new root_1.Root(configWithMinutesTimeout); expect(root.compiledTimeout()).toBe(300000); // 5 minutes in milliseconds }); it('should parse hours format', () => { const configWithHoursTimeout = { ...sampleRootConfig, timeout: '2h' }; const root = new root_1.Root(configWithHoursTimeout); expect(root.compiledTimeout()).toBe(7200000); // 2 hours in milliseconds }); it('should return default timeout for invalid format', () => { const configWithInvalidTimeout = { ...sampleRootConfig, timeout: 'invalid' }; const root = new root_1.Root(configWithInvalidTimeout); expect(root.compiledTimeout()).toBe(30000); // default 30 seconds }); it('should handle edge cases in timeout parsing', () => { const testCases = [ { input: '0s', expected: 0 }, { input: '1s', expected: 1000 }, { input: '1m', expected: 60000 }, { input: '1h', expected: 3600000 }, { input: '', expected: 30000 }, // default { input: '10x', expected: 30000 }, // invalid unit { input: 'abc', expected: 30000 }, // invalid format ]; testCases.forEach(({ input, expected }) => { const config = { ...sampleRootConfig, timeout: input }; const root = new root_1.Root(config); expect(root.compiledTimeout()).toBe(expected); }); }); }); describe('baseUrl', () => { it('should return base URL', () => { const root = new root_1.Root(sampleRootConfig); expect(root.baseUrl()).toBe('https://example.com'); }); }); describe('siteTitle', () => { it('should return site title', () => { const root = new root_1.Root(sampleRootConfig); expect(root.siteTitle()).toBe('Test Blog'); }); }); describe('getThemes', () => { it('should return theme array', () => { const root = new root_1.Root(sampleRootConfig); expect(root.getThemes()).toEqual(['mytheme', 'fallback']); }); }); describe('getDefaultContentLanguage', () => { it('should return default content language', () => { const root = new root_1.Root(sampleRootConfig); expect(root.getDefaultContentLanguage()).toBe('en'); }); }); describe('build flags', () => { it('should return correct build draft flag', () => { const root = new root_1.Root(sampleRootConfig); expect(root.shouldBuildDrafts()).toBe(false); const configWithDrafts = { ...sampleRootConfig, buildDrafts: true }; const rootWithDrafts = new root_1.Root(configWithDrafts); expect(rootWithDrafts.shouldBuildDrafts()).toBe(true); }); it('should return correct build future flag', () => { const root = new root_1.Root(sampleRootConfig); expect(root.shouldBuildFuture()).toBe(false); const configWithFuture = { ...sampleRootConfig, buildFuture: true }; const rootWithFuture = new root_1.Root(configWithFuture); expect(rootWithFuture.shouldBuildFuture()).toBe(true); }); it('should return correct build expired flag', () => { const root = new root_1.Root(sampleRootConfig); expect(root.shouldBuildExpired()).toBe(false); const configWithExpired = { ...sampleRootConfig, buildExpired: true }; const rootWithExpired = new root_1.Root(configWithExpired); expect(rootWithExpired.shouldBuildExpired()).toBe(true); }); }); describe('newRoot factory function', () => { it('should create Root instance from data', () => { const data = { baseURL: 'https://factory.test', title: 'Factory Test', theme: ['test-theme'], buildDrafts: true }; const params = { custom: 'param' }; const root = (0, root_1.newRoot)(data, params); expect(root.baseUrl()).toBe('https://factory.test'); expect(root.siteTitle()).toBe('Factory Test'); expect(root.getThemes()).toEqual(['test-theme']); expect(root.shouldBuildDrafts()).toBe(true); expect(root.configParams()).toEqual(params); }); it('should create Root instance with default params', () => { const data = { baseURL: 'https://factory.test', title: 'Factory Test' }; const root = (0, root_1.newRoot)(data); expect(root.baseUrl()).toBe('https://factory.test'); expect(root.configParams()).toEqual({}); }); it('should handle empty data with defaults', () => { const root = (0, root_1.newRoot)({}); expect(root.baseUrl()).toBe(''); // default expect(root.siteTitle()).toBe(''); // default expect(root.getThemes()).toEqual([]); // default expect(root.getDefaultContentLanguage()).toBe('en'); // default }); }); describe('complex scenarios', () => { it('should handle configuration with all options set', () => { const complexConfig = { baseURL: 'https://complex.example.com', title: 'Complex Site', theme: ['primary', 'secondary', 'fallback'], timeout: '2m', contentDir: 'posts', dataDir: 'site-data', layoutDir: 'templates', staticDir: 'assets', archetypeDir: 'archetypes', assetDir: 'src', publishDir: 'dist', buildDrafts: true, buildExpired: true, buildFuture: true, copyright: '© 2024 Complex Corp', defaultContentLanguage: 'zh', defaultContentLanguageInSubdir: true, disableAliases: true, disablePathToLower: true, disableKinds: ['RSS', 'sitemap'], disableLanguages: ['fr'], renderSegments: ['page', 'home'], disableHugoGeneratorInject: true, disableLiveReload: true, enableEmoji: true }; const complexParams = { author: 'Complex Author', social: { twitter: '@complex', github: 'complex-user' } }; const root = new root_1.Root(complexConfig, complexParams); expect(root.defaultTheme()).toBe('primary'); expect(root.compiledTimeout()).toBe(120000); // 2 minutes expect(root.baseUrl()).toBe('https://complex.example.com'); expect(root.siteTitle()).toBe('Complex Site'); expect(root.getDefaultContentLanguage()).toBe('zh'); expect(root.shouldBuildDrafts()).toBe(true); expect(root.shouldBuildExpired()).toBe(true); expect(root.shouldBuildFuture()).toBe(true); expect(root.configParams()).toEqual(complexParams); }); }); }); //# sourceMappingURL=entity-root.test.js.map