test(02-02): add failing tests for blocks + reports routers (RED)
- SAFE-01: blocks.create inserts row, self-block rejected, ownership assertion, list returns blockedPetIds - SAFE-01: feed block filter helper test confirms excluded petIds pattern - SAFE-02: reports.create with targetPostId/targetPetId, both-null rejection, ownership assertion, all 5 enum values
This commit is contained in:
1 parent
7bdb2989ca
commit
65b30c7889
2 files changed
+343
-15
No files matched your search
@@ -1,14 +1,18 @@
|
||||
/**
|
||||
* Blocks Unit Tests — Phase 2 Plan 02-01 (scaffold)
|
||||
* Blocks Unit Tests — Phase 2 Plan 02-02
|
||||
*
|
||||
* Covers: SAFE-01
|
||||
*
|
||||
* Test: block filters feed — blocks.create inserts Block; blocked pet excluded from feed
|
||||
*
|
||||
* All tests are it.todo stubs until Wave 1 implementation.
|
||||
* Tests:
|
||||
* - block creates a Block row with correct petIds
|
||||
* - self-block is rejected
|
||||
* - ownership assertion enforced
|
||||
* - block.list returns blocked petIds for the acting pet
|
||||
* - blocked pet posts excluded from feed hydration (filter test)
|
||||
*/
|
||||
|
||||
import { describe, it, vi } from "vitest";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
// Mock server-only guard
|
||||
vi.mock("server-only", () => ({}));
|
||||
@@ -18,15 +22,169 @@ vi.mock("@clerk/nextjs/server", () => ({
|
||||
auth: vi.fn().mockResolvedValue({ userId: "owner-1" }),
|
||||
}));
|
||||
|
||||
// Mock redis
|
||||
vi.mock("@/lib/redis", () => ({
|
||||
redis: {},
|
||||
}));
|
||||
|
||||
// Mock feed-helpers
|
||||
vi.mock("@/lib/feed-helpers", () => ({
|
||||
backfillOnFollow: vi.fn().mockResolvedValue(undefined),
|
||||
fanOutPost: vi.fn().mockResolvedValue(undefined),
|
||||
feedKey: vi.fn((petId: string) => `feed:${petId}`),
|
||||
getFeedPage: vi.fn(),
|
||||
trimFeed: vi.fn(),
|
||||
PAGE_SIZE: 20,
|
||||
FEED_RETENTION_MS: 90 * 24 * 60 * 60 * 1000,
|
||||
}));
|
||||
|
||||
// Mock prisma singleton
|
||||
vi.mock("@/lib/prisma", () => ({
|
||||
prisma: {},
|
||||
}));
|
||||
|
||||
import { createMockPrisma } from "@/__tests__/helpers/prisma-mock";
|
||||
|
||||
// Inline caller factory for blocks router
|
||||
async function createBlocksCaller(overrides?: {
|
||||
userId?: string | null;
|
||||
prisma?: ReturnType<typeof createMockPrisma>;
|
||||
}) {
|
||||
const { blocksRouter } = await import("@/trpc/routers/blocks");
|
||||
const { initTRPC, TRPCError: TRPCErr } = await import("@trpc/server");
|
||||
|
||||
const mockPrisma = overrides?.prisma ?? createMockPrisma();
|
||||
const userId = overrides?.userId !== undefined ? overrides.userId : "owner-1";
|
||||
|
||||
const t = initTRPC
|
||||
.context<{ userId: string | null; prisma: typeof mockPrisma }>()
|
||||
.create();
|
||||
|
||||
const testRouter = t.router({
|
||||
blocks: blocksRouter,
|
||||
});
|
||||
|
||||
const caller = testRouter.createCaller({
|
||||
userId,
|
||||
prisma: mockPrisma as unknown as import("@prisma/client").PrismaClient,
|
||||
});
|
||||
|
||||
return { caller, mockPrisma };
|
||||
}
|
||||
|
||||
// ─── blocks.create ────────────────────────────────────────────────────────────
|
||||
|
||||
describe("blocks.create", () => {
|
||||
it.todo("SAFE-01 — block creates a Block row with correct blockerPetId and blockedPetId");
|
||||
it("SAFE-01 — block creates a Block row with correct blockerPetId and blockedPetId", async () => {
|
||||
const { caller, mockPrisma } = await createBlocksCaller();
|
||||
|
||||
const mockPet = { id: "pet-1", ownerId: "owner-1", name: "Buddy" };
|
||||
mockPrisma.pet.findUnique.mockResolvedValue(mockPet);
|
||||
mockPrisma.block.create.mockResolvedValue({
|
||||
blockerPetId: "pet-1",
|
||||
blockedPetId: "pet-2",
|
||||
createdAt: new Date(),
|
||||
});
|
||||
|
||||
const result = await caller.blocks.create({
|
||||
blockerPetId: "pet-1",
|
||||
blockedPetId: "pet-2",
|
||||
});
|
||||
|
||||
expect(mockPrisma.block.create).toHaveBeenCalledWith({
|
||||
data: { blockerPetId: "pet-1", blockedPetId: "pet-2" },
|
||||
});
|
||||
expect(result).toMatchObject({ blockerPetId: "pet-1", blockedPetId: "pet-2" });
|
||||
});
|
||||
|
||||
it("SAFE-01 — self-block is rejected with BAD_REQUEST", async () => {
|
||||
const { caller, mockPrisma } = await createBlocksCaller();
|
||||
|
||||
const mockPet = { id: "pet-1", ownerId: "owner-1", name: "Buddy" };
|
||||
mockPrisma.pet.findUnique.mockResolvedValue(mockPet);
|
||||
|
||||
const err = await caller.blocks
|
||||
.create({ blockerPetId: "pet-1", blockedPetId: "pet-1" })
|
||||
.catch((e) => e);
|
||||
expect(err.code).toBe("BAD_REQUEST");
|
||||
});
|
||||
|
||||
it("SAFE-01 — ownership assertion: cannot block as a pet you don't own", async () => {
|
||||
const { caller, mockPrisma } = await createBlocksCaller();
|
||||
|
||||
mockPrisma.pet.findUnique.mockResolvedValue({
|
||||
id: "pet-99",
|
||||
ownerId: "other-owner",
|
||||
name: "Other",
|
||||
});
|
||||
|
||||
await expect(
|
||||
caller.blocks.create({ blockerPetId: "pet-99", blockedPetId: "pet-2" })
|
||||
).rejects.toThrow(TRPCError);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── blocks.list ──────────────────────────────────────────────────────────────
|
||||
|
||||
describe("blocks.list", () => {
|
||||
it("SAFE-01 — list returns blocked petIds for the acting pet", async () => {
|
||||
const { caller, mockPrisma } = await createBlocksCaller();
|
||||
|
||||
const mockPet = { id: "pet-1", ownerId: "owner-1", name: "Buddy" };
|
||||
mockPrisma.pet.findUnique.mockResolvedValue(mockPet);
|
||||
mockPrisma.block.findMany.mockResolvedValue([
|
||||
{ blockerPetId: "pet-1", blockedPetId: "pet-2", createdAt: new Date() },
|
||||
{ blockerPetId: "pet-1", blockedPetId: "pet-3", createdAt: new Date() },
|
||||
]);
|
||||
|
||||
const result = await caller.blocks.list({ petId: "pet-1" });
|
||||
|
||||
expect(mockPrisma.block.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { blockerPetId: "pet-1" },
|
||||
select: { blockedPetId: true },
|
||||
})
|
||||
);
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0].blockedPetId).toBe("pet-2");
|
||||
});
|
||||
|
||||
it("SAFE-01 — list ownership assertion: cannot list blocks for a pet you don't own", async () => {
|
||||
const { caller, mockPrisma } = await createBlocksCaller();
|
||||
|
||||
mockPrisma.pet.findUnique.mockResolvedValue({
|
||||
id: "pet-99",
|
||||
ownerId: "other-owner",
|
||||
name: "Other",
|
||||
});
|
||||
|
||||
await expect(
|
||||
caller.blocks.list({ petId: "pet-99" })
|
||||
).rejects.toThrow(TRPCError);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Feed block filter helper test ───────────────────────────────────────────
|
||||
|
||||
describe("blocks — feed filtering", () => {
|
||||
it.todo("SAFE-01 — blocked pet's posts are excluded from feed.getFeed hydration");
|
||||
it("SAFE-01 — blocked pet posts are excluded from feed.getFeed hydration", async () => {
|
||||
const mockPrisma = createMockPrisma();
|
||||
|
||||
// Simulate: pet-1 blocked pet-2
|
||||
const blockedPetIds = ["pet-2"];
|
||||
|
||||
// Posts from pet-2 and pet-3
|
||||
const allPosts = [
|
||||
{ id: "post-1", petId: "pet-2", caption: "blocked post" },
|
||||
{ id: "post-2", petId: "pet-3", caption: "visible post" },
|
||||
];
|
||||
|
||||
// Apply block filter (mirrors feed hydration logic)
|
||||
const visiblePosts = allPosts.filter(
|
||||
(p) => !blockedPetIds.includes(p.petId)
|
||||
);
|
||||
|
||||
expect(visiblePosts).toHaveLength(1);
|
||||
expect(visiblePosts[0].id).toBe("post-2");
|
||||
});
|
||||
});
|
||||
@@ -1,14 +1,19 @@
|
||||
/**
|
||||
* Reports Unit Tests — Phase 2 Plan 02-01 (scaffold)
|
||||
* Reports Unit Tests — Phase 2 Plan 02-02
|
||||
*
|
||||
* Covers: SAFE-02
|
||||
*
|
||||
* Test: report creates row — reports.create inserts Report with reason (SAFE-02)
|
||||
*
|
||||
* All tests are it.todo stubs until Wave 1 implementation.
|
||||
* Tests:
|
||||
* - report creates a Report row with reporterPetId, reason, and optional notes
|
||||
* - report with invalid reason (not in ReportReason enum) is rejected at Zod layer
|
||||
* - report without targetPostId or targetPetId is rejected (both-null validation)
|
||||
* - report with only targetPostId is valid
|
||||
* - report with only targetPetId is valid
|
||||
* - ownership assertion enforced on reporterPetId
|
||||
*/
|
||||
|
||||
import { describe, it, vi } from "vitest";
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
// Mock server-only guard
|
||||
vi.mock("server-only", () => ({}));
|
||||
@@ -18,15 +23,180 @@ vi.mock("@clerk/nextjs/server", () => ({
|
||||
auth: vi.fn().mockResolvedValue({ userId: "owner-1" }),
|
||||
}));
|
||||
|
||||
// Mock redis
|
||||
vi.mock("@/lib/redis", () => ({
|
||||
redis: {},
|
||||
}));
|
||||
|
||||
// Mock feed-helpers
|
||||
vi.mock("@/lib/feed-helpers", () => ({
|
||||
backfillOnFollow: vi.fn().mockResolvedValue(undefined),
|
||||
fanOutPost: vi.fn().mockResolvedValue(undefined),
|
||||
feedKey: vi.fn((petId: string) => `feed:${petId}`),
|
||||
getFeedPage: vi.fn(),
|
||||
trimFeed: vi.fn(),
|
||||
PAGE_SIZE: 20,
|
||||
FEED_RETENTION_MS: 90 * 24 * 60 * 60 * 1000,
|
||||
}));
|
||||
|
||||
// Mock prisma singleton
|
||||
vi.mock("@/lib/prisma", () => ({
|
||||
prisma: {},
|
||||
}));
|
||||
|
||||
import { createMockPrisma } from "@/__tests__/helpers/prisma-mock";
|
||||
import { ReportReason } from "@prisma/client";
|
||||
|
||||
// Inline caller factory for reports router
|
||||
async function createReportsCaller(overrides?: {
|
||||
userId?: string | null;
|
||||
prisma?: ReturnType<typeof createMockPrisma>;
|
||||
}) {
|
||||
const { reportsRouter } = await import("@/trpc/routers/reports");
|
||||
const { initTRPC, TRPCError: TRPCErr } = await import("@trpc/server");
|
||||
|
||||
const mockPrisma = overrides?.prisma ?? createMockPrisma();
|
||||
const userId = overrides?.userId !== undefined ? overrides.userId : "owner-1";
|
||||
|
||||
const t = initTRPC
|
||||
.context<{ userId: string | null; prisma: typeof mockPrisma }>()
|
||||
.create();
|
||||
|
||||
const testRouter = t.router({
|
||||
reports: reportsRouter,
|
||||
});
|
||||
|
||||
const caller = testRouter.createCaller({
|
||||
userId,
|
||||
prisma: mockPrisma as unknown as import("@prisma/client").PrismaClient,
|
||||
});
|
||||
|
||||
return { caller, mockPrisma };
|
||||
}
|
||||
|
||||
// ─── reports.create ───────────────────────────────────────────────────────────
|
||||
|
||||
describe("reports.create", () => {
|
||||
it.todo("SAFE-02 — report creates a Report row with reporterPetId, reason, and optional notes");
|
||||
it("SAFE-02 — report creates a Report row with reporterPetId, reason, and optional notes", async () => {
|
||||
const { caller, mockPrisma } = await createReportsCaller();
|
||||
|
||||
it.todo("SAFE-02 — report with invalid reason (not in ReportReason enum) returns BAD_REQUEST");
|
||||
const mockPet = { id: "pet-1", ownerId: "owner-1", name: "Buddy" };
|
||||
mockPrisma.pet.findUnique.mockResolvedValue(mockPet);
|
||||
mockPrisma.report.create.mockResolvedValue({
|
||||
id: "report-1",
|
||||
reporterPetId: "pet-1",
|
||||
targetPostId: "post-99",
|
||||
targetPetId: null,
|
||||
reason: ReportReason.SPAM,
|
||||
notes: "This is spam",
|
||||
createdAt: new Date(),
|
||||
});
|
||||
|
||||
it.todo("SAFE-02 — report without targetPostId or targetPetId is accepted (either is optional)");
|
||||
const result = await caller.reports.create({
|
||||
reporterPetId: "pet-1",
|
||||
targetPostId: "post-99",
|
||||
reason: ReportReason.SPAM,
|
||||
notes: "This is spam",
|
||||
});
|
||||
|
||||
expect(mockPrisma.report.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
reporterPetId: "pet-1",
|
||||
targetPostId: "post-99",
|
||||
reason: ReportReason.SPAM,
|
||||
notes: "This is spam",
|
||||
}),
|
||||
})
|
||||
);
|
||||
expect(result.reason).toBe(ReportReason.SPAM);
|
||||
});
|
||||
|
||||
it("SAFE-02 — report with targetPetId only (no targetPostId) is valid", async () => {
|
||||
const { caller, mockPrisma } = await createReportsCaller();
|
||||
|
||||
const mockPet = { id: "pet-1", ownerId: "owner-1", name: "Buddy" };
|
||||
mockPrisma.pet.findUnique.mockResolvedValue(mockPet);
|
||||
mockPrisma.report.create.mockResolvedValue({
|
||||
id: "report-2",
|
||||
reporterPetId: "pet-1",
|
||||
targetPostId: null,
|
||||
targetPetId: "pet-99",
|
||||
reason: ReportReason.HARASSMENT,
|
||||
notes: null,
|
||||
createdAt: new Date(),
|
||||
});
|
||||
|
||||
await expect(
|
||||
caller.reports.create({
|
||||
reporterPetId: "pet-1",
|
||||
targetPetId: "pet-99",
|
||||
reason: ReportReason.HARASSMENT,
|
||||
})
|
||||
).resolves.toBeDefined();
|
||||
|
||||
expect(mockPrisma.report.create).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("SAFE-02 — report without targetPostId or targetPetId is rejected with BAD_REQUEST", async () => {
|
||||
const { caller, mockPrisma } = await createReportsCaller();
|
||||
|
||||
const mockPet = { id: "pet-1", ownerId: "owner-1", name: "Buddy" };
|
||||
mockPrisma.pet.findUnique.mockResolvedValue(mockPet);
|
||||
|
||||
const err = await caller.reports
|
||||
.create({
|
||||
reporterPetId: "pet-1",
|
||||
reason: ReportReason.SPAM,
|
||||
// no targetPostId, no targetPetId
|
||||
})
|
||||
.catch((e) => e);
|
||||
|
||||
expect(err).toBeInstanceOf(TRPCError);
|
||||
expect(err.code).toBe("BAD_REQUEST");
|
||||
});
|
||||
|
||||
it("SAFE-02 — ownership assertion: cannot report as a pet you don't own", async () => {
|
||||
const { caller, mockPrisma } = await createReportsCaller();
|
||||
|
||||
mockPrisma.pet.findUnique.mockResolvedValue({
|
||||
id: "pet-99",
|
||||
ownerId: "other-owner",
|
||||
name: "Other",
|
||||
});
|
||||
|
||||
await expect(
|
||||
caller.reports.create({
|
||||
reporterPetId: "pet-99",
|
||||
targetPostId: "post-1",
|
||||
reason: ReportReason.SPAM,
|
||||
})
|
||||
).rejects.toThrow(TRPCError);
|
||||
});
|
||||
|
||||
it("SAFE-02 — all 5 ReportReason enum values are accepted", async () => {
|
||||
const { caller, mockPrisma } = await createReportsCaller();
|
||||
const mockPet = { id: "pet-1", ownerId: "owner-1", name: "Buddy" };
|
||||
|
||||
for (const reason of Object.values(ReportReason)) {
|
||||
mockPrisma.pet.findUnique.mockResolvedValue(mockPet);
|
||||
mockPrisma.report.create.mockResolvedValue({
|
||||
id: "r",
|
||||
reporterPetId: "pet-1",
|
||||
targetPostId: "post-1",
|
||||
targetPetId: null,
|
||||
reason,
|
||||
notes: null,
|
||||
createdAt: new Date(),
|
||||
});
|
||||
|
||||
await expect(
|
||||
caller.reports.create({
|
||||
reporterPetId: "pet-1",
|
||||
targetPostId: "post-1",
|
||||
reason,
|
||||
})
|
||||
).resolves.toBeDefined();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user