234 lines
7.6 KiB
TypeScript
234 lines
7.6 KiB
TypeScript
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
import { WorkflowConfigSchema } from '../src/shared/schemas/workflow.schema.js';
|
|
import { ConfigManager } from '../src/server/config/config-manager.js';
|
|
import { openDatabase } from '../src/server/db/database.js';
|
|
import { VaultService } from '../src/server/security/vault.js';
|
|
|
|
const tempDirs: string[] = [];
|
|
|
|
function makeEnv() {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gcoord-cfg-test-'));
|
|
tempDirs.push(dir);
|
|
const db = openDatabase(path.join(dir, 'gcoord.db'));
|
|
const vault = new VaultService(path.join(dir, 'vault.enc'));
|
|
const configDir = path.join(dir, 'config');
|
|
const mgr = new ConfigManager(configDir, db, vault);
|
|
return { dir, configDir, db, vault, mgr };
|
|
}
|
|
|
|
async function waitFor(predicate: () => boolean, timeoutMs = 4000): Promise<void> {
|
|
const start = Date.now();
|
|
while (Date.now() - start < timeoutMs) {
|
|
if (predicate()) return;
|
|
await new Promise((r) => setTimeout(r, 50));
|
|
}
|
|
throw new Error(`Timed out after ${timeoutMs}ms waiting for condition`);
|
|
}
|
|
|
|
afterEach(async () => {
|
|
while (tempDirs.length > 0) {
|
|
fs.rmSync(tempDirs.pop()!, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
describe('ConfigManager, Presets, Hot-Reload, DAG & Revisions', () => {
|
|
it('seeds presets on first boot, records revisions, computes diff, and performs 1-click rollback', async () => {
|
|
const { configDir, db, mgr } = makeEnv();
|
|
await mgr.init({ watch: false });
|
|
|
|
try {
|
|
expect(fs.existsSync(path.join(configDir, 'settings.json'))).toBe(true);
|
|
expect(fs.existsSync(path.join(configDir, 'alerts.json'))).toBe(true);
|
|
expect(fs.existsSync(path.join(configDir, 'snippets.json'))).toBe(true);
|
|
expect(mgr.listSystems().map((s) => s.id).sort()).toEqual([
|
|
'docker-compose',
|
|
'os-packages',
|
|
'systemd',
|
|
]);
|
|
|
|
const v1 = mgr.getSettings();
|
|
expect(v1.theme).toBe('phosphor-green');
|
|
|
|
mgr.saveConfigText(
|
|
'settings.json',
|
|
JSON.stringify({ ...v1, theme: 'amber-term' }, null, 2),
|
|
'ui',
|
|
'Switch to amber-term',
|
|
);
|
|
expect(mgr.getSettings().theme).toBe('amber-term');
|
|
|
|
const revs = mgr.listRevisions('settings.json');
|
|
expect(revs).toHaveLength(2);
|
|
const seedRev = revs[1]!;
|
|
const diffRes = mgr.diffRevision(seedRev.id);
|
|
expect(diffRes.diff.unified).toContain('- "theme": "phosphor-green"');
|
|
expect(diffRes.diff.unified).toContain('+ "theme": "amber-term"');
|
|
|
|
mgr.rollbackRevision(seedRev.id);
|
|
expect(mgr.getSettings().theme).toBe('phosphor-green');
|
|
expect(mgr.listRevisions('settings.json')).toHaveLength(3);
|
|
} finally {
|
|
await mgr.close();
|
|
db.close();
|
|
}
|
|
});
|
|
|
|
it('hot-reloads external edits and preserves isolated valid state when one file breaks', async () => {
|
|
const { configDir, db, mgr } = makeEnv();
|
|
await mgr.init({ watch: true });
|
|
|
|
try {
|
|
mgr.saveConfigText(
|
|
'servers/web-01.json',
|
|
JSON.stringify({
|
|
id: 'web-01',
|
|
name: 'Web 01',
|
|
host: '10.0.0.10',
|
|
username: 'ops',
|
|
credentialId: 'cred-1',
|
|
}),
|
|
);
|
|
mgr.saveConfigText(
|
|
'servers/db-01.json',
|
|
JSON.stringify({
|
|
id: 'db-01',
|
|
name: 'DB 01',
|
|
host: '10.0.0.11',
|
|
username: 'ops',
|
|
credentialId: 'cred-1',
|
|
}),
|
|
);
|
|
|
|
// External creation of new server JSON picked up via chokidar
|
|
fs.writeFileSync(
|
|
path.join(configDir, 'servers', 'edge-01.json'),
|
|
JSON.stringify({
|
|
id: 'edge-01',
|
|
name: 'Edge 01',
|
|
host: '10.0.0.12',
|
|
username: 'ops',
|
|
credentialId: 'cred-1',
|
|
}),
|
|
);
|
|
await waitFor(() => Boolean(mgr.getServer('edge-01')));
|
|
expect(mgr.getServer('edge-01')?.name).toBe('Edge 01');
|
|
|
|
// Corrupt ONLY web-01.json externally on disk
|
|
fs.writeFileSync(path.join(configDir, 'servers', 'web-01.json'), '{ invalid json');
|
|
await waitFor(() =>
|
|
mgr.getDiagnostics().errors.some((e) => e.filePath === 'servers/web-01.json'),
|
|
);
|
|
|
|
// Isolated fallback: prior valid web-01 remains in memory; db-01 & edge-01 unaffected
|
|
expect(mgr.getServer('web-01')?.host).toBe('10.0.0.10');
|
|
expect(mgr.getServer('db-01')?.host).toBe('10.0.0.11');
|
|
expect(mgr.getServer('edge-01')?.host).toBe('10.0.0.12');
|
|
} finally {
|
|
await mgr.close();
|
|
db.close();
|
|
}
|
|
});
|
|
|
|
it('validates Full-DAG workflows, rejects cycles, and emits non-blocking cross-ref warnings', async () => {
|
|
const { db, vault, mgr } = makeEnv();
|
|
vault.initializeNewVault();
|
|
await mgr.init({ watch: false });
|
|
|
|
try {
|
|
// Valid parallel diamond DAG
|
|
const validDag = WorkflowConfigSchema.safeParse({
|
|
id: 'deploy-dag',
|
|
name: 'Parallel Deploy DAG',
|
|
trigger: {
|
|
type: 'event',
|
|
event: 'system.unhealthy',
|
|
filter: { systemId: 'docker-compose' },
|
|
},
|
|
steps: [
|
|
{
|
|
id: 'build',
|
|
type: 'exec',
|
|
target: { serverId: 'app-01' },
|
|
dependsOn: [],
|
|
command: 'echo build',
|
|
},
|
|
{
|
|
id: 'backup',
|
|
type: 'exec',
|
|
target: { serverId: 'db-01' },
|
|
dependsOn: ['build'],
|
|
command: 'echo backup',
|
|
},
|
|
{
|
|
id: 'sync',
|
|
type: 'transfer',
|
|
mode: 'hub-relayed',
|
|
dependsOn: [{ stepId: 'build', on: 'success' }],
|
|
source: { serverId: 'app-01', path: '/tmp/a' },
|
|
destination: { serverId: 'edge-01', path: '/tmp/a' },
|
|
},
|
|
{
|
|
id: 'finalize',
|
|
type: 'exec',
|
|
target: { all: true },
|
|
dependsOn: ['backup', 'sync'],
|
|
command: 'echo done',
|
|
},
|
|
],
|
|
});
|
|
expect(validDag.success).toBe(true);
|
|
|
|
// Cyclic DAG must fail Zod validation
|
|
const cyclicDag = WorkflowConfigSchema.safeParse({
|
|
id: 'bad-cycle',
|
|
name: 'Cyclic DAG',
|
|
trigger: { type: 'manual' },
|
|
steps: [
|
|
{ id: 'step-a', type: 'exec', target: { all: true }, dependsOn: ['step-c'], command: 'true' },
|
|
{ id: 'step-b', type: 'exec', target: { all: true }, dependsOn: ['step-a'], command: 'true' },
|
|
{ id: 'step-c', type: 'exec', target: { all: true }, dependsOn: ['step-b'], command: 'true' },
|
|
],
|
|
});
|
|
expect(cyclicDag.success).toBe(false);
|
|
if (!cyclicDag.success) {
|
|
expect(JSON.stringify(cyclicDag.error.issues)).toContain('cycle');
|
|
}
|
|
|
|
// Non-blocking cross-ref warnings (missing credential, jumpHost cycle, missing system)
|
|
mgr.saveConfigText(
|
|
'servers/hop-a.json',
|
|
JSON.stringify({
|
|
id: 'hop-a',
|
|
name: 'Hop A',
|
|
host: '10.0.0.1',
|
|
username: 'ops',
|
|
credentialId: 'missing-cred',
|
|
jumpHostId: 'hop-b',
|
|
systems: [{ systemId: 'nonexistent-system', params: {} }],
|
|
}),
|
|
);
|
|
mgr.saveConfigText(
|
|
'servers/hop-b.json',
|
|
JSON.stringify({
|
|
id: 'hop-b',
|
|
name: 'Hop B',
|
|
host: '10.0.0.2',
|
|
username: 'ops',
|
|
credentialId: 'missing-cred',
|
|
jumpHostId: 'hop-a',
|
|
}),
|
|
);
|
|
|
|
const codes = mgr.getDiagnostics().warnings.map((w) => w.code);
|
|
expect(codes).toContain('MISSING_CREDENTIAL');
|
|
expect(codes).toContain('JUMP_HOST_CYCLE');
|
|
expect(codes).toContain('MISSING_SYSTEM');
|
|
} finally {
|
|
await mgr.close();
|
|
db.close();
|
|
}
|
|
});
|
|
});
|