mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-29 15:01:48 +03:00
perf(test): reduce web logout fs churn
This commit is contained in:
+21
-18
@@ -2,7 +2,7 @@ import fs from "node:fs";
|
|||||||
import fsPromises from "node:fs/promises";
|
import fsPromises from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
const runtime = {
|
const runtime = {
|
||||||
log: vi.fn(),
|
log: vi.fn(),
|
||||||
@@ -10,16 +10,24 @@ const runtime = {
|
|||||||
exit: vi.fn(),
|
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", () => {
|
describe("web logout", () => {
|
||||||
|
let fixtureRoot = "";
|
||||||
|
let caseId = 0;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
fixtureRoot = await fsPromises.mkdtemp(path.join(os.tmpdir(), "openclaw-test-web-logout-"));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await fsPromises.rm(fixtureRoot, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
const makeCaseDir = async () => {
|
||||||
|
const dir = path.join(fixtureRoot, `case-${caseId++}`);
|
||||||
|
await fsPromises.mkdir(dir, { recursive: true });
|
||||||
|
return dir;
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
});
|
});
|
||||||
@@ -29,29 +37,25 @@ describe("web logout", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("deletes cached credentials when present", { timeout: 60_000 }, async () => {
|
it("deletes cached credentials when present", { timeout: 60_000 }, async () => {
|
||||||
await withTempDir(async (authDir) => {
|
const authDir = await makeCaseDir();
|
||||||
const { logoutWeb } = await import("./session.js");
|
const { logoutWeb } = await import("./session.js");
|
||||||
fs.mkdirSync(authDir, { recursive: true });
|
|
||||||
fs.writeFileSync(path.join(authDir, "creds.json"), "{}");
|
fs.writeFileSync(path.join(authDir, "creds.json"), "{}");
|
||||||
const result = await logoutWeb({ authDir, runtime: runtime as never });
|
const result = await logoutWeb({ authDir, runtime: runtime as never });
|
||||||
expect(result).toBe(true);
|
expect(result).toBe(true);
|
||||||
expect(fs.existsSync(authDir)).toBe(false);
|
expect(fs.existsSync(authDir)).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it("no-ops when nothing to delete", { timeout: 60_000 }, async () => {
|
it("no-ops when nothing to delete", { timeout: 60_000 }, async () => {
|
||||||
await withTempDir(async (authDir) => {
|
const authDir = await makeCaseDir();
|
||||||
const { logoutWeb } = await import("./session.js");
|
const { logoutWeb } = await import("./session.js");
|
||||||
const result = await logoutWeb({ authDir, runtime: runtime as never });
|
const result = await logoutWeb({ authDir, runtime: runtime as never });
|
||||||
expect(result).toBe(false);
|
expect(result).toBe(false);
|
||||||
expect(runtime.log).toHaveBeenCalled();
|
expect(runtime.log).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it("keeps shared oauth.json when using legacy auth dir", async () => {
|
it("keeps shared oauth.json when using legacy auth dir", async () => {
|
||||||
await withTempDir(async (credsDir) => {
|
const credsDir = await makeCaseDir();
|
||||||
const { logoutWeb } = await import("./session.js");
|
const { logoutWeb } = await import("./session.js");
|
||||||
fs.mkdirSync(credsDir, { recursive: true });
|
|
||||||
fs.writeFileSync(path.join(credsDir, "creds.json"), "{}");
|
fs.writeFileSync(path.join(credsDir, "creds.json"), "{}");
|
||||||
fs.writeFileSync(path.join(credsDir, "oauth.json"), '{"token":true}');
|
fs.writeFileSync(path.join(credsDir, "oauth.json"), '{"token":true}');
|
||||||
fs.writeFileSync(path.join(credsDir, "session-abc.json"), "{}");
|
fs.writeFileSync(path.join(credsDir, "session-abc.json"), "{}");
|
||||||
@@ -67,4 +71,3 @@ describe("web logout", () => {
|
|||||||
expect(fs.existsSync(path.join(credsDir, "session-abc.json"))).toBe(false);
|
expect(fs.existsSync(path.join(credsDir, "session-abc.json"))).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user