From 19084632deedb5cf75cc79076059d5eeff686885 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 13 Mar 2026 06:32:15 +0000 Subject: [PATCH 1/2] test: add error path test for dashboard/year route Added missing test coverage for `src/app/api/dashboard/year/route.ts` including the error path when `fetchYearInReviewData` throws, as well as the happy path, 400, and 401 scenarios to fully cover the route. Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com> --- src/app/api/dashboard/year/route.test.ts | 95 ++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/app/api/dashboard/year/route.test.ts diff --git a/src/app/api/dashboard/year/route.test.ts b/src/app/api/dashboard/year/route.test.ts new file mode 100644 index 0000000..ecebca7 --- /dev/null +++ b/src/app/api/dashboard/year/route.test.ts @@ -0,0 +1,95 @@ +import { describe, expect, it, vi } from "vitest"; +import { NextRequest } from "next/server"; + +vi.mock("next-auth", () => ({ + getServerSession: vi.fn(), +})); + +vi.mock("@/lib/auth", () => ({ + authOptions: {}, +})); + +vi.mock("@/lib/githubViewer", () => ({ + fetchViewerLogin: vi.fn(), +})); + +vi.mock("@/lib/githubYearInReview", () => ({ + fetchYearInReviewData: vi.fn(), +})); + +describe("GET /api/dashboard/year", () => { + it("returns 401 if unauthorized", async () => { + const { getServerSession } = await import("next-auth"); + vi.mocked(getServerSession).mockResolvedValueOnce(null); + + const { GET } = await import("./route"); + const req = new NextRequest("http://localhost/api/dashboard/year?year=2023"); + + const response = await GET(req); + expect(response.status).toBe(401); + + const data = await response.json(); + expect(data).toEqual({ error: "Unauthorized" }); + }); + + it("returns 400 if year is invalid", async () => { + const { getServerSession } = await import("next-auth"); + vi.mocked(getServerSession).mockResolvedValueOnce({ + user: { login: "testuser" }, + accessToken: "testtoken", + expires: "9999-12-31T23:59:59.999Z", + }); + + const { GET } = await import("./route"); + const req = new NextRequest("http://localhost/api/dashboard/year?year=invalid"); + + const response = await GET(req); + expect(response.status).toBe(400); + + const data = await response.json(); + expect(data).toEqual({ error: "Invalid year" }); + }); + + it("handles error path when fetching data fails", async () => { + const { getServerSession } = await import("next-auth"); + vi.mocked(getServerSession).mockResolvedValueOnce({ + user: { login: "testuser" }, + accessToken: "testtoken", + expires: "9999-12-31T23:59:59.999Z", + }); + + const { fetchYearInReviewData } = await import("@/lib/githubYearInReview"); + vi.mocked(fetchYearInReviewData).mockRejectedValueOnce(new Error("API Error")); + + const { GET } = await import("./route"); + const req = new NextRequest("http://localhost/api/dashboard/year?year=2023"); + + const response = await GET(req); + expect(response.status).toBe(500); + + const data = await response.json(); + expect(data).toEqual({ error: "API Error" }); + }); + + it("returns 200 and data on success", async () => { + const { getServerSession } = await import("next-auth"); + vi.mocked(getServerSession).mockResolvedValueOnce({ + user: { login: "testuser" }, + accessToken: "testtoken", + expires: "9999-12-31T23:59:59.999Z", + }); + + const { fetchYearInReviewData } = await import("@/lib/githubYearInReview"); + const mockData = { totalContributions: 1000 }; + vi.mocked(fetchYearInReviewData).mockResolvedValueOnce(mockData as any); + + const { GET } = await import("./route"); + const req = new NextRequest("http://localhost/api/dashboard/year?year=2023"); + + const response = await GET(req); + expect(response.status).toBe(200); + + const data = await response.json(); + expect(data).toEqual(mockData); + }); +}); From 14155c326b16d4281fe472c489a2108fb0c08f37 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 13 Mar 2026 06:36:27 +0000 Subject: [PATCH 2/2] test: fix type issue in dashboard/year test Fixed TypeScript error in route.test.ts by casting `mockData` using `Awaited>` instead of `any` to satisfy lint rules. Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com> --- src/app/api/dashboard/year/route.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/api/dashboard/year/route.test.ts b/src/app/api/dashboard/year/route.test.ts index ecebca7..2c124a3 100644 --- a/src/app/api/dashboard/year/route.test.ts +++ b/src/app/api/dashboard/year/route.test.ts @@ -81,7 +81,7 @@ describe("GET /api/dashboard/year", () => { const { fetchYearInReviewData } = await import("@/lib/githubYearInReview"); const mockData = { totalContributions: 1000 }; - vi.mocked(fetchYearInReviewData).mockResolvedValueOnce(mockData as any); + vi.mocked(fetchYearInReviewData).mockResolvedValueOnce(mockData as unknown as Awaited>); const { GET } = await import("./route"); const req = new NextRequest("http://localhost/api/dashboard/year?year=2023");