From c7046ea2569624941577234eed593e9b057c51a2 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:30:19 +0000 Subject: [PATCH 1/2] test: add dashboard summary API route tests Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com> --- src/app/api/dashboard/summary/route.test.ts | 120 ++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/app/api/dashboard/summary/route.test.ts diff --git a/src/app/api/dashboard/summary/route.test.ts b/src/app/api/dashboard/summary/route.test.ts new file mode 100644 index 0000000..a38aa68 --- /dev/null +++ b/src/app/api/dashboard/summary/route.test.ts @@ -0,0 +1,120 @@ +import { describe, expect, it, vi, beforeEach } from "vitest"; +import { GET } from "./route"; + +import type { Session } from "next-auth"; + +vi.mock("next-auth", () => ({ + getServerSession: vi.fn(), +})); + +vi.mock("@/lib/github", () => ({ + fetchUserSummary: vi.fn(), +})); + +vi.mock("@/lib/githubViewer", () => ({ + fetchViewerLogin: vi.fn(), +})); + +describe("GET /api/dashboard/summary", () => { + beforeEach(() => { + vi.resetAllMocks(); + }); + + it("returns 401 if unauthorized", async () => { + const { getServerSession } = await import("next-auth"); + vi.mocked(getServerSession).mockResolvedValueOnce(null); + + const response = await GET(); + const data = await response.json(); + + expect(response.status).toBe(401); + expect(data).toEqual({ error: "Unauthorized" }); + }); + + it("returns summary if session contains user login", async () => { + const { getServerSession } = await import("next-auth"); + const { fetchUserSummary } = await import("@/lib/github"); + + vi.mocked(getServerSession).mockResolvedValueOnce({ + user: { login: "testuser" }, + accessToken: "token123", + } as unknown as Session); + + vi.mocked(fetchUserSummary).mockResolvedValueOnce({ + text: "This is a summary", + } as unknown as string); + + const response = await GET(); + const data = await response.json(); + + expect(response.status).toBe(200); + expect(data).toEqual({ + username: "testuser", + summary: { text: "This is a summary" }, + }); + expect(fetchUserSummary).toHaveBeenCalledWith("testuser", "token123"); + }); + + it("returns summary using fetchViewerLogin if session user login is missing", async () => { + const { getServerSession } = await import("next-auth"); + const { fetchUserSummary } = await import("@/lib/github"); + const { fetchViewerLogin } = await import("@/lib/githubViewer"); + + vi.mocked(getServerSession).mockResolvedValueOnce({ + accessToken: "token123", + } as unknown as Session); + + vi.mocked(fetchViewerLogin).mockResolvedValueOnce("viewerlogin"); + + vi.mocked(fetchUserSummary).mockResolvedValueOnce({ + text: "Viewer summary", + } as unknown as string); + + const response = await GET(); + const data = await response.json(); + + expect(response.status).toBe(200); + expect(data).toEqual({ + username: "viewerlogin", + summary: { text: "Viewer summary" }, + }); + expect(fetchViewerLogin).toHaveBeenCalledWith("token123"); + expect(fetchUserSummary).toHaveBeenCalledWith("viewerlogin", "token123"); + }); + + it("returns 500 if fetchUserSummary throws an Error", async () => { + const { getServerSession } = await import("next-auth"); + const { fetchUserSummary } = await import("@/lib/github"); + + vi.mocked(getServerSession).mockResolvedValueOnce({ + user: { login: "erroruser" }, + accessToken: "token123", + } as unknown as Session); + + vi.mocked(fetchUserSummary).mockRejectedValueOnce(new Error("API Error")); + + const response = await GET(); + const data = await response.json(); + + expect(response.status).toBe(500); + expect(data).toEqual({ error: "API Error" }); + }); + + it("returns 500 if fetchUserSummary throws a non-Error", async () => { + const { getServerSession } = await import("next-auth"); + const { fetchUserSummary } = await import("@/lib/github"); + + vi.mocked(getServerSession).mockResolvedValueOnce({ + user: { login: "erroruser" }, + accessToken: "token123", + } as unknown as Session); + + vi.mocked(fetchUserSummary).mockRejectedValueOnce("String error"); + + const response = await GET(); + const data = await response.json(); + + expect(response.status).toBe(500); + expect(data).toEqual({ error: "Unknown error" }); + }); +}); From a05d23e3ebe7a08c6b66ab751d6035bc8b8a91ee 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:37:14 +0000 Subject: [PATCH 2/2] test: fix typings in dashboard summary API tests Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com> --- src/app/api/dashboard/summary/route.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/api/dashboard/summary/route.test.ts b/src/app/api/dashboard/summary/route.test.ts index a38aa68..16d3b16 100644 --- a/src/app/api/dashboard/summary/route.test.ts +++ b/src/app/api/dashboard/summary/route.test.ts @@ -42,7 +42,7 @@ describe("GET /api/dashboard/summary", () => { vi.mocked(fetchUserSummary).mockResolvedValueOnce({ text: "This is a summary", - } as unknown as string); + } as unknown as import('@/lib/types').UserSummary); const response = await GET(); const data = await response.json(); @@ -68,7 +68,7 @@ describe("GET /api/dashboard/summary", () => { vi.mocked(fetchUserSummary).mockResolvedValueOnce({ text: "Viewer summary", - } as unknown as string); + } as unknown as import('@/lib/types').UserSummary); const response = await GET(); const data = await response.json();