Files
community-rule/tests/unit/rulesListRoute.test.ts
T
adilalloandCursor 1445cb7e1e Keep listed published rules public and guest-claim rules unlisted, with semantic document markup and share chrome that stays clickable.
Guest publishes remain share-by-link but stay out of the catalog and sitemap, and their pages send noindex until claimed. The published-rule view now uses heading and list markup matching Markdown export, and top banners sit below the header so Share is not covered.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 17:42:01 -06:00

83 lines
2.2 KiB
TypeScript

import { NextRequest } from "next/server";
import { beforeEach, describe, expect, it, vi } from "vitest";
const isDatabaseConfiguredMock = vi.fn();
const listListedMock = vi.fn();
vi.mock("../../lib/server/env", () => ({
isDatabaseConfigured: () => isDatabaseConfiguredMock(),
}));
vi.mock("../../lib/server/publishedRules", () => ({
listListedPublishedRules: (...args: unknown[]) => listListedMock(...args),
}));
vi.mock("../../lib/server/db", () => ({
prisma: {
publishedRule: {
findMany: vi.fn(),
create: vi.fn(),
},
},
}));
import { GET } from "../../app/api/rules/route";
beforeEach(() => {
isDatabaseConfiguredMock.mockReset();
listListedMock.mockReset();
});
describe("GET /api/rules", () => {
it("returns 503 when the database is not configured", async () => {
isDatabaseConfiguredMock.mockReturnValue(false);
const res = await GET(
new NextRequest("https://x.test/api/rules"),
undefined,
);
expect(res.status).toBe(503);
expect(listListedMock).not.toHaveBeenCalled();
});
it("lists listed rules from the shared helper", async () => {
isDatabaseConfiguredMock.mockReturnValue(true);
listListedMock.mockResolvedValueOnce([
{
id: "r1",
title: "Owned",
summary: null,
createdAt: new Date("2026-01-01T00:00:00.000Z"),
updatedAt: new Date("2026-01-02T00:00:00.000Z"),
},
]);
const res = await GET(
new NextRequest("https://x.test/api/rules?limit=10"),
undefined,
);
expect(res.status).toBe(200);
expect(listListedMock).toHaveBeenCalledWith(10);
const body = (await res.json()) as { rules: Array<{ id: string }> };
expect(body.rules).toEqual([
{
id: "r1",
title: "Owned",
summary: null,
createdAt: expect.any(String),
updatedAt: expect.any(String),
},
]);
});
it("returns 500 when the list helper fails", async () => {
isDatabaseConfiguredMock.mockReturnValue(true);
listListedMock.mockResolvedValueOnce(null);
const res = await GET(
new NextRequest("https://x.test/api/rules"),
undefined,
);
expect(res.status).toBe(500);
});
});