test(gateway): avoid startup auth module reset churn

This commit is contained in:
Vincent Koc
2026-04-14 23:59:13 +01:00
parent ed1dfe23d4
commit 06715db218

View File

@@ -1,6 +1,11 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { expectGeneratedTokenPersistedToGatewayAuth } from "../test-utils/auth-token-assertions.js";
import {
assertGatewayAuthNotKnownWeak,
assertHooksTokenSeparateFromGatewayAuth,
ensureGatewayStartupAuth,
} from "./startup-auth.js";
const mocks = vi.hoisted(() => ({
replaceConfigFile: vi.fn(async (_params: { nextConfig: OpenClawConfig }) => {}),
@@ -14,19 +19,6 @@ vi.mock("../config/config.js", async () => {
};
});
let assertGatewayAuthNotKnownWeak: typeof import("./startup-auth.js").assertGatewayAuthNotKnownWeak;
let assertHooksTokenSeparateFromGatewayAuth: typeof import("./startup-auth.js").assertHooksTokenSeparateFromGatewayAuth;
let ensureGatewayStartupAuth: typeof import("./startup-auth.js").ensureGatewayStartupAuth;
async function loadFreshStartupAuthModuleForTest() {
vi.resetModules();
({
assertGatewayAuthNotKnownWeak,
assertHooksTokenSeparateFromGatewayAuth,
ensureGatewayStartupAuth,
} = await import("./startup-auth.js"));
}
describe("ensureGatewayStartupAuth", () => {
async function expectEphemeralGeneratedTokenWhenOverridden(cfg: OpenClawConfig) {
const result = await ensureGatewayStartupAuth({
@@ -43,10 +35,9 @@ describe("ensureGatewayStartupAuth", () => {
expect(mocks.replaceConfigFile).not.toHaveBeenCalled();
}
beforeEach(async () => {
beforeEach(() => {
vi.restoreAllMocks();
mocks.replaceConfigFile.mockClear();
await loadFreshStartupAuthModuleForTest();
});
async function expectNoTokenGeneration(cfg: OpenClawConfig, mode: string) {
@@ -476,8 +467,9 @@ describe("ensureGatewayStartupAuth", () => {
});
describe("assertGatewayAuthNotKnownWeak", () => {
beforeEach(async () => {
await loadFreshStartupAuthModuleForTest();
beforeEach(() => {
vi.restoreAllMocks();
mocks.replaceConfigFile.mockClear();
});
it("throws on the known-weak token sentinel", () => {