@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
171 lines • 7.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const filemeta_1 = require("../vo/filemeta");
// Mock dependencies
jest.mock('@internal/domain/paths', () => ({
PATH_CONSTANTS: {
SYSTEM_PATH_SEPARATOR: '/',
},
}));
describe('FileMeta Value Object', () => {
let fileMeta;
let mockFileOpener;
beforeEach(() => {
mockFileOpener = jest.fn().mockResolvedValue({});
fileMeta = new filemeta_1.FileMeta('/path/to/file.txt', mockFileOpener);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('constructor', () => {
it('should initialize with filename and openFunc', () => {
expect(fileMeta.fileName()).toBe('/path/to/file.txt');
expect(fileMeta.openFunc).toBe(mockFileOpener);
});
it('should initialize with default values when no parameters provided', () => {
const emptyMeta = new filemeta_1.FileMeta();
expect(emptyMeta.fileName()).toBe('');
expect(emptyMeta.component()).toBe('');
expect(emptyMeta.root()).toBe('');
expect(emptyMeta.openFunc).toBeNull();
});
});
describe('fileName', () => {
it('should return the filename', () => {
expect(fileMeta.fileName()).toBe('/path/to/file.txt');
});
});
describe('relativeFilename', () => {
it('should return filename when no component root is set', () => {
const result = fileMeta.relativeFilename();
expect(result).toBe('/path/to/file.txt');
});
it('should return relative path when component root is set', () => {
fileMeta.setComponentRoot('/path');
const result = fileMeta.relativeFilename();
expect(result).toBe('/to/file.txt');
});
it('should add leading separator when relative path does not start with separator', () => {
fileMeta.setFileName('/path/file.txt');
fileMeta.setComponentRoot('/path');
const result = fileMeta.relativeFilename();
expect(result).toBe('/file.txt');
});
it('should throw error when filename has no root', () => {
fileMeta.setComponentRoot('/different/root');
expect(() => fileMeta.relativeFilename()).toThrow('filename /path/to/file.txt has no root /different/root');
});
});
describe('component', () => {
it('should return the component directory', () => {
fileMeta.setComponentDir('content');
expect(fileMeta.component()).toBe('content');
});
});
describe('root', () => {
it('should return the component root', () => {
fileMeta.setComponentRoot('/root');
expect(fileMeta.root()).toBe('/root');
});
});
describe('open', () => {
it('should call openFunc when set', async () => {
const mockFile = {};
mockFileOpener.mockResolvedValue(mockFile);
const result = await fileMeta.open();
expect(mockFileOpener).toHaveBeenCalledTimes(1);
expect(result).toBe(mockFile);
});
it('should throw error when openFunc is not set', async () => {
const metaWithoutOpener = new filemeta_1.FileMeta('/test.txt', null);
await expect(metaWithoutOpener.open()).rejects.toThrow('OpenFunc not set');
});
});
describe('setter methods', () => {
it('should set filename', () => {
fileMeta.setFileName('/new/file.txt');
expect(fileMeta.fileName()).toBe('/new/file.txt');
});
it('should set component root', () => {
fileMeta.setComponentRoot('/new/root');
expect(fileMeta.root()).toBe('/new/root');
});
it('should set component directory', () => {
fileMeta.setComponentDir('layouts');
expect(fileMeta.component()).toBe('layouts');
});
it('should set open function', async () => {
const newOpener = jest.fn().mockResolvedValue({});
fileMeta.setOpenFunc(newOpener);
await fileMeta.open();
expect(newOpener).toHaveBeenCalledTimes(1);
expect(mockFileOpener).not.toHaveBeenCalled();
});
});
describe('merge', () => {
let targetMeta;
let sourceMeta;
beforeEach(() => {
targetMeta = new filemeta_1.FileMeta();
sourceMeta = new filemeta_1.FileMeta('/source/file.txt');
sourceMeta.setComponentRoot('/source');
sourceMeta.setComponentDir('content');
sourceMeta.setOpenFunc(mockFileOpener);
});
it('should merge non-empty values from source to empty target', () => {
targetMeta.merge(sourceMeta);
expect(targetMeta.fileName()).toBe('/source/file.txt');
expect(targetMeta.root()).toBe('/source');
expect(targetMeta.component()).toBe('content');
expect(targetMeta.openFunc).toBe(mockFileOpener);
});
it('should not overwrite existing values in target', () => {
targetMeta.setFileName('/target/file.txt');
targetMeta.setComponentRoot('/target');
targetMeta.setComponentDir('layouts');
const targetOpener = jest.fn();
targetMeta.setOpenFunc(targetOpener);
targetMeta.merge(sourceMeta);
expect(targetMeta.fileName()).toBe('/target/file.txt');
expect(targetMeta.root()).toBe('/target');
expect(targetMeta.component()).toBe('layouts');
expect(targetMeta.openFunc).toBe(targetOpener);
});
it('should handle null/undefined source gracefully', () => {
const originalFilename = targetMeta.fileName();
targetMeta.merge(null);
targetMeta.merge(undefined);
expect(targetMeta.fileName()).toBe(originalFilename);
});
it('should merge only empty fields from source', () => {
targetMeta.setFileName('/target/file.txt');
// Leave other fields empty
targetMeta.merge(sourceMeta);
expect(targetMeta.fileName()).toBe('/target/file.txt'); // Not overwritten
expect(targetMeta.root()).toBe('/source'); // Merged
expect(targetMeta.component()).toBe('content'); // Merged
expect(targetMeta.openFunc).toBe(mockFileOpener); // Merged
});
});
describe('newFileMeta factory function', () => {
it('should create FileMeta with filename and opener', () => {
const result = (0, filemeta_1.newFileMeta)('/test.txt', mockFileOpener);
expect(result).toBeInstanceOf(filemeta_1.FileMeta);
expect(result.fileName()).toBe('/test.txt');
expect(result.openFunc).toBe(mockFileOpener);
});
it('should create FileMeta with only filename', () => {
const result = (0, filemeta_1.newFileMeta)('/test.txt');
expect(result).toBeInstanceOf(filemeta_1.FileMeta);
expect(result.fileName()).toBe('/test.txt');
expect(result.openFunc).toBeNull();
});
it('should create empty FileMeta when no parameters provided', () => {
const result = (0, filemeta_1.newFileMeta)();
expect(result).toBeInstanceOf(filemeta_1.FileMeta);
expect(result.fileName()).toBe('');
expect(result.openFunc).toBeNull();
});
});
});
//# sourceMappingURL=vo-filemeta.test.js.map