惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
D
Docker
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
美团技术团队
博客园 - 叶小钗
I
InfoQ
Jina AI
Jina AI
博客园 - 司徒正美
雷峰网
雷峰网
B
Blog
Y
Y Combinator Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
V
V2EX
N
Netflix TechBlog - Medium

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(whatsapp): require durable auth before login success ...
zhangguiping · 2026-06-14 · via Recent Commits to openclaw:main
11

// Whatsapp tests cover connection controller plugin behavior.

22

import { EventEmitter } from "node:events";

3+

import fs from "node:fs/promises";

4+

import os from "node:os";

5+

import path from "node:path";

36

import { DisconnectReason } from "baileys";

47

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

58

import { getRegisteredWhatsAppConnectionController } from "./connection-controller-registry.js";

@@ -8,8 +11,9 @@ import {

811

waitForWhatsAppLoginResult,

912

WhatsAppConnectionController,

1013

} from "./connection-controller.js";

14+

import { enqueueCredsSave, writeCredsJsonAtomically } from "./creds-persistence.js";

1115

import { createAcceptedWhatsAppSendResult } from "./inbound/send-result.test-helper.js";

12-

import { createWaSocket, waitForWaConnection } from "./session.js";

16+

import { createWaSocket, readWebAuthExistsForDecision, waitForWaConnection } from "./session.js";

1317

import { DEFAULT_WHATSAPP_SOCKET_TIMING } from "./socket-timing.js";

14181519

vi.mock("./session.js", async () => {

@@ -18,11 +22,13 @@ vi.mock("./session.js", async () => {

1822

...actual,

1923

createWaSocket: vi.fn(),

2024

waitForWaConnection: vi.fn(),

25+

readWebAuthExistsForDecision: vi.fn(async () => ({ outcome: "stable" as const, exists: true })),

2126

};

2227

});

23282429

const createWaSocketMock = vi.mocked(createWaSocket);

2530

const waitForWaConnectionMock = vi.mocked(waitForWaConnection);

31+

const readWebAuthExistsForDecisionMock = vi.mocked(readWebAuthExistsForDecision);

26322733

function createListenerStub(messageId = "ok") {

2834

return {

@@ -47,6 +53,9 @@ describe("WhatsAppConnectionController", () => {

47534854

beforeEach(() => {

4955

vi.clearAllMocks();

56+

readWebAuthExistsForDecisionMock

57+

.mockReset()

58+

.mockResolvedValue({ outcome: "stable", exists: true });

5059

controller = new WhatsAppConnectionController({

5160

accountId: "work",

5261

authDir: "/tmp/wa-auth",

@@ -249,6 +258,102 @@ describe("WhatsAppConnectionController", () => {

249258

expect(waitForConnection).toHaveBeenCalledTimes(2);

250259

});

251260261+

it("returns a retryable failure when the socket opens before auth persistence settles", async () => {

262+

readWebAuthExistsForDecisionMock.mockResolvedValue({ outcome: "unstable" });

263+

const waitForConnection = vi.fn().mockResolvedValueOnce(undefined);

264+265+

const result = await waitForWhatsAppLoginResult({

266+

sock: createSocketWithTransportEmitter() as never,

267+

authDir: "/tmp/wa-auth",

268+

isLegacyAuthDir: false,

269+

verbose: false,

270+

runtime: { log: vi.fn() } as never,

271+

waitForConnection: waitForConnection as never,

272+

});

273+274+

expect(result.outcome).toBe("failed");

275+

if (result.outcome === "failed") {

276+

expect(result.message).toMatch(/retry/i);

277+

expect((result.error as { code?: string })?.code).toBe("whatsapp-auth-unstable");

278+

}

279+

});

280+281+

it("returns a retryable failure when auth is not linked on disk after the socket opens", async () => {

282+

readWebAuthExistsForDecisionMock.mockResolvedValue({ outcome: "stable", exists: false });

283+

const waitForConnection = vi.fn().mockResolvedValueOnce(undefined);

284+285+

const result = await waitForWhatsAppLoginResult({

286+

sock: createSocketWithTransportEmitter() as never,

287+

authDir: "/tmp/wa-auth",

288+

isLegacyAuthDir: false,

289+

verbose: false,

290+

runtime: { log: vi.fn() } as never,

291+

waitForConnection: waitForConnection as never,

292+

});

293+294+

expect(result.outcome).toBe("failed");

295+

if (result.outcome === "failed") {

296+

expect(result.message).toMatch(/retry/i);

297+

expect((result.error as { code?: string })?.code).toBe("whatsapp-auth-unstable");

298+

}

299+

});

300+301+

it("returns connected only after auth is confirmed durable on disk", async () => {

302+

readWebAuthExistsForDecisionMock.mockResolvedValue({ outcome: "stable", exists: true });

303+

const waitForConnection = vi.fn().mockResolvedValueOnce(undefined);

304+

const sock = createSocketWithTransportEmitter();

305+306+

const result = await waitForWhatsAppLoginResult({

307+

sock: sock as never,

308+

authDir: "/tmp/wa-auth",

309+

isLegacyAuthDir: false,

310+

verbose: false,

311+

runtime: { log: vi.fn() } as never,

312+

waitForConnection: waitForConnection as never,

313+

});

314+315+

expect(result).toEqual({ outcome: "connected", restarted: false, sock });

316+

expect(readWebAuthExistsForDecisionMock).toHaveBeenCalledWith("/tmp/wa-auth");

317+

});

318+319+

it("waits for queued creds persistence so linked auth survives an auth-dir reuse", async () => {

320+

const actualSession = await vi.importActual<typeof import("./session.js")>("./session.js");

321+

const authDir = await fs.mkdtemp(path.join(os.tmpdir(), "wa-auth-durability-"));

322+

try {

323+

readWebAuthExistsForDecisionMock.mockImplementation(

324+

actualSession.readWebAuthExistsForDecision,

325+

);

326+

let credsSaved = false;

327+

enqueueCredsSave(

328+

authDir,

329+

async () => {

330+

await new Promise((resolve) => {

331+

setTimeout(resolve, 50);

332+

});

333+

await writeCredsJsonAtomically(authDir, { me: { id: "123@s.whatsapp.net" } });

334+

credsSaved = true;

335+

},

336+

() => {},

337+

);

338+339+

const result = await waitForWhatsAppLoginResult({

340+

sock: createSocketWithTransportEmitter() as never,

341+

authDir,

342+

isLegacyAuthDir: false,

343+

verbose: false,

344+

runtime: { log: vi.fn() } as never,

345+

waitForConnection: vi.fn().mockResolvedValueOnce(undefined) as never,

346+

});

347+348+

expect(credsSaved).toBe(true);

349+

expect(result.outcome).toBe("connected");

350+

// A fresh read of the same auth dir is what a restarted/rebuilt container does.

351+

await expect(actualSession.webAuthExists(authDir)).resolves.toBe(true);

352+

} finally {

353+

await fs.rm(authDir, { recursive: true, force: true });

354+

}

355+

});

356+252357

it("keeps the previous registered controller until a replacement listener is ready", async () => {

253358

const liveController = new WhatsAppConnectionController({

254359

accountId: "work",