Files
anonymous_github/test/anonymize-preview-route.test.js
T
Thomas DurieuxandClaude Opus 5.5 fc14b2c3ae fix(preview): anonymize form previews on a worker, trace slow ones to their project
A user's backtracking term made each anonymize-form preview block the API's
event loop for ~1.2 s and fail with a 500. One per keystroke, 45 of them
froze every request for about a minute (12:59 UTC, 2026-11-30).

- Previews run on their own worker pool (1 worker by default). A preview
  that runs past its deadline (3 s) terminates its worker and answers
  422 anonymization_timeout; the form explains the term is too slow.
- One preview per user: a newer edit, or the client leaving, stops the
  older preview (409 preview_superseded, logged at info).
- Slow (>=500 ms) or failed previews log an anonymization_preview record
  with the project's repoId, input bytes and term count.
- Heartbeats carry the interval's worst event-loop delay (loopMaxMs,
  loopP99Ms); a delay of 1 s or more logs event_loop_blocked at warn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 13:17:35 +00:00

50 lines
2.6 KiB
JavaScript

const { expect } = require("chai");
require("ts-node/register/transpile-only");
const { ContentAnonimizer } = require("../src/core/anonymize-utils");
const { AnonymizeTransformer: Anonymize } = require("../src/core/anonymize-utils");
const router = require("../src/server/routes/anonymize-preview").default;
const handler = router.stack.find(layer => layer.route?.methods.post).route.stack[0].handle;
describe("POST /api/anonymize-preview preservation", function () {
this.timeout(20000); // The first preview starts its worker.
const content = "Jane Smith https://example.org ![Jane Smith](plot.png)";
const options = { terms: ["Jane Smith", "Smith"], image: false, link: false };
function request(body) {
const res = { statusCode: 200, headers: {} };
res.status = code => { res.statusCode = code; return res; };
res.set = (key, value) => { res.headers[key] = value; return res; };
res.json = body => { res.body = body; return res; };
res.once = () => res;
return handler({ body }, res).then(() => res);
}
for (const [filePath, preserveFiles] of [
["README.md", ["docs/"]], ["docs/README.md", ["docs/"]],
["paper/refs.bib", ["*.bib"]], ["paper/refs.bib", ["*.bib", "!/paper/refs.bib"]],
]) {
it(`matches the production stream for ${filePath} with ${preserveFiles.join(", ")}`, async () => {
const opt = { ...options, filePath, preserveFiles };
const stream = new Anonymize(opt);
let expected = "";
stream.on("data", chunk => { expected += chunk.toString(); });
const done = new Promise((resolve, reject) => { stream.on("end", resolve); stream.on("error", reject); });
stream.end(content);
await done;
const res = await request({ content, options: opt });
expect(res.body.content).to.equal(expected);
expect(res.headers["Cache-Control"]).to.equal("private, no-store");
});
}
it("keeps snippet batches working without a file path", async () => {
const res = await request({ contents: [content, "Smith"], options });
expect(res.body.contents).to.deep.equal([content, "Smith"].map(text => new ContentAnonimizer(options).anonymize(text)));
});
it("rejects malformed preservation rules and paths", async () => {
for (const preserveFiles of ["*.bib", [42], ["../private"]]) {
expect((await request({ content, options: { ...options, preserveFiles } })).statusCode).to.equal(400);
}
for (const filePath of ["../private", "/absolute", ["README.md"]]) {
expect((await request({ content, options: { ...options, filePath } })).statusCode).to.equal(400);
}
});
});