Add Horse, Rabbit, Guinea Pig, Hamster, Mouse, Rat, Snake, Lizard, Turtle, Gecko, and Spider as Species (with breed taxonomies + DE translations), alongside the existing Dog/Cat/Bird. The species/breed system was already fully data-driven (pet creation, onboarding, ad targeting all read pets.listSpecies/listBreeds dynamically), so this is mostly seed data. Spiders get dedicated handling: an opt-in Owner.hideSpiders preference (offered once during onboarding via ArachnophobiaOptIn, always re-toggleable on /pets via HideSpidersToggle) fully excludes Species=Spider pets/posts from feed, explore, and search when enabled.
299 lines
9.0 KiB
TypeScript
299 lines
9.0 KiB
TypeScript
/**
|
|
* Feed Unit Tests — Phase 2 Plan 02-03
|
|
*
|
|
* Covers: FEED-01
|
|
*
|
|
* Test: empty feed — feed.getFeed returns empty for pet with no follows
|
|
* Test: cursor pagination — feed.getFeed returns nextCursor when hasNextPage
|
|
* Test: blocked pets excluded — blocked pets not included in hydrated results
|
|
*
|
|
* Redis is mocked via vi.mock('@/lib/redis') — no real Upstash calls in tests.
|
|
*/
|
|
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
// Mock server-only guard
|
|
vi.mock("server-only", () => ({}));
|
|
|
|
// Mock Redis — no real Upstash calls in unit tests
|
|
vi.mock("@/lib/redis", () => ({
|
|
redis: {
|
|
zrange: vi.fn(),
|
|
zadd: vi.fn(),
|
|
pipeline: vi.fn(() => ({
|
|
zadd: vi.fn(),
|
|
exec: vi.fn().mockResolvedValue([]),
|
|
})),
|
|
zremrangebyscore: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
// Mock Clerk server auth
|
|
vi.mock("@clerk/nextjs/server", () => ({
|
|
auth: vi.fn().mockResolvedValue({ userId: "owner-1" }),
|
|
}));
|
|
|
|
// Mock prisma singleton
|
|
vi.mock("@/lib/prisma", () => ({
|
|
prisma: {},
|
|
}));
|
|
|
|
// We need to import getFeedPage after mocks
|
|
// getFeed-helpers mock — we control what it returns in each test
|
|
vi.mock("@/lib/feed-helpers", async (importOriginal) => {
|
|
const actual =
|
|
await importOriginal<typeof import("@/lib/feed-helpers")>();
|
|
return {
|
|
...actual,
|
|
getFeedPage: vi.fn(),
|
|
};
|
|
});
|
|
|
|
import { feedRouter } from "@/trpc/routers/feed";
|
|
import { getFeedPage } from "@/lib/feed-helpers";
|
|
import { createMockPrisma } from "@/__tests__/helpers/prisma-mock";
|
|
|
|
const mockGetFeedPage = vi.mocked(getFeedPage);
|
|
|
|
// Helper to create a tRPC caller with mocked context
|
|
function createCaller(overrides?: {
|
|
userId?: string;
|
|
prisma?: ReturnType<typeof createMockPrisma>;
|
|
}) {
|
|
const mockPrisma = overrides?.prisma ?? createMockPrisma();
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const ctx = {
|
|
userId: overrides?.userId ?? "owner-1",
|
|
// Cast to any for test purposes — mock prisma satisfies the runtime interface
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
prisma: mockPrisma as any,
|
|
};
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
return { caller: feedRouter.createCaller(ctx as any), mockPrisma };
|
|
}
|
|
|
|
describe("feed.getFeed", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("FEED-01 — empty feed returns empty posts array and null nextCursor for pet with no follows", async () => {
|
|
const { caller, mockPrisma } = createCaller();
|
|
|
|
// Pet belongs to owner
|
|
mockPrisma.pet.findUnique.mockResolvedValue({
|
|
id: "pet-1",
|
|
ownerId: "owner-1",
|
|
});
|
|
|
|
// Redis returns no post IDs (empty feed)
|
|
mockGetFeedPage.mockResolvedValue({ postIds: [], nextCursor: null });
|
|
|
|
// No blocks
|
|
mockPrisma.block.findMany.mockResolvedValue([]);
|
|
|
|
// No posts to hydrate
|
|
mockPrisma.post.findMany.mockResolvedValue([]);
|
|
|
|
const result = await caller.getFeed({
|
|
petId: "pet-1",
|
|
cursor: null,
|
|
limit: 20,
|
|
});
|
|
|
|
expect(result.posts).toEqual([]);
|
|
expect(result.nextCursor).toBeNull();
|
|
});
|
|
|
|
it("FEED-01 — cursor pagination returns nextCursor when more results available", async () => {
|
|
const { caller, mockPrisma } = createCaller();
|
|
|
|
mockPrisma.pet.findUnique.mockResolvedValue({
|
|
id: "pet-1",
|
|
ownerId: "owner-1",
|
|
});
|
|
|
|
const postIds = ["post-1", "post-2", "post-3"];
|
|
const nextCursorValue = 1700000000000;
|
|
|
|
// Redis returns 3 post IDs with a next cursor
|
|
mockGetFeedPage.mockResolvedValue({
|
|
postIds,
|
|
nextCursor: nextCursorValue,
|
|
});
|
|
|
|
// No blocks
|
|
mockPrisma.block.findMany.mockResolvedValue([]);
|
|
|
|
// Hydrated posts — ordered to match Redis order
|
|
const mockPosts = postIds.map((id, i) => ({
|
|
id,
|
|
petId: "pet-2",
|
|
caption: `Caption ${i}`,
|
|
createdAt: new Date(nextCursorValue + i * 1000),
|
|
pet: { id: "pet-2", name: "Buddy", avatarKey: null },
|
|
images: [],
|
|
milestone: null,
|
|
}));
|
|
mockPrisma.post.findMany.mockResolvedValue(mockPosts);
|
|
|
|
const result = await caller.getFeed({
|
|
petId: "pet-1",
|
|
cursor: null,
|
|
limit: 20,
|
|
});
|
|
|
|
// nextCursor passed through from getFeedPage
|
|
expect(result.nextCursor).toBe(nextCursorValue);
|
|
// posts returned
|
|
expect(result.posts).toHaveLength(3);
|
|
// post order matches Redis postIds order
|
|
expect(result.posts.map((p) => p.id)).toEqual(postIds);
|
|
});
|
|
|
|
it("FEED-01 — blocked pets are excluded from feed hydration", async () => {
|
|
const { caller, mockPrisma } = createCaller();
|
|
|
|
mockPrisma.pet.findUnique.mockResolvedValue({
|
|
id: "pet-1",
|
|
ownerId: "owner-1",
|
|
});
|
|
|
|
const postIds = ["post-1", "post-2"];
|
|
mockGetFeedPage.mockResolvedValue({ postIds, nextCursor: null });
|
|
|
|
// pet-blocked is blocked
|
|
mockPrisma.block.findMany.mockResolvedValue([
|
|
{ blockedPetId: "pet-blocked" },
|
|
]);
|
|
|
|
// Prisma filters out blocked pet's post — only 1 post returned
|
|
const mockPosts = [
|
|
{
|
|
id: "post-1",
|
|
petId: "pet-2",
|
|
caption: "Hello",
|
|
createdAt: new Date(),
|
|
pet: { id: "pet-2", name: "Buddy", avatarKey: null },
|
|
images: [],
|
|
milestone: null,
|
|
},
|
|
];
|
|
mockPrisma.post.findMany.mockResolvedValue(mockPosts);
|
|
|
|
const result = await caller.getFeed({
|
|
petId: "pet-1",
|
|
cursor: null,
|
|
limit: 20,
|
|
});
|
|
|
|
// Only 1 post returned (blocked pet's post filtered at Prisma layer)
|
|
expect(result.posts).toHaveLength(1);
|
|
expect(result.posts[0].id).toBe("post-1");
|
|
|
|
// Verify block filter was queried with correct petId
|
|
expect(mockPrisma.block.findMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: { blockerPetId: "pet-1" },
|
|
})
|
|
);
|
|
});
|
|
|
|
it("Arachnophobie-Helfer: excludes Species=Spider posts when the caller opted in", async () => {
|
|
const { caller, mockPrisma } = createCaller();
|
|
|
|
mockPrisma.pet.findUnique.mockResolvedValue({ id: "pet-1", ownerId: "owner-1" });
|
|
mockPrisma.owner.findUnique.mockResolvedValue({ hideSpiders: true });
|
|
mockPrisma.species.findUnique.mockResolvedValue({ id: "species-spider" });
|
|
|
|
const postIds = ["post-1"];
|
|
mockGetFeedPage.mockResolvedValue({ postIds, nextCursor: null });
|
|
mockPrisma.block.findMany.mockResolvedValue([]);
|
|
mockPrisma.post.findMany.mockResolvedValue([
|
|
{ id: "post-1", petId: "pet-2", createdAt: new Date(), pet: { id: "pet-2" }, images: [], milestone: null },
|
|
]);
|
|
|
|
await caller.getFeed({ petId: "pet-1", cursor: null, limit: 20 });
|
|
|
|
expect(mockPrisma.post.findMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: expect.objectContaining({
|
|
pet: expect.objectContaining({ speciesId: { not: "species-spider" } }),
|
|
}),
|
|
})
|
|
);
|
|
});
|
|
|
|
it("cold-start: empty follow feed falls back to species-matched discovery posts", async () => {
|
|
const { caller, mockPrisma } = createCaller();
|
|
|
|
mockPrisma.pet.findUnique.mockResolvedValue({
|
|
id: "pet-1",
|
|
ownerId: "owner-1",
|
|
speciesId: "species-cat",
|
|
});
|
|
|
|
mockGetFeedPage.mockResolvedValue({ postIds: [], nextCursor: null });
|
|
mockPrisma.block.findMany.mockResolvedValue([]);
|
|
|
|
const discoveryPost = {
|
|
id: "post-discovery-1",
|
|
petId: "pet-stranger",
|
|
caption: "Hello from another cat",
|
|
createdAt: new Date(),
|
|
reactionCount: 2,
|
|
commentCount: 1,
|
|
repostCount: 0,
|
|
pet: { id: "pet-stranger", name: "Whiskers", avatarKey: null },
|
|
images: [],
|
|
milestone: null,
|
|
};
|
|
mockPrisma.post.findMany.mockResolvedValue([discoveryPost]);
|
|
|
|
const result = await caller.getFeed({ petId: "pet-1", cursor: null, limit: 20 });
|
|
|
|
expect(result.posts).toEqual([discoveryPost]);
|
|
expect(result.source).toBe("discovery");
|
|
expect(mockPrisma.post.findMany).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: expect.objectContaining({
|
|
pet: expect.objectContaining({ speciesId: "species-cat" }),
|
|
}),
|
|
})
|
|
);
|
|
});
|
|
|
|
it("cold-start: discovery fallback does not trigger when paginating (cursor present)", async () => {
|
|
const { caller, mockPrisma } = createCaller();
|
|
|
|
mockPrisma.pet.findUnique.mockResolvedValue({
|
|
id: "pet-1",
|
|
ownerId: "owner-1",
|
|
speciesId: "species-cat",
|
|
});
|
|
|
|
mockGetFeedPage.mockResolvedValue({ postIds: [], nextCursor: null });
|
|
|
|
const result = await caller.getFeed({ petId: "pet-1", cursor: 1700000000000, limit: 20 });
|
|
|
|
expect(result.posts).toEqual([]);
|
|
expect(result.source).toBe("following");
|
|
expect(mockPrisma.post.findMany).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("FEED-01 — throws FORBIDDEN when petId does not belong to requesting user", async () => {
|
|
const { caller, mockPrisma } = createCaller({ userId: "owner-1" });
|
|
|
|
// Pet belongs to a different owner
|
|
mockPrisma.pet.findUnique.mockResolvedValue({
|
|
id: "pet-1",
|
|
ownerId: "owner-DIFFERENT",
|
|
});
|
|
|
|
await expect(
|
|
caller.getFeed({ petId: "pet-1", cursor: null, limit: 20 })
|
|
).rejects.toThrow(TRPCError);
|
|
});
|
|
});
|