Harden the core privacy guarantee: - Add `strict` mode (default true): after detection, verify no mapped value survives as a whole token in the output (ignoring placeholders, whose context may legitimately echo a value like `B+`). Catches a model that redacts one mention of a value but leaves another in clear — which the placeholder/mapping bijection check missed. Fail-closed: throws AnonymizationError naming only the non-secret placeholder key, and runs on the final result so it is not swallowed into the regex fallback (which can't fix a name leak). Set strict:false to opt out. - Add `prefilter` option (default true): decouple the PII pre-filter from the presence of a regex fallback. Set false to always consult the LLM while keeping the fallback for LLM failures (max recall + graceful degradation). - Boundary-aware value substitution: applyKnown and the leak check now match values only as whole tokens (Unicode letter/digit boundaries, regex-escaped), so "Ann" no longer replaces inside "Anna" and "jean@x.ch" no longer matches inside "jean@x.church"; accented/non-Latin names preserved. - deanonymize restores longest placeholder keys first (prefix-overlap defense). Restructures anonymize/anonymizeChunks/anonymizeTurn to a single exit so the leak check runs once on the final result. Behavior is unchanged for callers that were already leak-free. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
565 lines
26 KiB
TypeScript
565 lines
26 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest';
|
|
import {
|
|
Anonymizer,
|
|
AnonymizationError,
|
|
presets,
|
|
type LlmProvider,
|
|
type AnonymizerSession,
|
|
} from '../src/index.js';
|
|
|
|
/** An LLM provider that is configured but always fails → forces the regex fallback. */
|
|
const failingLlm = (overrides: Partial<LlmProvider> = {}): LlmProvider => ({
|
|
isConfigured: () => true,
|
|
anonymize: vi.fn().mockRejectedValue(new Error('LLM_NOT_CONFIGURED')),
|
|
anonymizeBatch: vi.fn().mockRejectedValue(new Error('LLM_NOT_CONFIGURED')),
|
|
...overrides,
|
|
});
|
|
|
|
describe('Anonymizer (Swiss preset)', () => {
|
|
const svc = new Anonymizer({ llm: failingLlm(), patterns: presets.swiss });
|
|
|
|
it('skips anonymization (no LLM call) when there is no PII', async () => {
|
|
const llm = failingLlm();
|
|
const s = new Anonymizer({ llm, patterns: presets.swiss });
|
|
const r = await s.anonymize('Explique la différence entre INNER JOIN et LEFT JOIN');
|
|
expect(r.mapping).toEqual({});
|
|
expect(r.anon).toContain('INNER JOIN');
|
|
expect(llm.anonymize).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('falls back to regex for structured PII (email/phone/AVS)', async () => {
|
|
const r = await svc.anonymize('Contact: jean@exemple.ch, +41 79 123 45 67, AVS 756.1234.5678.90');
|
|
expect(r.anon).not.toContain('jean@exemple.ch');
|
|
expect(r.anon).not.toContain('756.1234.5678.90');
|
|
expect(Object.values(r.mapping)).toContain('jean@exemple.ch');
|
|
expect(svc.deanonymize(r.anon, r.mapping)).toContain('jean@exemple.ch');
|
|
});
|
|
|
|
it('deanonymizes a placeholder split across stream tokens (critical)', () => {
|
|
const mapping = { '[PER_1.NOM:M]': 'Alain JACCARD' };
|
|
const d = svc.makeStreamDeanonymizer(mapping);
|
|
let out = '';
|
|
out += d.push('Bonjour [PER_');
|
|
out += d.push('1.NOM');
|
|
out += d.push(':M], ravi');
|
|
out += d.flush();
|
|
expect(out).toBe('Bonjour Alain JACCARD, ravi');
|
|
expect(out).not.toContain('[PER_');
|
|
});
|
|
|
|
it('streams plain text through unchanged', () => {
|
|
const d = svc.makeStreamDeanonymizer({});
|
|
expect(d.push('Un INNER JOIN ') + d.push('retourne...') + d.flush()).toBe('Un INNER JOIN retourne...');
|
|
});
|
|
|
|
describe('anonymizeChunks', () => {
|
|
it('PII-free chunks → no LLM call, returned as-is', async () => {
|
|
const llm = failingLlm();
|
|
const s = new Anonymizer({ llm, patterns: presets.swiss });
|
|
const r = await s.anonymizeChunks(['Un INNER JOIN combine deux tables.'], { mapping: {} });
|
|
expect(llm.anonymizeBatch).not.toHaveBeenCalled();
|
|
expect(r.anon[0]).toContain('INNER JOIN');
|
|
expect(r.mapping).toEqual({});
|
|
});
|
|
|
|
it('reuses the question placeholder for the same person (deterministic, no LLM)', async () => {
|
|
const llm = failingLlm();
|
|
const s = new Anonymizer({ llm, patterns: presets.swiss });
|
|
const seed = { '[PER_1.NOM:M]': 'Alain JACCARD' };
|
|
const r = await s.anonymizeChunks(['Le dossier de Alain JACCARD est complet.'], { mapping: seed });
|
|
expect(llm.anonymizeBatch).not.toHaveBeenCalled();
|
|
expect(r.anon[0]).toContain('[PER_1.NOM:M]');
|
|
expect(r.anon[0]).not.toContain('Alain JACCARD');
|
|
expect(r.mapping).toEqual(seed);
|
|
});
|
|
|
|
it('renumbers a NEW person that collides with the question placeholder', async () => {
|
|
const anonymizeBatch = vi.fn().mockResolvedValue({
|
|
segments: ['[PER_1.NOM:M] a signé.'],
|
|
mapping: { '[PER_1.NOM:M]': 'Bob Martin' },
|
|
legend: { PER: 'Personne', NOM: 'Nom de famille', M: 'Masculin' },
|
|
});
|
|
const s = new Anonymizer({ llm: failingLlm({ anonymizeBatch }), patterns: presets.swiss });
|
|
const seed = { '[PER_1.NOM:M]': 'Alain JACCARD' };
|
|
const r = await s.anonymizeChunks(['Bob Martin a signé.'], { mapping: seed });
|
|
expect(anonymizeBatch).toHaveBeenCalledTimes(1);
|
|
expect(r.anon[0]).toContain('[PER_2.NOM:M]');
|
|
expect(r.mapping['[PER_1.NOM:M]']).toBe('Alain JACCARD');
|
|
expect(r.mapping['[PER_2.NOM:M]']).toBe('Bob Martin');
|
|
// legend covers the abbreviations used in the anonymized chunk
|
|
expect(r.legend).toMatchObject({ PER: 'Personne', NOM: 'Nom de famille', M: 'Masculin' });
|
|
});
|
|
|
|
it('falls back to regex (structured ids) when the LLM is unavailable', async () => {
|
|
const s = new Anonymizer({ llm: failingLlm(), patterns: presets.swiss });
|
|
const r = await s.anonymizeChunks(['Contact : jean@exemple.ch'], { mapping: {} });
|
|
expect(r.anon[0]).not.toContain('jean@exemple.ch');
|
|
expect(Object.values(r.mapping)).toContain('jean@exemple.ch');
|
|
});
|
|
});
|
|
|
|
describe('regex-only mode (no LLM provider)', () => {
|
|
const s = new Anonymizer({ patterns: presets.swiss });
|
|
|
|
it('still anonymizes structured PII without any provider', async () => {
|
|
const r = await s.anonymize('Écris à jean@exemple.ch');
|
|
expect(r.anon).not.toContain('jean@exemple.ch');
|
|
expect(Object.values(r.mapping)).toContain('jean@exemple.ch');
|
|
});
|
|
|
|
it('leaves a bare proper name untouched (no LLM to catch it)', async () => {
|
|
const r = await s.anonymize('Alain Jaccard a réussi');
|
|
// The name-hint flags it, but with no LLM the regex fallback finds no structured id.
|
|
expect(r.anon).toContain('Alain Jaccard');
|
|
expect(r.mapping).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe('configurable presets', () => {
|
|
it('generic preset anonymizes an IPv4 address', async () => {
|
|
const s = new Anonymizer({ patterns: presets.generic });
|
|
const r = await s.anonymize('Serveur 192.168.1.42 indisponible');
|
|
expect(r.anon).not.toContain('192.168.1.42');
|
|
expect(Object.values(r.mapping)).toContain('192.168.1.42');
|
|
});
|
|
|
|
it('accepts a fully custom pattern set', async () => {
|
|
const s = new Anonymizer({ patterns: [{ tag: 'TICKET', re: /\bJIRA-\d+\b/g }] });
|
|
const r = await s.anonymize('Voir JIRA-123');
|
|
expect(r.anon).toContain('[TICKET_1]');
|
|
expect(r.mapping['[TICKET_1]']).toBe('JIRA-123');
|
|
});
|
|
});
|
|
|
|
describe('legend', () => {
|
|
it('the regex fallback produces a French legend from tag meanings', async () => {
|
|
const s = new Anonymizer({ patterns: presets.swiss });
|
|
const r = await s.anonymize('Écris à jean@exemple.ch');
|
|
expect(r.legend).toEqual({ EMAIL: 'Adresse e-mail' });
|
|
});
|
|
|
|
it('backfills a missing legend entry from the built-in default (LLM omitted it)', async () => {
|
|
const anonymize = vi.fn().mockResolvedValue({
|
|
anon: 'Dossier de [PER_1.NOM:M]',
|
|
mapping: { '[PER_1.NOM:M]': 'Alain Jaccard' },
|
|
legend: {}, // model returned no legend
|
|
});
|
|
const s = new Anonymizer({ llm: failingLlm({ anonymize }), patterns: presets.swiss });
|
|
const r = await s.anonymize('Dossier de Alain Jaccard');
|
|
expect(r.legend).toEqual({ PER: 'Personne', NOM: 'Nom de famille', M: 'Masculin' });
|
|
});
|
|
|
|
it('a custom tag with a meaning surfaces it in the legend', async () => {
|
|
const s = new Anonymizer({
|
|
patterns: [{ tag: 'TICKET', re: /\bJIRA-\d+\b/g, meaning: 'Ticket de suivi' }],
|
|
});
|
|
const r = await s.anonymize('Voir JIRA-123');
|
|
expect(r.legend).toEqual({ TICKET: 'Ticket de suivi' });
|
|
});
|
|
});
|
|
|
|
describe('fail-closed (optional fallback)', () => {
|
|
it('throws if neither an LLM nor patterns are provided', () => {
|
|
expect(() => new Anonymizer({})).toThrowError(AnonymizationError);
|
|
});
|
|
|
|
it('LLM-only: a provider failure throws AnonymizationError with the cause', async () => {
|
|
const cause = new Error('LLM_HTTP_500');
|
|
const s = new Anonymizer({ llm: failingLlm({ anonymize: vi.fn().mockRejectedValue(cause) }) });
|
|
await expect(s.anonymize('Contact: jean@exemple.ch')).rejects.toBeInstanceOf(AnonymizationError);
|
|
await expect(s.anonymize('Contact: jean@exemple.ch')).rejects.toMatchObject({ cause });
|
|
});
|
|
|
|
it('LLM-only: bypasses the pre-filter so PII-free text still hits the provider', async () => {
|
|
const anonymize = vi.fn().mockResolvedValue({ anon: 'INNER JOIN', mapping: {}, legend: {} });
|
|
const s = new Anonymizer({ llm: failingLlm({ anonymize }) });
|
|
await s.anonymize('Explique INNER JOIN'); // no structured PII, no name hint
|
|
expect(anonymize).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('LLM-only: anonymizeChunks rejects with AnonymizationError on provider failure', async () => {
|
|
const s = new Anonymizer({ llm: failingLlm() }); // anonymizeBatch rejects
|
|
await expect(s.anonymizeChunks(['Bob Martin a signé.'], { mapping: {} })).rejects.toBeInstanceOf(
|
|
AnonymizationError,
|
|
);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('strict anti-leak mode', () => {
|
|
// A model that redacts the FIRST mention of a value but leaves a second in clear.
|
|
const leakyLlm = () =>
|
|
failingLlm({
|
|
anonymize: vi.fn().mockResolvedValue({
|
|
anon: 'Dossier de [PER_1.NOM:M] ; contactez Alain Jaccard directement.',
|
|
mapping: { '[PER_1.NOM:M]': 'Alain Jaccard' },
|
|
legend: {},
|
|
}),
|
|
});
|
|
|
|
it('throws AnonymizationError when a mapped value still appears in clear', async () => {
|
|
const s = new Anonymizer({ llm: leakyLlm(), strict: true });
|
|
await expect(s.anonymize('Dossier de Alain Jaccard')).rejects.toBeInstanceOf(AnonymizationError);
|
|
});
|
|
|
|
it('the leak error names the placeholder key, never the secret value', async () => {
|
|
const s = new Anonymizer({ llm: leakyLlm(), strict: true });
|
|
await expect(s.anonymize('Dossier de Alain Jaccard')).rejects.toMatchObject({
|
|
message: expect.stringContaining('[PER_1.NOM:M]'),
|
|
});
|
|
await expect(s.anonymize('Dossier de Alain Jaccard')).rejects.not.toMatchObject({
|
|
message: expect.stringContaining('Alain Jaccard'),
|
|
});
|
|
});
|
|
|
|
it('fails closed even with a fallback configured (does not silently degrade to regex)', async () => {
|
|
const s = new Anonymizer({ llm: leakyLlm(), patterns: presets.swiss, strict: true });
|
|
await expect(s.anonymize('Dossier de Alain Jaccard')).rejects.toBeInstanceOf(AnonymizationError);
|
|
});
|
|
|
|
it('with strict:false the leaky result is returned as-is', async () => {
|
|
const s = new Anonymizer({ llm: leakyLlm(), strict: false });
|
|
const r = await s.anonymize('Dossier de Alain Jaccard');
|
|
expect(r.anon).toContain('Alain Jaccard'); // leak check disabled → passes through
|
|
});
|
|
|
|
it('is enabled by default (no strict option → still throws on a leak)', async () => {
|
|
const s = new Anonymizer({ llm: leakyLlm() });
|
|
await expect(s.anonymize('Dossier de Alain Jaccard')).rejects.toBeInstanceOf(AnonymizationError);
|
|
});
|
|
|
|
it('does NOT flag a value that only appears inside a placeholder context (e.g. B+)', async () => {
|
|
const anonymize = vi.fn().mockResolvedValue({
|
|
anon: 'Groupe sanguin [PER_1.SANG:B+] confirmé.',
|
|
mapping: { '[PER_1.SANG:B+]': 'B+' },
|
|
legend: {},
|
|
});
|
|
const s = new Anonymizer({ llm: failingLlm({ anonymize }), strict: true });
|
|
const r = await s.anonymize('Groupe sanguin B+ confirmé.');
|
|
expect(r.anon).toBe('Groupe sanguin [PER_1.SANG:B+] confirmé.');
|
|
});
|
|
|
|
it('does NOT flag a value that is only a substring of a larger token', async () => {
|
|
// "Ann" is redacted; "Anna" (a different token) is left in clear → not a leak.
|
|
const anonymize = vi.fn().mockResolvedValue({
|
|
anon: '[PER_1.PRENOM:F] connaît Anna.',
|
|
mapping: { '[PER_1.PRENOM:F]': 'Ann' },
|
|
legend: {},
|
|
});
|
|
const s = new Anonymizer({ llm: failingLlm({ anonymize }), strict: true });
|
|
const r = await s.anonymize('Ann connaît Anna.');
|
|
expect(r.anon).toBe('[PER_1.PRENOM:F] connaît Anna.');
|
|
});
|
|
|
|
it('regex-only output never trips the check (every occurrence is replaced)', async () => {
|
|
const s = new Anonymizer({ patterns: presets.swiss, strict: true });
|
|
const r = await s.anonymize('Écris à jean@exemple.ch puis à jean@exemple.ch');
|
|
expect(r.anon).not.toContain('jean@exemple.ch');
|
|
});
|
|
});
|
|
|
|
describe('prefilter decoupling', () => {
|
|
it('prefilter:false consults the LLM even for PII-free text when a fallback exists', async () => {
|
|
const anonymize = vi.fn().mockResolvedValue({ anon: 'INNER JOIN', mapping: {}, legend: {} });
|
|
const s = new Anonymizer({ llm: failingLlm({ anonymize }), patterns: presets.swiss, prefilter: false });
|
|
await s.anonymize('Explique INNER JOIN'); // no structured PII, no name hint
|
|
expect(anonymize).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('prefilter default (true) still skips the LLM for PII-free text', async () => {
|
|
const anonymize = vi.fn();
|
|
const s = new Anonymizer({ llm: failingLlm({ anonymize }), patterns: presets.swiss });
|
|
await s.anonymize('Explique INNER JOIN');
|
|
expect(anonymize).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('boundary-aware applyKnown', () => {
|
|
it('reuses a known value only as a whole token, not inside a larger word', async () => {
|
|
const s = new Anonymizer({ llm: failingLlm(), patterns: presets.swiss });
|
|
const seed = { '[PER_1.PRENOM:F]': 'Ann' };
|
|
// "Anna" must stay intact; the old substring replace would mangle it.
|
|
const kept = await s.anonymizeChunks(['Anna arrive demain.'], { mapping: seed });
|
|
expect(kept.anon[0]).toBe('Anna arrive demain.');
|
|
// but a standalone "Ann" is reused deterministically (no LLM call).
|
|
const reused = await s.anonymizeChunks(['Ann arrive demain.'], { mapping: seed });
|
|
expect(reused.anon[0]).toBe('[PER_1.PRENOM:F] arrive demain.');
|
|
});
|
|
});
|
|
|
|
describe('multi-turn conversation (anonymizeTurn)', () => {
|
|
it('keeps stable ids across turns and accumulates the session', async () => {
|
|
const anonymizeInConversation = vi
|
|
.fn()
|
|
.mockResolvedValueOnce({
|
|
anon: 'Je suis [PER_1.NOM:F]',
|
|
mapping: { '[PER_1.NOM:F]': 'Nora Steiner' },
|
|
legend: { PER: 'Personne', NOM: 'Nom de famille', F: 'Féminin' },
|
|
})
|
|
.mockResolvedValueOnce({
|
|
anon: 'Ma collègue [PER_2.NOM:F] a aussi un souci',
|
|
mapping: { '[PER_2.NOM:F]': 'Yanis Berger' },
|
|
legend: { PER: 'Personne', NOM: 'Nom de famille', F: 'Féminin' },
|
|
});
|
|
const llm = failingLlm({ anonymizeInConversation });
|
|
const a = new Anonymizer({ llm });
|
|
|
|
const t1 = await a.anonymizeTurn('Je suis Nora Steiner');
|
|
expect(t1.mapping).toEqual({ '[PER_1.NOM:F]': 'Nora Steiner' });
|
|
|
|
const t2 = await a.anonymizeTurn('Ma collègue Yanis Berger a aussi un souci', t1.session);
|
|
expect(t2.mapping['[PER_1.NOM:F]']).toBe('Nora Steiner'); // stable across turns
|
|
expect(t2.mapping['[PER_2.NOM:F]']).toBe('Yanis Berger'); // new person, no collision
|
|
expect(t2.session.history).toHaveLength(2);
|
|
// the second call was told PER_1 is already used
|
|
expect(anonymizeInConversation.mock.calls[1][1].usedIds).toContain('[PER_1.NOM:F]');
|
|
});
|
|
|
|
it('conversation() wrapper threads the session and de-anonymizes', async () => {
|
|
const anonymizeInConversation = vi
|
|
.fn()
|
|
.mockResolvedValueOnce({
|
|
anon: '[PER_1.PRENOM:M]',
|
|
mapping: { '[PER_1.PRENOM:M]': 'Idris' },
|
|
legend: {},
|
|
})
|
|
.mockResolvedValueOnce({
|
|
anon: '[PER_1.PRENOM:M] et [PER_2.PRENOM:F]',
|
|
mapping: { '[PER_2.PRENOM:F]': 'Lina' },
|
|
legend: {},
|
|
});
|
|
const conv = new Anonymizer({ llm: failingLlm({ anonymizeInConversation }) }).conversation();
|
|
await conv.anonymize('Idris');
|
|
await conv.anonymize('Idris et Lina');
|
|
expect(conv.session().mapping).toEqual({ '[PER_1.PRENOM:M]': 'Idris', '[PER_2.PRENOM:F]': 'Lina' });
|
|
expect(conv.deanonymize('[PER_1.PRENOM:M]')).toBe('Idris');
|
|
});
|
|
|
|
it('falls back to the batch path when the provider has no anonymizeInConversation', async () => {
|
|
const anonymizeBatch = vi.fn().mockResolvedValue({
|
|
segments: ['[PER_1.NOM:M]'],
|
|
mapping: { '[PER_1.NOM:M]': 'Bruno Keller' },
|
|
legend: {},
|
|
});
|
|
const a = new Anonymizer({ llm: failingLlm({ anonymizeBatch }) });
|
|
const t = await a.anonymizeTurn('Bruno Keller');
|
|
expect(anonymizeBatch).toHaveBeenCalledWith(['Bruno Keller'], []);
|
|
expect(t.mapping['[PER_1.NOM:M]']).toBe('Bruno Keller');
|
|
});
|
|
|
|
it('realistic e-learning chat: stable coreference + attribute attribution + round-trips', async () => {
|
|
const turns = [
|
|
{
|
|
text: "Bonjour, je suis Nora Steiner, inscrite à la formation « Assistante médicale ». Je n'ai pas reçu ma convocation.",
|
|
anon: "Bonjour, je suis [PER_1.PRENOM:F] [PER_1.NOM:F], inscrite à la formation « Assistante médicale ». Je n'ai pas reçu ma convocation.",
|
|
mapping: { '[PER_1.PRENOM:F]': 'Nora', '[PER_1.NOM:F]': 'Steiner' },
|
|
},
|
|
{
|
|
text: 'Mon e-mail est nora.steiner@hotmail.ch et mon numéro AVS 756.2233.4455.66 au cas où.',
|
|
anon: 'Mon e-mail est [PER_1.EMAIL:F] et mon numéro AVS [PER_1.AVS:F] au cas où.',
|
|
mapping: { '[PER_1.EMAIL:F]': 'nora.steiner@hotmail.ch', '[PER_1.AVS:F]': '756.2233.4455.66' },
|
|
},
|
|
{
|
|
text: "En fait c'est aussi pour ma collègue Yanis Berger — elle veut s'inscrire, son tél. 078 111 22 33.",
|
|
anon: "En fait c'est aussi pour ma collègue [PER_2.PRENOM:F] [PER_2.NOM:F] — elle veut s'inscrire, son tél. [PER_2.TELEPHONE:F].",
|
|
mapping: {
|
|
'[PER_2.PRENOM:F]': 'Yanis',
|
|
'[PER_2.NOM:F]': 'Berger',
|
|
'[PER_2.TELEPHONE:F]': '078 111 22 33',
|
|
},
|
|
},
|
|
{
|
|
text: 'Le paiement se fera depuis mon IBAN CH88 0900 0000 1234 5678 9. Merci !',
|
|
anon: 'Le paiement se fera depuis mon IBAN [PER_1.IBAN:F]. Merci !',
|
|
mapping: { '[PER_1.IBAN:F]': 'CH88 0900 0000 1234 5678 9' },
|
|
},
|
|
];
|
|
const anonymizeInConversation = vi.fn();
|
|
for (const t of turns) {
|
|
anonymizeInConversation.mockResolvedValueOnce({ anon: t.anon, mapping: t.mapping, legend: {} });
|
|
}
|
|
const a = new Anonymizer({ llm: failingLlm({ anonymizeInConversation }) });
|
|
|
|
let session: AnonymizerSession = { mapping: {}, legend: {}, history: [] };
|
|
for (const t of turns) {
|
|
const r = await a.anonymizeTurn(t.text, session);
|
|
session = r.session;
|
|
// each turn restores to the exact original
|
|
expect(a.deanonymize(r.anon, r.mapping)).toBe(t.text);
|
|
}
|
|
// Nora = PER_1 throughout (name + email + AVS + IBAN attached to her); Yanis = PER_2.
|
|
expect(session.mapping['[PER_1.PRENOM:F]']).toBe('Nora');
|
|
expect(session.mapping['[PER_1.EMAIL:F]']).toBe('nora.steiner@hotmail.ch');
|
|
expect(session.mapping['[PER_1.IBAN:F]']).toBe('CH88 0900 0000 1234 5678 9');
|
|
expect(session.mapping['[PER_2.PRENOM:F]']).toBe('Yanis');
|
|
expect(session.mapping['[PER_2.TELEPHONE:F]']).toBe('078 111 22 33');
|
|
expect(session.history).toHaveLength(4);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Five multi-turn conversation examples (fictional Swiss e-learning chats), each
|
|
* driven by a deterministic scripted provider. Every turn must restore exactly,
|
|
* and entity ids must stay stable across the whole conversation.
|
|
*/
|
|
describe('conversation examples (multi-turn scenarios)', () => {
|
|
type Turn = { text: string; anon: string; mapping: Record<string, string> };
|
|
|
|
async function runConversation(turns: Turn[]): Promise<AnonymizerSession> {
|
|
const anonymizeInConversation = vi.fn();
|
|
for (const t of turns) {
|
|
anonymizeInConversation.mockResolvedValueOnce({ anon: t.anon, mapping: t.mapping, legend: {} });
|
|
}
|
|
const a = new Anonymizer({ llm: failingLlm({ anonymizeInConversation }) });
|
|
let session: AnonymizerSession = { mapping: {}, legend: {}, history: [] };
|
|
for (const t of turns) {
|
|
const r = await a.anonymizeTurn(t.text, session);
|
|
session = r.session;
|
|
expect(a.deanonymize(r.anon, r.mapping)).toBe(t.text); // exact round-trip, every turn
|
|
}
|
|
expect(session.history).toHaveLength(turns.length);
|
|
return session;
|
|
}
|
|
|
|
it('1 — login support: attributes attach to the same learner (PER_1) across turns', async () => {
|
|
const s = await runConversation([
|
|
{
|
|
text: "Bonjour, je suis Élodie Vogt et je n'arrive plus à me connecter.",
|
|
anon: "Bonjour, je suis [PER_1.PRENOM:F] [PER_1.NOM:F] et je n'arrive plus à me connecter.",
|
|
mapping: { '[PER_1.PRENOM:F]': 'Élodie', '[PER_1.NOM:F]': 'Vogt' },
|
|
},
|
|
{
|
|
text: 'Mon e-mail de compte est elodie.vogt@gmx.ch.',
|
|
anon: 'Mon e-mail de compte est [PER_1.EMAIL:Perso].',
|
|
mapping: { '[PER_1.EMAIL:Perso]': 'elodie.vogt@gmx.ch' },
|
|
},
|
|
{
|
|
text: 'Merci de réinitialiser mon mot de passe pour la formation « Comptabilité ».',
|
|
anon: 'Merci de réinitialiser mon mot de passe pour la formation « Comptabilité ».',
|
|
mapping: {},
|
|
},
|
|
]);
|
|
expect(s.mapping['[PER_1.PRENOM:F]']).toBe('Élodie');
|
|
expect(s.mapping['[PER_1.EMAIL:Perso]']).toBe('elodie.vogt@gmx.ch');
|
|
expect(Object.keys(s.mapping).some((k) => k.startsWith('[PER_2'))).toBe(false); // only one person
|
|
});
|
|
|
|
it('2 — two learners: a friend introduced then re-mentioned keeps PER_2 (applyKnown reuse)', async () => {
|
|
const s = await runConversation([
|
|
{
|
|
text: "Salut, c'est Idris Haldimann. Je veux m'inscrire à la formation « Développement web ».",
|
|
anon: "Salut, c'est [PER_1.PRENOM:M] [PER_1.NOM:M]. Je veux m'inscrire à la formation « Développement web ».",
|
|
mapping: { '[PER_1.PRENOM:M]': 'Idris', '[PER_1.NOM:M]': 'Haldimann' },
|
|
},
|
|
{
|
|
text: 'Mon amie Léa Bianchi aimerait aussi participer.',
|
|
anon: 'Mon amie [PER_2.PRENOM:F] [PER_2.NOM:F] aimerait aussi participer.',
|
|
mapping: { '[PER_2.PRENOM:F]': 'Léa', '[PER_2.NOM:F]': 'Bianchi' },
|
|
},
|
|
{
|
|
// "Léa" is re-mentioned literally → applyKnown reuses [PER_2.PRENOM:F] before the model.
|
|
text: "Léa a déjà un compte sous l'e-mail lea.bianchi@bluewin.ch.",
|
|
anon: "[PER_2.PRENOM:F] a déjà un compte sous l'e-mail [PER_2.EMAIL:Perso].",
|
|
mapping: { '[PER_2.EMAIL:Perso]': 'lea.bianchi@bluewin.ch' },
|
|
},
|
|
]);
|
|
expect(s.mapping['[PER_1.PRENOM:M]']).toBe('Idris');
|
|
expect(s.mapping['[PER_2.PRENOM:F]']).toBe('Léa'); // stable across turns 2 & 3
|
|
expect(s.mapping['[PER_2.EMAIL:Perso]']).toBe('lea.bianchi@bluewin.ch');
|
|
});
|
|
|
|
it('3 — billing: old vs new IBAN kept distinct under one person', async () => {
|
|
const s = await runConversation([
|
|
{
|
|
text: "Bonjour, ici Bruno Keller, j'ai un souci de facturation.",
|
|
anon: "Bonjour, ici [PER_1.PRENOM:M] [PER_1.NOM:M], j'ai un souci de facturation.",
|
|
mapping: { '[PER_1.PRENOM:M]': 'Bruno', '[PER_1.NOM:M]': 'Keller' },
|
|
},
|
|
{
|
|
text: "Mon ancien IBAN CH51 0483 5012 3456 7800 9 n'est plus valide.",
|
|
anon: "Mon ancien IBAN [PER_1.IBAN:Ancien] n'est plus valide.",
|
|
mapping: { '[PER_1.IBAN:Ancien]': 'CH51 0483 5012 3456 7800 9' },
|
|
},
|
|
{
|
|
text: 'Le nouveau est CH33 0900 0000 8765 4321 0, merci.',
|
|
anon: 'Le nouveau est [PER_1.IBAN:Nouveau], merci.',
|
|
mapping: { '[PER_1.IBAN:Nouveau]': 'CH33 0900 0000 8765 4321 0' },
|
|
},
|
|
]);
|
|
expect(s.mapping['[PER_1.IBAN:Ancien]']).toBe('CH51 0483 5012 3456 7800 9');
|
|
expect(s.mapping['[PER_1.IBAN:Nouveau]']).toBe('CH33 0900 0000 8765 4321 0');
|
|
});
|
|
|
|
it('4 — parent + minor child + doctor: three distinct people across turns', async () => {
|
|
const s = await runConversation([
|
|
{
|
|
text: "Bonjour, je souhaite inscrire mon fils Timéo Moret, né le 09.09.2012, à l'atelier « Robotique ».",
|
|
anon: "Bonjour, je souhaite inscrire mon fils [PER_1.PRENOM:M] [PER_1.NOM:M], né le [PER_1.DATE_NAISSANCE:2012], à l'atelier « Robotique ».",
|
|
mapping: {
|
|
'[PER_1.PRENOM:M]': 'Timéo',
|
|
'[PER_1.NOM:M]': 'Moret',
|
|
'[PER_1.DATE_NAISSANCE:2012]': '09.09.2012',
|
|
},
|
|
},
|
|
{
|
|
text: "C'est moi sa mère, Sandra Baumann, qui gère le dossier ; mon numéro est 079 222 33 44.",
|
|
anon: "C'est moi sa mère, [PER_2.PRENOM:F] [PER_2.NOM:F], qui gère le dossier ; mon numéro est [PER_2.TELEPHONE:Mobile].",
|
|
mapping: {
|
|
'[PER_2.PRENOM:F]': 'Sandra',
|
|
'[PER_2.NOM:F]': 'Baumann',
|
|
'[PER_2.TELEPHONE:Mobile]': '079 222 33 44',
|
|
},
|
|
},
|
|
{
|
|
text: 'Le certificat médical a été établi par le Dr Rui Almeida.',
|
|
anon: 'Le certificat médical a été établi par le Dr [PER_3.PRENOM:M] [PER_3.NOM:M].',
|
|
mapping: { '[PER_3.PRENOM:M]': 'Rui', '[PER_3.NOM:M]': 'Almeida' },
|
|
},
|
|
]);
|
|
expect(s.mapping['[PER_1.NOM:M]']).toBe('Moret'); // child
|
|
expect(s.mapping['[PER_2.NOM:F]']).toBe('Baumann'); // mother
|
|
expect(s.mapping['[PER_3.NOM:M]']).toBe('Almeida'); // doctor
|
|
});
|
|
|
|
it('5 — HR: manager + org reused, two employees added across turns', async () => {
|
|
const s = await runConversation([
|
|
{
|
|
text: 'Bonjour, je suis Patrick Nussbaum, RH chez Migros.',
|
|
anon: 'Bonjour, je suis [PER_1.PRENOM:M] [PER_1.NOM:M], RH chez [ORG_1.NOM:Entreprise].',
|
|
mapping: {
|
|
'[PER_1.PRENOM:M]': 'Patrick',
|
|
'[PER_1.NOM:M]': 'Nussbaum',
|
|
'[ORG_1.NOM:Entreprise]': 'Migros',
|
|
},
|
|
},
|
|
{
|
|
text: 'Je veux inscrire Chloé Aebischer (chloe.aebischer@migros.ch) à « Sécurité ».',
|
|
anon: 'Je veux inscrire [PER_2.PRENOM:F] [PER_2.NOM:F] ([PER_2.EMAIL:Pro]) à « Sécurité ».',
|
|
mapping: {
|
|
'[PER_2.PRENOM:F]': 'Chloé',
|
|
'[PER_2.NOM:F]': 'Aebischer',
|
|
'[PER_2.EMAIL:Pro]': 'chloe.aebischer@migros.ch',
|
|
},
|
|
},
|
|
{
|
|
text: 'Ainsi que Deniz Yilmaz, joignable au 076 555 88 99.',
|
|
anon: 'Ainsi que [PER_3.PRENOM:U] [PER_3.NOM:U], joignable au [PER_3.TELEPHONE:Mobile].',
|
|
mapping: {
|
|
'[PER_3.PRENOM:U]': 'Deniz',
|
|
'[PER_3.NOM:U]': 'Yilmaz',
|
|
'[PER_3.TELEPHONE:Mobile]': '076 555 88 99',
|
|
},
|
|
},
|
|
{
|
|
text: 'La facture va à notre IBAN CH70 0076 2011 6238 5295 7.',
|
|
anon: 'La facture va à notre IBAN [ORG_1.IBAN:Entreprise].',
|
|
mapping: { '[ORG_1.IBAN:Entreprise]': 'CH70 0076 2011 6238 5295 7' },
|
|
},
|
|
]);
|
|
expect(s.mapping['[ORG_1.NOM:Entreprise]']).toBe('Migros'); // org stable across turns 1 & 4
|
|
expect(s.mapping['[PER_2.PRENOM:F]']).toBe('Chloé');
|
|
expect(s.mapping['[PER_3.NOM:U]']).toBe('Yilmaz');
|
|
expect(s.mapping['[ORG_1.IBAN:Entreprise]']).toBe('CH70 0076 2011 6238 5295 7');
|
|
});
|
|
});
|