From a0fea672935a8899a4cf713281a95e21de4dc366 Mon Sep 17 00:00:00 2001 From: Shakker Date: Sun, 10 May 2026 16:20:38 +0100 Subject: [PATCH] test: clear line lifecycle broad matchers --- extensions/line/src/monitor.lifecycle.test.ts | 68 ++++++++++++------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/extensions/line/src/monitor.lifecycle.test.ts b/extensions/line/src/monitor.lifecycle.test.ts index c07a6f4e1cb..9a5bf04ba8e 100644 --- a/extensions/line/src/monitor.lifecycle.test.ts +++ b/extensions/line/src/monitor.lifecycle.test.ts @@ -31,14 +31,41 @@ let getLineRuntimeState: typeof import("./monitor.js").getLineRuntimeState; let clearLineRuntimeStateForTests: typeof import("./monitor.js").clearLineRuntimeStateForTests; let innerLineWebhookHandlerMock: ReturnType>; -function requireRegisteredRoute(): { handler: LineNodeWebhookHandler } { - const route = registerWebhookTargetWithPluginRouteMock.mock.calls[0]?.[0]?.route as - | { handler: LineNodeWebhookHandler } +type RegisteredRoute = { + accountId?: string; + auth?: string; + handler?: LineNodeWebhookHandler; + path?: string; + pluginId?: string; + replaceExisting?: boolean; +}; + +type RegisteredTarget = { + accountId?: string; + path: string; +}; + +type WebhookRegistration = { + route: RegisteredRoute; + target: RegisteredTarget; +}; + +function requireWebhookRegistration(): WebhookRegistration { + const registration = registerWebhookTargetWithPluginRouteMock.mock.calls[0]?.[0] as + | WebhookRegistration | undefined; - if (!route) { + if (!registration) { + throw new Error("expected registered LINE webhook target"); + } + return registration; +} + +function requireRegisteredRoute(): { handler: LineNodeWebhookHandler } { + const route = requireWebhookRegistration().route; + if (!route.handler) { throw new Error("expected registered LINE webhook route"); } - return route; + return { handler: route.handler }; } vi.mock("./bot.js", () => ({ @@ -205,9 +232,7 @@ describe("monitorLineProvider lifecycle", () => { }); expect(registerWebhookTargetWithPluginRouteMock).toHaveBeenCalledTimes(1); - expect(registerWebhookTargetWithPluginRouteMock).toHaveBeenCalledWith( - expect.objectContaining({ route: expect.objectContaining({ auth: "plugin" }) }), - ); + expect(requireWebhookRegistration().route.auth).toBe("plugin"); expect(resolved).toBe(false); abort.abort(); @@ -224,19 +249,14 @@ describe("monitorLineProvider lifecycle", () => { runtime: {} as RuntimeEnv, }); - const registration = registerWebhookTargetWithPluginRouteMock.mock.calls[0]?.[0]; - expect(registration).toEqual( - expect.objectContaining({ - target: expect.objectContaining({ accountId: "work", path: "/line/webhook" }), - route: expect.objectContaining({ - accountId: "work", - auth: "plugin", - pluginId: "line", - }), - }), - ); - expect(registration?.route).not.toHaveProperty("path"); - expect(registration?.route).not.toHaveProperty("replaceExisting"); + const registration = requireWebhookRegistration(); + expect(registration.target.accountId).toBe("work"); + expect(registration.target.path).toBe("/line/webhook"); + expect(registration.route.accountId).toBe("work"); + expect(registration.route.auth).toBe("plugin"); + expect(registration.route.pluginId).toBe("line"); + expect(registration.route).not.toHaveProperty("path"); + expect(registration.route).not.toHaveProperty("replaceExisting"); monitor.stop(); }); @@ -289,11 +309,7 @@ describe("monitorLineProvider lifecycle", () => { runtime: {} as RuntimeEnv, }); - expect(getLineRuntimeState("work")).toEqual( - expect.objectContaining({ - running: true, - }), - ); + expect(getLineRuntimeState("work")?.running).toBe(true); expect(getLineRuntimeState("default")).toBeUndefined(); monitor.stop();