import { describe, it, expect, beforeEach } from "vitest";
import { OpenAPIStore } from "../src/openapi-store.js";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const PETSTORE_YAML = join(__dirname, "fixtures", "petstore.yaml");
describe("OpenAPIStore", () => {
let store: OpenAPIStore;
beforeEach(() => {
store = new OpenAPIStore();
});
describe("load", () => {
it("should load spec from local YAML file", async () => {
const result = await store.load("petstore", PETSTORE_YAML);
expect(result).not.toHaveProperty("error");
expect(result).toMatchObject({
success: true,
alias: "petstore",
title: "Swagger Petstore - OpenAPI 3.0",
version: "1.0.27",
});
expect((result as any).endpointCount).toBeGreaterThan(0);
expect((result as any).tags).toContain("pet");
expect((result as any).tags).toContain("store");
expect((result as any).tags).toContain("user");
});
it("should load spec from local JSON file", async () => {
// Create a simple JSON spec inline for testing
const jsonSpecPath = join(__dirname, "fixtures", "simple.json");
const fs = await import("fs/promises");
const simpleSpec = {
openapi: "3.0.0",
info: { title: "Simple API", version: "1.0.0" },
paths: {
"/test": {
get: {
summary: "Test endpoint",
responses: { "200": { description: "OK" } },
},
},
},
};
await fs.writeFile(jsonSpecPath, JSON.stringify(simpleSpec));
const result = await store.load("simple", jsonSpecPath);
expect(result).not.toHaveProperty("error");
expect(result).toMatchObject({
success: true,
alias: "simple",
title: "Simple API",
version: "1.0.0",
endpointCount: 1,
});
// Cleanup
await fs.unlink(jsonSpecPath);
});
it("should replace existing spec with same alias", async () => {
await store.load("petstore", PETSTORE_YAML);
// Load again with same alias
const result = await store.load("petstore", PETSTORE_YAML);
expect(result).not.toHaveProperty("error");
expect(result).toMatchObject({
success: true,
alias: "petstore",
});
// Should only have one spec
expect(store.list()).toEqual(["petstore"]);
});
it("should return error for non-existent file", async () => {
const result = await store.load("missing", "/non/existent/file.yaml");
expect(result).toHaveProperty("error", true);
expect(result).toHaveProperty("code", "FETCH_ERROR");
});
it("should return error for invalid spec content", async () => {
const invalidPath = join(__dirname, "fixtures", "invalid.yaml");
const fs = await import("fs/promises");
await fs.writeFile(invalidPath, "not: valid: yaml: content:");
const result = await store.load("invalid", invalidPath);
expect(result).toHaveProperty("error", true);
expect(result).toHaveProperty("code", "PARSE_ERROR");
// Cleanup
await fs.unlink(invalidPath);
});
});
describe("get", () => {
it("should return loaded spec", async () => {
await store.load("petstore", PETSTORE_YAML);
const spec = store.get("petstore");
expect(spec).toBeDefined();
expect(spec?.info.title).toBe("Swagger Petstore - OpenAPI 3.0");
});
it("should return undefined for non-existent alias", () => {
const spec = store.get("nonexistent");
expect(spec).toBeUndefined();
});
});
describe("has", () => {
it("should return true for loaded spec", async () => {
await store.load("petstore", PETSTORE_YAML);
expect(store.has("petstore")).toBe(true);
});
it("should return false for non-existent alias", () => {
expect(store.has("nonexistent")).toBe(false);
});
});
describe("list", () => {
it("should return all loaded aliases", async () => {
await store.load("petstore", PETSTORE_YAML);
await store.load("petstore2", PETSTORE_YAML);
const aliases = store.list();
expect(aliases).toContain("petstore");
expect(aliases).toContain("petstore2");
expect(aliases).toHaveLength(2);
});
it("should return empty array when no specs loaded", () => {
expect(store.list()).toEqual([]);
});
});
describe("remove", () => {
it("should remove loaded spec", async () => {
await store.load("petstore", PETSTORE_YAML);
const removed = store.remove("petstore");
expect(removed).toBe(true);
expect(store.has("petstore")).toBe(false);
});
it("should return false for non-existent alias", () => {
const removed = store.remove("nonexistent");
expect(removed).toBe(false);
});
});
describe("clear", () => {
it("should remove all specs", async () => {
await store.load("petstore", PETSTORE_YAML);
await store.load("petstore2", PETSTORE_YAML);
store.clear();
expect(store.list()).toEqual([]);
});
});
});