/* Copyright 2026 Marimo. All rights reserved. */ /* oxlint-disable typescript/no-explicit-any */ import { afterEach, beforeEach, describe, expect, it } from "vitest"; import type { OutputMessage } from "@/core/kernel/messages"; import { getInitialShowCode, groupCellsByColumn, shouldHideCode, updateShowCodeQueryParam, } from "../utils"; const originalHref = window.location.href; let originalStaticState: typeof window.__MARIMO_STATIC__; describe("show code", () => { beforeEach(() => { originalStaticState = window.__MARIMO_STATIC__; window.history.replaceState(null, "", "/?existing=value#section"); window.__MARIMO_STATIC__ = { files: {}, modelNotifications: [], }; }); afterEach(() => { window.history.replaceState(null, "", originalHref); window.__MARIMO_STATIC__ = originalStaticState; }); it("does not add a query parameter when reading the initial value", () => { expect( getInitialShowCode({ showCodeInRunModePreference: true, kioskMode: false, }), ).toBe(true); expect(window.location.search).toBe("?existing=value"); }); it("reflects static notebook toggles in the URL", () => { updateShowCodeQueryParam(false); expect(window.location.search).toBe("?existing=value&show-code=false"); expect(window.location.hash).toBe("#section"); expect( getInitialShowCode({ showCodeInRunModePreference: true, kioskMode: false, }), ).toBe(false); updateShowCodeQueryParam(true); expect(window.location.search).toBe("?existing=value&show-code=true"); expect( getInitialShowCode({ showCodeInRunModePreference: true, kioskMode: false, }), ).toBe(true); }); it("does not reflect non-static notebook toggles in the URL", () => { delete window.__MARIMO_STATIC__; updateShowCodeQueryParam(false); expect(window.location.search).toBe("?existing=value"); }); }); describe("groupCellsByColumn", () => { it("should group cells by column and maintain order", () => { const cells = [ { config: { column: 0 }, id: "1" }, { config: { column: 1 }, id: "2" }, { config: { column: 0 }, id: "3" }, { config: { column: 2 }, id: "4" }, { config: { column: 1 }, id: "5" }, ] as any[]; const result = groupCellsByColumn(cells); expect(result).toEqual([ [0, [cells[0], cells[2]]], [1, [cells[1], cells[4]]], [2, [cells[3]]], ]); }); it("should use last seen column when column not specified", () => { const cells = [ { config: { column: 0 }, id: "1" }, { config: {}, id: "2" }, { config: { column: 1 }, id: "3" }, { config: {}, id: "4" }, ] as any[]; const result = groupCellsByColumn(cells); expect(result).toEqual([ [0, [cells[0], cells[1]]], [1, [cells[2], cells[3]]], ]); }); }); describe("shouldHideCode", () => { // Helper function to create valid OutputMessage const createOutput = ( data: any = "some output", mimetype: OutputMessage["mimetype"] = "text/plain", ): OutputMessage => ({ channel: "output", mimetype, data, timestamp: Date.now(), }); describe("empty/whitespace code", () => { it("should return true for empty string", () => { expect(shouldHideCode("", null)).toBe(true); expect(shouldHideCode("", createOutput())).toBe(true); }); it("should return true for whitespace-only strings", () => { expect(shouldHideCode(" ", null)).toBe(true); expect(shouldHideCode(" ", createOutput())).toBe(true); expect(shouldHideCode("\n\n", null)).toBe(true); expect(shouldHideCode("\n\n", createOutput())).toBe(true); expect(shouldHideCode(" \n \t ", null)).toBe(true); expect(shouldHideCode(" \n \t ", createOutput())).toBe(true); expect(shouldHideCode("\r\n \r\n", null)).toBe(true); expect(shouldHideCode("\r\n \r\n", createOutput())).toBe(true); }); }); describe("markdown code with output", () => { it("should return true for basic markdown with output", () => { const markdownCode = 'mo.md("# Hello World")'; expect(shouldHideCode(markdownCode, createOutput())).toBe(true); }); it("should return true for multi-line markdown with output", () => { const markdownCode = 'mo.md("""\n# Hello\nThis is content\n""")'; expect(shouldHideCode(markdownCode, createOutput())).toBe(true); }); it("should return true for f-string markdown with output", () => { const markdownCode = 'mo.md(f"# Hello {name}")'; expect(shouldHideCode(markdownCode, createOutput())).toBe(true); }); it("should return true for raw string markdown with output", () => { const markdownCode = 'mo.md(r"# Hello World")'; expect(shouldHideCode(markdownCode, createOutput())).toBe(true); }); it("should return true for rf-string markdown with output", () => { const markdownCode = 'mo.md(rf"# Hello {name}")'; expect(shouldHideCode(markdownCode, createOutput())).toBe(true); }); it("should return true for markdown with various output types", () => { const markdownCode = 'mo.md("# Hello")'; expect( shouldHideCode(markdownCode, createOutput("text", "text/plain")), ).toBe(true); expect( shouldHideCode( markdownCode, createOutput({ key: "value" }, "application/json"), ), ).toBe(true); expect( shouldHideCode( markdownCode, createOutput("