BREAKING CHANGE: regex fallback is now opt-in (patterns no longer defaults
to presets.swiss). With no fallback an LLM failure throws AnonymizationError
(fail-closed) and the pre-filter is bypassed; at least one of llm/patterns is
required. AnonymizationResult gains a required `legend`; anonymizeChunks seed
is now { mapping, legend? } and returns legend.
- prompt: model may coin new UPPERCASE abbreviations and returns a 'legende'
explaining every abbreviation used (French); backfilled by DEFAULT_LEGEND
- PatternDef.meaning surfaces in the legend; swiss/generic presets get meanings
- AnonymizationError (exported) wraps the cause on fail-closed
- README: drop the chatbot provenance line; add 'How it works' + nLPD sections
- 34 tests / 99% coverage; bump to 0.2.0
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
129 lines
5.1 KiB
TypeScript
129 lines
5.1 KiB
TypeScript
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
import { openAICompatibleProvider } from '../src/index.js';
|
|
|
|
const opts = { baseUrl: 'https://api.example.com/v1', apiKey: 'k', model: 'm' };
|
|
|
|
function mockFetchJson(content: unknown) {
|
|
return vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ choices: [{ message: { content: JSON.stringify(content) } }] }),
|
|
});
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('openAICompatibleProvider', () => {
|
|
it('isConfigured() reflects whether baseUrl/apiKey/model are present', () => {
|
|
expect(openAICompatibleProvider(opts).isConfigured()).toBe(true);
|
|
expect(openAICompatibleProvider({ ...opts, apiKey: '' }).isConfigured()).toBe(false);
|
|
});
|
|
|
|
it('anonymize() parses {texte_anonymise, mapping} and posts temperature 0 + json_object', async () => {
|
|
const fetchMock = mockFetchJson({ texte_anonymise: '[EMAIL_1]', mapping: { '[EMAIL_1]': 'a@b.ch' } });
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const r = await openAICompatibleProvider(opts).anonymize('a@b.ch');
|
|
expect(r).toEqual({ anon: '[EMAIL_1]', mapping: { '[EMAIL_1]': 'a@b.ch' }, legend: {} });
|
|
|
|
const [url, init] = fetchMock.mock.calls[0];
|
|
expect(url).toBe('https://api.example.com/v1/chat/completions');
|
|
const body = JSON.parse(init.body);
|
|
expect(body.temperature).toBe(0);
|
|
expect(body.response_format).toEqual({ type: 'json_object' });
|
|
expect(init.headers.Authorization).toBe('Bearer k');
|
|
});
|
|
|
|
it('parses the model legende into result.legend, keeping only string values', async () => {
|
|
const fetchMock = mockFetchJson({
|
|
texte_anonymise: '[PER_1.NOM:M]',
|
|
mapping: { '[PER_1.NOM:M]': 'Alain Jaccard' },
|
|
legende: { PER: 'Personne', NOM: 'Nom de famille', M: 'Masculin', BAD: 42 },
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const r = await openAICompatibleProvider(opts).anonymize('Alain Jaccard');
|
|
expect(r.legend).toEqual({ PER: 'Personne', NOM: 'Nom de famille', M: 'Masculin' });
|
|
});
|
|
|
|
it('anonymizeBatch() returns {segments, mapping} and includes used ids in the prompt', async () => {
|
|
const fetchMock = mockFetchJson({ segments: ['[PER_2.NOM:M]'], mapping: { '[PER_2.NOM:M]': 'Bob' } });
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const r = await openAICompatibleProvider(opts).anonymizeBatch(['Bob'], ['[PER_1.NOM:M]']);
|
|
expect(r.segments).toEqual(['[PER_2.NOM:M]']);
|
|
|
|
const system = JSON.parse(fetchMock.mock.calls[0][1].body).messages[0].content;
|
|
expect(system).toContain('[PER_1.NOM:M]');
|
|
});
|
|
|
|
it('throws on a non-OK HTTP status (no retry when retries: 0)', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({ ok: false, status: 500 });
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
await expect(openAICompatibleProvider({ ...opts, retries: 0 }).anonymize('x')).rejects.toThrow(
|
|
'LLM_HTTP_500',
|
|
);
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('throws on a malformed response shape', async () => {
|
|
vi.stubGlobal('fetch', mockFetchJson({ wrong: true }));
|
|
await expect(openAICompatibleProvider({ ...opts, retries: 0 }).anonymize('x')).rejects.toThrow(
|
|
'LLM_BAD_SHAPE',
|
|
);
|
|
});
|
|
|
|
describe('retry/backoff', () => {
|
|
const retryOpts = { ...opts, retries: 1, retryDelayMs: 0 };
|
|
|
|
it('retries once on a transient 500 then succeeds (2 calls)', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValueOnce({ ok: false, status: 500 })
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
json: async () => ({
|
|
choices: [{ message: { content: JSON.stringify({ texte_anonymise: 'ok', mapping: {} }) } }],
|
|
}),
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const r = await openAICompatibleProvider(retryOpts).anonymize('x');
|
|
expect(r.anon).toBe('ok');
|
|
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it('retries on a network error then succeeds', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockRejectedValueOnce(new Error('ECONNRESET'))
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
json: async () => ({
|
|
choices: [{ message: { content: JSON.stringify({ texte_anonymise: 'ok', mapping: {} }) } }],
|
|
}),
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const r = await openAICompatibleProvider(retryOpts).anonymize('x');
|
|
expect(r.anon).toBe('ok');
|
|
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it('does NOT retry a 400 (non-transient)', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({ ok: false, status: 400 });
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
await expect(openAICompatibleProvider(retryOpts).anonymize('x')).rejects.toThrow('LLM_HTTP_400');
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('gives up after exhausting retries on persistent 503', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({ ok: false, status: 503 });
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
await expect(openAICompatibleProvider(retryOpts).anonymize('x')).rejects.toThrow('LLM_HTTP_503');
|
|
expect(fetchMock).toHaveBeenCalledTimes(2); // initial + 1 retry
|
|
});
|
|
});
|
|
});
|