Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
buvard
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PLN
buvard
Commits
19f0fc05
Commit
19f0fc05
authored
Nov 11, 2026
by
PLN (Algolia)
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests: the fix engine against a mock local endpoint — prompt, guards, cap, dry run, clean failure
parent
acf636ed
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
199 additions
and
0 deletions
+199
-0
fix.test.mjs
test/fix.test.mjs
+199
-0
No files found.
test/fix.test.mjs
0 → 100644
View file @
19f0fc05
/**
* Tests du moteur de fix — aucun réseau : le point d'entrée est un serveur
* http local monté dans le processus, à port éphémère.
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import { spawn } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { lint, bodyOf, buildPrompt, parseSuggestions, dataLost, fixText, FixError } from '../lib/index.mjs';
const CLI = fileURLToPath(new URL('../bin/buvard.js', import.meta.url));
const SLOPPY = 'Soyons honnêtes : le montage cassait. Il est essentiel de noter que le débit tombait à 5 Go/s.';
/** Un faux LM Studio : POST unique, réponse JSON, port éphémère sur 127.0.0.1. */
function mockServer(handler) {
return new Promise((resolve) => {
const server = http.createServer((req, res) => {
let raw = '';
req.on('data', (c) => (raw += c));
req.on('end', () => handler(JSON.parse(raw), res));
});
server.listen(0, '127.0.0.1', () =>
resolve({ server, url: `http://127.0.0.1:${server.address().port}/v1` }),
);
});
}
const reply = (res, content) => {
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ choices: [{ message: { content } }] }));
};
const jsonOf = (suggestions) => JSON.stringify(suggestions);
/** Le CLI en sous-processus — spawn asynchrone : spawnSync gèlerait la boucle
* d'événements du parent, et le serveur mock ne pourrait jamais répondre. */
const runCli = (args, env) =>
new Promise((resolve) => {
const p = spawn(process.execPath, [CLI, ...args], { env, stdio: ['ignore', 'pipe', 'pipe'] });
let stdout = '';
let stderr = '';
p.stdout.on('data', (c) => (stdout += c));
p.stderr.on('data', (c) => (stderr += c));
p.on('close', (status) => resolve({ status, stdout, stderr }));
});
test('the prompt carries the rules, their rationale and the voice clause', () => {
const p = buildPrompt(SLOPPY, lint(bodyOf(SLOPPY)));
assert.match(p, /ouverture-vide/);
assert.match(p, /annonce sans contenu/); // la raison, minée du catalogue
assert.match(p, /impératif impersonnel/);
assert.match(p, /jamais des verdicts/); // la règle de voix, mot pour mot
assert.match(p, /voix de l'auteur/);
assert.match(p, /KEEP/);
assert.match(p, /Soyons honnêtes/); // l'extrait signalé
});
test('parseSuggestions strips fences and surrounding chatter', () => {
const s = parseSuggestions('Voici mes propositions :\n```json\n[{"excerpt":"a","suggestion":"KEEP","rule":"r","why":"w"}]\n```\nCordialement.');
assert.equal(s.length, 1);
assert.equal(s[0].suggestion, 'KEEP');
});
test('dataLost catches altered numbers and names', () => {
assert.equal(dataLost('tombait à 5 Go/s', 'tombait à 15 Go/s'), true);
assert.equal(dataLost('à 5 Go/s', 'à 12 Go/s'), true);
assert.equal(dataLost('le montage de Paul-Louis', 'le montage de Paul'), true);
assert.equal(dataLost('chez Paul-Louis', 'chez Pauline'), true);
assert.equal(dataLost('à 5 Go/s', 'environ 5 Go/s'), false); // reformuler sans toucher aux données
assert.equal(dataLost('un jour de pluie', 'une journée pluvieuse'), false);
});
test('a suggestion that alters a number is dropped, text untouched', async () => {
const { server, url } = await mockServer((body, res) =>
reply(res, jsonOf([{ excerpt: 'tombait à 5 Go/s', suggestion: 'tombait à 15 Go/s', rule: 'anti-copule', why: 'x' }])),
);
const r = await fixText(SLOPPY, { url, maxRounds: 1 });
server.close();
assert.equal(r.changed, false);
const s = r.rounds[0].suggestions[0];
assert.equal(s.status, 'abandonnée');
assert.match(s.reason, /chiffre/);
});
test('a suggestion that swaps one slop pattern for another gains nothing', async () => {
const { server, url } = await mockServer((body, res) =>
reply(res, jsonOf([{ excerpt: 'Soyons honnêtes :', suggestion: 'Il faut le dire :', rule: 'ouverture-vide', why: 'x' }])),
);
const r = await fixText(SLOPPY, { url, maxRounds: 1 });
server.close();
const s = r.rounds[0].suggestions[0];
assert.equal(s.status, 'abandonnée');
assert.match(s.reason, /sans effet/);
});
test('a good suggestion is applied and the score moves', async () => {
const { server, url } = await mockServer((body, res) =>
reply(res, jsonOf([{ excerpt: 'Soyons honnêtes : ', suggestion: '', rule: 'ouverture-vide', why: 'ouverture à vide' }])),
);
const r = await fixText(SLOPPY, { url, maxRounds: 1 });
server.close();
assert.equal(r.rounds[0].suggestions[0].status, 'appliquée');
assert.ok(r.after > r.before);
assert.equal(r.changed, true);
assert.equal(lint(bodyOf(r.text)).hits.some((h) => h.id === 'ouverture-vide'), false);
});
test('the loop caps at three rounds', async () => {
// Quatre flags indépendants ; le mock n'en répare qu'un par round — un
// quatrième round serait nécessaire, le plafond doit l'empêcher.
const text = 'Soyons honnêtes : un. Il est essentiel de noter deux. Au final, trois. En résumé, quatre.';
const fixes = [
[{ excerpt: 'Soyons honnêtes : ', suggestion: '', rule: 'ouverture-vide', why: 'x' }],
[{ excerpt: 'Il est essentiel de noter ', suggestion: '', rule: 'impératif impersonnel', why: 'x' }],
[{ excerpt: 'Au final, ', suggestion: '', rule: 'conclusion-bilan', why: 'x' }],
];
let calls = 0;
const { server, url } = await mockServer((body, res) => reply(res, jsonOf(fixes[calls++] ?? [])));
const r = await fixText(text, { url });
server.close();
assert.equal(calls, 3);
assert.equal(r.rounds.length, 3);
});
test('KEEP passes through, the text stays as written', async () => {
const { server, url } = await mockServer((body, res) =>
reply(res, jsonOf([{ excerpt: 'Soyons honnêtes :', suggestion: 'KEEP', rule: 'ouverture-vide', why: 'la voix l\'assume' }])),
);
const r = await fixText(SLOPPY, { url, maxRounds: 1 });
server.close();
assert.equal(r.rounds[0].suggestions[0].status, 'KEEP');
assert.equal(r.changed, false);
});
test('clean text never calls the endpoint', async () => {
// Port 9 : rien n'écoute — si l'endpoint était appelé, le test jetterait.
const r = await fixText('Le montage marche. Le débit tient.', { url: 'http://127.0.0.1:9/v1' });
assert.deepEqual([r.before, r.after, r.rounds.length, r.changed], [100, 100, 0, false]);
});
test('a down endpoint fails clean, with the hint', async () => {
const { server, url } = await mockServer(() => {});
server.close(); // le port redevient libre, plus personne n'écoute
await assert.rejects(
() => fixText(SLOPPY, { url, maxRounds: 1, timeoutMs: 2000 }),
(e) => {
assert.ok(e instanceof FixError);
assert.match(e.message, /LM Studio|BUVARD_FIX_URL/);
return true;
},
);
});
test('the CLI is a dry run by default and writes only with --write', async () => {
const { server, url } = await mockServer((body, res) =>
reply(res, jsonOf([{ excerpt: 'Soyons honnêtes : ', suggestion: '', rule: 'ouverture-vide', why: 'ouverture à vide' }])),
);
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'buvard-fix-'));
const file = path.join(dir, 'article.md');
fs.writeFileSync(file, `${SLOPPY}\n`);
const env = { ...process.env, BUVARD_FIX_URL: url };
const dry = await runCli(['fix', file], env);
assert.equal(dry.status, 0, dry.stderr);
assert.equal(fs.readFileSync(file, 'utf8'), `${SLOPPY}\n`); // les octets, intacts
assert.match(dry.stdout, /dry-run/);
assert.match(dry.stdout, /appliquée/);
const wet = await runCli(['fix', file, '--write'], env);
assert.equal(wet.status, 0, wet.stderr);
assert.notEqual(fs.readFileSync(file, 'utf8'), `${SLOPPY}\n`);
assert.match(wet.stdout, /écrit/);
server.close();
});
test('the CLI --json reports the whole structured result', async () => {
const { server, url } = await mockServer((body, res) =>
reply(res, jsonOf([{ excerpt: 'Soyons honnêtes : ', suggestion: '', rule: 'ouverture-vide', why: 'x' }])),
);
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'buvard-fix-'));
const file = path.join(dir, 'article.md');
fs.writeFileSync(file, SLOPPY);
const out = await runCli(['fix', file, '--json'], { ...process.env, BUVARD_FIX_URL: url });
server.close();
assert.equal(out.status, 0, out.stderr);
const parsed = JSON.parse(out.stdout);
assert.equal(parsed.fix.before, 90);
assert.equal(parsed.fix.after, 95);
assert.equal(parsed.fix.changed, true);
assert.equal(parsed.fix.written, false);
assert.equal(parsed.fix.rounds[0].suggestions[0].status, 'appliquée');
assert.match(parsed.fix.diff, /^-/m);
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment