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"; 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(() => { repository = new VPSRepository(":memory:"); repository.init(); repository.seedIfEmpty(CORE_FIXTURE); service = new VPSService(repository); }); 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 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 fixture VPS with correct properties", () => { const list = service.listAll(); expect(list).toHaveLength(3); const names = list.map((v) => v.name); expect(names).toEqual(["shop-prod-01", "staging-01", "db-prod-01"]); }); }); describe("getByName", () => { it("should return the exact VPS when name matches", () => { const vps = service.getByName("shop-prod-01"); expect(vps).toBeDefined(); expect(vps?.name).toBe("shop-prod-01"); expect(vps?.provider).toBe("Vultr"); expect(vps?.region).toBe("Singapore"); expect(vps?.environment).toBe("Production"); expect(vps?.cpu).toBe(4); expect(vps?.ramGB).toBe(8); expect(vps?.diskGB).toBe(160); }); it("should support case-insensitive name lookup", () => { const vps1 = service.getByName("SHOP-PROD-01"); const vps2 = service.getByName("sHoP-pRoD-01"); expect(vps1).toBeDefined(); expect(vps1?.id).toBe("vps-001"); expect(vps2?.id).toBe("vps-001"); }); it("should trim surrounding whitespace in name lookup", () => { const vps = service.getByName(" staging-01 "); expect(vps).toBeDefined(); expect(vps?.name).toBe("staging-01"); }); it("should return undefined for non-existent VPS", () => { const vps = service.getByName("non-existent-vps"); expect(vps).toBeUndefined(); }); it("should return undefined for empty string or whitespace-only name", () => { expect(service.getByName("")).toBeUndefined(); expect(service.getByName(" ")).toBeUndefined(); }); }); describe("search", () => { it("should search by partial name case-insensitively", () => { const results = service.search("prod"); expect(results).toHaveLength(2); expect(results.map((v) => v.name)).toEqual(["shop-prod-01", "db-prod-01"]); }); it("should return staging-01 when searching 'stag'", () => { const results = service.search("STAG"); expect(results).toHaveLength(1); expect(results[0].name).toBe("staging-01"); }); 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("shop-prod-01"); const noIp = service.search("203.0.113", false); expect(noIp).toHaveLength(0); }); it("should return empty array for empty search query", () => { const results = service.search(" "); expect(results).toEqual([]); }); it("should return empty array when no VPS matches", () => { const results = service.search("unknown-query-xyz"); expect(results).toEqual([]); }); }); describe("filter", () => { it("should filter by Provider case-insensitively", () => { const vultr = service.filter({ provider: "vultr" }); expect(vultr).toHaveLength(2); expect(vultr.map((v) => v.name)).toEqual(["shop-prod-01", "db-prod-01"]); const doServers = service.filter({ provider: "DigitalOcean" }); expect(doServers).toHaveLength(1); expect(doServers[0].name).toBe("staging-01"); }); it("should filter by Environment", () => { const prod = service.filter({ environment: "Production" }); expect(prod).toHaveLength(2); const staging = service.filter({ environment: "Staging" }); expect(staging).toHaveLength(1); expect(staging[0].name).toBe("staging-01"); const dev = service.filter({ environment: "Development" }); expect(dev).toHaveLength(0); }); it("should support combining both filters using AND logic", () => { const result1 = service.filter({ provider: "Vultr", environment: "Production" }); expect(result1).toHaveLength(2); const result2 = service.filter({ provider: "DigitalOcean", environment: "Production" }); expect(result2).toHaveLength(0); const result3 = service.filter({ provider: "DigitalOcean", environment: "Staging" }); expect(result3).toHaveLength(1); expect(result3[0].name).toBe("staging-01"); }); it("should return empty array when no records match filter criteria", () => { const none = service.filter({ provider: "NonExistentProvider" }); expect(none).toEqual([]); }); }); describe("getStats", () => { it("should accurately compute all aggregate statistics for fixture data", () => { const stats = service.getStats(); expect(stats.totalVPS).toBe(3); expect(stats.totalVCPU).toBe(10); expect(stats.totalRamGB).toBe(28); expect(stats.totalDiskGB).toBe(560); expect(stats.byEnvironment).toEqual({ Production: 2, Staging: 1, }); expect(stats.byProvider).toEqual({ Vultr: 2, DigitalOcean: 1, }); }); }); });