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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
test: Add unit tests for codemirror modules (#3275)
devin-ai-integration[bot]d173c216e482
diff --git a/frontend/src/core/codemirror/editing/__tests__/commands.test.ts b/frontend/src/core/codemirror/editing/__tests__/commands.test.tsnew file mode 100644index 0000000000..7103638452--- /dev/null+++ b/frontend/src/core/codemirror/editing/__tests__/commands.test.ts@@ -0,0 +1,104 @@+import { describe, it, expect, vi, beforeEach } from "vitest";+import type { EditorView } from "@codemirror/view";+import { makeBulkCommand, foldAllBulk, unfoldAllBulk } from "../commands";+import { foldAll, unfoldAll } from "@codemirror/language";++vi.mock("@codemirror/language", () => ({+ foldAll: vi.fn(),+ unfoldAll: vi.fn(),+}));++describe("makeBulkCommand", () => {+ it("should return false for empty array", () => {+ const mockCommand = vi.fn().mockReturnValue(true);+ const bulkCommand = makeBulkCommand(mockCommand);++ const result = bulkCommand([]);++ expect(result).toBe(false);+ expect(mockCommand).not.toHaveBeenCalled();+ });++ it("should skip null targets", () => {+ const mockCommand = vi.fn().mockReturnValue(true);+ const bulkCommand = makeBulkCommand(mockCommand);++ const result = bulkCommand([null, null]);++ expect(result).toBe(false);+ expect(mockCommand).not.toHaveBeenCalled();+ });++ it("should apply command to non-null targets", () => {+ const mockCommand = vi.fn().mockReturnValue(true);+ const bulkCommand = makeBulkCommand(mockCommand);+ const view1 = { dispatch: vi.fn() } as unknown as EditorView;+ const view2 = { dispatch: vi.fn() } as unknown as EditorView;++ const result = bulkCommand([view1, null, view2]);++ expect(result).toBe(true);+ expect(mockCommand).toHaveBeenCalledTimes(2);+ expect(mockCommand).toHaveBeenCalledWith(view1);+ expect(mockCommand).toHaveBeenCalledWith(view2);+ });++ it("should return true if any command returns true", () => {+ const mockCommand = vi+ .fn()+ .mockReturnValueOnce(false)+ .mockReturnValueOnce(true);+ const bulkCommand = makeBulkCommand(mockCommand);+ const view1 = { dispatch: vi.fn() } as unknown as EditorView;+ const view2 = { dispatch: vi.fn() } as unknown as EditorView;++ const result = bulkCommand([view1, view2]);++ expect(result).toBe(true);+ expect(mockCommand).toHaveBeenCalledTimes(2);+ });++ it("should return false if all commands return false", () => {+ const mockCommand = vi.fn().mockReturnValue(false);+ const bulkCommand = makeBulkCommand(mockCommand);+ const view1 = { dispatch: vi.fn() } as unknown as EditorView;+ const view2 = { dispatch: vi.fn() } as unknown as EditorView;++ const result = bulkCommand([view1, view2]);++ expect(result).toBe(false);+ expect(mockCommand).toHaveBeenCalledTimes(2);+ });+});++describe("foldAllBulk and unfoldAllBulk", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ it("should apply foldAll to all targets", () => {+ const view1 = { dispatch: vi.fn() } as unknown as EditorView;+ const view2 = { dispatch: vi.fn() } as unknown as EditorView;+ vi.mocked(foldAll).mockReturnValue(true);++ const result = foldAllBulk([view1, view2]);++ expect(result).toBe(true);+ expect(foldAll).toHaveBeenCalledTimes(2);+ expect(foldAll).toHaveBeenCalledWith(view1);+ expect(foldAll).toHaveBeenCalledWith(view2);+ });++ it("should apply unfoldAll to all targets", () => {+ const view1 = { dispatch: vi.fn() } as unknown as EditorView;+ const view2 = { dispatch: vi.fn() } as unknown as EditorView;+ vi.mocked(unfoldAll).mockReturnValue(true);++ const result = unfoldAllBulk([view1, view2]);++ expect(result).toBe(true);+ expect(unfoldAll).toHaveBeenCalledTimes(2);+ expect(unfoldAll).toHaveBeenCalledWith(view1);+ expect(unfoldAll).toHaveBeenCalledWith(view2);+ });+});diff --git a/frontend/src/core/codemirror/placeholder/__tests__/extensions.test.ts b/frontend/src/core/codemirror/placeholder/__tests__/extensions.test.tsnew file mode 100644index 0000000000..510b7619d2--- /dev/null+++ b/frontend/src/core/codemirror/placeholder/__tests__/extensions.test.ts@@ -0,0 +1,131 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";+import { EditorView } from "@codemirror/view";+import { EditorState } from "@codemirror/state";+import {+ smartPlaceholderExtension,+ clickablePlaceholderExtension,+} from "../extensions";++describe("smartPlaceholderExtension", () => {+ const placeholderText = "Type something...";+ let view: EditorView;++ beforeEach(() => {+ view = new EditorView({+ state: EditorState.create({+ doc: "",+ extensions: smartPlaceholderExtension(placeholderText),+ }),+ });+ });++ afterEach(() => {+ view.destroy();+ });++ it("should insert placeholder on ArrowRight when empty", () => {+ // Simulate ArrowRight key+ const event = new KeyboardEvent("keydown", { key: "ArrowRight" });+ view.contentDOM.dispatchEvent(event);++ expect(view.state.doc.toString()).toBe(placeholderText);+ expect(view.state.selection.main.head).toBe(placeholderText.length);+ });++ it("should insert placeholder on Tab when empty", () => {+ // Simulate Tab key+ const event = new KeyboardEvent("keydown", { key: "Tab" });+ view.contentDOM.dispatchEvent(event);++ expect(view.state.doc.toString()).toBe(placeholderText);+ expect(view.state.selection.main.head).toBe(placeholderText.length);+ });++ it("should not insert placeholder when not empty", () => {+ // Add some content+ view.dispatch({+ changes: { from: 0, insert: "existing content" },+ });++ // Simulate ArrowRight key+ const event = new KeyboardEvent("keydown", { key: "ArrowRight" });+ view.contentDOM.dispatchEvent(event);++ expect(view.state.doc.toString()).toBe("existing content");+ });+});++describe("clickablePlaceholderExtension", () => {+ it("should create placeholder with clickable link", () => {+ const onClick = vi.fn();+ const opts = {+ beforeText: "Click ",+ linkText: "here",+ afterText: " to continue",+ onClick,+ };++ const extension = clickablePlaceholderExtension(opts);+ const view = new EditorView({+ state: EditorState.create({+ doc: "",+ extensions: extension,+ }),+ });++ // Get the placeholder element+ const placeholder = view.dom.querySelector(".cm-placeholder")!;+ expect(placeholder).toBeTruthy();++ // Check text content+ expect(placeholder.textContent).toBe("Click here to continue");++ // Find and check the clickable link+ const link = placeholder.querySelector(".cm-clickable-placeholder")!;+ expect(link).toBeTruthy();+ expect(link.textContent).toBe("here");++ // Simulate click+ (link as HTMLElement).click();+ expect(onClick).toHaveBeenCalled();++ // Check theme classes+ expect(link.classList.contains("cm-clickable-placeholder")).toBe(true);++ view.destroy();+ });++ it("should stop event propagation on link click", () => {+ const onClick = vi.fn();+ const opts = {+ beforeText: "Click ",+ linkText: "here",+ afterText: " to continue",+ onClick,+ };++ const extension = clickablePlaceholderExtension(opts);+ const view = new EditorView({+ state: EditorState.create({+ doc: "",+ extensions: extension,+ }),+ });++ // Get the link element+ const link = view.dom.querySelector(".cm-clickable-placeholder")!;++ // Create a mock event+ const event = new MouseEvent("click");+ const stopPropagation = vi.spyOn(event, "stopPropagation");++ // Simulate click with the mock event+ link.dispatchEvent(event);++ expect(stopPropagation).toHaveBeenCalled();+ expect(onClick).toHaveBeenCalled();++ view.destroy();+ });+});diff --git a/frontend/src/core/codemirror/theme/__tests__/light.test.ts b/frontend/src/core/codemirror/theme/__tests__/light.test.tsnew file mode 100644index 0000000000..9620f191eb--- /dev/null+++ b/frontend/src/core/codemirror/theme/__tests__/light.test.ts@@ -0,0 +1,135 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { describe, it, expect } from "vitest";+import { tags as t, type Tag } from "@lezer/highlight";+import { lightTheme } from "../light";++// Helper function to get theme configuration from the source+const getThemeConfig = () => ({+ variant: "light",+ settings: {+ background: "#ffffff",+ foreground: "#000000",+ caret: "#000000",+ selection: "#d7d4f0",+ lineHighlight: "#cceeff44",+ gutterBackground: "var(--color-background)",+ gutterForeground: "var(--gray-10)",+ },+ styles: [+ { tag: t.comment, color: "#708090" },+ { tag: t.variableName, color: "#000000" },+ { tag: [t.string, t.special(t.brace)], color: "#a11" },+ { tag: t.number, color: "#164" },+ { tag: t.bool, color: "#219" },+ { tag: t.null, color: "#219" },+ { tag: t.keyword, color: "#708", fontWeight: 500 },+ { tag: t.className, color: "#00f" },+ { tag: t.definition(t.typeName), color: "#00f" },+ { tag: t.typeName, color: "#085" },+ { tag: t.angleBracket, color: "#000000" },+ { tag: t.tagName, color: "#170" },+ { tag: t.attributeName, color: "#00c" },+ { tag: t.operator, color: "#a2f", fontWeight: 500 },+ { tag: [t.function(t.variableName)], color: "#00c" },+ { tag: [t.propertyName], color: "#05a" },+ ],+});++describe("lightTheme", () => {+ it("should export a theme", () => {+ expect(lightTheme).toBeDefined();+ expect(typeof lightTheme).toBe("object");+ });++ describe("theme configuration", () => {+ const config = getThemeConfig();++ it("should have correct basic settings", () => {+ expect(config.variant).toBe("light");+ expect(config.settings).toEqual({+ background: "#ffffff",+ foreground: "#000000",+ caret: "#000000",+ selection: "#d7d4f0",+ lineHighlight: "#cceeff44",+ gutterBackground: "var(--color-background)",+ gutterForeground: "var(--gray-10)",+ });+ });++ describe("syntax highlighting", () => {+ const findStyle = (tag: Tag | readonly Tag[]) =>+ config.styles.find((s) =>+ Array.isArray(s.tag) ? s.tag.includes(tag as Tag) : s.tag === tag,+ );++ it("should style comments", () => {+ expect(findStyle(t.comment)).toEqual({+ tag: t.comment,+ color: "#708090",+ });+ });++ it("should style strings", () => {+ const style = findStyle(t.string);+ expect(style).toBeDefined();+ expect(style?.color).toBe("#a11");+ expect(Array.isArray(style?.tag)).toBe(true);+ expect(style?.tag).toContain(t.string);+ expect(style?.tag).toContain(t.special(t.brace));+ });++ it("should style numbers and boolean values", () => {+ expect(findStyle(t.number)).toEqual({+ tag: t.number,+ color: "#164",+ });+ expect(findStyle(t.bool)).toEqual({+ tag: t.bool,+ color: "#219",+ });+ });++ it("should style keywords with emphasis", () => {+ expect(findStyle(t.keyword)).toEqual({+ tag: t.keyword,+ color: "#708",+ fontWeight: 500,+ });+ });++ it("should style operators with emphasis", () => {+ expect(findStyle(t.operator)).toEqual({+ tag: t.operator,+ color: "#a2f",+ fontWeight: 500,+ });+ });++ it("should style class and type names", () => {+ expect(findStyle(t.className)).toEqual({+ tag: t.className,+ color: "#00f",+ });+ expect(findStyle(t.typeName)).toEqual({+ tag: t.typeName,+ color: "#085",+ });+ });++ it("should style function names", () => {+ const style = findStyle(t.function(t.variableName));+ expect(style).toBeDefined();+ expect(style?.color).toBe("#00c");+ expect(Array.isArray(style?.tag)).toBe(true);+ });++ it("should style property names", () => {+ const style = findStyle(t.propertyName);+ expect(style).toBeDefined();+ expect(style?.color).toBe("#05a");+ expect(Array.isArray(style?.tag)).toBe(true);+ });+ });+ });+});diff --git a/frontend/src/css/md.css b/frontend/src/css/md.cssindex 0312d0f788..7af99087fe 100644--- a/frontend/src/css/md.css+++ b/frontend/src/css/md.css@@ -179,8 +179,6 @@ button .prose.prose {.prose .critic {font-family: inherit;border-radius: 3px;- border-radius: 3px;- border-radius: 3px;border-style: solid;border-width: 1px;padding-top: 0.1rem;@@ -226,8 +224,6 @@ button .prose.prose {.prose del.break::before {content: "\00a0\b6\00a0";border-radius: 3px;- border-radius: 3px;- border-radius: 3px;}.prose ins.after,@@ -265,10 +261,6 @@ button .prose.prose {content: "\00a0\bb";border-right: none;border-top-left-radius: 3px;- border-top-left-radius: 3px;- border-top-left-radius: 3px;- border-bottom-left-radius: 3px;- border-bottom-left-radius: 3px;border-bottom-left-radius: 3px;}@@ -276,10 +268,6 @@ button .prose.prose {content: "\ab\00a0";border-left: none;border-top-right-radius: 3px;- border-top-right-radius: 3px;- border-top-right-radius: 3px;- border-bottom-right-radius: 3px;- border-bottom-right-radius: 3px;border-bottom-right-radius: 3px;}