169 lines
5.7 KiB
JavaScript
169 lines
5.7 KiB
JavaScript
import { describe, it, before } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import { SettingsService, DEFAULT_THEMES, DEFAULT_IDENTITY } from '../../src/services/settings.service.js';
|
|
import { ProjectService } from '../../src/services/project.service.js';
|
|
import { FtpService } from '../../src/services/ftp.service.js';
|
|
|
|
describe('Unit Tests: Settings, Projects and FTP Services', () => {
|
|
describe('1. SettingsService', () => {
|
|
const testSettingsPath = './.tmp/test_settings_clean.json';
|
|
const settings = new SettingsService(testSettingsPath);
|
|
|
|
before(() => {
|
|
if (fs.existsSync(testSettingsPath)) {
|
|
fs.unlinkSync(testSettingsPath);
|
|
}
|
|
});
|
|
|
|
it('debe inicializar y retornar la identidad por defecto', async () => {
|
|
await settings.init();
|
|
const identity = settings.getIdentity();
|
|
|
|
assert.equal(identity.appName, 'a2host');
|
|
assert.ok(identity.panelTitle);
|
|
assert.ok(identity.baseDomain);
|
|
});
|
|
|
|
it('debe actualizar campos de identidad del sistema', () => {
|
|
const updated = settings.updateIdentity({
|
|
appName: 'MiHost VPS',
|
|
slogan: 'Orquestador Profesional Apache2'
|
|
});
|
|
|
|
assert.equal(updated.appName, 'MiHost VPS');
|
|
assert.equal(updated.slogan, 'Orquestador Profesional Apache2');
|
|
});
|
|
|
|
it('debe listar temas predefinidos y permitir activar un tema', () => {
|
|
const list = settings.getThemesList();
|
|
assert.ok(list.length >= 6);
|
|
|
|
const active = settings.setActiveTheme('cyberpunk-neon');
|
|
assert.equal(active.id, 'cyberpunk-neon');
|
|
});
|
|
|
|
it('debe guardar, exportar e importar temas en formato JSON', () => {
|
|
const customTheme = {
|
|
id: 'aurora-borealis',
|
|
name: 'Aurora Borealis',
|
|
description: 'Tema personalizado verde y violeta',
|
|
colors: {
|
|
'--bg-app': '#031014',
|
|
'--accent-primary': '#22d3ee'
|
|
}
|
|
};
|
|
|
|
const saved = settings.saveCustomTheme(customTheme);
|
|
assert.equal(saved.id, 'aurora-borealis');
|
|
assert.equal(saved.isCustom, true);
|
|
|
|
const exported = settings.exportActiveThemeJson();
|
|
assert.ok(exported.id);
|
|
|
|
const imported = settings.importThemeJson(JSON.stringify({
|
|
id: 'neon-synth',
|
|
name: 'Neon Synth',
|
|
colors: { '--bg-app': '#110022' }
|
|
}));
|
|
assert.equal(imported.id, 'neon-synth');
|
|
});
|
|
|
|
it('debe restaurar tema por defecto', () => {
|
|
const def = settings.resetTheme();
|
|
assert.equal(def.id, 'obsidian-emerald');
|
|
});
|
|
});
|
|
|
|
describe('2. ProjectService', () => {
|
|
const projectService = new ProjectService({ filePath: './.tmp/test_projects.json' });
|
|
|
|
it('debe crear un proyecto con subdominios y carpeta pública personalizada en /var/www', async () => {
|
|
await projectService.init();
|
|
|
|
const created = await projectService.createProject({
|
|
name: 'ecommerce-app',
|
|
title: 'Tienda Electrónica',
|
|
type: 'static',
|
|
publicFolder: 'public',
|
|
subdomains: ['tienda', 'shop']
|
|
});
|
|
|
|
assert.equal(created.name, 'ecommerce-app');
|
|
assert.equal(created.publicFolder, 'public');
|
|
assert.ok(created.subdomains.includes('tienda'));
|
|
assert.ok(created.subdomains.includes('shop'));
|
|
assert.ok(created.effectiveDocRoot.endsWith('ecommerce-app/public'));
|
|
});
|
|
|
|
it('debe listar proyectos con metadatos de disco', async () => {
|
|
const list = await projectService.listProjects();
|
|
assert.ok(list.length >= 1);
|
|
const proj = list.find((p) => p.name === 'ecommerce-app');
|
|
assert.ok(proj);
|
|
assert.ok(proj.rootDirectory);
|
|
});
|
|
|
|
it('debe permitir añadir y remover subdominios de un proyecto', async () => {
|
|
const updated = await projectService.addSubdomain('ecommerce-app', 'checkout');
|
|
assert.ok(updated.subdomains.includes('checkout'));
|
|
|
|
const removed = await projectService.removeSubdomain('ecommerce-app', 'checkout');
|
|
assert.ok(!removed.subdomains.includes('checkout'));
|
|
});
|
|
|
|
it('debe actualizar subcarpeta pública y puerto del proyecto', async () => {
|
|
const updated = await projectService.updateProject('ecommerce-app', {
|
|
publicFolder: 'dist',
|
|
title: 'Tienda V2'
|
|
});
|
|
|
|
assert.equal(updated.publicFolder, 'dist');
|
|
assert.equal(updated.title, 'Tienda V2');
|
|
});
|
|
|
|
it('debe eliminar proyecto y sus VirtualHosts asociados', async () => {
|
|
const res = await projectService.deleteProject('ecommerce-app', { deleteFiles: false });
|
|
assert.equal(res.name, 'ecommerce-app');
|
|
});
|
|
});
|
|
|
|
describe('3. FtpService', () => {
|
|
const ftpService = new FtpService('./.tmp/test_ftp.json');
|
|
|
|
it('debe retornar datos de conexión SFTP y FTP', () => {
|
|
const info = ftpService.getConnectionInfo();
|
|
assert.ok(info.host);
|
|
assert.ok(info.sftpPort);
|
|
assert.ok(info.baseRoot);
|
|
});
|
|
|
|
it('debe crear, listar, actualizar y eliminar cuentas FTP', async () => {
|
|
await ftpService.init();
|
|
|
|
const account = await ftpService.createAccount({
|
|
username: 'dev_tienda',
|
|
password: 'secure_password_123',
|
|
project: 'test-site',
|
|
permission: 'rw'
|
|
});
|
|
|
|
assert.equal(account.username, 'dev_tienda');
|
|
assert.equal(account.project, 'test-site');
|
|
|
|
const list = await ftpService.listAccounts();
|
|
assert.ok(list.some((a) => a.username === 'dev_tienda'));
|
|
|
|
const updated = await ftpService.updateAccount(account.id, {
|
|
permission: 'ro',
|
|
active: false
|
|
});
|
|
assert.equal(updated.permission, 'ro');
|
|
assert.equal(updated.active, false);
|
|
|
|
const deleted = await ftpService.deleteAccount(account.id);
|
|
assert.equal(deleted, true);
|
|
});
|
|
});
|
|
});
|