Add tests for DC-105/106/108 endpoints + fleet env fix
- Wizard: 6 tests (categories, recommend, hardware profiles, apply) - Caddycode: 5 tests (generate, validate, templates) - Fleet: 4 tests (register, list, deploy, validation) - Fleet: loadHosts/saveHosts now reads env at call time for test isolation - 1648 tests pass, 72 suites
This commit is contained in:
@@ -0,0 +1,135 @@
|
|||||||
|
/**
|
||||||
|
* DC-106 + DC-108: Caddycode + Fleet endpoint tests
|
||||||
|
*/
|
||||||
|
const express = require('express');
|
||||||
|
const request = require('supertest');
|
||||||
|
|
||||||
|
function createCaddycodeApp() {
|
||||||
|
const app = express();
|
||||||
|
app.use(express.json());
|
||||||
|
const routes = require('../../routes/caddycode');
|
||||||
|
const wrap = (fn) => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
|
||||||
|
app.use('/api/v1', routes({ asyncHandler: wrap }));
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createFleetApp(log) {
|
||||||
|
const app = express();
|
||||||
|
app.use(express.json());
|
||||||
|
const routes = require('../../routes/fleet');
|
||||||
|
const wrap = (fn) => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
|
||||||
|
app.use('/api/v1', routes({ log: log || { info: jest.fn(), error: jest.fn() }, asyncHandler: wrap }));
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('DC-106: Caddyfile-as-Code', () => {
|
||||||
|
it('POST /generate creates Caddyfile from config', async () => {
|
||||||
|
const app = createCaddycodeApp();
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/caddycode/generate')
|
||||||
|
.send({
|
||||||
|
domain: 'app.example.com',
|
||||||
|
upstream: 'localhost:8080',
|
||||||
|
websocket: true,
|
||||||
|
cors: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.caddyfile).toContain('app.example.com');
|
||||||
|
expect(res.body.caddyfile).toContain('reverse_proxy');
|
||||||
|
expect(res.body.caddyfile).toContain('Access-Control-Allow-Origin');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /generate returns 400 without domain', async () => {
|
||||||
|
const app = createCaddycodeApp();
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/caddycode/generate')
|
||||||
|
.send({ upstream: 'localhost:8080' });
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /validate finds unbalanced braces', async () => {
|
||||||
|
const app = createCaddycodeApp();
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/caddycode/validate')
|
||||||
|
.send({ caddyfile: 'app.com {\n reverse_proxy localhost:8080\n' });
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.valid).toBe(false);
|
||||||
|
expect(res.body.issues[0]).toContain('Unbalanced');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /validate passes for valid Caddyfile', async () => {
|
||||||
|
const app = createCaddycodeApp();
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/caddycode/validate')
|
||||||
|
.send({ caddyfile: 'app.com {\n reverse_proxy localhost:8080\n}' });
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.valid).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('GET /templates returns preset configs', async () => {
|
||||||
|
const app = createCaddycodeApp();
|
||||||
|
const res = await request(app).get('/api/v1/caddycode/templates');
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(Object.keys(res.body.templates).length).toBeGreaterThanOrEqual(5);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('DC-108: Fleet Management', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env.FLEET_HOSTS_FILE = `/tmp/fleet-test-${Date.now()}-${Math.random().toString(36).slice(2)}.json`;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
try { require('fs').unlinkSync(process.env.FLEET_HOSTS_FILE); } catch { /* ok */ }
|
||||||
|
});
|
||||||
|
|
||||||
|
it('GET /hosts returns empty list initially', async () => {
|
||||||
|
const app = createFleetApp();
|
||||||
|
const res = await request(app).get('/api/v1/fleet/hosts');
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.total).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /hosts registers a new host', async () => {
|
||||||
|
const app = createFleetApp();
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/fleet/hosts')
|
||||||
|
.send({ name: 'Test Host', hostname: '192.168.1.100', apiKey: 'dk_test_12345', tags: ['prod'] });
|
||||||
|
|
||||||
|
expect(res.status).toBe(201);
|
||||||
|
expect(res.body.host.name).toBe('Test Host');
|
||||||
|
expect(res.body.host.apiKey).toBe('***'); // Key is masked
|
||||||
|
expect(res.body.host.apiKeyHash).toBeTruthy();
|
||||||
|
expect(res.body.host.id).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /hosts returns 400 without name', async () => {
|
||||||
|
const app = createFleetApp();
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/fleet/hosts')
|
||||||
|
.send({ hostname: '192.168.1.100' });
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /deploy generates deployment plan', async () => {
|
||||||
|
const app = createFleetApp();
|
||||||
|
// First register a host
|
||||||
|
await request(app)
|
||||||
|
.post('/api/v1/fleet/hosts')
|
||||||
|
.send({ name: 'Host 1', hostname: '10.0.0.1' });
|
||||||
|
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/fleet/deploy')
|
||||||
|
.send({ templateId: 'plex', config: { port: 32400 } });
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.totalHosts).toBeGreaterThanOrEqual(1);
|
||||||
|
expect(res.body.plan[0].templateId).toBe('plex');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
/**
|
||||||
|
* DC-105: Wizard endpoint tests
|
||||||
|
*/
|
||||||
|
const express = require('express');
|
||||||
|
const request = require('supertest');
|
||||||
|
|
||||||
|
function createApp(templates) {
|
||||||
|
const app = express();
|
||||||
|
app.use(express.json());
|
||||||
|
const wizardRoutes = require('../../routes/wizard');
|
||||||
|
const wrap = (fn) => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
|
||||||
|
app.use('/api/v1', wizardRoutes({ APP_TEMPLATES: templates || [], asyncHandler: wrap }));
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('DC-105: Smart Defaults Wizard', () => {
|
||||||
|
it('GET /categories returns 6 categories', async () => {
|
||||||
|
const app = createApp();
|
||||||
|
const res = await request(app).get('/api/v1/wizard/categories');
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.success).toBe(true);
|
||||||
|
expect(res.body.categories).toHaveLength(6);
|
||||||
|
expect(res.body.categories[0]).toHaveProperty('id');
|
||||||
|
expect(res.body.categories[0]).toHaveProperty('label');
|
||||||
|
expect(res.body.categories[0]).toHaveProperty('icon');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /recommend returns services for media-streaming', async () => {
|
||||||
|
const app = createApp([
|
||||||
|
{ id: 'plex', name: 'Plex', image: 'plexinc/pms-docker', ports: [32400] },
|
||||||
|
{ id: 'sonarr', name: 'Sonarr', image: 'lscr.io/linuxserver/sonarr', ports: [8989] },
|
||||||
|
]);
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/wizard/recommend')
|
||||||
|
.send({ categories: ['media-streaming'], hardwareProfile: 'medium' });
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.totalRecommended).toBeGreaterThan(0);
|
||||||
|
expect(res.body.services[0].template).toBe('plex');
|
||||||
|
expect(res.body.services[0].available).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /recommend returns 400 without categories', async () => {
|
||||||
|
const app = createApp();
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/wizard/recommend')
|
||||||
|
.send({ categories: [] });
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /recommend limits services by hardware profile', async () => {
|
||||||
|
const app = createApp();
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/wizard/recommend')
|
||||||
|
.send({ categories: ['media-streaming', 'development', 'monitoring'], hardwareProfile: 'minimal' });
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.totalRecommended).toBeLessThanOrEqual(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /apply returns deployment plan', async () => {
|
||||||
|
const app = createApp();
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/wizard/apply')
|
||||||
|
.send({ services: ['plex', 'sonarr'], subdomainPrefix: 'sami-' });
|
||||||
|
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.totalSteps).toBe(2);
|
||||||
|
expect(res.body.plan[0].subdomain).toBe('sami-plex');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('POST /apply returns 400 without services', async () => {
|
||||||
|
const app = createApp();
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/api/v1/wizard/apply')
|
||||||
|
.send({ services: [] });
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -28,8 +28,9 @@ module.exports = function({ log, asyncHandler }) {
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
async function loadHosts() {
|
async function loadHosts() {
|
||||||
|
const hostsFile = process.env.FLEET_HOSTS_FILE || HOSTS_FILE;
|
||||||
try {
|
try {
|
||||||
const data = await fsp.readFile(HOSTS_FILE, 'utf8');
|
const data = await fsp.readFile(hostsFile, 'utf8');
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
} catch {
|
} catch {
|
||||||
return [];
|
return [];
|
||||||
@@ -37,8 +38,9 @@ module.exports = function({ log, asyncHandler }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function saveHosts(hosts) {
|
async function saveHosts(hosts) {
|
||||||
await fsp.mkdir(path.dirname(HOSTS_FILE), { recursive: true });
|
const hostsFile = process.env.FLEET_HOSTS_FILE || HOSTS_FILE;
|
||||||
await fsp.writeFile(HOSTS_FILE, JSON.stringify(hosts, null, 2));
|
await fsp.mkdir(path.dirname(hostsFile), { recursive: true });
|
||||||
|
await fsp.writeFile(hostsFile, JSON.stringify(hosts, null, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /api/v1/fleet/hosts
|
// GET /api/v1/fleet/hosts
|
||||||
|
|||||||
Reference in New Issue
Block a user