emrgilhzp: Proxy the home feed with cursor paging
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
import { GET } from "@/app/api/feed/route";
|
||||
import { createSession } from "@/lib/session-store";
|
||||
|
||||
describe("GET /api/feed", () => {
|
||||
it("no cookie -> 401 sign_in", async () => {
|
||||
const res = await GET(new NextRequest("http://localhost/api/feed"));
|
||||
expect(res.status).toBe(401);
|
||||
expect(await res.json()).toEqual({ error: "sign_in" });
|
||||
});
|
||||
|
||||
it("signed in: page then next page with cursor; limit capped at 50", async () => {
|
||||
const sid = createSession({
|
||||
profileAccessToken: "p", profileAccessTokenExpiresAt: null, profileRefreshToken: "r",
|
||||
feedAccessToken: "T", feedAccessTokenExpiresAt: null,
|
||||
person: { userId: "u", name: "A", email: "a@b.co", pictureUrl: null }, createdAt: Date.now(),
|
||||
});
|
||||
const urls: string[] = [];
|
||||
const orig = globalThis.fetch;
|
||||
globalThis.fetch = vi.fn(async (url: string) => {
|
||||
urls.push(String(url));
|
||||
const next = urls.length === 1 ? "C1" : null;
|
||||
return new Response(JSON.stringify({ data: [{ n: urls.length }], metadata: { nextCursor: next } }), { status: 200 });
|
||||
}) as unknown as typeof fetch;
|
||||
try {
|
||||
const h = { cookie: `mfb_session=${sid}` };
|
||||
const r1 = await GET(new NextRequest("http://localhost/api/feed", { headers: h }));
|
||||
expect(await r1.json()).toEqual({ items: [{ n: 1 }], nextCursor: "C1" });
|
||||
const r2 = await GET(new NextRequest("http://localhost/api/feed?cursor=C1&limit=500", { headers: h }));
|
||||
expect(await r2.json()).toEqual({ items: [{ n: 2 }], nextCursor: null });
|
||||
expect(new URL(urls[0]).searchParams.has("cursor")).toBe(false);
|
||||
const u2 = new URL(urls[1]);
|
||||
expect(u2.searchParams.get("cursor")).toBe("C1");
|
||||
expect(u2.searchParams.get("limit")).toBe("50");
|
||||
} finally {
|
||||
globalThis.fetch = orig;
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user