Make every URL the console page uses page-relative, guarded by a source test

AI Assisted
This commit is contained in:
Seth committed 2026-12-05 00:10:44 -08:00
1 parent 4ced32be5c
commit 1d007737b1
47 files changed
+401 -202

No files matched your search

+18 -13
View File
@@ -144,14 +144,14 @@ test("pictures: JPEG only, listed oldest first, with thumbnails when sent", () =
const first = addImage(folder, tab, JPEG);
const second = addImage(folder, tab, JPEG);
assert.match(first.id, IMAGE_ID_RE);
assert.equal(first.url, `/notepad/images/${tab}/${first.id}.jpg`);
assert.equal(first.url, `notepad/images/${tab}/${first.id}.jpg`);
// Make the order unambiguous: the first is older.
const older = new Date(Date.now() - 60_000);
fs.utimesSync(path.join(tabDir(folder, tab), "images", first.id + ".jpg"), older, older);
addThumb(folder, tab, second.id, JPEG);
assert.deepEqual(readNotepad(folder).images, [
{ id: first.id, url: first.url, thumb: null },
{ id: second.id, url: second.url, thumb: `/notepad/images/${tab}/${second.id}.thumb.jpg` },
{ id: second.id, url: second.url, thumb: `notepad/images/${tab}/${second.id}.thumb.jpg` },
]);
});
@@ -509,8 +509,8 @@ test("the first shape reads as one Notes tab, and the read writes nothing", () =
[U1, U2],
"oldest first",
);
assert.equal(read.images[0].url, `/notepad/images/${LEGACY_TAB_ID}/${U1}.jpg`);
assert.equal(read.images[0].thumb, `/notepad/images/${LEGACY_TAB_ID}/${U1}.thumb.jpg`);
assert.equal(read.images[0].url, `notepad/images/${LEGACY_TAB_ID}/${U1}.jpg`);
assert.equal(read.images[0].thumb, `notepad/images/${LEGACY_TAB_ID}/${U1}.thumb.jpg`);
assert.equal(read.images[1].thumb, null);
assert.deepEqual(listTabs(folder), { tabs: [{ id: LEGACY_TAB_ID, name: "Notes" }], last: LEGACY_TAB_ID });
assert.equal(readNotepad(folder, LEGACY_TAB_ID).text, "old note\n");
@@ -564,7 +564,7 @@ test("any first write migrates: a picture, a new tab, and an old picture can be
const f2 = freshFolder("legacy");
legacyNotepad(f2);
const added = addImage(f2, LEGACY_TAB_ID, JPEG);
assert.equal(added.url, `/notepad/images/${LEGACY_TAB_ID}/${added.id}.jpg`);
assert.equal(added.url, `notepad/images/${LEGACY_TAB_ID}/${added.id}.jpg`);
assert.equal(readNotepad(f2).images.length, 3);
// A first-shape notepad with a picture and no note still migrates.
const f3 = freshFolder("legacy");
@@ -718,7 +718,10 @@ test("pictures through the routes: upload, thumb, fetch, delete and every refusa
const up = await postJpeg(s, "/notepad/image", JPEG);
assert.equal(up.status, 200);
const { id, url } = await up.json();
const { id, url: link } = await up.json();
assert.doesNotMatch(link, /^\//, "page-relative");
// The page at / asks for it here.
const url = `/${link}`;
assert.match(id, IMAGE_ID_RE);
const unknownThumb = await postJpeg(s, "/notepad/thumb?id=00000000-0000-0000-0000-000000000000", JPEG);
@@ -853,7 +856,7 @@ test("GET /notepad answers a notepad still in the first shape as a Notes tab, an
assert.equal(res.body.text, "old note\n");
assert.equal(res.body.images.length, 2);
assert.deepEqual(treeOf(dir), before, "the read wrote nothing");
const img = await fetch(`${s.base}${res.body.images[0].url}`, { headers: { cookie: s.cookie, connection: "close" } });
const img = await fetch(`${s.base}/${res.body.images[0].url}`, { headers: { cookie: s.cookie, connection: "close" } });
assert.equal(img.status, 200, "the picture is served from where it still is");
// The unchanged page's save, with no tab named, lands in Notes and migrates.
assert.equal((await s.send("/notepad/text", { text: "page save" })).status, 200);
@@ -1004,16 +1007,18 @@ test("pictures through ?tab=: upload, thumb, fetch and delete, each under the ta
const up = await postJpeg(s, `/notepad/image?tab=${a}`, JPEG);
assert.equal(up.status, 200);
const { id, url } = await up.json();
assert.equal(url, `/notepad/images/${a}/${id}.jpg`);
assert.equal(url, `notepad/images/${a}/${id}.jpg`);
// Page-relative: the page at / asks for it here.
const served = `/${url}`;
assert.equal((await postJpeg(s, `/notepad/thumb?tab=${a}&id=${id}`, JPEG)).status, 200);
assert.equal((await get(url)).status, 200);
assert.equal((await get(served)).status, 200);
assert.equal((await get(`/notepad/images/${a}/${id}.thumb.jpg`)).status, 200);
// Not under another tab, not in the old shape, not under a tab that is not there.
assert.equal((await get(`/notepad/images/${b}/${id}.jpg`)).status, 404);
assert.equal((await get(`/notepad/images/${id}.jpg`)).status, 404);
assert.equal((await get(`/notepad/images/ffffffffffff/${id}.jpg`)).status, 404);
assert.deepEqual((await getJson(s, `/notepad?tab=${b}`)).body.images, []);
assert.equal((await getJson(s, `/notepad?tab=${a}`)).body.images[0].thumb, `/notepad/images/${a}/${id}.thumb.jpg`);
assert.equal((await getJson(s, `/notepad?tab=${a}`)).body.images[0].thumb, `notepad/images/${a}/${id}.thumb.jpg`);
// A picture or thumb for a tab that is not there, and a delete from the wrong tab.
const stray = await postJpeg(s, "/notepad/image?tab=ffffffffffff", JPEG);
assert.equal(stray.status, 404);
@@ -1022,12 +1027,12 @@ test("pictures through ?tab=: upload, thumb, fetch and delete, each under the ta
assert.equal((await postJpeg(s, `/notepad/image?tab=nope`, JPEG)).status, 404);
assert.equal((await s.send(`/notepad/image/delete?tab=${b}`, { id })).status, 404, "tab B does not hold it");
assert.equal((await s.send(`/notepad/image/delete?tab=ffffffffffff`, { id })).status, 404);
assert.equal((await get(url)).status, 200, "still there");
assert.equal((await get(served)).status, 200, "still there");
assert.equal((await s.send(`/notepad/image/delete?tab=${a}`, { id })).status, 200);
assert.equal((await get(url)).status, 404);
assert.equal((await get(served)).status, 404);
// With no tab named, the picture goes to the last tab used.
const unnamed = await (await postJpeg(s, "/notepad/image", JPEG)).json();
assert.equal(unnamed.url.split("/")[3], b);
assert.equal(unnamed.url.split("/")[2], b);
assert.equal((await s.send("/notepad/image/delete", { id: unnamed.id })).status, 200);
assert.deepEqual(await (await postJpeg(s, "/notepad/thumb?id=00000000-0000-0000-0000-000000000000", JPEG)).json(), { error: "not-found" });
});