test: align profile env bootstrap with lazy dotenv

This commit is contained in:
Peter Steinberger 2026-03-27 22:48:09 +00:00
parent 8908f6c23b
commit a74c50c861
1 changed files with 21 additions and 0 deletions

View File

@ -1,5 +1,9 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const fileState = vi.hoisted(() => ({
hasCliDotEnv: false,
}));
const dotenvState = vi.hoisted(() => {
const state = {
profileAtDotenvLoad: undefined as string | undefined,
@ -18,6 +22,20 @@ const maybeRunCliInContainerMock = vi.hoisted(() =>
vi.fn((argv: string[]) => ({ handled: false, argv })),
);
vi.mock("node:fs", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:fs")>();
type ExistsSyncPath = Parameters<typeof actual.existsSync>[0];
return {
...actual,
existsSync: vi.fn((target: ExistsSyncPath) => {
if (typeof target === "string" && target.endsWith(".env")) {
return fileState.hasCliDotEnv;
}
return actual.existsSync(target);
}),
};
});
vi.mock("./dotenv.js", () => ({
loadCliDotEnv: dotenvState.loadDotEnv,
}));
@ -75,6 +93,7 @@ describe("runCli profile env bootstrap", () => {
dotenvState.state.containerAtDotenvLoad = undefined;
dotenvState.loadDotEnv.mockClear();
maybeRunCliInContainerMock.mockClear();
fileState.hasCliDotEnv = false;
});
afterEach(() => {
@ -121,6 +140,7 @@ describe("runCli profile env bootstrap", () => {
});
it("applies --profile before dotenv loading", async () => {
fileState.hasCliDotEnv = true;
await runCli(["node", "openclaw", "--profile", "rawdog", "status"]);
expect(dotenvState.loadDotEnv).toHaveBeenCalledOnce();
@ -150,6 +170,7 @@ describe("runCli profile env bootstrap", () => {
});
it("does not let dotenv change container target resolution", async () => {
fileState.hasCliDotEnv = true;
dotenvState.loadDotEnv.mockImplementationOnce(() => {
process.env.OPENCLAW_CONTAINER = "demo";
dotenvState.state.profileAtDotenvLoad = process.env.OPENCLAW_PROFILE;