first commit

This commit is contained in:
khuongnguyen committed 2026-11-28 20:37:00 +07:00
commit 41d9e9e8eb
21 files changed
+3538

No files matched your search

+197
View File
@@ -0,0 +1,197 @@
import { describe, it, expect, beforeEach } from "vitest";
import path from "node:path";
import { 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", () => {
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);
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);
});
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");
});
});
describe("listAll", () => {
it("should return all three 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", () => {
// 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);
expect(byIp).toHaveLength(1);
expect(byIp[0].name).toBe("test-vps");
// When IP search is not permitted (e.g. unauthenticated)
const noIp = testService.search("192.0.2", 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 seed 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,
});
});
});
});