refactor: centralize whatsapp auth detection

This commit is contained in:
Peter Steinberger
2026-01-15 04:00:50 +00:00
parent 1732932c57
commit 6320f739d4
4 changed files with 102 additions and 47 deletions

View File

@@ -0,0 +1,63 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { hasAnyWhatsAppAuth, listWhatsAppAuthDirs } from "./accounts.js";
describe("hasAnyWhatsAppAuth", () => {
let previousOauthDir: string | undefined;
let tempOauthDir: string | undefined;
const writeCreds = (dir: string) => {
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, "creds.json"), JSON.stringify({ me: {} }));
};
beforeEach(() => {
previousOauthDir = process.env.CLAWDBOT_OAUTH_DIR;
tempOauthDir = fs.mkdtempSync(path.join(os.tmpdir(), "clawdbot-oauth-"));
process.env.CLAWDBOT_OAUTH_DIR = tempOauthDir;
});
afterEach(() => {
if (previousOauthDir === undefined) {
delete process.env.CLAWDBOT_OAUTH_DIR;
} else {
process.env.CLAWDBOT_OAUTH_DIR = previousOauthDir;
}
if (tempOauthDir) {
fs.rmSync(tempOauthDir, { recursive: true, force: true });
tempOauthDir = undefined;
}
});
it("returns false when no auth exists", () => {
expect(hasAnyWhatsAppAuth({})).toBe(false);
});
it("returns true when legacy auth exists", () => {
fs.writeFileSync(path.join(tempOauthDir ?? "", "creds.json"), JSON.stringify({ me: {} }));
expect(hasAnyWhatsAppAuth({})).toBe(true);
});
it("returns true when non-default auth exists", () => {
writeCreds(path.join(tempOauthDir ?? "", "whatsapp", "work"));
expect(hasAnyWhatsAppAuth({})).toBe(true);
});
it("includes authDir overrides", () => {
const customDir = fs.mkdtempSync(path.join(os.tmpdir(), "clawdbot-wa-auth-"));
try {
writeCreds(customDir);
const cfg = {
channels: { whatsapp: { accounts: { work: { authDir: customDir } } } },
};
expect(listWhatsAppAuthDirs(cfg)).toContain(customDir);
expect(hasAnyWhatsAppAuth(cfg)).toBe(true);
} finally {
fs.rmSync(customDir, { recursive: true, force: true });
}
});
});