Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

test: frontend filename edge case tests (#6401)

Myles Scolnick56efea9f9846
diff --git a/frontend/src/__tests__/mocks.ts b/frontend/src/__tests__/mocks.ts
new file mode 100644
index 0000000000..f027c18d9f
--- /dev/null
+++ b/frontend/src/__tests__/mocks.ts
@@ -0,0 +1,43 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+// Edge case filenames for testing unicode, spaces, and special characters
+export const EDGE_CASE_FILENAMES = [
+ // Unicode
+ "tést.py",
+ "café.py",
+ "测试.py",
+ // Emojis
+ "🚀notebook.py",
+ // Spaces
+ "test file.py",
+ "file with spaces.py",
+ // Multiple
+ "café & 测试.py",
+ "café notebook.py",
+ // URL characters
+ "test-file.py",
+ "test_file.py",
+ "test_file.backup.py",
+ "file&with&ampersands.py",
+ "file=with=equals.py",
+ "file?with?questions.py",
+];
+
+// Cell names with unicode and special characters for frontend tests
+export const EDGE_CASE_CELL_NAMES = [
+ "tést_cell",
+ "café_notebook",
+ "测试_cell",
+ "🚀_my_cell",
+ "cell with spaces",
+ "café notebook cell",
+];
+
+// URL test cases with special characters that could break query parameters
+export const URL_SPECIAL_CHAR_FILENAMES = [
+ "file with spaces.py",
+ "file&with&ampersands.py",
+ "file=with=equals.py",
+ "file?with?questions.py",
+ "café & 测试.py",
+];
diff --git a/frontend/src/core/saving/__tests__/filename.test.ts b/frontend/src/core/saving/__tests__/filename.test.ts
new file mode 100644
index 0000000000..8fea6bfc88
--- /dev/null
+++ b/frontend/src/core/saving/__tests__/filename.test.ts
@@ -0,0 +1,37 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { describe, expect, it } from "vitest";
+import { EDGE_CASE_FILENAMES } from "../../../__tests__/mocks";
+import { Paths } from "../../../utils/paths";
+
+describe("filename handling logic", () => {
+ it.each(EDGE_CASE_FILENAMES)(
+ "should extract basename correctly for document title: %s",
+ (filename) => {
+ const basename = Paths.basename(filename);
+ expect(basename).toBe(filename); // Since no path separator
+ },
+ );
+
+ it("should handle full paths with unicode filenames", () => {
+ EDGE_CASE_FILENAMES.forEach((filename) => {
+ const fullPath = `/path/to/${filename}`;
+
+ const basename = Paths.basename(fullPath);
+ expect(basename).toBe(filename);
+ });
+ });
+
+ it("should handle document title setting with unicode", () => {
+ EDGE_CASE_FILENAMES.forEach((filename) => {
+ const originalTitle = document.title;
+
+ // In case this does any conversions, we want to simulate reading/writing the title
+ document.title = filename;
+ expect(document.title).toBe(filename);
+
+ // Restore
+ document.title = originalTitle;
+ });
+ });
+});
diff --git a/frontend/src/core/static/__tests__/download-html.test.ts b/frontend/src/core/static/__tests__/download-html.test.ts
index ee3ee9916d..ad06e1f492 100644
--- a/frontend/src/core/static/__tests__/download-html.test.ts
+++ b/frontend/src/core/static/__tests__/download-html.test.ts
@@ -1,5 +1,8 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
+import { EDGE_CASE_FILENAMES } from "../../../__tests__/mocks";
+import { Filenames } from "../../../utils/filenames";
+import { Paths } from "../../../utils/paths";
import { visibleForTesting } from "../download-html";
const { updateAssetUrl } = visibleForTesting;
@@ -39,3 +42,42 @@ describe("updateAssetUrl", () => {
expect(updateAssetUrl(existingUrl, assetBaseUrl)).toBe(existingUrl);
});
});
+
+describe("filename handling for downloads", () => {
+ it.each(EDGE_CASE_FILENAMES)(
+ "should handle edge case filenames in download operations: %s",
+ (filename) => {
+ // Test that basename extraction works correctly for downloads
+ const basename = Paths.basename(filename);
+ expect(basename).toBe(filename);
+
+ // Test filename conversion for HTML downloads
+ const htmlFilename = Filenames.toHTML(filename);
+ expect(htmlFilename).toMatch(/\.html$/);
+ expect(htmlFilename).toContain(Filenames.withoutExtension(filename));
+
+ // Ensure unicode and spaces are preserved in basename
+ const withoutExt = Filenames.withoutExtension(filename);
+ expect(withoutExt).not.toBe("");
+ expect(typeof withoutExt).toBe("string");
+ },
+ );
+
+ it("should handle blob download filename generation", () => {
+ // Mock URL.createObjectURL for blob testing
+ const mockCreateObjectURL = vi.fn().mockReturnValue("blob:mock-url");
+ global.URL.createObjectURL = mockCreateObjectURL;
+
+ EDGE_CASE_FILENAMES.forEach((filename) => {
+ const htmlFilename = Filenames.toHTML(filename);
+
+ // Verify blob can be created with unicode filenames
+ expect(() => new Blob(["test"], { type: "text/html" })).not.toThrow();
+ expect(htmlFilename).toMatch(/\.html$/);
+
+ // Verify filename maintains unicode characters
+ const baseName = Filenames.withoutExtension(filename);
+ expect(htmlFilename).toContain(baseName);
+ });
+ });
+});
diff --git a/frontend/src/utils/__tests__/cell-urls.test.ts b/frontend/src/utils/__tests__/cell-urls.test.ts
index 6249956691..00d5cf0b12 100644
--- a/frontend/src/utils/__tests__/cell-urls.test.ts
+++ b/frontend/src/utils/__tests__/cell-urls.test.ts
@@ -1,5 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { EDGE_CASE_CELL_NAMES } from "../../__tests__/mocks";
import {
canLinkToCell,
createCellLink,
@@ -89,4 +90,32 @@ describe("cell-urls utilities", () => {
expect(canLinkToCell("_")).toBe(false);
});
});
+
+ describe("edge case cell names with unicode and special characters", () => {
+ it.each(EDGE_CASE_CELL_NAMES)(
+ "should handle unicode cell names in createCellLink: %s",
+ (cellName) => {
+ const url = createCellLink(cellName);
+ expect(url).toContain("scrollTo=");
+ expect(url).toContain(encodeURIComponent(cellName));
+ },
+ );
+
+ it.each(EDGE_CASE_CELL_NAMES)(
+ "should round-trip unicode cell names correctly: %s",
+ (cellName) => {
+ const url = createCellLink(cellName);
+ const hash = url.split("#")[1];
+ const extracted = extractCellNameFromHash(`#${hash}`);
+ expect(extracted).toBe(cellName);
+ },
+ );
+
+ it.each(EDGE_CASE_CELL_NAMES)(
+ "should allow linking to unicode cell names: %s",
+ (cellName) => {
+ expect(canLinkToCell(cellName)).toBe(true);
+ },
+ );
+ });
});
diff --git a/frontend/src/utils/__tests__/filenames.test.ts b/frontend/src/utils/__tests__/filenames.test.ts
index d0328f8c75..f4d8e03dcd 100644
--- a/frontend/src/utils/__tests__/filenames.test.ts
+++ b/frontend/src/utils/__tests__/filenames.test.ts
@@ -1,6 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { describe, expect, it } from "vitest";
+import { EDGE_CASE_FILENAMES } from "../../__tests__/mocks";
import { Filenames } from "../filenames";
describe("Filenames", () => {
@@ -27,4 +28,21 @@ describe("Filenames", () => {
expect(Filenames.withoutExtension("test.txt")).toEqual("test");
expect(Filenames.withoutExtension("test.foo.txt")).toEqual("test.foo");
});
+
+ it.each(EDGE_CASE_FILENAMES)(
+ "should handle edge case filenames: %s",
+ (filename) => {
+ // Test all filename operations with edge cases
+ const withoutExt = Filenames.withoutExtension(filename);
+
+ expect(Filenames.toMarkdown(filename)).toEqual(`${withoutExt}.md`);
+ expect(Filenames.toHTML(filename)).toEqual(`${withoutExt}.html`);
+ expect(Filenames.toPNG(filename)).toEqual(`${withoutExt}.png`);
+ expect(Filenames.toPY(filename)).toEqual(`${withoutExt}.py`);
+
+ // Ensure operations preserve unicode and special characters in base name
+ expect(withoutExt).not.toEqual("");
+ expect(typeof withoutExt).toBe("string");
+ },
+ );
});
diff --git a/frontend/src/utils/__tests__/path.test.ts b/frontend/src/utils/__tests__/path.test.ts
index 3c7db7d612..73e75c2429 100644
--- a/frontend/src/utils/__tests__/path.test.ts
+++ b/frontend/src/utils/__tests__/path.test.ts
@@ -1,5 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { describe, expect, it } from "vitest";
+import { EDGE_CASE_FILENAMES } from "../../__tests__/mocks";
import { type FilePath, PathBuilder, Paths } from "../paths";
describe("Paths", () => {
@@ -167,4 +168,41 @@ describe("PathBuilder", () => {
expect(Paths.extension("file.tar.gz")).toBe("gz");
});
});
+
+ describe("edge case filenames", () => {
+ it.each(EDGE_CASE_FILENAMES)(
+ "should handle unicode and spaces in basename: %s",
+ (filename) => {
+ const basename = Paths.basename(filename);
+ expect(basename).toBe(filename);
+ expect(typeof basename).toBe("string");
+ expect(basename).not.toBe("");
+ },
+ );
+
+ it.each(EDGE_CASE_FILENAMES)(
+ "should handle unicode and spaces in dirname: %s",
+ (filename) => {
+ const fullPath = `/path/to/${filename}`;
+ const dirname = Paths.dirname(fullPath);
+ expect(dirname).toBe("/path/to");
+ },
+ );
+
+ it.each(EDGE_CASE_FILENAMES)(
+ "should handle unicode and spaces in path operations: %s",
+ (filename) => {
+ const baseName = Paths.basename(filename);
+ const extension = Paths.extension(filename);
+
+ // Should preserve unicode characters in basename
+ expect(baseName).toContain(filename.split(".")[0]);
+
+ // Should correctly extract extension
+ if (filename.includes(".")) {
+ expect(extension).toBe(filename.split(".").pop());
+ }
+ },
+ );
+ });
});
diff --git a/frontend/src/utils/__tests__/urls.test.ts b/frontend/src/utils/__tests__/urls.test.ts
index ba7fce1922..121e806499 100644
--- a/frontend/src/utils/__tests__/urls.test.ts
+++ b/frontend/src/utils/__tests__/urls.test.ts
@@ -1,6 +1,10 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { describe, expect, it } from "vitest";
-import { isUrl } from "../urls";
+import {
+ EDGE_CASE_FILENAMES,
+ URL_SPECIAL_CHAR_FILENAMES,
+} from "../../__tests__/mocks";
+import { isUrl, updateQueryParams } from "../urls";
describe("isUrl", () => {
it("should return true for a valid URL", () => {
@@ -8,3 +12,54 @@ describe("isUrl", () => {
expect(isUrl("curl -X GET http://example.com")).toBe(false);
});
});
+
+describe("URL parameter handling with edge case filenames", () => {
+ it.each(EDGE_CASE_FILENAMES)(
+ "should handle unicode filenames in URL parameters: %s",
+ (filename) => {
+ // Test that updateQueryParams can handle unicode filenames
+ updateQueryParams((params) => {
+ params.set("file", filename);
+ });
+
+ // Verify URL encoding/decoding works with unicode
+ const encoded = encodeURIComponent(filename);
+ const decoded = decodeURIComponent(encoded);
+ expect(decoded).toBe(filename);
+
+ // Verify filename can be safely added to URL parameters
+ const url = new URL("https://example.com");
+ url.searchParams.set("file", filename);
+ expect(url.searchParams.get("file")).toBe(filename);
+ },
+ );
+
+ it("should preserve unicode in query string round-trip", () => {
+ EDGE_CASE_FILENAMES.forEach((filename) => {
+ const url = new URL("https://example.com");
+ url.searchParams.set("filename", filename);
+
+ // Convert to string and back
+ const urlString = url.toString();
+ const reconstructed = new URL(urlString);
+ const retrievedFilename = reconstructed.searchParams.get("filename");
+
+ expect(retrievedFilename).toBe(filename);
+ });
+ });
+
+ it("should handle special characters in updateQueryParams", () => {
+ URL_SPECIAL_CHAR_FILENAMES.forEach((filename) => {
+ let res: string | null = null;
+ expect(() => {
+ updateQueryParams((params) => {
+ // To and from conversion
+ params.set("file", filename);
+ res = params.get("file");
+ });
+ }).not.toThrow();
+ expect(res).not.toBeNull();
+ expect(res).toBe(filename);
+ });
+ });
+});