102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { CorpConfig } from "./types.js";
|
|
import { CorpContext, resolveCorpConfig } from "./manager.js";
|
|
|
|
function corp(id: string, name = id): CorpConfig {
|
|
return {
|
|
id,
|
|
name,
|
|
environment: "production",
|
|
seal: {
|
|
endpoints: {
|
|
approvalStylePreferences: "api/v1/agent/ai-approval/config"
|
|
}
|
|
},
|
|
source: {
|
|
type: "direct",
|
|
token: `${id}-token`,
|
|
sealUrl: `https://${id}.seal.test`,
|
|
expiresIn: 3600,
|
|
staffId: "direct"
|
|
},
|
|
auth: {
|
|
refreshTtl: 300
|
|
}
|
|
};
|
|
}
|
|
|
|
describe("CorpContext", () => {
|
|
test("refresh adds newly loaded corps", () => {
|
|
const context = new CorpContext([corp("corp-a")], { stateful: true });
|
|
|
|
context.refresh([corp("corp-a"), corp("corp-b")]);
|
|
|
|
expect(context.listCorps().map((item) => item.id)).toEqual(["corp-a", "corp-b"]);
|
|
});
|
|
|
|
test("refresh keeps the selected corp when it still exists", () => {
|
|
const context = new CorpContext([corp("corp-a"), corp("corp-b")], { stateful: true });
|
|
expect(context.switchCorp("corp-b")).toBe(true);
|
|
|
|
context.refresh([corp("corp-a"), corp("corp-b", "Corp B updated")]);
|
|
|
|
expect(context.getCurrent()?.id).toBe("corp-b");
|
|
expect(context.getCurrent()?.name).toBe("Corp B updated");
|
|
});
|
|
|
|
test("clears the default when the selected corp disappears", () => {
|
|
const context = new CorpContext([corp("corp-a"), corp("corp-b")], { stateful: true });
|
|
expect(context.switchCorp("corp-b")).toBe(true);
|
|
|
|
context.refresh([corp("corp-c")]);
|
|
|
|
expect(context.getCurrent()).toBeNull();
|
|
});
|
|
|
|
test("does not keep a current corp in stateless mode", () => {
|
|
const context = new CorpContext([corp("corp-a"), corp("corp-b")]);
|
|
|
|
expect(context.getCurrent()).toBeNull();
|
|
expect(context.switchCorp("corp-b")).toBe(false);
|
|
expect(context.resolve("corp-b")?.id).toBe("corp-b");
|
|
});
|
|
|
|
test("uses the configured default corp in stateful mode", () => {
|
|
const context = new CorpContext(
|
|
[corp("corp-a"), corp("corp-b")],
|
|
{ stateful: true, defaultCorpId: "corp-b" }
|
|
);
|
|
|
|
expect(context.getCurrent()?.id).toBe("corp-b");
|
|
});
|
|
});
|
|
|
|
describe("resolveCorpConfig", () => {
|
|
test("requires an explicit corp in stateless mode", () => {
|
|
expect(() => resolveCorpConfig([corp("corp-a")], { stateful: false })).toThrow(
|
|
"Every enterprise command requires --corp <corpId>"
|
|
);
|
|
});
|
|
|
|
test("resolves an explicit corp in stateless mode", () => {
|
|
expect(resolveCorpConfig(
|
|
[corp("corp-a"), corp("corp-b")],
|
|
{ stateful: false, corpId: "corp-b" }
|
|
).id).toBe("corp-b");
|
|
});
|
|
|
|
test("resolves an explicit corp case-insensitively and by enterprise name", () => {
|
|
const corpA = { ...corp("corp-a"), name: "Alpha Corp" };
|
|
const corpB = { ...corp("corp-b"), name: "Beta Corp" };
|
|
expect(resolveCorpConfig([corpA, corpB], { stateful: false, corpId: "CORP-B" }).id).toBe("corp-b");
|
|
expect(resolveCorpConfig([corpA, corpB], { stateful: false, corpId: "alpha corp" }).id).toBe("corp-a");
|
|
});
|
|
|
|
test("resolves the configured default corp in stateful mode", () => {
|
|
expect(resolveCorpConfig(
|
|
[corp("corp-a"), corp("corp-b")],
|
|
{ stateful: true, defaultCorpId: "corp-b" }
|
|
).id).toBe("corp-b");
|
|
});
|
|
});
|