Files
petfeed/src/__tests__/milestones.test.ts
T

147 lines
4.5 KiB
TypeScript

/**
* Milestones Unit Tests — Phase 2 Plan 02-06
*
* Covers: MILE-01
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
import { TRPCError } from "@trpc/server";
vi.mock("server-only", () => ({}));
vi.mock("@/lib/redis", () => ({
redis: {
pipeline: vi.fn(() => ({ zadd: vi.fn(), exec: vi.fn().mockResolvedValue([]) })),
},
}));
vi.mock("@/lib/feed-helpers", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/feed-helpers")>();
return { ...actual, fanOutPost: vi.fn().mockResolvedValue(undefined) };
});
vi.mock("@clerk/nextjs/server", () => ({
auth: vi.fn().mockResolvedValue({ userId: "owner-1" }),
}));
vi.mock("@/lib/prisma", () => ({ prisma: {} }));
import { milestonesRouter } from "@/trpc/routers/milestones";
import { fanOutPost } from "@/lib/feed-helpers";
import { createMockPrisma } from "@/__tests__/helpers/prisma-mock";
const mockFanOutPost = vi.mocked(fanOutPost);
function createCaller(overrides?: {
userId?: string;
prisma?: ReturnType<typeof createMockPrisma>;
}) {
const mockPrisma = overrides?.prisma ?? createMockPrisma();
const ctx = {
userId: overrides?.userId ?? "owner-1",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
prisma: mockPrisma as any,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { caller: milestonesRouter.createCaller(ctx as any), mockPrisma };
}
describe("milestones.create", () => {
beforeEach(() => vi.clearAllMocks());
it("MILE-01 — valid milestone type creates Post type MILESTONE + Milestone row + fanOut", async () => {
const { caller, mockPrisma } = createCaller();
mockPrisma.pet.findUnique.mockResolvedValue({ id: "pet-1", ownerId: "owner-1" });
const mockPost = {
id: "post-1",
petId: "pet-1",
type: "MILESTONE",
caption: "Happy birthday!",
createdAt: new Date("2026-06-13T00:00:00Z"),
updatedAt: new Date(),
images: [],
milestone: { id: "ms-1", postId: "post-1", milestoneType: "BIRTHDAY", notes: null },
pet: { id: "pet-1", name: "Buddy", avatarKey: null },
};
mockPrisma.post.create.mockResolvedValue(mockPost);
const result = await caller.create({
petId: "pet-1",
milestoneType: "BIRTHDAY",
caption: "Happy birthday!",
});
expect(mockPrisma.post.create).toHaveBeenCalledOnce();
expect(mockPrisma.post.create).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
petId: "pet-1",
type: "MILESTONE",
milestone: expect.objectContaining({
create: expect.objectContaining({ milestoneType: "BIRTHDAY" }),
}),
}),
})
);
expect(mockFanOutPost).toHaveBeenCalledOnce();
expect(result.id).toBe("post-1");
expect(result.type).toBe("MILESTONE");
});
it("MILE-01 — invalid milestone type (not in enum) is rejected by Zod", async () => {
const { caller } = createCaller();
await expect(
caller.create({
petId: "pet-1",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
milestoneType: "INVALID_TYPE" as any,
})
).rejects.toThrow(TRPCError);
});
it("MILE-01 — LITTER milestone accepts free-text notes", async () => {
const { caller, mockPrisma } = createCaller();
mockPrisma.pet.findUnique.mockResolvedValue({ id: "pet-1", ownerId: "owner-1" });
mockPrisma.post.create.mockResolvedValue({
id: "post-2",
petId: "pet-1",
type: "MILESTONE",
caption: null,
createdAt: new Date(),
updatedAt: new Date(),
images: [],
milestone: { milestoneType: "LITTER", notes: "5 healthy kittens" },
pet: { id: "pet-1", name: "Mittens", avatarKey: null },
});
await caller.create({
petId: "pet-1",
milestoneType: "LITTER",
notes: "5 healthy kittens",
});
expect(mockPrisma.post.create).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
milestone: expect.objectContaining({
create: expect.objectContaining({ notes: "5 healthy kittens" }),
}),
}),
})
);
});
it("MILE-01 — create throws FORBIDDEN if pet does not belong to userId", async () => {
const { caller, mockPrisma } = createCaller({ userId: "owner-1" });
mockPrisma.pet.findUnique.mockResolvedValue({ id: "pet-2", ownerId: "owner-2" });
await expect(
caller.create({ petId: "pet-2", milestoneType: "BIRTHDAY" })
).rejects.toMatchObject({ code: "FORBIDDEN" });
});
});