feat(web): add logout command and tests
This commit is contained in:
@@ -5,7 +5,12 @@ import { statusCommand } from "../commands/status.js";
|
||||
import { webhookCommand } from "../commands/webhook.js";
|
||||
import { ensureTwilioEnv } from "../env.js";
|
||||
import { danger, info, setVerbose, setYes, warn } from "../globals.js";
|
||||
import { loginWeb, monitorWebProvider, pickProvider } from "../provider-web.js";
|
||||
import {
|
||||
loginWeb,
|
||||
logoutWeb,
|
||||
monitorWebProvider,
|
||||
pickProvider,
|
||||
} from "../provider-web.js";
|
||||
import { defaultRuntime } from "../runtime.js";
|
||||
import type { Provider } from "../utils.js";
|
||||
import { VERSION } from "../version.js";
|
||||
@@ -104,6 +109,18 @@ export function buildProgram() {
|
||||
}
|
||||
});
|
||||
|
||||
program
|
||||
.command("logout")
|
||||
.description("Clear cached WhatsApp Web credentials")
|
||||
.action(async () => {
|
||||
try {
|
||||
await logoutWeb(defaultRuntime);
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(`Logout failed: ${String(err)}`));
|
||||
defaultRuntime.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
program
|
||||
.command("send")
|
||||
.description("Send a WhatsApp message")
|
||||
|
||||
@@ -18,6 +18,7 @@ export {
|
||||
createWaSocket,
|
||||
formatError,
|
||||
getStatusCode,
|
||||
logoutWeb,
|
||||
logWebSelfId,
|
||||
pickProvider,
|
||||
WA_WEB_AUTH_DIR,
|
||||
|
||||
@@ -2,14 +2,14 @@ import { EventEmitter } from "node:events";
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||
import { loginWeb } from "./login.js";
|
||||
import type { waitForWaConnection } from "./session.js";
|
||||
import {
|
||||
baileys,
|
||||
resetBaileysMocks,
|
||||
resetLoadConfigMock,
|
||||
} from "./test-helpers.js";
|
||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||
import { loginWeb } from "./login.js";
|
||||
import type { waitForWaConnection } from "./session.js";
|
||||
|
||||
describe("web login", () => {
|
||||
beforeEach(() => {
|
||||
|
||||
51
src/web/logout.test.ts
Normal file
51
src/web/logout.test.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
@@ -5,13 +5,13 @@ import path from "node:path";
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||
import { monitorWebInbox } from "./inbound.js";
|
||||
import {
|
||||
getLastSocket,
|
||||
resetBaileysMocks,
|
||||
resetLoadConfigMock,
|
||||
} from "./test-helpers.js";
|
||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||
import { monitorWebInbox } from "./inbound.js";
|
||||
|
||||
describe("web monitor inbox", () => {
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||
import { sendMessageWeb } from "./outbound.js";
|
||||
import {
|
||||
getLastSocket,
|
||||
resetBaileysMocks,
|
||||
resetLoadConfigMock,
|
||||
} from "./test-helpers.js";
|
||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||
import { sendMessageWeb } from "./outbound.js";
|
||||
|
||||
describe("web outbound", () => {
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -3,18 +3,18 @@ import fsSync from "node:fs";
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||
import {
|
||||
createWaSocket,
|
||||
logWebSelfId,
|
||||
waitForWaConnection,
|
||||
} from "./session.js";
|
||||
import {
|
||||
baileys,
|
||||
getLastSocket,
|
||||
resetBaileysMocks,
|
||||
resetLoadConfigMock,
|
||||
} from "./test-helpers.js";
|
||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||
import {
|
||||
createWaSocket,
|
||||
logWebSelfId,
|
||||
waitForWaConnection,
|
||||
} from "./session.js";
|
||||
|
||||
describe("web session", () => {
|
||||
beforeEach(() => {
|
||||
@@ -29,6 +29,7 @@ describe("web session", () => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
|
||||
it("creates WA socket with QR handler", async () => {
|
||||
await createWaSocket(true, false);
|
||||
const makeWASocket = baileys.makeWASocket as ReturnType<typeof vi.fn>;
|
||||
|
||||
@@ -133,6 +133,21 @@ export async function webAuthExists() {
|
||||
.catch(() => false);
|
||||
}
|
||||
|
||||
export async function logoutWeb(runtime: RuntimeEnv = defaultRuntime) {
|
||||
const exists = await webAuthExists();
|
||||
if (!exists) {
|
||||
runtime.log(info("No WhatsApp Web session found; nothing to delete."));
|
||||
return false;
|
||||
}
|
||||
await fs.rm(WA_WEB_AUTH_DIR, { recursive: true, force: true });
|
||||
runtime.log(
|
||||
success(
|
||||
"Cleared WhatsApp Web credentials. Run `warelay login --provider web` to relink.",
|
||||
),
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
function readWebSelfId() {
|
||||
// Read the cached WhatsApp Web identity (jid + E.164) from disk if present.
|
||||
const credsPath = path.join(WA_WEB_AUTH_DIR, "creds.json");
|
||||
|
||||
Reference in New Issue
Block a user