/home/techb158/cosmic.abdallabala.com/tests
Edit: /home/techb158/cosmic.abdallabala.com/tests/storage-layer.test.js (3272B)
const assert = require("assert");
const fs = require("fs");
const path = require("path");
const os = require("os");
const { JsonDatabase } = require("../src/storage/jsonDatabase.js");
const { DashboardService } = require("../src/services/dashboardService.js");
const { ProjectRepository } = require("../src/repositories/projectRepository.js");
const { RiskRepository } = require("../src/repositories/riskRepository.js");
const source = path.join(__dirname, "..", "data", "database.json");
const temp = path.join(os.tmpdir(), `cosmic-db-${Date.now()}.json`);
fs.copyFileSync(source, temp);
const database = new JsonDatabase(temp);
const projects = new ProjectRepository(database);
const risks = new RiskRepository(database);
const dashboard = new DashboardService(database);
const projectId = "cosmic-ai-risk-001";
const projectList = projects.listProjects();
assert.strictEqual(projectList.length, 1, "Expected one seed project");
const aggregate = projects.getAggregate(projectId);
assert.strictEqual(aggregate.risks.length, 35, "Expected 35 seed risks");
assert.strictEqual(aggregate.lifecycle.length, 6, "Expected 6 lifecycle phases");
assert.strictEqual(aggregate.indicators.length, 6, "Expected 6 indicators");
assert.strictEqual(aggregate.experiments.length, 3, "Expected 3 experiments");
const dashboardPayload = dashboard.getDashboard(projectId);
assert.ok(dashboardPayload.dashboard.summary.overallScore >= 0, "Dashboard score should calculate");
assert.ok(["Ready", "Warning", "Blocked"].includes(dashboardPayload.dashboard.gate.status), "Gate status should calculate");
const created = risks.createRisk(projectId, {
title: "Synthetic test risk for repository layer",
dimension: "Technical",
domain: "Technical risks",
lifecyclePhase: "Testing and performance evaluation",
probability: 3,
impact: 4,
detectability: 3,
owner: "Test Owner",
mitigations: ["Add automated regression test"]
});
assert.ok(created.id, "Created risk should have an id");
assert.strictEqual(risks.listRisks(projectId).length, 36, "Risk count should increase after create");
const updated = risks.updateRisk(created.id, { status: "In mitigation", approvalStatus: "Approved" });
assert.strictEqual(updated.status, "In mitigation", "Risk status should update");
assert.strictEqual(updated.approvalStatus, "Approved", "Approval status should update");
const mitigation = risks.createMitigation(created.id, {
title: "Complete test mitigation",
status: "Done",
progress_percent: 100,
effectiveness_percent: 80
});
assert.ok(mitigation.id, "Mitigation should have an id");
const gate = dashboard.evaluateGate(projectId);
assert.ok(gate.criteria.length >= 8, "Gate evaluation should persist criteria and return them");
const deleted = risks.deleteRisk(created.id);
assert.strictEqual(deleted, true, "Risk should delete");
assert.strictEqual(risks.listRisks(projectId).length, 35, "Risk count should return to seed size");
const dbAfter = database.read();
assert.ok(dbAfter.tables.audit_events.length >= 4, "Audit events should be recorded for create, update, mitigation, delete");
assert.ok(dbAfter.tables.deployment_gates.length >= 1, "Gate evaluation should be stored");
fs.unlinkSync(temp);
console.log("All COSMIC AI-Risk storage layer tests passed.");