Files
Epicnabbo-Catalogus-Updated…/src/lib/services/moderation.test.ts
T
openhands 17847545dd Improvements: remove dead config, fix ESM, add URL validation, unify types, add missing logging
- Remove .prettierrc (dead config, Biome replaces Prettier)
- Rename lighthouserc.json to lighthouserc.cjs with module.exports for ESM compat
- Add logger.warn to empty catch blocks in auth, register, site-settings, prisma-cache, redis, security, rate-limit
- Unify ActionResult type: action-helper.ts uses 'ok' consistent with safe-action-shared.ts
- Add noUnusedLocals + noUnusedParameters to tsconfig + fix 25 pre-existing unused vars
- Replace barrel export src/types/index.ts with direct @/types/common imports
- Make trustHost conditional (development only) in auth.ts
- Add pre-flight URL validation to update-Nitrov3.sh to catch image.library.url misconfigurations
- Improve NITRO_IMAGE_LIBRARY_URL content validation in pre-flight & post-compute checks
2026-07-26 20:28:11 +02:00

62 lines
1.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const findMany = vi.hoisted(() => vi.fn());
vi.mock("@/lib/prisma", () => ({
prisma: { websiteWordfilter: { findMany } },
}));
vi.mock("@/env", () => ({
env: {
OPENAI_API_KEY: undefined,
},
}));
import { isAllowed, moderateOrThrow, reloadWordFilter } from "./moderation";
beforeEach(() => {
findMany.mockReset();
reloadWordFilter();
});
describe("moderation", () => {
it("allows clean text", async () => {
findMany.mockResolvedValue([{ word: "badword" }]);
const result = await isAllowed("hello world");
expect(result).toEqual({ ok: true });
});
it("blocks text containing a filtered word", async () => {
findMany.mockResolvedValue([{ word: "badword" }]);
const result = await isAllowed("this contains badword here");
expect(result).toEqual({
ok: false,
reason: 'Blocked by word filter: "badword"',
});
});
it("allows empty text", async () => {
const result = await isAllowed("");
expect(result).toEqual({ ok: true });
});
it("caches the word list and respects TTL", async () => {
findMany.mockResolvedValue([{ word: "first" }]);
await isAllowed("test");
await isAllowed("test");
expect(findMany).toHaveBeenCalledTimes(1);
});
it("moderateOrThrow throws on blocked content", async () => {
findMany.mockResolvedValue([{ word: "bad" }]);
await expect(moderateOrThrow("this is bad")).rejects.toThrow(
"Blocked by word filter",
);
});
it("moderateOrThrow resolves on clean content", async () => {
findMany.mockResolvedValue([{ word: "bad" }]);
await expect(moderateOrThrow("clean text")).resolves.toBeUndefined();
});
});