|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { hmacSHA256Base64 } from "../src/utils/hmacSHA256Base64"; |
| 3 | + |
| 4 | +const mocks = vi.hoisted(() => ({ |
| 5 | + eventRouter: vi.fn().mockResolvedValue(undefined), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock("../src/eventRouter", () => ({ |
| 9 | + eventRouter: mocks.eventRouter, |
| 10 | +})); |
| 11 | + |
| 12 | +import { router } from "../src/router"; |
| 13 | + |
| 14 | +const mockEnv = { |
| 15 | + LINE_CHANNEL_SECRET: { get: vi.fn() }, |
| 16 | + LINE_CHANNEL_ACCESS_TOKEN: { get: vi.fn() }, |
| 17 | +}; |
| 18 | + |
| 19 | +describe("router extra branches", () => { |
| 20 | + beforeEach(() => { |
| 21 | + vi.clearAllMocks(); |
| 22 | + mockEnv.LINE_CHANNEL_SECRET.get.mockResolvedValue("secret"); |
| 23 | + mockEnv.LINE_CHANNEL_ACCESS_TOKEN.get.mockResolvedValue("accessToken"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("returns 400 when body is not valid JSON", async () => { |
| 27 | + const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined); |
| 28 | + const request = new Request("https://example.com", { |
| 29 | + method: "POST", |
| 30 | + headers: { "x-line-signature": "sig" }, |
| 31 | + body: "{", |
| 32 | + }); |
| 33 | + |
| 34 | + const response = await router(request, mockEnv as any, {} as any); |
| 35 | + |
| 36 | + expect(response.status).toBe(400); |
| 37 | + expect(await response.text()).toBe("Invalid JSON"); |
| 38 | + errorSpy.mockRestore(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("dispatches each event to eventRouter", async () => { |
| 42 | + const eventA = { type: "message", message: { type: "text", text: "Hi" } }; |
| 43 | + const eventB = { type: "message", message: { type: "text", text: "hello" } }; |
| 44 | + const rawBody = JSON.stringify({ destination: "", events: [eventA, eventB] }); |
| 45 | + const signature = await hmacSHA256Base64("secret", rawBody); |
| 46 | + |
| 47 | + const request = new Request("https://example.com", { |
| 48 | + method: "POST", |
| 49 | + headers: { "x-line-signature": signature }, |
| 50 | + body: rawBody, |
| 51 | + }); |
| 52 | + const ctx = { waitUntil: vi.fn() } as any; |
| 53 | + |
| 54 | + const response = await router(request, mockEnv as any, ctx); |
| 55 | + |
| 56 | + expect(response.status).toBe(200); |
| 57 | + expect(mocks.eventRouter).toHaveBeenCalledTimes(2); |
| 58 | + expect(mocks.eventRouter).toHaveBeenNthCalledWith(1, eventA, "accessToken", ctx); |
| 59 | + expect(mocks.eventRouter).toHaveBeenNthCalledWith(2, eventB, "accessToken", ctx); |
| 60 | + }); |
| 61 | + |
| 62 | + it("returns 200 when events field is missing", async () => { |
| 63 | + const rawBody = JSON.stringify({ destination: "" }); |
| 64 | + const signature = await hmacSHA256Base64("secret", rawBody); |
| 65 | + |
| 66 | + const request = new Request("https://example.com", { |
| 67 | + method: "POST", |
| 68 | + headers: { "x-line-signature": signature }, |
| 69 | + body: rawBody, |
| 70 | + }); |
| 71 | + const ctx = { waitUntil: vi.fn() } as any; |
| 72 | + |
| 73 | + const response = await router(request, mockEnv as any, ctx); |
| 74 | + |
| 75 | + expect(response.status).toBe(200); |
| 76 | + expect(mocks.eventRouter).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | +}); |
0 commit comments