feat(web): add logout command and tests
parent
1fd4485716
commit
a2586b8b06
|
|
@ -5,7 +5,12 @@ import { statusCommand } from "../commands/status.js";
|
||||||
import { webhookCommand } from "../commands/webhook.js";
|
import { webhookCommand } from "../commands/webhook.js";
|
||||||
import { ensureTwilioEnv } from "../env.js";
|
import { ensureTwilioEnv } from "../env.js";
|
||||||
import { danger, info, setVerbose, setYes, warn } from "../globals.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 { defaultRuntime } from "../runtime.js";
|
||||||
import type { Provider } from "../utils.js";
|
import type { Provider } from "../utils.js";
|
||||||
import { VERSION } from "../version.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
|
program
|
||||||
.command("send")
|
.command("send")
|
||||||
.description("Send a WhatsApp message")
|
.description("Send a WhatsApp message")
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ export {
|
||||||
createWaSocket,
|
createWaSocket,
|
||||||
formatError,
|
formatError,
|
||||||
getStatusCode,
|
getStatusCode,
|
||||||
|
logoutWeb,
|
||||||
logWebSelfId,
|
logWebSelfId,
|
||||||
pickProvider,
|
pickProvider,
|
||||||
WA_WEB_AUTH_DIR,
|
WA_WEB_AUTH_DIR,
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,14 @@ import { EventEmitter } from "node:events";
|
||||||
|
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
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 {
|
import {
|
||||||
baileys,
|
baileys,
|
||||||
resetBaileysMocks,
|
resetBaileysMocks,
|
||||||
resetLoadConfigMock,
|
resetLoadConfigMock,
|
||||||
} from "./test-helpers.js";
|
} from "./test-helpers.js";
|
||||||
|
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||||
|
import { loginWeb } from "./login.js";
|
||||||
|
import type { waitForWaConnection } from "./session.js";
|
||||||
|
|
||||||
describe("web login", () => {
|
describe("web login", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|
|
||||||
|
|
@ -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 { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
|
||||||
import { monitorWebInbox } from "./inbound.js";
|
|
||||||
import {
|
import {
|
||||||
getLastSocket,
|
getLastSocket,
|
||||||
resetBaileysMocks,
|
resetBaileysMocks,
|
||||||
resetLoadConfigMock,
|
resetLoadConfigMock,
|
||||||
} from "./test-helpers.js";
|
} from "./test-helpers.js";
|
||||||
|
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||||
|
import { monitorWebInbox } from "./inbound.js";
|
||||||
|
|
||||||
describe("web monitor inbox", () => {
|
describe("web monitor inbox", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
|
||||||
import { sendMessageWeb } from "./outbound.js";
|
|
||||||
import {
|
import {
|
||||||
getLastSocket,
|
getLastSocket,
|
||||||
resetBaileysMocks,
|
resetBaileysMocks,
|
||||||
resetLoadConfigMock,
|
resetLoadConfigMock,
|
||||||
} from "./test-helpers.js";
|
} from "./test-helpers.js";
|
||||||
|
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||||
|
import { sendMessageWeb } from "./outbound.js";
|
||||||
|
|
||||||
describe("web outbound", () => {
|
describe("web outbound", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|
|
||||||
|
|
@ -3,18 +3,18 @@ import fsSync from "node:fs";
|
||||||
|
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
|
||||||
import {
|
|
||||||
createWaSocket,
|
|
||||||
logWebSelfId,
|
|
||||||
waitForWaConnection,
|
|
||||||
} from "./session.js";
|
|
||||||
import {
|
import {
|
||||||
baileys,
|
baileys,
|
||||||
getLastSocket,
|
getLastSocket,
|
||||||
resetBaileysMocks,
|
resetBaileysMocks,
|
||||||
resetLoadConfigMock,
|
resetLoadConfigMock,
|
||||||
} from "./test-helpers.js";
|
} from "./test-helpers.js";
|
||||||
|
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||||
|
import {
|
||||||
|
createWaSocket,
|
||||||
|
logWebSelfId,
|
||||||
|
waitForWaConnection,
|
||||||
|
} from "./session.js";
|
||||||
|
|
||||||
describe("web session", () => {
|
describe("web session", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|
@ -29,6 +29,7 @@ describe("web session", () => {
|
||||||
vi.useRealTimers();
|
vi.useRealTimers();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it("creates WA socket with QR handler", async () => {
|
it("creates WA socket with QR handler", async () => {
|
||||||
await createWaSocket(true, false);
|
await createWaSocket(true, false);
|
||||||
const makeWASocket = baileys.makeWASocket as ReturnType<typeof vi.fn>;
|
const makeWASocket = baileys.makeWASocket as ReturnType<typeof vi.fn>;
|
||||||
|
|
|
||||||
|
|
@ -133,6 +133,21 @@ export async function webAuthExists() {
|
||||||
.catch(() => false);
|
.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() {
|
function readWebSelfId() {
|
||||||
// Read the cached WhatsApp Web identity (jid + E.164) from disk if present.
|
// Read the cached WhatsApp Web identity (jid + E.164) from disk if present.
|
||||||
const credsPath = path.join(WA_WEB_AUTH_DIR, "creds.json");
|
const credsPath = path.join(WA_WEB_AUTH_DIR, "creds.json");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue