|
| 1 | +import { Hono } from "hono"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import { cleanDatabase } from "../../tests/helpers"; |
| 5 | +import { getLoginCookie } from "../../tests/helpers/web"; |
| 6 | +import { getMediaWithDeletableThumbnails } from "../entities/medium"; |
| 7 | +import thumbnailCleanup from "./thumbnail_cleanup"; |
| 8 | + |
| 9 | +vi.mock("../entities/medium", () => ({ |
| 10 | + getMediaWithDeletableThumbnails: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +const app = new Hono(); |
| 14 | +app.route("/thumbnail_cleanup", thumbnailCleanup); |
| 15 | + |
| 16 | +type DeletableMedia = Awaited< |
| 17 | + ReturnType<typeof getMediaWithDeletableThumbnails> |
| 18 | +>; |
| 19 | + |
| 20 | +function createMediaItems(count: number): DeletableMedia { |
| 21 | + return Array.from({ length: count }, (_, index) => ({ |
| 22 | + created: new Date(Date.UTC(2025, 0, index + 1)), |
| 23 | + })) as DeletableMedia; |
| 24 | +} |
| 25 | + |
| 26 | +describe.sequential("thumbnail cleanup", () => { |
| 27 | + beforeEach(async () => { |
| 28 | + await cleanDatabase(); |
| 29 | + vi.mocked(getMediaWithDeletableThumbnails).mockReset(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("preserves preview counts over 999 across the redirect", async () => { |
| 33 | + expect.assertions(5); |
| 34 | + |
| 35 | + vi.mocked(getMediaWithDeletableThumbnails).mockResolvedValue( |
| 36 | + createMediaItems(4980), |
| 37 | + ); |
| 38 | + |
| 39 | + const formData = new FormData(); |
| 40 | + formData.append("before", "2026-04-12"); |
| 41 | + const cookie = await getLoginCookie(); |
| 42 | + const response = await app.request("/thumbnail_cleanup/clean_preview", { |
| 43 | + method: "POST", |
| 44 | + body: formData, |
| 45 | + headers: { |
| 46 | + Cookie: cookie, |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + expect(response.status).toBe(302); |
| 51 | + |
| 52 | + const location = response.headers.get("Location"); |
| 53 | + expect(location).not.toBeNull(); |
| 54 | + |
| 55 | + const url = new URL(location!, "http://localhost"); |
| 56 | + expect(url.searchParams.get("fileCount")).toBe("4980"); |
| 57 | + |
| 58 | + const previewResponse = await app.request(url.pathname + url.search, { |
| 59 | + headers: { |
| 60 | + Cookie: cookie, |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + expect(previewResponse.status).toBe(200); |
| 65 | + expect(await previewResponse.text()).toContain("Number of Items: 4,980"); |
| 66 | + }); |
| 67 | +}); |
0 commit comments