diff --git a/extensions/synology-chat/src/client.test.ts b/extensions/synology-chat/src/client.test.ts index 7d49bcb43f0..833ce755f78 100644 --- a/extensions/synology-chat/src/client.test.ts +++ b/extensions/synology-chat/src/client.test.ts @@ -158,22 +158,15 @@ describe("sendFileUrl", () => { }); // Helper to mock the user_list API response for fetchChatUsers / resolveLegacyWebhookNameToChatUserId -function mockUserListResponse( - users: Array<{ user_id: number; username: string; nickname: string }>, -) { +function mockUserListResponse(users: Array>) { mockUserListResponseImpl(users, false); } -function mockUserListResponseOnce( - users: Array<{ user_id: number; username: string; nickname: string }>, -) { +function mockUserListResponseOnce(users: Array>) { mockUserListResponseImpl(users, true); } -function mockUserListResponseImpl( - users: Array<{ user_id: number; username: string; nickname: string }>, - once: boolean, -) { +function mockUserListResponseImpl(users: Array>, once: boolean) { const httpsGet = vi.mocked(https.get); const impl: MockRequestHandler = (_url, _opts, callback) => { const res = createMockResponseEmitter(200); @@ -298,29 +291,10 @@ describe("fetchChatUsers", () => { installFakeTimerHarness(); it("filters malformed user entries while keeping valid ones", async () => { - const httpsGet = vi.mocked(https.get); - httpsGet.mockImplementation(((_url, _opts, callback) => { - const res = createMockResponseEmitter(200); - process.nextTick(() => { - callback?.(res); - res.emit( - "data", - Buffer.from( - JSON.stringify({ - success: true, - data: { - users: [ - { user_id: 4, username: "jmn67", nickname: "jmn" }, - { user_id: "bad", username: "broken" }, - ], - }, - }), - ), - ); - res.emit("end"); - }); - return createMockRequestEmitter(); - }) as MockRequestHandler); + mockUserListResponse([ + { user_id: 4, username: "jmn67", nickname: "jmn" }, + { user_id: "bad", username: "broken" }, + ]); const users = await fetchChatUsers( "https://nas.example.com/webapi/entry.cgi?api=SYNO.Chat.External&method=chatbot&version=2&token=%22test%22", diff --git a/extensions/synology-chat/src/core.test.ts b/extensions/synology-chat/src/core.test.ts index 54e9c913c90..3480b131104 100644 --- a/extensions/synology-chat/src/core.test.ts +++ b/extensions/synology-chat/src/core.test.ts @@ -33,6 +33,26 @@ const synologyChatSetupPlugin = { const synologyChatConfigure = createPluginSetupWizardConfigure(synologyChatSetupPlugin); const originalEnv = { ...process.env }; +function createSynologySetupPrompter(params: { allowedUserIds?: string } = {}) { + return createTestWizardPrompter({ + text: vi.fn(async ({ message }: { message: string }) => { + if (message === "Enter Synology Chat outgoing webhook token") { + return "synology-token"; + } + if (message === "Incoming webhook URL") { + return "https://nas.example.com/webapi/entry.cgi?token=incoming"; + } + if (message === "Outgoing webhook path (optional)") { + return ""; + } + if (params.allowedUserIds && message === "Allowed Synology Chat user ids") { + return params.allowedUserIds; + } + throw new Error(`Unexpected prompt: ${message}`); + }) as WizardPrompter["text"], + }); +} + describe("synology-chat core", () => { beforeEach(() => { vi.unstubAllEnvs(); @@ -83,20 +103,7 @@ describe("synology-chat core", () => { }); it("configures token and incoming webhook for the default account", async () => { - const prompter = createTestWizardPrompter({ - text: vi.fn(async ({ message }: { message: string }) => { - if (message === "Enter Synology Chat outgoing webhook token") { - return "synology-token"; - } - if (message === "Incoming webhook URL") { - return "https://nas.example.com/webapi/entry.cgi?token=incoming"; - } - if (message === "Outgoing webhook path (optional)") { - return ""; - } - throw new Error(`Unexpected prompt: ${message}`); - }) as WizardPrompter["text"], - }); + const prompter = createSynologySetupPrompter(); const result = await runSetupWizardConfigure({ configure: synologyChatConfigure, @@ -114,22 +121,8 @@ describe("synology-chat core", () => { }); it("records allowed user ids when setup forces allowFrom", async () => { - const prompter = createTestWizardPrompter({ - text: vi.fn(async ({ message }: { message: string }) => { - if (message === "Enter Synology Chat outgoing webhook token") { - return "synology-token"; - } - if (message === "Incoming webhook URL") { - return "https://nas.example.com/webapi/entry.cgi?token=incoming"; - } - if (message === "Outgoing webhook path (optional)") { - return ""; - } - if (message === "Allowed Synology Chat user ids") { - return "123456, synology-chat:789012"; - } - throw new Error(`Unexpected prompt: ${message}`); - }) as WizardPrompter["text"], + const prompter = createSynologySetupPrompter({ + allowedUserIds: "123456, synology-chat:789012", }); const result = await runSetupWizardConfigure({