import assert from 'assert'; import * as fs from 'fs/promises'; import * as os from 'os'; import path from 'path'; import { normalizePageName, parseMetadata, savePageMetadata, loadPageMetadata, savePageState, loadPageState, listPages, deletePage, copyPage, PAGE_VERSION, } from '../src/pages'; import { SynthOSConfig } from '../src/init'; import { FsStorageProvider } from '../src/storage'; /** Create a minimal SynthOSConfig for testing. */ function makeConfig(pagesFolder: string): SynthOSConfig { return { localFolder: '.synthos', pagesFolder, requiredPagesFolders: [], defaultPagesFolders: [], defaultScriptsFolders: [], defaultThemesFolders: [], staticFilesFolders: [], serviceConnectorsFolders: [], requiredPages: [], storageProvider: new FsStorageProvider(), debug: false, debugPageUpdates: false, }; } // --------------------------------------------------------------------------- // normalizePageName // --------------------------------------------------------------------------- describe('normalizePageName', () => { it('lowercases the name', () => { assert.strictEqual(normalizePageName('MyPage'), 'mypage'); }); it('replaces invalid characters with underscores', () => { assert.strictEqual(normalizePageName('my page!'), 'my_page_'); }); it('preserves allowed special characters', () => { // Allowed: a-z 0-9 - _ [ ] ( ) { } @ # $ % & const input = 'test-name_[ok](yes){no}@#$%&'; assert.strictEqual(normalizePageName(input), input.toLowerCase()); }); it('returns undefined for empty string', () => { assert.strictEqual(normalizePageName(''), undefined); }); it('returns undefined for undefined input', () => { assert.strictEqual(normalizePageName(undefined), undefined); }); }); // --------------------------------------------------------------------------- // parseMetadata // --------------------------------------------------------------------------- describe('parseMetadata', () => { it('parses complete input', () => { const input = { title: 'My Page', categories: ['Tools'], pinned: true, createdDate: '2025-01-01T00:00:00Z', lastModified: '2025-06-01T00:00:00Z', pageVersion: 2, mode: 'locked', }; const result = parseMetadata(input); assert.strictEqual(result.title, 'My Page'); assert.deepStrictEqual(result.categories, ['Tools']); assert.strictEqual(result.pinned, true); assert.strictEqual(result.createdDate, '2025-01-01T00:00:00Z'); assert.strictEqual(result.lastModified, '2025-06-01T00:00:00Z'); assert.strictEqual(result.pageVersion, 2); assert.strictEqual(result.mode, 'locked'); }); it('provides defaults for missing fields', () => { const result = parseMetadata({}); assert.strictEqual(result.title, ''); assert.deepStrictEqual(result.categories, []); assert.strictEqual(result.pinned, false); assert.strictEqual(result.createdDate, ''); assert.strictEqual(result.lastModified, ''); assert.strictEqual(result.pageVersion, 0); assert.strictEqual(result.mode, 'unlocked'); }); it('provides defaults for wrong types', () => { const result = parseMetadata({ title: 42, categories: 'not-array', pinned: 'yes', createdDate: 123, lastModified: null, pageVersion: 'two', mode: 'invalid', }); assert.strictEqual(result.title, ''); assert.deepStrictEqual(result.categories, []); assert.strictEqual(result.pinned, false); assert.strictEqual(result.createdDate, ''); assert.strictEqual(result.lastModified, ''); assert.strictEqual(result.pageVersion, 0); assert.strictEqual(result.mode, 'unlocked'); }); it('falls back from uxVersion to pageVersion', () => { const result = parseMetadata({ uxVersion: 1 }); assert.strictEqual(result.pageVersion, 1); }); it('prefers pageVersion over uxVersion when both present', () => { const result = parseMetadata({ pageVersion: 2, uxVersion: 1 }); assert.strictEqual(result.pageVersion, 2); }); it('validates mode — only "locked" passes, everything else becomes "unlocked"', () => { assert.strictEqual(parseMetadata({ mode: 'locked' }).mode, 'locked'); assert.strictEqual(parseMetadata({ mode: 'unlocked' }).mode, 'unlocked'); assert.strictEqual(parseMetadata({ mode: 'read-only' }).mode, 'unlocked'); assert.strictEqual(parseMetadata({ mode: 123 }).mode, 'unlocked'); }); }); // --------------------------------------------------------------------------- // Page I/O (temp-dir tests) // --------------------------------------------------------------------------- describe('page I/O', () => { let tmpDir: string; let config: SynthOSConfig; let fallbackFolder: string; beforeEach(async () => { tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'synthos-test-')); const pagesFolder = path.join(tmpDir, 'synthos'); fallbackFolder = path.join(tmpDir, 'fallback'); await fs.mkdir(pagesFolder, { recursive: true }); await fs.mkdir(fallbackFolder, { recursive: true }); config = makeConfig(pagesFolder); }); afterEach(async () => { await fs.rm(tmpDir, { recursive: true, force: true }); }); // -- savePageMetadata / loadPageMetadata --------------------------------- describe('savePageMetadata / loadPageMetadata', () => { it('roundtrips metadata correctly', async () => { const meta = { title: 'Test Page', categories: ['Tools', 'Dev'], pinned: true, showInAll: true, createdDate: '2025-01-01T00:00:00Z', lastModified: '2025-06-01T00:00:00Z', pageVersion: 2, mode: 'locked' as const, }; await savePageMetadata(config, 'mypage', meta); const loaded = await loadPageMetadata(config, 'mypage'); assert.ok(loaded); assert.strictEqual(loaded.title, 'Test Page'); assert.deepStrictEqual(loaded.categories, ['Tools', 'Dev']); assert.strictEqual(loaded.pinned, true); assert.strictEqual(loaded.pageVersion, 2); assert.strictEqual(loaded.mode, 'locked'); }); it('returns undefined when no metadata file exists', async () => { const loaded = await loadPageMetadata(config, 'nonexistent'); assert.strictEqual(loaded, undefined); }); it('falls back to fallbackFolder', async () => { // Write a fallback JSON file in folder-based structure const fallbackPageDir = path.join(fallbackFolder, 'system-page'); await fs.mkdir(fallbackPageDir, { recursive: true }); await fs.writeFile( path.join(fallbackPageDir, 'page.json'), JSON.stringify({ title: 'System', categories: ['System'], pinned: true, pageVersion: 2, mode: 'locked' }), ); const loaded = await loadPageMetadata(config, 'system-page', [fallbackFolder]); assert.ok(loaded); assert.strictEqual(loaded.title, 'System'); }); }); // -- savePageState / loadPageState --------------------------------------- describe('savePageState / loadPageState', () => { it('roundtrips page HTML and creates metadata', async () => { const html = 'Hello'; await savePageState(config, 'testpage', html, 'Test Title', ['Cat']); const loaded = await loadPageState(config, 'testpage'); assert.strictEqual(loaded, html); // Metadata should have been created const meta = await loadPageMetadata(config, 'testpage'); assert.ok(meta); assert.strictEqual(meta.pageVersion, PAGE_VERSION); }); it('returns undefined for non-existent page', async () => { const loaded = await loadPageState(config, 'nope'); assert.strictEqual(loaded, undefined); }); }); // -- listPages ----------------------------------------------------------- describe('listPages', () => { it('lists folder-based pages', async () => { await savePageState(config, 'alpha', 'A'); await savePageState(config, 'beta', 'B'); const pages = await listPages(config, [fallbackFolder]); const names = pages.map(p => p.name); assert.ok(names.includes('alpha')); assert.ok(names.includes('beta')); }); it('lists legacy flat HTML files', async () => { await fs.writeFile(path.join(config.pagesFolder, 'legacy.html'), 'Legacy'); const pages = await listPages(config, [fallbackFolder]); const legacy = pages.find(p => p.name === 'legacy'); assert.ok(legacy); assert.strictEqual(legacy.pageVersion, 1); }); it('returns pages sorted alphabetically', async () => { await savePageState(config, 'zebra', ''); await savePageState(config, 'apple', ''); const pages = await listPages(config, [fallbackFolder]); const names = pages.map(p => p.name); const sortedNames = [...names].sort(); assert.deepStrictEqual(names, sortedNames); }); }); // -- deletePage ---------------------------------------------------------- describe('deletePage', () => { it('removes folder-based page', async () => { await savePageState(config, 'doomed', 'bye'); await deletePage(config, 'doomed'); // Folder should be gone const folderExists = await fs.access(path.join(config.pagesFolder, 'pages', 'doomed')) .then(() => true).catch(() => false); assert.strictEqual(folderExists, false); // Loading should return undefined const loaded = await loadPageState(config, 'doomed'); assert.strictEqual(loaded, undefined); }); }); // -- copyPage ------------------------------------------------------------ describe('copyPage', () => { it('copies HTML and creates metadata with correct title/categories', async () => { await savePageState(config, 'source', 'Source'); await copyPage(config, 'source', 'target', 'Copied Page', ['Copy'], [fallbackFolder]); const html = await loadPageState(config, 'target'); assert.strictEqual(html, 'Source'); const meta = await loadPageMetadata(config, 'target'); assert.ok(meta); assert.strictEqual(meta.title, 'Copied Page'); assert.deepStrictEqual(meta.categories, ['Copy']); assert.strictEqual(meta.pageVersion, PAGE_VERSION); }); it('throws when source page does not exist', async () => { await assert.rejects( () => copyPage(config, 'ghost', 'target', 'T', [], [fallbackFolder]), /Source page "ghost" not found/, ); }); }); });