Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/routes/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ describe("POST /api/mask", () => {
expect(body.entities).toEqual([]);
});

test("preserves leading and trailing whitespace in masked text", async () => {
const text = " John Smith lives in Berlin\n\n";
mockDetectPII.mockResolvedValueOnce([{ entity_type: "PERSON", start: 2, end: 12, score: 0.9 }]);

const res = await app.request("/api/mask", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text }),
});

expect(res.status).toBe(200);
expect(mockDetectPII).toHaveBeenLastCalledWith(text);
const body = (await res.json()) as { masked: string; context: Record<string, string> };
expect(body.masked).toBe(" [[PERSON_1]] lives in Berlin\n\n");
expect(body.context["[[PERSON_1]]"]).toBe("John Smith");
});

test("masks PII entities", async () => {
mockDetectPII.mockResolvedValueOnce([
{ entity_type: "EMAIL_ADDRESS", start: 6, end: 22, score: 0.9 },
Expand Down
3 changes: 2 additions & 1 deletion src/routes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import { createLogData } from "./utils";

// Request schema
const MaskRequestSchema = z.object({
text: z.string().trim().min(1, "text is required"),
// Reject blank text without trimming it: clients align `masked` against the original input.
text: z.string().refine((text) => text.trim().length > 0, "text is required"),
startFrom: z.record(z.string(), z.number()).optional(),
detect: z.array(z.enum(["pii", "secrets"])).optional(),
});
Expand Down
Loading