miridev-cli
Version:
Official CLI tool for deploying static sites to miri.dev - Deploy your websites in seconds
128 lines (102 loc) • 3.99 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const { formatFileSize, loadConfig } = require('./files');
describe('Files Utility', () => {
describe('formatFileSize', () => {
test('should format bytes correctly', () => {
expect(formatFileSize(0)).toBe('0 B');
expect(formatFileSize(512)).toBe('512 B');
expect(formatFileSize(1024)).toBe('1 KB');
expect(formatFileSize(1536)).toBe('1.5 KB');
expect(formatFileSize(1048576)).toBe('1 MB');
expect(formatFileSize(1073741824)).toBe('1 GB');
});
test('should handle decimal places correctly', () => {
expect(formatFileSize(1500)).toBe('1.5 KB');
expect(formatFileSize(2560000)).toBe('2.4 MB');
});
});
describe('loadConfig', () => {
let tempDir;
beforeEach(() => {
tempDir = global.testHelpers.createTempDir();
});
afterEach(() => {
global.testHelpers.cleanupTempDir(tempDir);
});
test('should return empty object for non-existent config', async () => {
const configPath = path.join(tempDir, 'non-existent.js');
const config = await loadConfig(configPath);
expect(config).toEqual({});
});
test('should load JavaScript config file', async () => {
const configPath = path.join(tempDir, 'miri.config.js');
const configContent = `
module.exports = {
site: {
name: "test-site"
},
deploy: {
ignore: ["*.tmp"]
}
};
`;
await fs.writeFile(configPath, configContent);
const config = await loadConfig(configPath);
expect(config.site.name).toBe('test-site');
expect(config.deploy.ignore).toContain('*.tmp');
});
test('should load JSON config file', async () => {
const configPath = path.join(tempDir, 'miri.config.json');
const configData = {
site: {
name: 'json-test-site'
},
build: {
command: 'npm run build'
}
};
await fs.writeJson(configPath, configData);
const config = await loadConfig(configPath);
expect(config.site.name).toBe('json-test-site');
expect(config.build.command).toBe('npm run build');
});
test('should throw error for unsupported config format', async () => {
const configPath = path.join(tempDir, 'miri.config.yaml');
await fs.writeFile(configPath, 'site:\n name: test');
await expect(loadConfig(configPath)).rejects.toThrow('Unsupported config file format');
});
test('should throw error for invalid JSON', async () => {
const configPath = path.join(tempDir, 'invalid.json');
await fs.writeFile(configPath, '{ invalid json }');
await expect(loadConfig(configPath)).rejects.toThrow('Failed to load config file');
});
});
});
// Test file scanning functionality
describe('File Scanning', () => {
let tempDir;
beforeEach(() => {
tempDir = global.testHelpers.createTempDir();
});
afterEach(() => {
global.testHelpers.cleanupTempDir(tempDir);
});
test('should require index.html file', async () => {
// Create a directory without index.html
await fs.writeFile(path.join(tempDir, 'other.html'), '<html></html>');
const { scanFiles } = require('./files');
await expect(scanFiles(tempDir)).rejects.toThrow('index.html file is required');
});
test('should scan files with index.html present', async () => {
// Create test files
await fs.writeFile(path.join(tempDir, 'index.html'), '<html><body>Home</body></html>');
await fs.writeFile(path.join(tempDir, 'style.css'), 'body { margin: 0; }');
const { scanFiles } = require('./files');
const result = await scanFiles(tempDir);
expect(result.files).toHaveLength(2);
expect(result.totalFiles).toBe(2);
expect(result.files.find(f => f.relativePath === 'index.html')).toBeDefined();
expect(result.files.find(f => f.relativePath === 'style.css')).toBeDefined();
});
});