|
| 1 | +import { describe, it, expect, vi, beforeEach } from "bun:test"; |
| 2 | + |
| 3 | +vi.mock("../utils/github.js", () => ({ |
| 4 | + generateGitHubAppToken: vi.fn().mockResolvedValue("fake-token"), |
| 5 | +})); |
| 6 | + |
| 7 | +vi.mock("./tag-patterns.js", () => ({ |
| 8 | + tagPatterns: vi.fn().mockResolvedValue({ tagged: true }), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("./learning-status.js", () => ({ |
| 12 | + postLearningStatus: vi.fn().mockResolvedValue({ posted: true }), |
| 13 | +})); |
| 14 | + |
| 15 | +import { handleInternalDispatch } from "./internal-dispatch.js"; |
| 16 | +import { tagPatterns } from "./tag-patterns.js"; |
| 17 | +import { postLearningStatus } from "./learning-status.js"; |
| 18 | +import { generateGitHubAppToken } from "../utils/github.js"; |
| 19 | + |
| 20 | +const VALID_SECRET = "test-secret-123"; |
| 21 | + |
| 22 | +function makeRequest(pathname, { secret, body } = {}) { |
| 23 | + const headers = { "Content-Type": "application/json" }; |
| 24 | + if (secret !== undefined) { |
| 25 | + headers["X-Internal-Secret"] = secret; |
| 26 | + } |
| 27 | + return new Request(`https://example.com${pathname}`, { |
| 28 | + method: "POST", |
| 29 | + headers, |
| 30 | + body: JSON.stringify(body ?? {}), |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +describe("handleInternalDispatch — authentication", () => { |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("returns 401 when INTERNAL_SECRET is not configured in env", async () => { |
| 40 | + const request = makeRequest("/internal/tag-patterns", { |
| 41 | + secret: "anything", |
| 42 | + body: { repoOwner: "DaleStudy", repoName: "leetcode-study", prNumber: 1 }, |
| 43 | + }); |
| 44 | + |
| 45 | + const response = await handleInternalDispatch( |
| 46 | + request, |
| 47 | + {}, |
| 48 | + "/internal/tag-patterns" |
| 49 | + ); |
| 50 | + |
| 51 | + expect(response.status).toBe(401); |
| 52 | + const body = await response.json(); |
| 53 | + expect(body.error).toBe("Unauthorized"); |
| 54 | + expect(generateGitHubAppToken).not.toHaveBeenCalled(); |
| 55 | + expect(tagPatterns).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns 401 when X-Internal-Secret header is missing", async () => { |
| 59 | + const request = makeRequest("/internal/tag-patterns", { |
| 60 | + body: { repoOwner: "DaleStudy", repoName: "leetcode-study", prNumber: 1 }, |
| 61 | + }); |
| 62 | + |
| 63 | + const response = await handleInternalDispatch( |
| 64 | + request, |
| 65 | + { INTERNAL_SECRET: VALID_SECRET }, |
| 66 | + "/internal/tag-patterns" |
| 67 | + ); |
| 68 | + |
| 69 | + expect(response.status).toBe(401); |
| 70 | + expect(tagPatterns).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("returns 401 when X-Internal-Secret header does not match env.INTERNAL_SECRET", async () => { |
| 74 | + const request = makeRequest("/internal/tag-patterns", { |
| 75 | + secret: "wrong-secret", |
| 76 | + body: { repoOwner: "DaleStudy", repoName: "leetcode-study", prNumber: 1 }, |
| 77 | + }); |
| 78 | + |
| 79 | + const response = await handleInternalDispatch( |
| 80 | + request, |
| 81 | + { INTERNAL_SECRET: VALID_SECRET }, |
| 82 | + "/internal/tag-patterns" |
| 83 | + ); |
| 84 | + |
| 85 | + expect(response.status).toBe(401); |
| 86 | + expect(tagPatterns).not.toHaveBeenCalled(); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe("handleInternalDispatch — routing", () => { |
| 91 | + const env = { INTERNAL_SECRET: VALID_SECRET, OPENAI_API_KEY: "fake-openai" }; |
| 92 | + |
| 93 | + beforeEach(() => { |
| 94 | + vi.clearAllMocks(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("routes /internal/tag-patterns to tagPatterns with payload fields", async () => { |
| 98 | + const prData = { number: 42, head: { sha: "abc123" } }; |
| 99 | + const request = makeRequest("/internal/tag-patterns", { |
| 100 | + secret: VALID_SECRET, |
| 101 | + body: { |
| 102 | + repoOwner: "DaleStudy", |
| 103 | + repoName: "leetcode-study", |
| 104 | + prNumber: 42, |
| 105 | + headSha: "abc123", |
| 106 | + prData, |
| 107 | + }, |
| 108 | + }); |
| 109 | + |
| 110 | + const response = await handleInternalDispatch( |
| 111 | + request, |
| 112 | + env, |
| 113 | + "/internal/tag-patterns" |
| 114 | + ); |
| 115 | + |
| 116 | + expect(response.status).toBe(200); |
| 117 | + const body = await response.json(); |
| 118 | + expect(body.handler).toBe("tag-patterns"); |
| 119 | + expect(tagPatterns).toHaveBeenCalledWith( |
| 120 | + "DaleStudy", |
| 121 | + "leetcode-study", |
| 122 | + 42, |
| 123 | + "abc123", |
| 124 | + prData, |
| 125 | + "fake-token", |
| 126 | + "fake-openai" |
| 127 | + ); |
| 128 | + expect(postLearningStatus).not.toHaveBeenCalled(); |
| 129 | + }); |
| 130 | + |
| 131 | + it("routes /internal/learning-status to postLearningStatus with payload fields", async () => { |
| 132 | + const request = makeRequest("/internal/learning-status", { |
| 133 | + secret: VALID_SECRET, |
| 134 | + body: { |
| 135 | + repoOwner: "DaleStudy", |
| 136 | + repoName: "leetcode-study", |
| 137 | + prNumber: 42, |
| 138 | + username: "testuser", |
| 139 | + }, |
| 140 | + }); |
| 141 | + |
| 142 | + const response = await handleInternalDispatch( |
| 143 | + request, |
| 144 | + env, |
| 145 | + "/internal/learning-status" |
| 146 | + ); |
| 147 | + |
| 148 | + expect(response.status).toBe(200); |
| 149 | + const body = await response.json(); |
| 150 | + expect(body.handler).toBe("learning-status"); |
| 151 | + expect(postLearningStatus).toHaveBeenCalledWith( |
| 152 | + "DaleStudy", |
| 153 | + "leetcode-study", |
| 154 | + 42, |
| 155 | + "testuser", |
| 156 | + "fake-token", |
| 157 | + "fake-openai" |
| 158 | + ); |
| 159 | + expect(tagPatterns).not.toHaveBeenCalled(); |
| 160 | + }); |
| 161 | + |
| 162 | + it("returns 404 for an unknown /internal/* pathname", async () => { |
| 163 | + const request = makeRequest("/internal/unknown", { |
| 164 | + secret: VALID_SECRET, |
| 165 | + body: {}, |
| 166 | + }); |
| 167 | + |
| 168 | + const response = await handleInternalDispatch( |
| 169 | + request, |
| 170 | + env, |
| 171 | + "/internal/unknown" |
| 172 | + ); |
| 173 | + |
| 174 | + expect(response.status).toBe(404); |
| 175 | + expect(tagPatterns).not.toHaveBeenCalled(); |
| 176 | + expect(postLearningStatus).not.toHaveBeenCalled(); |
| 177 | + }); |
| 178 | +}); |
| 179 | + |
| 180 | +describe("handleInternalDispatch — error handling", () => { |
| 181 | + const env = { INTERNAL_SECRET: VALID_SECRET, OPENAI_API_KEY: "fake-openai" }; |
| 182 | + |
| 183 | + beforeEach(() => { |
| 184 | + vi.clearAllMocks(); |
| 185 | + }); |
| 186 | + |
| 187 | + it("returns 500 when the handler throws", async () => { |
| 188 | + tagPatterns.mockRejectedValueOnce(new Error("boom")); |
| 189 | + |
| 190 | + const request = makeRequest("/internal/tag-patterns", { |
| 191 | + secret: VALID_SECRET, |
| 192 | + body: { |
| 193 | + repoOwner: "DaleStudy", |
| 194 | + repoName: "leetcode-study", |
| 195 | + prNumber: 1, |
| 196 | + headSha: "sha", |
| 197 | + prData: {}, |
| 198 | + }, |
| 199 | + }); |
| 200 | + |
| 201 | + const response = await handleInternalDispatch( |
| 202 | + request, |
| 203 | + env, |
| 204 | + "/internal/tag-patterns" |
| 205 | + ); |
| 206 | + |
| 207 | + expect(response.status).toBe(500); |
| 208 | + const body = await response.json(); |
| 209 | + expect(body.error).toContain("boom"); |
| 210 | + }); |
| 211 | +}); |
0 commit comments