Skip to content

Commit 5a1e171

Browse files
committed
test: Add tests for NRP/PERSON deprecation
1 parent 65239e7 commit 5a1e171

File tree

1 file changed

+134
-2
lines changed

1 file changed

+134
-2
lines changed

src/__tests__/unit/checks/pii.test.ts

Lines changed: 134 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Unit tests for the PII guardrail functionality.
33
*/
44

5-
import { describe, it, expect } from 'vitest';
6-
import { pii, PIIConfig, PIIEntity } from '../../../checks/pii';
5+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
6+
import { pii, PIIConfig, PIIEntity, _clearDeprecationWarnings } from '../../../checks/pii';
77

88
describe('pii guardrail', () => {
99
it('masks detected PII when block=false', async () => {
@@ -286,4 +286,136 @@ describe('pii guardrail', () => {
286286
);
287287
expect(result.info?.checked_text).toBe('Ship to <LOCATION> for delivery.');
288288
});
289+
290+
describe('NRP and PERSON deprecation (Issue #47)', () => {
291+
beforeEach(() => {
292+
// Clear deprecation warnings before each test to ensure clean state
293+
_clearDeprecationWarnings();
294+
});
295+
296+
it('excludes NRP and PERSON from default entities', () => {
297+
const config = PIIConfig.parse({});
298+
299+
expect(config.entities).not.toContain(PIIEntity.NRP);
300+
expect(config.entities).not.toContain(PIIEntity.PERSON);
301+
});
302+
303+
it('does not mask common two-word phrases when using defaults', async () => {
304+
const config = PIIConfig.parse({
305+
block: false,
306+
});
307+
const text = 'crea un nuevo cliente con email [email protected]';
308+
309+
const result = await pii({}, text, config);
310+
311+
// Should only mask the email, not "crea un" or "nuevo cliente"
312+
expect(result.info?.checked_text).toBe('crea un nuevo cliente con email <EMAIL_ADDRESS>');
313+
expect((result.info?.detected_entities as Record<string, string[]>)?.NRP).toBeUndefined();
314+
});
315+
316+
it('does not mask capitalized phrases when using defaults', async () => {
317+
const config = PIIConfig.parse({
318+
block: false,
319+
});
320+
const text = 'Welcome to New York, The User can access the system.';
321+
322+
const result = await pii({}, text, config);
323+
324+
// Should not mask "New York" or "The User"
325+
expect(result.info?.checked_text).toBe('Welcome to New York, The User can access the system.');
326+
expect((result.info?.detected_entities as Record<string, string[]>)?.PERSON).toBeUndefined();
327+
});
328+
329+
it('still detects NRP when explicitly configured', async () => {
330+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
331+
332+
const config = PIIConfig.parse({
333+
entities: [PIIEntity.NRP],
334+
block: false,
335+
});
336+
const text = 'hello world';
337+
338+
const result = await pii({}, text, config);
339+
340+
expect((result.info?.detected_entities as Record<string, string[]>)?.NRP).toEqual(['hello world']);
341+
expect(result.info?.checked_text).toBe('<NRP>');
342+
343+
consoleWarnSpy.mockRestore();
344+
});
345+
346+
it('still detects PERSON when explicitly configured', async () => {
347+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
348+
349+
const config = PIIConfig.parse({
350+
entities: [PIIEntity.PERSON],
351+
block: false,
352+
});
353+
const text = 'John Smith lives in New York';
354+
355+
const result = await pii({}, text, config);
356+
357+
expect((result.info?.detected_entities as Record<string, string[]>)?.PERSON).toContain('John Smith');
358+
expect((result.info?.detected_entities as Record<string, string[]>)?.PERSON).toContain('New York');
359+
360+
consoleWarnSpy.mockRestore();
361+
});
362+
363+
it('shows deprecation warning for NRP', async () => {
364+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
365+
366+
const config = PIIConfig.parse({
367+
entities: [PIIEntity.NRP],
368+
block: false,
369+
});
370+
371+
await pii({}, 'test data', config);
372+
373+
expect(consoleWarnSpy).toHaveBeenCalledWith(
374+
expect.stringContaining('DEPRECATION WARNING: PIIEntity.NRP')
375+
);
376+
expect(consoleWarnSpy).toHaveBeenCalledWith(
377+
expect.stringContaining('https://github.com/openai/openai-guardrails-js/issues/47')
378+
);
379+
380+
consoleWarnSpy.mockRestore();
381+
});
382+
383+
it('shows deprecation warning for PERSON', async () => {
384+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
385+
386+
const config = PIIConfig.parse({
387+
entities: [PIIEntity.PERSON],
388+
block: false,
389+
});
390+
391+
await pii({}, 'test data', config);
392+
393+
expect(consoleWarnSpy).toHaveBeenCalledWith(
394+
expect.stringContaining('DEPRECATION WARNING: PIIEntity.PERSON')
395+
);
396+
expect(consoleWarnSpy).toHaveBeenCalledWith(
397+
expect.stringContaining('https://github.com/openai/openai-guardrails-js/issues/47')
398+
);
399+
400+
consoleWarnSpy.mockRestore();
401+
});
402+
403+
it('only shows deprecation warning once per entity', async () => {
404+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
405+
406+
const config = PIIConfig.parse({
407+
entities: [PIIEntity.NRP, PIIEntity.PERSON],
408+
block: false,
409+
});
410+
411+
await pii({}, 'test data', config);
412+
await pii({}, 'more test data', config);
413+
await pii({}, 'even more data', config);
414+
415+
// Should only be called once for each entity (2 total)
416+
expect(consoleWarnSpy).toHaveBeenCalledTimes(2);
417+
418+
consoleWarnSpy.mockRestore();
419+
});
420+
});
289421
});

0 commit comments

Comments
 (0)