feat(web): add logout command and tests

This commit is contained in:
Peter Steinberger
2025-11-26 01:29:02 +01:00
parent 1fd4485716
commit a2586b8b06
8 changed files with 99 additions and 14 deletions

51
src/web/logout.test.ts Normal file
View File

@@ -0,0 +1,51 @@
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";
const runtime = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
describe("web logout", () => {
const origHomedir = os.homedir;
let tmpDir: string;
beforeEach(() => {
vi.clearAllMocks();
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "warelay-logout-"));
vi.spyOn(os, "homedir").mockReturnValue(tmpDir);
});
afterEach(async () => {
vi.restoreAllMocks();
await fsPromises.rm(tmpDir, { recursive: true, force: true }).catch(() => {});
// restore for safety
// eslint-disable-next-line @typescript-eslint/unbound-method
(os.homedir as unknown as typeof origHomedir) = origHomedir;
});
it("deletes cached credentials when present", async () => {
const credsDir = path.join(tmpDir, ".warelay", "credentials");
fs.mkdirSync(credsDir, { recursive: true });
fs.writeFileSync(path.join(credsDir, "creds.json"), "{}");
const { logoutWeb, WA_WEB_AUTH_DIR } = await import("./session.js");
expect(WA_WEB_AUTH_DIR.startsWith(tmpDir)).toBe(true);
const result = await logoutWeb(runtime as never);
expect(result).toBe(true);
expect(fs.existsSync(credsDir)).toBe(false);
});
it("no-ops when nothing to delete", async () => {
const { logoutWeb } = await import("./session.js");
const result = await logoutWeb(runtime as never);
expect(result).toBe(false);
expect(runtime.log).toHaveBeenCalled();
});
});