v3: the backend gets a test suite, and the bugs it turned up are fixed — deno task test runs 186 tests over api/ in about 18s, against a throwaway database and uploads directory (GERBEUR_DB_PATH and GERBEUR_UPLOADS_DIR now override the defaults; api/test/setup.ts sets them before any app module loads), with requests sent through the whole middleware stack in-process via app.handle, outbound fetches and DNS stubbed, and the socket tests on a real Oak listener since an upgraded request has no response for handle() to return. It covers auth (token purposes, revocation after a password change, live roles, login rate limiting, invites claimed once even under parallel registrations, password resets), dumps end to end (create, privacy, by-url matching on the canonical key, backlinks, categories, edits, file replacement, thumbnails, refresh, delete), file serving headers and ranges, attachments, avatars, comments and likes, playlists, follows and feeds, notifications and mentions, categories, search, chat cursors and read markers, link previews and the SSRF guard on every redirect hop, the Bandcamp and YouTube resolvers, migrations (a fresh schema is left untouched; the data rewrites in 0004/0005 and 0010–0013 do what they say), CORS, OG tags, static caching, and the WebSocket protocol. Fixed along the way: an unknown category id on a new dump returned 400 but kept the dump row, and for a file dump deleted the file while keeping the row; an edit or file replacement with one was half-applied before the 400 — categories are now checked before anything is written. A suffix range (bytes=-3) was read as an end offset and got a 416 instead of the last three bytes. Reordering a playlist accepted a list naming a member twice. verifyPassword threw on a stored hash without a salt instead of answering false. And a request with no Accept-Encoding, which RFC 9110 lets a server treat as accepting anything, was sent the brotli sibling; curl and bots now get the plain file.
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 46s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
khannurien
2026-09-27 12:31:05 +00:00
parent 16b510764c
commit 25601edaf8
29 changed files with 5521 additions and 19 deletions

View File

@@ -0,0 +1,104 @@
import "./setup.ts";
import { assertEquals } from "@std/assert";
import { canonicalizeUrl } from "../lib/canonical-url.ts";
import { up as up0013 } from "../db/migrations/0013_dump_url_canonical.ts";
import { DatabaseSync } from "node:sqlite";
Deno.test("canonicalizeUrl: scheme, www and trailing slash collapse", () => {
const want = "https://example.com/a";
for (
const url of [
"http://example.com/a",
"https://www.example.com/a",
"https://example.com/a/",
"https://EXAMPLE.com/a//",
]
) {
assertEquals(canonicalizeUrl(url), want, url);
}
assertEquals(canonicalizeUrl("https://example.com/"), "https://example.com");
});
Deno.test("canonicalizeUrl: tracking parameters dropped, the rest sorted", () => {
assertEquals(
canonicalizeUrl(
"https://example.com/p?utm_source=x&b=2&fbclid=y&a=1&UTM_Medium=z&si=q",
),
"https://example.com/p?a=1&b=2",
);
assertEquals(
canonicalizeUrl("https://example.com/p?b=1&a=2"),
canonicalizeUrl("https://example.com/p?a=2&b=1"),
);
});
Deno.test("canonicalizeUrl: section anchors dropped, hash routes kept", () => {
assertEquals(
canonicalizeUrl("https://example.com/doc#intro"),
"https://example.com/doc",
);
assertEquals(
canonicalizeUrl("https://app.example.com/#/inbox"),
"https://app.example.com#/inbox",
);
assertEquals(
canonicalizeUrl("https://app.example.com/#!/inbox"),
"https://app.example.com#!/inbox",
);
});
Deno.test("canonicalizeUrl: every YouTube video shape is one key", () => {
const want = "https://youtube.com/watch?v=dQw4w9WgXcQ";
for (
const url of [
"https://youtu.be/dQw4w9WgXcQ",
"https://youtu.be/dQw4w9WgXcQ?t=42",
"https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=PL1&t=1m",
"https://m.youtube.com/watch?v=dQw4w9WgXcQ",
"https://music.youtube.com/watch?v=dQw4w9WgXcQ",
"https://www.youtube.com/embed/dQw4w9WgXcQ",
"https://youtube.com/shorts/dQw4w9WgXcQ",
"https://youtube.com/live/dQw4w9WgXcQ",
"https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ",
]
) {
assertEquals(canonicalizeUrl(url), want, url);
}
assertEquals(
canonicalizeUrl("https://www.youtube.com/playlist?list=PLx&si=abc"),
"https://youtube.com/playlist?list=PLx",
);
});
Deno.test("canonicalizeUrl: non-http and unparseable input", () => {
assertEquals(canonicalizeUrl("ftp://example.com/a"), null);
assertEquals(canonicalizeUrl("javascript:alert(1)"), null);
assertEquals(canonicalizeUrl("not a url"), null);
assertEquals(canonicalizeUrl(""), null);
});
// Migration 0013 carries a frozen copy of the canonicalizer to backfill old
// rows. Until the rules change on purpose (with a new backfill migration), the
// two must agree, or rows from before and after the migration won't match.
Deno.test("migration 0013 backfills the same keys canonicalizeUrl computes", () => {
const urls = [
"http://www.example.com/a/?utm_source=x&b=2&a=1#top",
"https://youtu.be/abc123?t=5",
"https://www.youtube.com/playlist?list=PLx",
"https://app.example.com/#/route",
];
const db = new DatabaseSync(":memory:");
db.exec(`CREATE TABLE dumps (id TEXT PRIMARY KEY, url TEXT);`);
urls.forEach((url, i) =>
db.prepare(`INSERT INTO dumps (id, url) VALUES (?, ?);`).run(String(i), url)
);
up0013(db);
up0013(db); // idempotent
const rows = db.prepare(`SELECT id, url_canonical FROM dumps ORDER BY id;`)
.all() as { id: string; url_canonical: string }[];
assertEquals(
rows.map((r) => r.url_canonical),
urls.map(canonicalizeUrl),
);
db.close();
});