|
| 1 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { Hono } from "hono"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import { referenceRewriter, registerFileRoutes } from "./files"; |
| 7 | +import type { StudioApiAdapter } from "../types"; |
| 8 | + |
| 9 | +const rewrite = (text: string, from: string, to: string, folder: boolean) => |
| 10 | + referenceRewriter(from, to, folder)(text); |
| 11 | + |
| 12 | +describe("rename references", () => { |
| 13 | + it("rewrites a folder where files under it are named, in every form a project writes them", () => { |
| 14 | + const text = [ |
| 15 | + '<img src="assets/a.png">', |
| 16 | + "url(./assets/a.png)", |
| 17 | + '"../assets/a.png"', |
| 18 | + '{"poster":"assets/b/c.png"}', |
| 19 | + ].join("\n"); |
| 20 | + expect(rewrite(text, "assets", "brand", true)).toBe( |
| 21 | + [ |
| 22 | + '<img src="brand/a.png">', |
| 23 | + "url(./brand/a.png)", |
| 24 | + '"../brand/a.png"', |
| 25 | + '{"poster":"brand/b/c.png"}', |
| 26 | + ].join("\n"), |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it("leaves a sibling folder with the same beginning, another folder's path and prose alone", () => { |
| 31 | + const text = 'assets-backup/b.png my-assets/x.png other/assets/y.png "The assets folder"'; |
| 32 | + expect(rewrite(text, "assets", "brand", true)).toBe(text); |
| 33 | + }); |
| 34 | + |
| 35 | + it("rewrites a file's path whole, not a longer name that starts with it", () => { |
| 36 | + const text = '"assets/a.png" "assets/a.png2" "assets/a.png-old"'; |
| 37 | + expect(rewrite(text, "assets/a.png", "assets/b.png", false)).toBe( |
| 38 | + '"assets/b.png" "assets/a.png2" "assets/a.png-old"', |
| 39 | + ); |
| 40 | + expect( |
| 41 | + rewrite('url(assets/a.png) srcset="assets/a.png 2x"', "assets/a.png", "x/b.png", false), |
| 42 | + ).toBe('url(x/b.png) srcset="x/b.png 2x"'); |
| 43 | + }); |
| 44 | + |
| 45 | + it("keeps a root-relative lead, and reads JSON-escaped and Windows separators", () => { |
| 46 | + const text = [ |
| 47 | + '<img src="/assets/a.png">', |
| 48 | + "url(/assets/a.png)", |
| 49 | + '{"src":"assets\\/a.png"}', |
| 50 | + '{"path":"assets\\\\a.png"}', |
| 51 | + ].join("\n"); |
| 52 | + expect(rewrite(text, "assets", "brand", true)).toBe( |
| 53 | + [ |
| 54 | + '<img src="/brand/a.png">', |
| 55 | + "url(/brand/a.png)", |
| 56 | + '{"src":"brand\\/a.png"}', |
| 57 | + '{"path":"brand\\\\a.png"}', |
| 58 | + ].join("\n"), |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it("leaves a longer path that exists alone, whatever characters its name holds", () => { |
| 63 | + const rewriteWith = ( |
| 64 | + text: string, |
| 65 | + from: string, |
| 66 | + to: string, |
| 67 | + folder: boolean, |
| 68 | + existing: string[], |
| 69 | + ) => referenceRewriter(from, to, folder, existing)(text); |
| 70 | + const folders = '<img src="other assets/a.png"> <img src="other,assets/a.png">'; |
| 71 | + expect( |
| 72 | + rewriteWith(folders, "assets", "brand", true, ["other assets", "other,assets", "assets"]), |
| 73 | + ).toBe(folders); |
| 74 | + const files = |
| 75 | + '<img src="assets/a.png&backup.png"><img src="assets/a.png 2x.png"><img src="assets/a.png).png">'; |
| 76 | + expect( |
| 77 | + rewriteWith(files, "assets/a.png", "assets/b.png", false, [ |
| 78 | + "assets/a.png&backup.png", |
| 79 | + "assets/a.png 2x.png", |
| 80 | + "assets/a.png).png", |
| 81 | + ]), |
| 82 | + ).toBe(files); |
| 83 | + }); |
| 84 | + |
| 85 | + it("sees an existing path the match sits in the middle of, and in escaped spellings", () => { |
| 86 | + const middle = '<img src="other a.png&backup.png">'; |
| 87 | + expect(referenceRewriter("a.png", "b.png", false, ["other a.png&backup.png"])(middle)).toBe( |
| 88 | + middle, |
| 89 | + ); |
| 90 | + const escaped = String.raw`{"path":"assets\\a.png&backup.png"} {"path":"assets\/a.png&backup.png"}`; |
| 91 | + expect( |
| 92 | + referenceRewriter("assets/a.png", "assets/b.png", false, ["assets/a.png&backup.png"])( |
| 93 | + escaped, |
| 94 | + ), |
| 95 | + ).toBe(escaped); |
| 96 | + }); |
| 97 | + |
| 98 | + it("compares escaped spellings by their text, not by offsets", () => { |
| 99 | + const text = String.raw`"dir\/other a.png&backup.png" "dir\\other a.png&backup.png"`; |
| 100 | + expect(referenceRewriter("a.png", "b.png", false, ["dir/other a.png&backup.png"])(text)).toBe( |
| 101 | + text, |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it("stays fast with thousands of existing paths and references", () => { |
| 106 | + const existing = Array.from({ length: 5000 }, (_, i) => `archive/assets/image-${i}.png`); |
| 107 | + const text = Array.from( |
| 108 | + { length: 5000 }, |
| 109 | + (_, i) => `"assets/a${i}.png" "archive/assets/image-${i}.png"`, |
| 110 | + ).join("\n"); |
| 111 | + const started = Date.now(); |
| 112 | + const out = referenceRewriter("assets", "brand", true, existing)(text); |
| 113 | + expect(Date.now() - started).toBeLessThan(2000); |
| 114 | + expect(out).toContain('"brand/a0.png" "archive/assets/image-0.png"'); |
| 115 | + }); |
| 116 | + |
| 117 | + it("does not multiply suffix and prefix lengths", () => { |
| 118 | + const existing = Array.from( |
| 119 | + { length: 200 }, |
| 120 | + (_, i) => `${"z".repeat(i + 1)} a/${"x".repeat(i + 1)}`, |
| 121 | + ); |
| 122 | + const text = Array.from({ length: 3000 }, () => `"a/${"x".repeat(210)}.png"`).join("\n"); |
| 123 | + const started = Date.now(); |
| 124 | + referenceRewriter("a", "brand", true, existing)(text); |
| 125 | + expect(Date.now() - started).toBeLessThan(3000); |
| 126 | + }); |
| 127 | + |
| 128 | + it("rewrites references a bare scan could mistake: unquoted attributes, a space before a paren, srcset", () => { |
| 129 | + const text = |
| 130 | + '<img src=assets/a.png alt="x"> url(assets/a.png ) srcset="assets/a.png 1x, assets/a.png 2x"'; |
| 131 | + expect(rewrite(text, "assets/a.png", "x/b.png", false)).toBe( |
| 132 | + '<img src=x/b.png alt="x"> url(x/b.png ) srcset="x/b.png 1x, x/b.png 2x"', |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + it("takes no path that is part of a longer one by its start: another root, a plus, a backslash", () => { |
| 137 | + const text = String.raw`other\assets\a.png other\/assets\/a.png my+assets/a.png`; |
| 138 | + expect(rewrite(text, "assets", "brand", true)).toBe(text); |
| 139 | + }); |
| 140 | + |
| 141 | + it("does not stall on a long run of backslashes", () => { |
| 142 | + const text = `${"\\".repeat(200)}unrelated`; |
| 143 | + const started = Date.now(); |
| 144 | + expect(rewrite(text, "assets", "brand", true)).toBe(text); |
| 145 | + expect(Date.now() - started).toBeLessThan(500); |
| 146 | + }); |
| 147 | + |
| 148 | + it("does not take another folder's path whose middle matches", () => { |
| 149 | + const text = "other/./assets/a.png other/../assets/a.png"; |
| 150 | + expect(rewrite(text, "assets", "brand", true)).toBe(text); |
| 151 | + }); |
| 152 | +}); |
| 153 | + |
| 154 | +describe("renaming a folder over the route", () => { |
| 155 | + const dirs: string[] = []; |
| 156 | + afterEach(() => dirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true }))); |
| 157 | + |
| 158 | + it("rewrites references under it and leaves a sibling folder's, and prose, alone", async () => { |
| 159 | + const project = mkdtempSync(join(tmpdir(), "hf-rename-refs-")); |
| 160 | + dirs.push(project); |
| 161 | + mkdirSync(join(project, "assets")); |
| 162 | + mkdirSync(join(project, "assets-backup")); |
| 163 | + mkdirSync(join(project, "other assets")); |
| 164 | + mkdirSync(join(project, "empty assets")); |
| 165 | + writeFileSync(join(project, "other assets", "a.png"), "x"); |
| 166 | + writeFileSync(join(project, "assets", "a.png"), "x"); |
| 167 | + const html = |
| 168 | + '<img src="assets/a.png"><img src="assets-backup/a.png"><img src="other assets/a.png"><a href="empty assets/">x</a><p>The assets folder</p>'; |
| 169 | + writeFileSync(join(project, "index.html"), html); |
| 170 | + const adapter = { |
| 171 | + resolveProject: async (id: string) => ({ id, dir: project }), |
| 172 | + } as unknown as StudioApiAdapter; |
| 173 | + const app = new Hono(); |
| 174 | + registerFileRoutes(app, adapter); |
| 175 | + |
| 176 | + const response = await app.request("/projects/p/files/assets", { |
| 177 | + method: "PATCH", |
| 178 | + body: JSON.stringify({ newPath: "brand" }), |
| 179 | + }); |
| 180 | + |
| 181 | + expect(response.status).toBe(200); |
| 182 | + expect(readFileSync(join(project, "index.html"), "utf8")).toBe( |
| 183 | + '<img src="brand/a.png"><img src="assets-backup/a.png"><img src="other assets/a.png"><a href="empty assets/">x</a><p>The assets folder</p>', |
| 184 | + ); |
| 185 | + }); |
| 186 | + |
| 187 | + it("leaves a path reached through a linked folder alone", async () => { |
| 188 | + const project = mkdtempSync(join(tmpdir(), "hf-rename-link-")); |
| 189 | + dirs.push(project); |
| 190 | + mkdirSync(join(project, "assets")); |
| 191 | + mkdirSync(join(project, "shared")); |
| 192 | + writeFileSync(join(project, "assets", "a.png"), "x"); |
| 193 | + writeFileSync(join(project, "shared", "a.png"), "y"); |
| 194 | + symlinkSync(join(project, "shared"), join(project, "other assets"), "dir"); |
| 195 | + writeFileSync( |
| 196 | + join(project, "index.html"), |
| 197 | + '<img src="assets/a.png"><img src="other assets/a.png">', |
| 198 | + ); |
| 199 | + const adapter = { |
| 200 | + resolveProject: async (id: string) => ({ id, dir: project }), |
| 201 | + } as unknown as StudioApiAdapter; |
| 202 | + const app = new Hono(); |
| 203 | + registerFileRoutes(app, adapter); |
| 204 | + |
| 205 | + await app.request("/projects/p/files/assets/a.png", { |
| 206 | + method: "PATCH", |
| 207 | + body: JSON.stringify({ newPath: "assets/b.png" }), |
| 208 | + }); |
| 209 | + |
| 210 | + expect(readFileSync(join(project, "index.html"), "utf8")).toBe( |
| 211 | + '<img src="assets/b.png"><img src="other assets/a.png">', |
| 212 | + ); |
| 213 | + }); |
| 214 | + |
| 215 | + it("moves a relative link whose target the move leaves behind, and still rewrites references", async () => { |
| 216 | + const project = mkdtempSync(join(tmpdir(), "hf-rename-relink-")); |
| 217 | + dirs.push(project); |
| 218 | + mkdirSync(join(project, "assets")); |
| 219 | + mkdirSync(join(project, "shared")); |
| 220 | + writeFileSync(join(project, "shared", "logo.png"), "x"); |
| 221 | + symlinkSync("../shared/logo.png", join(project, "assets", "logo.png"), "file"); |
| 222 | + writeFileSync(join(project, "index.html"), '<img src="assets/logo.png">'); |
| 223 | + const adapter = { |
| 224 | + resolveProject: async (id: string) => ({ id, dir: project }), |
| 225 | + } as unknown as StudioApiAdapter; |
| 226 | + const app = new Hono(); |
| 227 | + registerFileRoutes(app, adapter); |
| 228 | + |
| 229 | + const response = await app.request("/projects/p/files/assets/logo.png", { |
| 230 | + method: "PATCH", |
| 231 | + body: JSON.stringify({ newPath: "logo.png" }), |
| 232 | + }); |
| 233 | + |
| 234 | + expect(response.status).toBe(200); |
| 235 | + expect(readFileSync(join(project, "index.html"), "utf8")).toBe('<img src="logo.png">'); |
| 236 | + }); |
| 237 | +}); |
0 commit comments