





















@@ -1,20 +1,35 @@
11import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22import { getRegisteredWhatsAppConnectionController } from "./connection-controller-registry.js";
33import { WhatsAppConnectionController } from "./connection-controller.js";
4-import { createWaSocket, waitForWaConnection } from "./session.js";
4+import {
5+createWaSocket,
6+waitForCredsSaveQueueWithTimeout,
7+waitForWaConnection,
8+} from "./session.js";
59610vi.mock("./session.js", async () => {
711const actual = await vi.importActual<typeof import("./session.js")>("./session.js");
812return {
913 ...actual,
1014createWaSocket: vi.fn(),
15+waitForCredsSaveQueueWithTimeout: vi.fn(async () => {}),
1116waitForWaConnection: vi.fn(),
1217};
1318});
14191520const createWaSocketMock = vi.mocked(createWaSocket);
21+const waitForCredsSaveQueueWithTimeoutMock = vi.mocked(waitForCredsSaveQueueWithTimeout);
1622const waitForWaConnectionMock = vi.mocked(waitForWaConnection);
172324+function createListenerStub(messageId = "ok") {
25+return {
26+sendMessage: vi.fn(async () => ({ messageId })),
27+sendPoll: vi.fn(async () => ({ messageId })),
28+sendReaction: vi.fn(async () => {}),
29+sendComposingTo: vi.fn(async () => {}),
30+};
31+}
32+1833describe("WhatsAppConnectionController", () => {
1934let controller: WhatsAppConnectionController;
2035@@ -66,6 +81,26 @@ describe("WhatsAppConnectionController", () => {
6681expect(controller.getActiveListener()).toBeNull();
6782});
688384+it("flushes pending creds saves before opening a socket", async () => {
85+const callOrder: string[] = [];
86+waitForCredsSaveQueueWithTimeoutMock.mockImplementationOnce(async () => {
87+callOrder.push("wait");
88+});
89+createWaSocketMock.mockImplementationOnce(async () => {
90+callOrder.push("create");
91+return { ws: { close: vi.fn() } } as never;
92+});
93+waitForWaConnectionMock.mockResolvedValueOnce(undefined);
94+95+await controller.openConnection({
96+connectionId: "conn-flush-first",
97+createListener: async () => createListenerStub() as never,
98+});
99+100+expect(waitForCredsSaveQueueWithTimeoutMock).toHaveBeenCalledWith("/tmp/wa-auth");
101+expect(callOrder).toEqual(["wait", "create"]);
102+});
103+69104it("keeps the previous registered controller until a replacement listener is ready", async () => {
70105const liveController = new WhatsAppConnectionController({
71106accountId: "work",
@@ -83,12 +118,7 @@ describe("WhatsAppConnectionController", () => {
83118maxAttempts: 5,
84119},
85120});
86-const liveListener = {
87-sendMessage: vi.fn(async () => ({ messageId: "live-msg" })),
88-sendPoll: vi.fn(async () => ({ messageId: "live-poll" })),
89-sendReaction: vi.fn(async () => {}),
90-sendComposingTo: vi.fn(async () => {}),
91-};
121+const liveListener = createListenerStub("live");
92122createWaSocketMock.mockResolvedValueOnce({ ws: { close: vi.fn() } } as never);
93123waitForWaConnectionMock.mockResolvedValueOnce(undefined);
94124await liveController.openConnection({
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。