fix(uploads): finish the sweeps round 1 left half-done
Deep-review round 2, on round 1's own fix commit. It found MORE than round 1 did (28 raw, against 13), and most of it was mine -- a new piece of UI attracts findings a backend-only diff never does. The pattern in almost all of them is the same: a principle applied to the site that prompted it and not to its siblings. * `display_text` reached three of the FIVE writers of a user-supplied name. `POST /api/downloads` and `POST /api/downloads/edit` still handed Postgres an unbounded string, so a 300-character name pasted into the Add-download dialog or the editor was a bare 500 (main.py re-raises every DataError but the NUL case). Four of the seven reviewers found it independently. The bound now lives in `models.py`, the three columns are DECLARED with it, and the guard for the next writer is parsed rather than remembered: every `display_name` / `display_uploader` assignment under `app/routes/` must go through the helper. That guard immediately found two more -- the Google sign-in callback writes the profile name into the same 255-wide column -- so it is six, not five. * The connection release reached one of the THREE routes that read their own body. The screenshot route (10 MiB, from a phone) and the client log sink both still held a pooled connection idle-in-transaction for the length of the upload, which is the invariant db.py sizes the pool on. All three now call `db.release_read_connection`, whose precondition -- nothing pending on the shared session -- is CHECKED rather than described, because a whole-session rollback discarding a caller's staged write is a mistake this repo has already made once. `test_body_limit.py` holds the rule for the next such route. * The AST drift registry keyed its sites by (module, limit expression), so a second route copied from the first collapsed onto the same entry and went uncovered -- the exact hole the registry exists to close. Keyed per call site now, and it carries the release requirement too. * The dialog's own refusal was announced once and never again: two over-cap picks leave an identical node with identical text, and an untouched live region says nothing. It is keyed per pick now, the drop zone reads as invalid and points at the sentence, and the reason is reachable from the control rather than only from a one-shot announcement. * Two tests rebuilt the blind spot they were written to remove: the dialog's cap test sat nowhere near the boundary (a `>` loosened to `>=` survived it, which is the mutation round 1 fixed on the server side), and the header-precedence test only covered an exact-case duplicate while a differently-cased one would have been COMBINED onto the wire rather than replaced. Both fixed, and the merge now dedupes case-insensitively because header names are. Also: the upload dialog's unstubbed usage query was firing a real request from jsdom in a neighbouring suite and leaking a delayed "connection lost" toast across test boundaries; `GET /api/downloads/usage` had no test at all though the whole client-side check hangs off it; and the comment claiming the edge 413 is readable now says what was measured -- readable to 200 MiB over loopback, a bare `onerror` through a proxy -- so the pre-send check is named as the mechanism and that branch as the backstop it is. Every test added or changed here was mutation-checked. One mutation SURVIVED and is recorded rather than papered over: the `tooBig` term inside `submit()` cannot be reached, because `ui/Button` swallows the activation itself, so it is marked as belt and braces instead of being described as the guard.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
// A bound that has to hold in two languages at once. The backend truncates a user-supplied display
|
||||
// name to `DISPLAY_TEXT_MAX` on its way into a varchar column of the same width (the columns are
|
||||
// declared with it); the upload dialog stops the user typing at the same number, so the truncation
|
||||
// is never a silent surprise. Neither side can see the other's copy, so the gate reads both.
|
||||
//
|
||||
// Widening one side alone is the failure this catches: the column and the Python constant move
|
||||
// together, the input keeps clipping at the old number, and the field simply stops accepting
|
||||
// keystrokes with no message — visible only by reading two files in two languages.
|
||||
//
|
||||
// Read as TEXT, like `i18n/backendIds.contract.test.ts` does, rather than by importing the module:
|
||||
// the api slice pulls in i18n, which touches localStorage at load and would drag a DOM into a check
|
||||
// that is pure string comparison.
|
||||
const src = (u: string) => readFileSync(new URL(u, import.meta.url), "utf-8");
|
||||
|
||||
describe("display-name bound", () => {
|
||||
it("matches the backend's DISPLAY_TEXT_MAX", () => {
|
||||
const backend = src("../../../../backend/app/models.py").match(
|
||||
/^DISPLAY_TEXT_MAX\s*=\s*(\d+)$/m,
|
||||
);
|
||||
const frontend = src("./downloads.ts").match(/^export const DISPLAY_NAME_MAX = (\d+);$/m);
|
||||
expect(backend, "backend/app/models.py no longer defines DISPLAY_TEXT_MAX").toBeTruthy();
|
||||
expect(frontend, "lib/api/downloads.ts no longer exports DISPLAY_NAME_MAX").toBeTruthy();
|
||||
expect(Number(frontend![1])).toBe(Number(backend![1]));
|
||||
});
|
||||
|
||||
it("is the width the columns are actually declared with", () => {
|
||||
// The mutation guard for the check above: both constants could agree on a number neither
|
||||
// column uses. `String(DISPLAY_TEXT_MAX)` is how the three display columns are declared, so a
|
||||
// literal creeping back into one of them fails here.
|
||||
const models = src("../../../../backend/app/models.py");
|
||||
const declared = [...models.matchAll(/display_(?:name|uploader): Mapped.*?String\((.*?)\)/g)];
|
||||
expect(declared).toHaveLength(3);
|
||||
for (const m of declared) expect(m[1]).toBe("DISPLAY_TEXT_MAX");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user