UNPKG

@mdfriday/foundry

Version:

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

162 lines 6.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("./index"); // Mock shifter for testing class MockShifter { forEachInDimension(n, d, f) { // Simple implementation - just call f with n f(n); } insert(old, newItem) { // For testing, just return the new item return newItem; } insertInto(old, newItem, dimension) { // For testing, just return the new item return newItem; } delete(v, dimension) { // For testing, always return true, true (deleted, isEmpty) return [true, true]; } shift(v, dimension, exact) { // For testing, just return the value as is return [v, true, index_1.DimensionFlag.DimensionLanguage]; } } describe('DocTree', () => { describe('validateKey', () => { test('should validate keys correctly', () => { expect((0, index_1.validateKey)('')).toBeNull(); // Root is valid expect((0, index_1.validateKey)('/valid/path')).toBeNull(); expect((0, index_1.validateKey)('invalid')).toBeInstanceOf(Error); // Must start with / expect((0, index_1.validateKey)('/valid/')).toBeInstanceOf(Error); // Must not end with / expect((0, index_1.validateKey)('/a')).toBeNull(); // Two characters minimum expect((0, index_1.validateKey)('/')).toBeInstanceOf(Error); // Single / is too short }); }); describe('cleanKey', () => { test('should clean keys correctly', () => { expect((0, index_1.cleanKey)('/')).toBe(''); expect((0, index_1.cleanKey)('/path')).toBe('/path'); expect((0, index_1.cleanKey)('')).toBe(''); }); }); describe('SimpleTree', () => { test('should create and operate on simple tree', () => { const tree = (0, index_1.newSimpleTree)(); // Insert and get const result = tree.insert('/test', 'value'); expect(result).toBe('value'); expect(tree.get('/test')).toBe('value'); // Longest prefix const [key, value] = tree.longestPrefix('/test/child'); expect(key).toBe('/test'); expect(value).toBe('value'); // Non-existent key expect(tree.get('/nonexistent')).toBeUndefined(); }); test('should handle walk prefix', async () => { const tree = (0, index_1.newSimpleTree)(); tree.insert('/path1', 'value1'); tree.insert('/path2', 'value2'); tree.insert('/other', 'value3'); const visited = []; const err = await tree.walkPrefix(index_1.LockType.LockTypeNone, '/path', (key, value) => { visited.push([key, value]); return [false, null]; // Continue }); expect(err).toBeNull(); expect(visited).toHaveLength(2); expect(visited).toContainEqual(['/path1', 'value1']); expect(visited).toContainEqual(['/path2', 'value2']); }); }); describe('TreeShiftTree', () => { test('should create and shape tree shift tree', () => { const tree = index_1.TreeShiftTree.newTreeShiftTree(0, 2); // Insert in default dimension tree.insert('/test', 'value1'); expect(tree.get('/test')).toBe('value1'); // Shape to different dimension const shaped = tree.shape(0, 1); shaped.insert('/test', 'value2'); // Original tree should still have value1 expect(tree.get('/test')).toBe('value1'); // Shaped tree should have value2 expect(shaped.get('/test')).toBe('value2'); }); }); describe('NodeShiftTree', () => { test('should create and operate on node shift tree', () => { const config = { shifter: new MockShifter() }; const tree = index_1.NodeShiftTree.new(config); // Test basic operations expect(tree.len()).toBe(0); const [value, inserted] = tree.insertIntoValuesDimension('/test', 'value'); expect(value).toBe('value'); expect(inserted).toBe(true); expect(tree.len()).toBe(1); expect(tree.get('/test')).toBe('value'); expect(tree.has('/test')).toBe(true); expect(tree.has('/nonexistent')).toBe(false); }); test('should handle deletion', () => { const config = { shifter: new MockShifter() }; const tree = index_1.NodeShiftTree.new(config); tree.insertIntoValuesDimension('/test', 'value'); expect(tree.len()).toBe(1); tree.delete('/test'); expect(tree.len()).toBe(0); expect(tree.has('/test')).toBe(false); }); test('should handle prefix operations', async () => { const config = { shifter: new MockShifter() }; const tree = index_1.NodeShiftTree.new(config); tree.insertIntoValuesDimension('/path/1', 'value1'); tree.insertIntoValuesDimension('/path/2', 'value2'); tree.insertIntoValuesDimension('/other', 'value3'); const count = await tree.deletePrefix('/path'); expect(count).toBe(2); expect(tree.has('/path/1')).toBe(false); expect(tree.has('/path/2')).toBe(false); expect(tree.has('/other')).toBe(true); }); test('should handle shaping', () => { const config = { shifter: new MockShifter() }; const tree = index_1.NodeShiftTree.new(config); const shaped = tree.shape(0, 1); expect(shaped).not.toBe(tree); expect(shaped.toString()).toContain('1'); }); test('should handle longest prefix', () => { const config = { shifter: new MockShifter() }; const tree = index_1.NodeShiftTree.new(config); tree.insertIntoValuesDimension('/path', 'value'); const [key, value] = tree.longestPrefix('/path/child', true); expect(key).toBe('/path'); expect(value).toBe('value'); }); }); describe('DimensionFlag operations', () => { test('should handle dimension flag operations', () => { const flag1 = index_1.DimensionFlag.DimensionLanguage; const flag2 = index_1.DimensionFlag.DimensionNone; // Test bitwise operations const combined = flag1 | flag2; expect((combined & flag1) === flag1).toBe(true); expect((combined & flag2) === flag2).toBe(true); }); }); }); //# sourceMappingURL=doctree.test.js.map