jebin2/personel / electrobun-comma-path-bug /openFileDialog.test.ts
jebin2's picture
download
raw
4.09 kB
/**
* Regression test for openFileDialog() splitting paths on commas.
*
* Bug: Utils.ts line 188 does `result.split(",")` on the raw native string.
* This breaks any filename that contains a comma, e.g.:
* /home/user/Downloads/Generated Image February 26, 2025.png
* → ["/home/user/Downloads/Generated Image February 26", " 2025.png"]
*
* The fix should use a delimiter that cannot appear in a file path
* (e.g. "\0" / null byte, which is the POSIX standard for separating paths).
*/
import { test, expect, describe } from "bun:test";
// ── Pure-logic reproduction of the bug ────────────────────────────────────────
// We isolate the split logic so the test runs without a native file dialog.
/** Current (buggy) implementation */
function splitPathsBuggy(nativeResult: string): string[] {
return nativeResult.split(",");
}
/** Proposed fix — native side should join with \0 instead of , */
function splitPathsFixed(nativeResult: string): string[] {
return nativeResult.split("\0").filter(Boolean);
}
// ── Tests ─────────────────────────────────────────────────────────────────────
describe("openFileDialog path splitting", () => {
describe("buggy split(',') behaviour", () => {
test("single path without comma — works fine", () => {
const raw = "/home/user/Downloads/photo.png";
expect(splitPathsBuggy(raw)).toEqual(["/home/user/Downloads/photo.png"]);
});
test("single path WITH comma in filename — BROKEN", () => {
const raw = "/home/user/Downloads/Generated Image February 26, 2025.png";
const result = splitPathsBuggy(raw);
// Bug: splits into two useless fragments
expect(result).toHaveLength(2);
expect(result[0]).toBe("/home/user/Downloads/Generated Image February 26");
expect(result[1]).toBe(" 2025.png");
// Neither fragment is a valid path to the actual file
});
test("multiple paths without commas — works fine", () => {
const raw = "/home/user/a.png,/home/user/b.png";
expect(splitPathsBuggy(raw)).toEqual(["/home/user/a.png", "/home/user/b.png"]);
});
test("multiple paths where one filename has a comma — BROKEN", () => {
const raw = "/home/user/report, final.pdf,/home/user/photo.png";
const result = splitPathsBuggy(raw);
// Bug: produces 3 fragments instead of 2 paths
expect(result).toHaveLength(3);
});
});
describe("fixed split('\\0') behaviour", () => {
test("single path without comma — still works", () => {
const raw = "/home/user/Downloads/photo.png\0";
expect(splitPathsFixed(raw)).toEqual(["/home/user/Downloads/photo.png"]);
});
test("single path WITH comma in filename — fixed", () => {
const raw = "/home/user/Downloads/Generated Image February 26, 2025.png\0";
const result = splitPathsFixed(raw);
expect(result).toHaveLength(1);
expect(result[0]).toBe("/home/user/Downloads/Generated Image February 26, 2025.png");
});
test("multiple paths without commas — still works", () => {
const raw = "/home/user/a.png\0/home/user/b.png\0";
expect(splitPathsFixed(raw)).toEqual(["/home/user/a.png", "/home/user/b.png"]);
});
test("multiple paths where one filename has a comma — fixed", () => {
const raw = "/home/user/report, final.pdf\0/home/user/photo.png\0";
const result = splitPathsFixed(raw);
expect(result).toHaveLength(2);
expect(result[0]).toBe("/home/user/report, final.pdf");
expect(result[1]).toBe("/home/user/photo.png");
});
test("path with multiple commas — fixed", () => {
const raw = "/home/user/a, b, c.png\0";
expect(splitPathsFixed(raw)).toEqual(["/home/user/a, b, c.png"]);
});
test("empty result — returns empty array", () => {
expect(splitPathsFixed("")).toEqual([]);
});
});
});

Xet Storage Details

Size:
4.09 kB
·
Xet hash:
ac1a8e26a18e0314dc652f13321ddcc059459d6bb43afaeba5d5fa6acfc7fefc

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.