|
| 1 | +/* Copyright 2026 Marimo. All rights reserved. */ |
| 2 | +// @vitest-environment jsdom |
| 3 | + |
| 4 | +import type { ExtractAtomValue } from "jotai"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 6 | +import { hasRunAnyCellAtom } from "@/components/editor/cell/useRunCells"; |
| 7 | +import { userConfigAtom } from "@/core/config/config"; |
| 8 | +import { parseUserConfig } from "@/core/config/config-schema"; |
| 9 | +import { initialModeAtom } from "@/core/mode"; |
| 10 | +import { store } from "@/core/state/jotai"; |
| 11 | +import { |
| 12 | + getMarimoExportContext, |
| 13 | + hasTrustedExportContext, |
| 14 | + hasTrustedNotebookContext, |
| 15 | +} from "../export-context"; |
| 16 | + |
| 17 | +type ExportContextWindow = Window & { |
| 18 | + __MARIMO_EXPORT_CONTEXT__?: { |
| 19 | + trusted: boolean; |
| 20 | + notebookCode?: string; |
| 21 | + }; |
| 22 | +}; |
| 23 | + |
| 24 | +function setAutoInstantiate(value: boolean) { |
| 25 | + const cleared = parseUserConfig({}); |
| 26 | + store.set(userConfigAtom, { |
| 27 | + ...cleared, |
| 28 | + runtime: { ...cleared.runtime, auto_instantiate: value }, |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +describe("hasTrustedNotebookContext", () => { |
| 33 | + let previousHasRunAnyCell: ExtractAtomValue<typeof hasRunAnyCellAtom>; |
| 34 | + let previousConfig: ExtractAtomValue<typeof userConfigAtom>; |
| 35 | + let previousMode: ExtractAtomValue<typeof initialModeAtom>; |
| 36 | + let w: ExportContextWindow; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + w = window as ExportContextWindow; |
| 40 | + previousHasRunAnyCell = store.get(hasRunAnyCellAtom); |
| 41 | + previousConfig = store.get(userConfigAtom); |
| 42 | + previousMode = store.get(initialModeAtom); |
| 43 | + store.set(hasRunAnyCellAtom, false); |
| 44 | + setAutoInstantiate(false); |
| 45 | + store.set(initialModeAtom, "edit"); |
| 46 | + delete w.__MARIMO_EXPORT_CONTEXT__; |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + store.set(hasRunAnyCellAtom, previousHasRunAnyCell); |
| 51 | + store.set(userConfigAtom, previousConfig); |
| 52 | + store.set(initialModeAtom, previousMode); |
| 53 | + delete w.__MARIMO_EXPORT_CONTEXT__; |
| 54 | + }); |
| 55 | + |
| 56 | + it("returns false in untrusted edit mode before interaction", () => { |
| 57 | + expect(hasTrustedNotebookContext()).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it("returns true once the user has run a cell", () => { |
| 61 | + store.set(hasRunAnyCellAtom, true); |
| 62 | + expect(hasTrustedNotebookContext()).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it("returns true when a trusted export context is installed", () => { |
| 66 | + w.__MARIMO_EXPORT_CONTEXT__ = { trusted: true }; |
| 67 | + expect(hasTrustedNotebookContext()).toBe(true); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns true when auto_instantiate is enabled", () => { |
| 71 | + setAutoInstantiate(true); |
| 72 | + expect(hasTrustedNotebookContext()).toBe(true); |
| 73 | + }); |
| 74 | + |
| 75 | + it("returns true in read mode", () => { |
| 76 | + store.set(initialModeAtom, "read"); |
| 77 | + expect(hasTrustedNotebookContext()).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it("returns false if initialMode throws (config not yet applied)", () => { |
| 81 | + store.set(initialModeAtom, undefined); |
| 82 | + expect(hasTrustedNotebookContext()).toBe(false); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("hasTrustedExportContext / getMarimoExportContext shape validation", () => { |
| 87 | + let w: ExportContextWindow; |
| 88 | + |
| 89 | + beforeEach(() => { |
| 90 | + w = window as ExportContextWindow; |
| 91 | + delete w.__MARIMO_EXPORT_CONTEXT__; |
| 92 | + }); |
| 93 | + |
| 94 | + afterEach(() => { |
| 95 | + delete w.__MARIMO_EXPORT_CONTEXT__; |
| 96 | + }); |
| 97 | + |
| 98 | + it("accepts a valid context", () => { |
| 99 | + w.__MARIMO_EXPORT_CONTEXT__ = { trusted: true, notebookCode: "x = 1" }; |
| 100 | + expect(hasTrustedExportContext()).toBe(true); |
| 101 | + expect(getMarimoExportContext()).toEqual({ |
| 102 | + trusted: true, |
| 103 | + notebookCode: "x = 1", |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("rejects a context where `trusted` is not exactly true", () => { |
| 108 | + w.__MARIMO_EXPORT_CONTEXT__ = { |
| 109 | + trusted: "yes" as unknown as true, |
| 110 | + }; |
| 111 | + expect(hasTrustedExportContext()).toBe(false); |
| 112 | + expect(getMarimoExportContext()).toBeUndefined(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("rejects a context with non-string notebookCode", () => { |
| 116 | + w.__MARIMO_EXPORT_CONTEXT__ = { |
| 117 | + trusted: true, |
| 118 | + notebookCode: 42 as unknown as string, |
| 119 | + }; |
| 120 | + expect(getMarimoExportContext()).toBeUndefined(); |
| 121 | + }); |
| 122 | +}); |
0 commit comments