



















@@ -78,6 +78,34 @@ vi.mock("../commands/channel-setup/plugin-install.js", () => ({
7878mocks.loadChannelSetupPluginRegistrySnapshotForChannel,
7979}));
808081+function expectFields(value: unknown, expected: Record<string, unknown>): void {
82+expect(value).toBeTypeOf("object");
83+expect(value).not.toBeNull();
84+const record = value as Record<string, unknown>;
85+for (const [key, expectedValue] of Object.entries(expected)) {
86+expect(record[key], key).toEqual(expectedValue);
87+}
88+}
89+90+function readFirstCallArg(mock: ReturnType<typeof vi.fn>): Record<string, unknown> {
91+const [arg] = mock.mock.calls[0] ?? [];
92+expect(arg).toBeTypeOf("object");
93+expect(arg).not.toBeNull();
94+return arg as Record<string, unknown>;
95+}
96+97+function findCallArg(
98+mock: ReturnType<typeof vi.fn>,
99+predicate: (arg: Record<string, unknown>) => boolean,
100+): Record<string, unknown> | undefined {
101+for (const [arg] of mock.mock.calls) {
102+if (arg && typeof arg === "object" && predicate(arg as Record<string, unknown>)) {
103+return arg as Record<string, unknown>;
104+}
105+}
106+return undefined;
107+}
108+81109describe("channel-auth", () => {
82110const runtime = { log: vi.fn(), error: vi.fn(), exit: vi.fn() };
83111const plugin = {
@@ -164,15 +192,13 @@ describe("channel-auth", () => {
164192165193expect(mocks.setVerbose).toHaveBeenCalledWith(true);
166194expect(mocks.resolveChannelDefaultAccountId).not.toHaveBeenCalled();
167-expect(mocks.login).toHaveBeenCalledWith(
168-expect.objectContaining({
169-cfg: { channels: { whatsapp: {} } },
170-accountId: "acct-1",
171- runtime,
172-verbose: true,
173-channelInput: "wa",
174-}),
175-);
195+expectFields(readFirstCallArg(mocks.login), {
196+cfg: { channels: { whatsapp: {} } },
197+accountId: "acct-1",
198+ runtime,
199+verbose: true,
200+channelInput: "wa",
201+});
176202expect(mocks.callGateway).toHaveBeenCalledWith({
177203config: { channels: { whatsapp: {} } },
178204method: "channels.start",
@@ -195,7 +221,7 @@ describe("channel-auth", () => {
195221await runChannelLogin({ channel: "whatsapp", account: "acct-1" }, runtime);
196222197223expect(mocks.callGateway).not.toHaveBeenCalled();
198-expect(runtime.log).toHaveBeenCalledWith(expect.stringContaining("Gateway is in remote mode"));
224+expect(String(runtime.log.mock.calls[0]?.[0])).toContain("Gateway is in remote mode");
199225});
200226201227it("keeps login successful when local gateway runtime reconcile fails", async () => {
@@ -205,20 +231,16 @@ describe("channel-auth", () => {
205231runChannelLogin({ channel: "whatsapp", account: "acct-1" }, runtime),
206232).resolves.toBeUndefined();
207233208-expect(runtime.log).toHaveBeenCalledWith(
209-expect.stringContaining("running gateway did not restart it: gateway unreachable"),
234+expect(String(runtime.log.mock.calls[0]?.[0])).toContain(
235+"running gateway did not restart it: gateway unreachable",
210236);
211237});
212238213239it("auto-picks the single configured channel that supports login when opts are empty", async () => {
214240await runChannelLogin({}, runtime);
215241216242expect(mocks.normalizeChannelId).toHaveBeenCalledWith("whatsapp");
217-expect(mocks.login).toHaveBeenCalledWith(
218-expect.objectContaining({
219-channelInput: "whatsapp",
220-}),
221-);
243+expectFields(readFirstCallArg(mocks.login), { channelInput: "whatsapp" });
222244});
223245224246it("does not auto-pick enabled-only channel stubs when channel is omitted", async () => {
@@ -241,12 +263,10 @@ describe("channel-auth", () => {
241263config: {},
242264env: process.env,
243265});
244-expect(mocks.login).toHaveBeenCalledWith(
245-expect.objectContaining({
246-cfg: autoEnabledCfg,
247-channelInput: "whatsapp",
248-}),
249-);
266+expectFields(readFirstCallArg(mocks.login), {
267+cfg: autoEnabledCfg,
268+channelInput: "whatsapp",
269+});
250270expect(mocks.replaceConfigFile).toHaveBeenCalledWith({
251271nextConfig: autoEnabledCfg,
252272baseHash: "config-1",
@@ -260,12 +280,10 @@ describe("channel-auth", () => {
260280261281await runChannelLogout({}, runtime);
262282263-expect(mocks.callGateway).toHaveBeenCalledWith(
264-expect.objectContaining({
265-config: autoEnabledCfg,
266-method: "channels.logout",
267-}),
268-);
283+expectFields(readFirstCallArg(mocks.callGateway), {
284+config: autoEnabledCfg,
285+method: "channels.logout",
286+});
269287expect(mocks.replaceConfigFile).toHaveBeenCalledWith({
270288nextConfig: autoEnabledCfg,
271289baseHash: "config-1",
@@ -386,19 +404,21 @@ describe("channel-auth", () => {
386404387405await runChannelLogin({ channel: "whatsapp" }, runtime);
388406389-expect(mocks.ensureChannelSetupPluginInstalled).toHaveBeenCalledWith(
390-expect.objectContaining({
391-entry: catalogEntry,
392- runtime,
393-workspaceDir: "/tmp/workspace",
394-}),
395-);
396-expect(mocks.loadChannelSetupPluginRegistrySnapshotForChannel).toHaveBeenCalledWith(
397-expect.objectContaining({
407+expectFields(readFirstCallArg(mocks.ensureChannelSetupPluginInstalled), {
408+entry: catalogEntry,
409+ runtime,
410+workspaceDir: "/tmp/workspace",
411+});
412+expectFields(
413+findCallArg(
414+mocks.loadChannelSetupPluginRegistrySnapshotForChannel,
415+(arg) => arg.pluginId === "whatsapp",
416+),
417+{
398418channel: "whatsapp",
399419pluginId: "whatsapp",
400420workspaceDir: "/tmp/workspace",
401-}),
421+},
402422);
403423expect(mocks.replaceConfigFile).toHaveBeenCalledWith({
404424nextConfig: { channels: { whatsapp: {} } },
@@ -462,16 +482,14 @@ describe("channel-auth", () => {
462482baseHash: "config-1",
463483writeOptions: { unsetPaths: [["plugins", "installs"]] },
464484});
465-expect(mocks.login).toHaveBeenCalledWith(
466-expect.objectContaining({
467-cfg: {
468-channels: { whatsapp: {} },
469-plugins: {
470-entries: { whatsapp: { enabled: true } },
471-},
485+expectFields(readFirstCallArg(mocks.login), {
486+cfg: {
487+channels: { whatsapp: {} },
488+plugins: {
489+entries: { whatsapp: { enabled: true } },
472490},
473-}),
474-);
491+},
492+});
475493});
476494477495it("resolves explicit channel login through the catalog when registry normalize misses", async () => {
@@ -505,18 +523,13 @@ describe("channel-auth", () => {
505523506524await runChannelLogin({ channel: "whatsapp" }, runtime);
507525508-expect(mocks.ensureChannelSetupPluginInstalled).toHaveBeenCalledWith(
509-expect.objectContaining({
510-entry: expect.objectContaining({ id: "whatsapp" }),
511- runtime,
512-workspaceDir: "/tmp/workspace",
513-}),
514-);
515-expect(mocks.login).toHaveBeenCalledWith(
516-expect.objectContaining({
517-channelInput: "whatsapp",
518-}),
519-);
526+const installArg = readFirstCallArg(mocks.ensureChannelSetupPluginInstalled);
527+expectFields(installArg, {
528+ runtime,
529+workspaceDir: "/tmp/workspace",
530+});
531+expectFields(installArg.entry, { id: "whatsapp" });
532+expectFields(readFirstCallArg(mocks.login), { channelInput: "whatsapp" });
520533});
521534522535it("runs logout through the live gateway with resolved account and explicit account id", async () => {
@@ -550,8 +563,8 @@ describe("channel-auth", () => {
550563account: { id: "resolved-account" },
551564 runtime,
552565});
553-expect(runtime.log).toHaveBeenCalledWith(
554-expect.stringContaining("running gateway did not stop it: gateway unreachable"),
566+expect(String(runtime.log.mock.calls[0]?.[0])).toContain(
567+"running gateway did not stop it: gateway unreachable",
555568);
556569expect(mocks.setVerbose).not.toHaveBeenCalled();
557570});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。