You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.4 KiB

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { resetEnvCache } from "@/lib/env";
vi.mock("@/services/cc/settings-config", async () => {
const actual = await vi.importActual<
typeof import("@/services/cc/settings-config")
>("@/services/cc/settings-config");
return {
...actual,
resolveCcRuntime: vi.fn(),
};
});
import { resolveCcRuntime } from "@/services/cc/settings-config";
import {
assertCcLiveReady,
getCcModeStatus,
hasCcCredentials,
} from "@/services/cc/mode";
const mockedResolve = vi.mocked(resolveCcRuntime);
describe("cc mode gate", () => {
beforeEach(() => {
process.env.DATABASE_URL =
process.env.DATABASE_URL ||
"mysql://app:app@localhost:7023/email_forecast";
resetEnvCache();
mockedResolve.mockReset();
});
afterEach(() => {
vi.clearAllMocks();
});
it("mock without credentials → not live_ready", async () => {
mockedResolve.mockResolvedValue({
mock: true,
apiBase: "https://test.example/api",
saasHeader: "TEST",
loginMark: "11111111-2222-3333-4444-555555555555",
username: "",
passwordMd5: "",
source: "env",
hasPassword: false,
});
const s = await getCcModeStatus();
expect(s.mode).toBe("mock");
expect(s.live_ready).toBe(false);
expect(await hasCcCredentials()).toBe(false);
});
it("live without credentials → credentials_ready false", async () => {
mockedResolve.mockResolvedValue({
mock: false,
apiBase: "https://test.example/api",
saasHeader: "TEST",
loginMark: "11111111-2222-3333-4444-555555555555",
username: "",
passwordMd5: "",
source: "env",
hasPassword: false,
});
const s = await getCcModeStatus();
expect(s.mode).toBe("live");
expect(s.live_ready).toBe(false);
await expect(assertCcLiveReady()).rejects.toThrow(/CC_CREDENTIALS_MISSING/);
});
it("live with credentials → live_ready", async () => {
mockedResolve.mockResolvedValue({
mock: false,
apiBase: "https://test.example/api",
saasHeader: "TEST",
loginMark: "11111111-2222-3333-4444-555555555555",
username: "test-user",
passwordMd5: "abc",
source: "db",
hasPassword: true,
});
const s = await getCcModeStatus();
expect(s.live_ready).toBe(true);
await expect(assertCcLiveReady()).resolves.toBeUndefined();
});
});