Files
datacat/backend/utils/responseHandlers.test.js
GandClaude Opus 5 53d2e70723 chore: add a formatter (#233)
* chore: add a formatter, matching the style this repo already writes

singleQuote=true was chosen by counting this repo's own
imports, not by fleet decree. The fleet is genuinely split and the two repos
that already had a .prettierrc disagreed with each other, so there was no
standard to restore. Quote style does not cross repo boundaries; having a
gate does.

Markdown is ignored for now — prettier rewraps prose, which would bury the
real diff.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* style: format with prettier (199 files)

Mechanical. No behaviour change. This SHA is listed in
.git-blame-ignore-revs so `git blame` skips it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore: teach git blame to skip the reformat

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(ci): put prettier where CI actually installs

The formatter was added to the root package.json, but CI installs with
working-directory: frontend and then runs npm run verify at the root, so root
devDependencies are never installed at all. format:check died with
'prettier: not found' before checking a single file.

Root now delegates into frontend the way every other root script already does,
and prettier is a devDependency of frontend, whose lockfile CI installs.

frontend also needs its own .prettierignore: prettier resolves that file
relative to the working directory, so the root one stopped applying the moment
the command ran from frontend/ — which is how README.md and Playwright output
ended up being checked.

* fix(ci): do not format contentlayer's generated output

format:check ran green locally and failed in CI on files that do not exist
locally: CI builds contentlayer before verify, so .contentlayer/generated is
present by the time prettier looks. One generated module imports with
assert { type: 'json' }, which prettier's parser rejects outright.

A local --check cannot see build output, so generated directories have to be
ignored by name rather than discovered.

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-29 11:06:25 +00:00

115 lines
4.0 KiB
JavaScript

/**
* responseHandlers is the SSOT for what every API response looks like. Two
* properties are worth holding still:
*
* 1. The envelope shape. Clients branch on `success`, so a handler that omits
* it turns a failure into something the frontend reads as neither.
* 2. `error()` attaches `details` ONLY outside production. That is a
* deliberate leak guard — details carry stack traces and driver messages —
* and it is exactly the kind of conditional that survives a refactor in
* form while losing its meaning.
*
* Nothing executed this file before: backend/package.json's `test` was
* `echo "Error: no test specified" && exit 1`, a placeholder that fails by
* design, and the repo's only real suite was Playwright.
*/
// ESM imports even though the module under test is CommonJS: vitest cannot be
// `require`d, and Vite handles the CJS interop on the default import.
import { afterEach, describe, expect, it } from 'vitest';
import handlers from './responseHandlers.js';
/** Minimal Express `res` double that records what was sent. */
function fakeRes() {
return {
statusCode: undefined,
body: undefined,
status(code) {
this.statusCode = code;
return this;
},
json(payload) {
this.body = payload;
return this;
},
};
}
const originalEnv = process.env.NODE_ENV;
afterEach(() => {
process.env.NODE_ENV = originalEnv;
});
describe('success', () => {
it('sends 200 with success:true by default', () => {
const res = handlers.success(fakeRes(), { id: 1 });
expect(res.statusCode).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.id).toBe(1);
});
it('honours an explicit status', () => {
expect(handlers.success(fakeRes(), {}, 'Created', 201).statusCode).toBe(201);
});
it('carries the message through', () => {
expect(handlers.success(fakeRes(), {}, 'Saved').body.message).toBe('Saved');
});
});
describe('error', () => {
it('sends 500 with success:false by default', () => {
const res = handlers.error(fakeRes());
expect(res.statusCode).toBe(500);
expect(res.body.success).toBe(false);
});
it('honours an explicit status', () => {
expect(handlers.error(fakeRes(), 'Not found', 404).statusCode).toBe(404);
});
it('includes details in development', () => {
process.env.NODE_ENV = 'development';
const res = handlers.error(fakeRes(), 'Boom', 500, { stack: 'at db.query' });
expect(res.body.details).toEqual({ stack: 'at db.query' });
});
it('NEVER includes details in production', () => {
// The leak guard. `details` is where stack traces and driver errors go;
// shipping them to a client is how internals reach an attacker. Asserted
// directly rather than trusted, because the check is one `if` away from
// being refactored into always-on.
process.env.NODE_ENV = 'production';
const res = handlers.error(fakeRes(), 'Boom', 500, { stack: 'at db.query' });
expect(res.body.details).toBeUndefined();
});
it('omits details when none are given, in any environment', () => {
for (const env of ['development', 'production', 'test']) {
process.env.NODE_ENV = env;
expect(handlers.error(fakeRes(), 'Boom').body.details).toBeUndefined();
}
});
});
describe('validationError', () => {
it('sends 400 with success:false and the errors array', () => {
const res = handlers.validationError(fakeRes(), 'Invalid', [{ field: 'email' }]);
expect(res.statusCode).toBe(400);
expect(res.body.success).toBe(false);
expect(res.body.errors).toEqual([{ field: 'email' }]);
});
});
describe('the envelope is uniform', () => {
it('every exported handler sets a boolean `success`', () => {
// A client that branches on `success` must never meet a response without
// it — that reads as neither a win nor a failure.
for (const [name, handler] of Object.entries(handlers)) {
if (typeof handler !== 'function') continue;
const res = handler(fakeRes());
expect(typeof res.body?.success, `${name} did not set a boolean success`).toBe('boolean');
}
});
});