import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { writeFileSync, unlinkSync, mkdirSync, rmSync } from "fs";
import { join } from "path";
import { parseIonApiFile } from "../../src/config/ionapi.js";
describe("ionapi", () => {
const testDir = join(process.cwd(), "test-fixtures");
const testFilePath = join(testDir, "test.ionapi");
const validConfig = {
ti: "TEST_TENANT",
cn: "TestConnection",
dt: "Browser",
ci: "test-client-id",
cs: "test-client-secret",
iu: "https://api.example.com",
pu: "https://auth.example.com",
oa: "/oauth/authorize",
ot: "/oauth/token",
or: "/oauth/revoke",
ev: "1.0",
v: "1.0",
saak: "test-access-key",
sask: "test-secret-key",
};
beforeEach(() => {
mkdirSync(testDir, { recursive: true });
});
afterEach(() => {
rmSync(testDir, { recursive: true, force: true });
});
describe("parseIonApiFile", () => {
it("should parse a valid .ionapi file", () => {
writeFileSync(testFilePath, JSON.stringify(validConfig));
const result = parseIonApiFile(testFilePath);
expect(result.ti).toBe("TEST_TENANT");
expect(result.ci).toBe("test-client-id");
expect(result.cs).toBe("test-client-secret");
expect(result.iu).toBe("https://api.example.com");
expect(result.saak).toBe("test-access-key");
expect(result.sask).toBe("test-secret-key");
});
it("should throw an error if file does not exist", () => {
expect(() => parseIonApiFile("/nonexistent/path.ionapi")).toThrow(
"ION API file not found"
);
});
it("should throw an error if JSON is invalid", () => {
writeFileSync(testFilePath, "not valid json");
expect(() => parseIonApiFile(testFilePath)).toThrow(
"Invalid JSON in ION API file"
);
});
it("should throw an error if required fields are missing", () => {
const invalidConfig = { ...validConfig };
delete (invalidConfig as Record<string, unknown>).ti;
writeFileSync(testFilePath, JSON.stringify(invalidConfig));
expect(() => parseIonApiFile(testFilePath)).toThrow(
"Missing required field: ti"
);
});
it("should throw an error if saak is missing", () => {
const invalidConfig = { ...validConfig };
delete (invalidConfig as Record<string, unknown>).saak;
writeFileSync(testFilePath, JSON.stringify(invalidConfig));
expect(() => parseIonApiFile(testFilePath)).toThrow(
"Missing required field: saak"
);
});
it("should throw an error if sask is missing", () => {
const invalidConfig = { ...validConfig };
delete (invalidConfig as Record<string, unknown>).sask;
writeFileSync(testFilePath, JSON.stringify(invalidConfig));
expect(() => parseIonApiFile(testFilePath)).toThrow(
"Missing required field: sask"
);
});
});
});