|
| 1 | +/** |
| 2 | + * Tests for actions/discord-activity.ts - getDiscordMarkedMedia |
| 3 | + * |
| 4 | + * Covers: admin gating, per-type summary, mark-type/source/search/date filters, |
| 5 | + * pagination, serialization, and error handling. |
| 6 | + */ |
| 7 | + |
| 8 | +import { getDiscordMarkedMedia } from "@/actions/discord-activity" |
| 9 | +import { requireAdmin } from "@/lib/admin" |
| 10 | +import { prisma } from "@/lib/prisma" |
| 11 | + |
| 12 | +jest.mock("@/lib/admin", () => ({ |
| 13 | + requireAdmin: jest.fn(), |
| 14 | +})) |
| 15 | + |
| 16 | +jest.mock("@/lib/prisma", () => ({ |
| 17 | + prisma: { |
| 18 | + userMediaMark: { |
| 19 | + findMany: jest.fn(), |
| 20 | + count: jest.fn(), |
| 21 | + groupBy: jest.fn(), |
| 22 | + }, |
| 23 | + }, |
| 24 | +})) |
| 25 | + |
| 26 | +jest.mock("@/lib/utils/logger", () => ({ |
| 27 | + createLogger: () => ({ info: jest.fn(), error: jest.fn(), warn: jest.fn() }), |
| 28 | +})) |
| 29 | + |
| 30 | +const mockRequireAdmin = requireAdmin as jest.MockedFunction<typeof requireAdmin> |
| 31 | +const findMany = prisma.userMediaMark.findMany as jest.Mock |
| 32 | +const count = prisma.userMediaMark.count as jest.Mock |
| 33 | +const groupBy = prisma.userMediaMark.groupBy as jest.Mock |
| 34 | + |
| 35 | +function makeRow(overrides: Record<string, unknown> = {}) { |
| 36 | + return { |
| 37 | + id: "mark-1", |
| 38 | + title: "The Office", |
| 39 | + year: 2005, |
| 40 | + mediaType: "TV_SERIES", |
| 41 | + markType: "KEEP_FOREVER", |
| 42 | + seasonNumber: null, |
| 43 | + episodeNumber: null, |
| 44 | + parentTitle: null, |
| 45 | + note: null, |
| 46 | + markedVia: "discord", |
| 47 | + markedAt: new Date("2026-07-01T12:00:00Z"), |
| 48 | + radarrTitleSlug: null, |
| 49 | + sonarrTitleSlug: "the-office", |
| 50 | + user: { id: "u1", name: "Alice", email: "a@x.com", image: null }, |
| 51 | + ...overrides, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +beforeEach(() => { |
| 56 | + jest.clearAllMocks() |
| 57 | + mockRequireAdmin.mockResolvedValue(undefined) |
| 58 | + findMany.mockResolvedValue([]) |
| 59 | + count.mockResolvedValue(0) |
| 60 | + groupBy.mockResolvedValue([]) |
| 61 | +}) |
| 62 | + |
| 63 | +describe("getDiscordMarkedMedia", () => { |
| 64 | + it("requires admin access", async () => { |
| 65 | + await getDiscordMarkedMedia() |
| 66 | + expect(mockRequireAdmin).toHaveBeenCalled() |
| 67 | + }) |
| 68 | + |
| 69 | + it("returns a per-type summary covering all mark types (zero-filled)", async () => { |
| 70 | + groupBy.mockResolvedValue([ |
| 71 | + { markType: "KEEP_FOREVER", _count: { _all: 3 } }, |
| 72 | + { markType: "POOR_QUALITY", _count: { _all: 1 } }, |
| 73 | + ]) |
| 74 | + |
| 75 | + const result = await getDiscordMarkedMedia() |
| 76 | + |
| 77 | + expect(result.success).toBe(true) |
| 78 | + // All 6 mark types present, missing ones zero-filled. |
| 79 | + expect(result.summary).toHaveLength(6) |
| 80 | + const byType = Object.fromEntries(result.summary.map((s) => [s.markType, s.count])) |
| 81 | + expect(byType.KEEP_FOREVER).toBe(3) |
| 82 | + expect(byType.POOR_QUALITY).toBe(1) |
| 83 | + expect(byType.FINISHED_WATCHING).toBe(0) |
| 84 | + }) |
| 85 | + |
| 86 | + it("serializes marks with user info and ISO timestamps", async () => { |
| 87 | + findMany.mockResolvedValue([makeRow()]) |
| 88 | + count.mockResolvedValue(1) |
| 89 | + |
| 90 | + const result = await getDiscordMarkedMedia() |
| 91 | + |
| 92 | + expect(result.marks).toHaveLength(1) |
| 93 | + expect(result.marks[0]).toMatchObject({ |
| 94 | + title: "The Office", |
| 95 | + markType: "KEEP_FOREVER", |
| 96 | + markedVia: "discord", |
| 97 | + markedAt: "2026-07-01T12:00:00.000Z", |
| 98 | + user: { name: "Alice", email: "a@x.com" }, |
| 99 | + }) |
| 100 | + expect(result.total).toBe(1) |
| 101 | + }) |
| 102 | + |
| 103 | + it("filters the list by mark type but keeps the summary scoped to date/source only", async () => { |
| 104 | + await getDiscordMarkedMedia({ markType: "POOR_QUALITY" as never }) |
| 105 | + |
| 106 | + // List query includes markType… |
| 107 | + expect(findMany.mock.calls[0][0].where.markType).toBe("POOR_QUALITY") |
| 108 | + // …but the summary groupBy where does NOT (so counts stay comparable). |
| 109 | + expect(groupBy.mock.calls[0][0].where.markType).toBeUndefined() |
| 110 | + }) |
| 111 | + |
| 112 | + it("applies a case-insensitive title search to the list", async () => { |
| 113 | + await getDiscordMarkedMedia({ search: "office" }) |
| 114 | + expect(findMany.mock.calls[0][0].where.title).toEqual({ |
| 115 | + contains: "office", |
| 116 | + mode: "insensitive", |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + it("maps source=discord to markedVia and source=web through verbatim", async () => { |
| 121 | + await getDiscordMarkedMedia({ source: "discord" }) |
| 122 | + expect(findMany.mock.calls[0][0].where.markedVia).toBe("discord") |
| 123 | + |
| 124 | + jest.clearAllMocks() |
| 125 | + findMany.mockResolvedValue([]) |
| 126 | + count.mockResolvedValue(0) |
| 127 | + groupBy.mockResolvedValue([]) |
| 128 | + await getDiscordMarkedMedia({ source: "web" }) |
| 129 | + expect(findMany.mock.calls[0][0].where.markedVia).toBe("web") |
| 130 | + }) |
| 131 | + |
| 132 | + it("does not filter by source when source is 'all'", async () => { |
| 133 | + await getDiscordMarkedMedia({ source: "all" }) |
| 134 | + expect(findMany.mock.calls[0][0].where.markedVia).toBeUndefined() |
| 135 | + }) |
| 136 | + |
| 137 | + it("applies date range to markedAt", async () => { |
| 138 | + await getDiscordMarkedMedia({ startDate: "2026-07-01", endDate: "2026-07-31" }) |
| 139 | + const where = findMany.mock.calls[0][0].where |
| 140 | + expect(where.markedAt.gte).toBeInstanceOf(Date) |
| 141 | + expect(where.markedAt.lt).toBeInstanceOf(Date) |
| 142 | + }) |
| 143 | + |
| 144 | + it("passes take/skip for pagination", async () => { |
| 145 | + await getDiscordMarkedMedia({ limit: 10, offset: 20 }) |
| 146 | + expect(findMany.mock.calls[0][0].take).toBe(10) |
| 147 | + expect(findMany.mock.calls[0][0].skip).toBe(20) |
| 148 | + }) |
| 149 | + |
| 150 | + it("returns a safe error shape when the query throws", async () => { |
| 151 | + findMany.mockRejectedValue(new Error("db down")) |
| 152 | + const result = await getDiscordMarkedMedia() |
| 153 | + expect(result.success).toBe(false) |
| 154 | + expect(result.marks).toEqual([]) |
| 155 | + expect(result.total).toBe(0) |
| 156 | + expect(result.summary).toEqual([]) |
| 157 | + }) |
| 158 | +}) |
0 commit comments