import { beforeEach, describe, expect, it, vi } from "vitest"; import { sql, type SQL } from "drizzle-orm"; import { PgDialect } from "drizzle-orm/pg-core"; const state = vi.hoisted(() => ({ filter: undefined as unknown, order: [] as unknown[] })); vi.mock("server-only", () => ({})); vi.mock("@/lib/auth/server", () => ({ getCustomerSession: vi.fn() })); vi.mock("@/lib/notifications/events", () => ({ notifyNotificationChange: vi.fn() })); vi.mock("./events", () => ({ notifyCommentChange: vi.fn() })); vi.mock("@/lib/media/storage", () => ({ getMediaStorage: vi.fn(), publicMediaUrl: vi.fn() })); vi.mock("@/lib/audit-log", () => ({ auditActor: vi.fn(), writeAuditLog: vi.fn() })); vi.mock("@/db", () => ({ getDb: () => ({ select: () => { const query = { from: () => query, innerJoin: () => query, leftJoin: () => query, where: (condition: unknown) => { state.filter = condition; return query; }, orderBy: (...order: unknown[]) => { state.order = order; return query; }, limit: async () => [], }; return query; } }) })); import { listComments } from "./repository"; import { encodeCursor } from "./validation"; beforeEach(() => { state.filter = undefined; state.order = []; }); describe("admin comment chronological sorting", () => { it.each([undefined, "newest", "oldest"])("uses matching ordering and cursor direction for %s", async (sort) => { await listComments({ viewer: { id: "admin", admin: true }, inbox: true, grouped: true, sort, cursor: encodeCursor({ id: "00000000-0000-4000-8000-000000000001", time: "2026-10-13T00:00:00Z", likes: 0 }) }); const dialect = new PgDialect(); const order = dialect.sqlToQuery(sql.join(state.order as SQL[], sql`, `)).sql; const filter = dialect.sqlToQuery(state.filter as SQL).sql; const direction = sort === "oldest" ? "asc" : "desc"; expect(order).toContain(`"created_at" ${direction}`); expect(order).toContain(`"id" ${direction}`); expect(filter).toContain(sort === "oldest" ? ") > (" : ") < ("); }); });