perf(test): speed up memory batch + web logout

This commit is contained in:
Peter Steinberger
2026-02-14 15:39:25 +00:00
parent 76e4e9d176
commit 2b5e0a6075
2 changed files with 41 additions and 37 deletions

View File

@@ -1,8 +1,8 @@
import fs from "node:fs";
import fsPromises from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { isPathWithinBase } from "../../test/helpers/paths.js";
import { withTempHome } from "../../test/helpers/temp-home.js";
const runtime = {
log: vi.fn(),
@@ -10,6 +10,15 @@ const runtime = {
exit: vi.fn(),
};
async function withTempDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
const dir = await fsPromises.mkdtemp(path.join(os.tmpdir(), "openclaw-test-web-logout-"));
try {
return await fn(dir);
} finally {
await fsPromises.rm(dir, { recursive: true, force: true });
}
}
describe("web logout", () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -20,38 +29,28 @@ describe("web logout", () => {
});
it("deletes cached credentials when present", { timeout: 60_000 }, async () => {
await withTempHome(async (home) => {
await withTempDir(async (authDir) => {
const { logoutWeb } = await import("./session.js");
const { resolveDefaultWebAuthDir } = await import("./auth-store.js");
const authDir = resolveDefaultWebAuthDir();
expect(isPathWithinBase(home, authDir)).toBe(true);
fs.mkdirSync(authDir, { recursive: true });
fs.writeFileSync(path.join(authDir, "creds.json"), "{}");
const result = await logoutWeb({ runtime: runtime as never });
const result = await logoutWeb({ authDir, runtime: runtime as never });
expect(result).toBe(true);
expect(fs.existsSync(authDir)).toBe(false);
});
});
it("no-ops when nothing to delete", { timeout: 60_000 }, async () => {
await withTempHome(async () => {
await withTempDir(async (authDir) => {
const { logoutWeb } = await import("./session.js");
const result = await logoutWeb({ runtime: runtime as never });
const result = await logoutWeb({ authDir, runtime: runtime as never });
expect(result).toBe(false);
expect(runtime.log).toHaveBeenCalled();
});
});
it("keeps shared oauth.json when using legacy auth dir", async () => {
await withTempHome(async () => {
await withTempDir(async (credsDir) => {
const { logoutWeb } = await import("./session.js");
const { resolveOAuthDir } = await import("../config/paths.js");
const credsDir = resolveOAuthDir();
fs.mkdirSync(credsDir, { recursive: true });
fs.writeFileSync(path.join(credsDir, "creds.json"), "{}");
fs.writeFileSync(path.join(credsDir, "oauth.json"), '{"token":true}');