feat: everything
This commit is contained in:
117
tests/config.test.ts
Normal file
117
tests/config.test.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { describe, it, expect, afterEach } from "bun:test";
|
||||
import { getConfig } from "../src/config.js";
|
||||
|
||||
const ENV_KEYS = [
|
||||
"SAGE_X3_URL",
|
||||
"SAGE_X3_ENDPOINT",
|
||||
"SAGE_X3_CLIENT_ID",
|
||||
"SAGE_X3_SECRET",
|
||||
"SAGE_X3_USER",
|
||||
"SAGE_X3_TOKEN_LIFETIME",
|
||||
"SAGE_X3_TLS_REJECT_UNAUTHORIZED",
|
||||
"SAGE_X3_MODE",
|
||||
] as const;
|
||||
|
||||
function clearSageEnv() {
|
||||
for (const key of ENV_KEYS) {
|
||||
delete process.env[key];
|
||||
}
|
||||
}
|
||||
|
||||
describe("getConfig", () => {
|
||||
const savedEnv: Record<string, string | undefined> = {};
|
||||
|
||||
for (const key of ENV_KEYS) {
|
||||
savedEnv[key] = process.env[key];
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const key of ENV_KEYS) {
|
||||
if (savedEnv[key] === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = savedEnv[key];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("returns sandbox defaults when no env vars are set", () => {
|
||||
clearSageEnv();
|
||||
const config = getConfig();
|
||||
|
||||
expect(config.mode).toBe("sandbox");
|
||||
expect(config.url).toBe(
|
||||
"https://api-devna.dev-sagex3.com/demo/service/X3CLOUDV2_SEED/api",
|
||||
);
|
||||
expect(config.tokenLifetime).toBe(600);
|
||||
expect(config.clientId).toBeUndefined();
|
||||
expect(config.secret).toBeUndefined();
|
||||
expect(config.user).toBeUndefined();
|
||||
expect(config.endpoint).toBeUndefined();
|
||||
expect(config.tlsRejectUnauthorized).toBe(true);
|
||||
});
|
||||
|
||||
it("returns authenticated mode when all credentials are set", () => {
|
||||
clearSageEnv();
|
||||
process.env.SAGE_X3_URL = "https://my-x3.example.com/api";
|
||||
process.env.SAGE_X3_ENDPOINT = "SEED";
|
||||
process.env.SAGE_X3_CLIENT_ID = "test-client";
|
||||
process.env.SAGE_X3_SECRET = "test-secret";
|
||||
process.env.SAGE_X3_USER = "admin";
|
||||
process.env.SAGE_X3_TOKEN_LIFETIME = "300";
|
||||
process.env.SAGE_X3_TLS_REJECT_UNAUTHORIZED = "false";
|
||||
|
||||
const config = getConfig();
|
||||
|
||||
expect(config.mode).toBe("authenticated");
|
||||
expect(config.url).toBe("https://my-x3.example.com/api");
|
||||
expect(config.endpoint).toBe("SEED");
|
||||
expect(config.clientId).toBe("test-client");
|
||||
expect(config.secret).toBe("test-secret");
|
||||
expect(config.user).toBe("admin");
|
||||
expect(config.tokenLifetime).toBe(300);
|
||||
expect(config.tlsRejectUnauthorized).toBe(false);
|
||||
});
|
||||
|
||||
it("auto-detects authenticated mode when clientId, secret, and user are present", () => {
|
||||
clearSageEnv();
|
||||
process.env.SAGE_X3_CLIENT_ID = "cid";
|
||||
process.env.SAGE_X3_SECRET = "sec";
|
||||
process.env.SAGE_X3_USER = "usr";
|
||||
|
||||
const config = getConfig();
|
||||
expect(config.mode).toBe("authenticated");
|
||||
});
|
||||
|
||||
it("stays sandbox when only some credentials are set", () => {
|
||||
clearSageEnv();
|
||||
process.env.SAGE_X3_CLIENT_ID = "cid";
|
||||
|
||||
const config = getConfig();
|
||||
expect(config.mode).toBe("sandbox");
|
||||
});
|
||||
|
||||
it("respects SAGE_X3_MODE override even with full credentials", () => {
|
||||
clearSageEnv();
|
||||
process.env.SAGE_X3_CLIENT_ID = "cid";
|
||||
process.env.SAGE_X3_SECRET = "sec";
|
||||
process.env.SAGE_X3_USER = "usr";
|
||||
process.env.SAGE_X3_MODE = "sandbox";
|
||||
|
||||
const config = getConfig();
|
||||
expect(config.mode).toBe("sandbox");
|
||||
});
|
||||
|
||||
it("defaults TLS reject to true when env var is absent", () => {
|
||||
clearSageEnv();
|
||||
const config = getConfig();
|
||||
expect(config.tlsRejectUnauthorized).toBe(true);
|
||||
});
|
||||
|
||||
it("sets TLS reject to false when env var is 'false'", () => {
|
||||
clearSageEnv();
|
||||
process.env.SAGE_X3_TLS_REJECT_UNAUTHORIZED = "false";
|
||||
const config = getConfig();
|
||||
expect(config.tlsRejectUnauthorized).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user