Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(tests): load exporter assets from encoded filesystem paths
  • Loading branch information
minwookshin committed Sep 23, 2026
commit 75cce5a8781e87dbacdf32ae7d9fa253df64e3f6
3 changes: 2 additions & 1 deletion shared/util/fileUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export async function loadFileBuffer(requireUrl: {
// in vitest, this is the url we need to load with readfilesync
// eslint-disable-next-line
const fs = require("fs");
let url = requireUrl.default;
// Vite percent-encodes asset URLs, including spaces and non-ASCII paths.
let url = decodeURIComponent(requireUrl.default);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

if (url.startsWith("/@fs/")) {
url = url.substring("/@fs".length);
Expand Down
53 changes: 53 additions & 0 deletions tests/src/unit/shared/fileUtil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { loadFileBuffer, loadFontDataUrl } from "@shared/util/fileUtil.js";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";

describe("Vite asset file loading", () => {
let directory: string;
const contents = Buffer.from([0, 127, 128, 255]);

beforeEach(() => {
directory = mkdtempSync(join(tmpdir(), "blocknote-file-url-"));
});

afterEach(() => {
rmSync(directory, { recursive: true, force: true });
});

for (const prefix of ["", "/@fs"]) {
it.each(["한글 font.ttf", "font #100% %20.ttf"])(
`reads encoded ${prefix || "absolute"} asset paths: %s`,
async (name) => {
const file = join(directory, name);
writeFileSync(file, contents);

const buffer = await loadFileBuffer({
default: prefix + pathToFileURL(file).pathname,
});

expect(buffer).toEqual(contents);
},
);
}

it("reads an unencoded filesystem path", async () => {
const file = join(directory, "font.ttf");
writeFileSync(file, contents);

expect(await loadFileBuffer({ default: file })).toEqual(contents);
});

it("creates a font data URL from an encoded asset path", async () => {
const file = join(directory, "한글 font.ttf");
writeFileSync(file, contents);

expect(
await loadFontDataUrl({
default: "/@fs" + pathToFileURL(file).pathname,
}),
).toBe(`data:font/ttf;base64,${contents.toString("base64")}`);
});
});