mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-24 23:21:30 +00:00
test(infra): dedupe gateway-lock setup and cover guard paths
This commit is contained in:
@@ -5,7 +5,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { resolveConfigPath, resolveGatewayLockDir, resolveStateDir } from "../config/paths.js";
|
||||
import { acquireGatewayLock, GatewayLockError } from "./gateway-lock.js";
|
||||
import { acquireGatewayLock, GatewayLockError, type GatewayLockOptions } from "./gateway-lock.js";
|
||||
|
||||
let fixtureRoot = "";
|
||||
let fixtureCount = 0;
|
||||
@@ -17,15 +17,25 @@ async function makeEnv() {
|
||||
await fs.writeFile(configPath, "{}", "utf8");
|
||||
await fs.mkdir(resolveGatewayLockDir(), { recursive: true });
|
||||
return {
|
||||
env: {
|
||||
...process.env,
|
||||
OPENCLAW_STATE_DIR: dir,
|
||||
OPENCLAW_CONFIG_PATH: configPath,
|
||||
},
|
||||
cleanup: async () => {},
|
||||
...process.env,
|
||||
OPENCLAW_STATE_DIR: dir,
|
||||
OPENCLAW_CONFIG_PATH: configPath,
|
||||
};
|
||||
}
|
||||
|
||||
async function acquireForTest(
|
||||
env: NodeJS.ProcessEnv,
|
||||
opts: Omit<GatewayLockOptions, "env" | "allowInTests"> = {},
|
||||
) {
|
||||
return await acquireGatewayLock({
|
||||
env,
|
||||
allowInTests: true,
|
||||
timeoutMs: 30,
|
||||
pollIntervalMs: 2,
|
||||
...opts,
|
||||
});
|
||||
}
|
||||
|
||||
function resolveLockPath(env: NodeJS.ProcessEnv) {
|
||||
const stateDir = resolveStateDir(env);
|
||||
const configPath = resolveConfigPath(env, stateDir);
|
||||
@@ -105,38 +115,22 @@ describe("gateway lock", () => {
|
||||
// Fake timers can hang on Windows CI when combined with fs open loops.
|
||||
// Keep this test on real timers and use small timeouts.
|
||||
vi.useRealTimers();
|
||||
const { env, cleanup } = await makeEnv();
|
||||
const lock = await acquireGatewayLock({
|
||||
env,
|
||||
allowInTests: true,
|
||||
timeoutMs: 50,
|
||||
pollIntervalMs: 2,
|
||||
});
|
||||
const env = await makeEnv();
|
||||
const lock = await acquireForTest(env, { timeoutMs: 50 });
|
||||
expect(lock).not.toBeNull();
|
||||
|
||||
const pending = acquireGatewayLock({
|
||||
env,
|
||||
allowInTests: true,
|
||||
timeoutMs: 15,
|
||||
pollIntervalMs: 2,
|
||||
});
|
||||
const pending = acquireForTest(env, { timeoutMs: 15 });
|
||||
await expect(pending).rejects.toBeInstanceOf(GatewayLockError);
|
||||
|
||||
await lock?.release();
|
||||
const lock2 = await acquireGatewayLock({
|
||||
env,
|
||||
allowInTests: true,
|
||||
timeoutMs: 30,
|
||||
pollIntervalMs: 2,
|
||||
});
|
||||
const lock2 = await acquireForTest(env);
|
||||
await lock2?.release();
|
||||
await cleanup();
|
||||
});
|
||||
|
||||
it("treats recycled linux pid as stale when start time mismatches", async () => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-02-06T10:05:00.000Z"));
|
||||
const { env, cleanup } = await makeEnv();
|
||||
const env = await makeEnv();
|
||||
const { lockPath, configPath } = resolveLockPath(env);
|
||||
const payload = createLockPayload({ configPath, startTime: 111 });
|
||||
await fs.writeFile(lockPath, JSON.stringify(payload), "utf8");
|
||||
@@ -146,9 +140,7 @@ describe("gateway lock", () => {
|
||||
onProcRead: () => statValue,
|
||||
});
|
||||
|
||||
const lock = await acquireGatewayLock({
|
||||
env,
|
||||
allowInTests: true,
|
||||
const lock = await acquireForTest(env, {
|
||||
timeoutMs: 80,
|
||||
pollIntervalMs: 5,
|
||||
platform: "linux",
|
||||
@@ -157,12 +149,11 @@ describe("gateway lock", () => {
|
||||
|
||||
await lock?.release();
|
||||
spy.mockRestore();
|
||||
await cleanup();
|
||||
});
|
||||
|
||||
it("keeps lock on linux when proc access fails unless stale", async () => {
|
||||
vi.useRealTimers();
|
||||
const { env, cleanup } = await makeEnv();
|
||||
const env = await makeEnv();
|
||||
const { lockPath, configPath } = resolveLockPath(env);
|
||||
const payload = createLockPayload({ configPath, startTime: 111 });
|
||||
await fs.writeFile(lockPath, JSON.stringify(payload), "utf8");
|
||||
@@ -173,11 +164,8 @@ describe("gateway lock", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const pending = acquireGatewayLock({
|
||||
env,
|
||||
allowInTests: true,
|
||||
const pending = acquireForTest(env, {
|
||||
timeoutMs: 15,
|
||||
pollIntervalMs: 2,
|
||||
staleMs: 10_000,
|
||||
platform: "linux",
|
||||
});
|
||||
@@ -198,11 +186,7 @@ describe("gateway lock", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const lock = await acquireGatewayLock({
|
||||
env,
|
||||
allowInTests: true,
|
||||
timeoutMs: 30,
|
||||
pollIntervalMs: 2,
|
||||
const lock = await acquireForTest(env, {
|
||||
staleMs: 1,
|
||||
platform: "linux",
|
||||
});
|
||||
@@ -210,6 +194,33 @@ describe("gateway lock", () => {
|
||||
|
||||
await lock?.release();
|
||||
staleSpy.mockRestore();
|
||||
await cleanup();
|
||||
});
|
||||
|
||||
it("returns null when multi-gateway override is enabled", async () => {
|
||||
const env = await makeEnv();
|
||||
const lock = await acquireGatewayLock({
|
||||
env: { ...env, OPENCLAW_ALLOW_MULTI_GATEWAY: "1", VITEST: "" },
|
||||
});
|
||||
expect(lock).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null in test env unless allowInTests is set", async () => {
|
||||
const env = await makeEnv();
|
||||
const lock = await acquireGatewayLock({
|
||||
env: { ...env, VITEST: "1" },
|
||||
});
|
||||
expect(lock).toBeNull();
|
||||
});
|
||||
|
||||
it("wraps unexpected fs errors as GatewayLockError", async () => {
|
||||
const env = await makeEnv();
|
||||
const openSpy = vi.spyOn(fs, "open").mockRejectedValueOnce(
|
||||
Object.assign(new Error("denied"), {
|
||||
code: "EACCES",
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(acquireForTest(env)).rejects.toBeInstanceOf(GatewayLockError);
|
||||
openSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user