feat: SQLite RBAC, granular permissions, 100-VPS seeder, DM pagination
This commit is contained in:
1 parent
41d9e9e8eb
commit
4ccb35a44c
16 files changed
+1690
-327
No files matched your search
+79
-45
@@ -1,34 +1,93 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import path from "node:path";
|
||||
import { VPSRepository } from "../src/repositories/vps.repository.js";
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { SEED_VPS_RECORDS, VPSRepository } from "../src/repositories/vps.repository.js";
|
||||
import { VPSService } from "../src/services/vps.service.js";
|
||||
import { VPS } from "../src/types/vps.js";
|
||||
|
||||
describe("VPS Repository and Service", () => {
|
||||
const CORE_FIXTURE: VPS[] = [
|
||||
{
|
||||
id: "vps-001",
|
||||
name: "shop-prod-01",
|
||||
provider: "Vultr",
|
||||
region: "Singapore",
|
||||
environment: "Production",
|
||||
cpu: 4,
|
||||
ramGB: 8,
|
||||
diskGB: 160,
|
||||
ip: "203.0.113.10",
|
||||
sshUser: "deploy",
|
||||
sshPort: 22,
|
||||
},
|
||||
{
|
||||
id: "vps-002",
|
||||
name: "staging-01",
|
||||
provider: "DigitalOcean",
|
||||
region: "Singapore",
|
||||
environment: "Staging",
|
||||
cpu: 2,
|
||||
ramGB: 4,
|
||||
diskGB: 80,
|
||||
ip: "198.51.100.20",
|
||||
sshUser: "ubuntu",
|
||||
sshPort: 2222,
|
||||
},
|
||||
{
|
||||
id: "vps-003",
|
||||
name: "db-prod-01",
|
||||
provider: "Vultr",
|
||||
region: "Singapore",
|
||||
environment: "Production",
|
||||
cpu: 4,
|
||||
ramGB: 16,
|
||||
diskGB: 320,
|
||||
ip: "192.0.2.30",
|
||||
},
|
||||
];
|
||||
|
||||
describe("SQLite VPS Repository and Service", () => {
|
||||
let repository: VPSRepository;
|
||||
let service: VPSService;
|
||||
|
||||
beforeEach(() => {
|
||||
// Point to data/vps.json
|
||||
const seedPath = path.resolve(process.cwd(), "data/vps.json");
|
||||
repository = new VPSRepository(seedPath);
|
||||
repository = new VPSRepository(":memory:");
|
||||
repository.init();
|
||||
repository.seedIfEmpty(CORE_FIXTURE);
|
||||
service = new VPSService(repository);
|
||||
});
|
||||
|
||||
describe("Repository loading and validation", () => {
|
||||
it("should load exactly the 3 seed VPS records", () => {
|
||||
const all = repository.getAll();
|
||||
expect(all).toHaveLength(3);
|
||||
afterEach(() => {
|
||||
repository.close();
|
||||
});
|
||||
|
||||
describe("SQLite initialization and 100-VPS seeding", () => {
|
||||
it("should seed 100 sample VPS records when default SEED_VPS_RECORDS is used", () => {
|
||||
const fullRepo = new VPSRepository(":memory:");
|
||||
fullRepo.init();
|
||||
const inserted = fullRepo.seedIfEmpty();
|
||||
|
||||
expect(SEED_VPS_RECORDS).toHaveLength(100);
|
||||
expect(inserted).toBe(100);
|
||||
expect(fullRepo.getAll()).toHaveLength(100);
|
||||
expect(fullRepo.getByName("shop-prod-01")?.id).toBe("vps-001");
|
||||
expect(fullRepo.getById("vps-100")).toBeDefined();
|
||||
|
||||
fullRepo.close();
|
||||
});
|
||||
|
||||
it("should throw a descriptive error when file is missing", () => {
|
||||
const invalidRepo = new VPSRepository("non-existent-path.json");
|
||||
expect(() => invalidRepo.getAll()).toThrow("Inventory data file not found");
|
||||
it("should not duplicate records if seedIfEmpty is called multiple times", () => {
|
||||
const inserted = repository.seedIfEmpty(CORE_FIXTURE);
|
||||
expect(inserted).toBe(0);
|
||||
expect(repository.getAll()).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("should find a VPS by exact ID", () => {
|
||||
const vps = repository.getById("vps-002");
|
||||
expect(vps).toBeDefined();
|
||||
expect(vps?.name).toBe("staging-01");
|
||||
});
|
||||
});
|
||||
|
||||
describe("listAll", () => {
|
||||
it("should return all three VPS with correct properties", () => {
|
||||
it("should return all fixture VPS with correct properties", () => {
|
||||
const list = service.listAll();
|
||||
expect(list).toHaveLength(3);
|
||||
|
||||
@@ -88,31 +147,12 @@ describe("VPS Repository and Service", () => {
|
||||
expect(results[0].name).toBe("staging-01");
|
||||
});
|
||||
|
||||
it("should search by IP address when IP is configured", () => {
|
||||
// Mock repository with an IP configured
|
||||
const mockRepo = new VPSRepository();
|
||||
const mockVPSList: VPS[] = [
|
||||
{
|
||||
id: "vps-test",
|
||||
name: "test-vps",
|
||||
provider: "AWS",
|
||||
region: "Tokyo",
|
||||
environment: "Development",
|
||||
cpu: 2,
|
||||
ramGB: 4,
|
||||
diskGB: 40,
|
||||
ip: "192.0.2.15",
|
||||
},
|
||||
];
|
||||
mockRepo.getAll = () => mockVPSList;
|
||||
const testService = new VPSService(mockRepo);
|
||||
|
||||
const byIp = testService.search("192.0.2", true);
|
||||
it("should search by IP address when IP is configured and allowed", () => {
|
||||
const byIp = service.search("203.0.113", true);
|
||||
expect(byIp).toHaveLength(1);
|
||||
expect(byIp[0].name).toBe("test-vps");
|
||||
expect(byIp[0].name).toBe("shop-prod-01");
|
||||
|
||||
// When IP search is not permitted (e.g. unauthenticated)
|
||||
const noIp = testService.search("192.0.2", false);
|
||||
const noIp = service.search("203.0.113", false);
|
||||
expect(noIp).toHaveLength(0);
|
||||
});
|
||||
|
||||
@@ -169,25 +209,19 @@ describe("VPS Repository and Service", () => {
|
||||
});
|
||||
|
||||
describe("getStats", () => {
|
||||
it("should accurately compute all aggregate statistics for seed data", () => {
|
||||
it("should accurately compute all aggregate statistics for fixture data", () => {
|
||||
const stats = service.getStats();
|
||||
|
||||
// Verified exact calculations:
|
||||
// shop-prod-01: 4 CPU, 8 RAM, 160 Disk
|
||||
// staging-01: 2 CPU, 4 RAM, 80 Disk
|
||||
// db-prod-01: 4 CPU, 16 RAM, 320 Disk
|
||||
expect(stats.totalVPS).toBe(3);
|
||||
expect(stats.totalVCPU).toBe(10);
|
||||
expect(stats.totalRamGB).toBe(28);
|
||||
expect(stats.totalDiskGB).toBe(560);
|
||||
|
||||
// Environment breakdown
|
||||
expect(stats.byEnvironment).toEqual({
|
||||
Production: 2,
|
||||
Staging: 1,
|
||||
});
|
||||
|
||||
// Provider breakdown
|
||||
expect(stats.byProvider).toEqual({
|
||||
Vultr: 2,
|
||||
DigitalOcean: 1,
|
||||
|
||||
Reference in new issue
Block a user