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.

3.7 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import NodeChildProcess from "node:child_process";
import NodeFs from "node:fs";
import NodeOs from "node:os";
import NodePath from "node:path";
import NodeUrl from "node:url";
import { expect, it } from "vitest";
import { z } from "zod";

it("enforces replica dependencies for runtime and type imports", () => {
  const frontend = NodeUrl.fileURLToPath(
    new NodeUrl.URL("../..", import.meta.url),
  );
  const directory = NodeFs.mkdtempSync(
    NodePath.join(NodeOs.tmpdir(), "replica-imports-"),
  );
  const files = {
    "src/core/notebook/replica/allowed.ts": `
      import { atom } from "jotai/vanilla";
      import { z } from "zod";
      import * as Protocol from "@marimo-team/client/protocol";
      import { Logger } from "@/utils/Logger";
      import type { Document } from "./document";
      export * from "@/core/notebook/replica/events";
      type Event = import("./events").NotebookEvent;
      void import("./protocol");
    `,
    "src/core/notebook/replica/protocol.ts": `
      import type { NotificationMessage as A } from "../kernel/messages";
      import { type NotificationMessage as B } from "@/core/kernel/messages";
      import { NotificationMessage as C } from "../kernel/messages";
      import type { NotificationPayload } from "../kernel/messages";
    `,
    "src/core/notebook/replica/forbidden.ts": `
      import { useEffect } from "react";
      import type { NotificationMessage } from "../kernel/messages";
      export * from "../kernel/messages";
      export { Logger } from "@/utils/Logger/../other";
      void import("../kernel/messages");
      type Message = import("../kernel/messages").NotificationMessage;
      const module = require("../kernel/messages");
      void import(module);
      import "./__tests__/allowed";
      import "./allowed.test";
    `,
    "src/core/notebook/replica/__tests__/allowed.ts": `import { useEffect } from "react";`,
    "src/core/notebook/replica/allowed.test.ts": `import { useEffect } from "react";`,
    "src/core/notebook/replica/allowed.spec.ts": `import { useEffect } from "react";`,
    "src/core/session/allowed.ts": `import { useEffect } from "react";`,
  };
  try {
    for (const [path, source] of Object.entries(files)) {
      const destination = NodePath.join(directory, path);
      NodeFs.mkdirSync(NodePath.dirname(destination), { recursive: true });
      NodeFs.writeFileSync(destination, source);
    }
    const result = NodeChildProcess.spawnSync(
      NodePath.join(frontend, "node_modules/.bin/oxlint"),
      [
        "--config",
        NodePath.join(frontend, ".oxlintrc.json"),
        "--format",
        "json",
        directory,
      ],
      { cwd: frontend, encoding: "utf8" },
    );
    const report = z
      .object({
        diagnostics: z.array(
          z.object({
            severity: z.string(),
            filename: z.string(),
            labels: z.array(z.object({ span: z.object({ line: z.number() }) })),
          }),
        ),
      })
      .parse(JSON.parse(result.stdout));
    const errors = report.diagnostics.filter(
      (diagnostic) => diagnostic.severity === "error",
    );
    expect(
      errors
        .map((error) => ({
          file: NodePath.basename(error.filename),
          line: error.labels[0].span.line,
        }))
        .toSorted((a, b) => a.file.localeCompare(b.file) || a.line - b.line),
    ).toEqual([
      ...[2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((line) => ({
        file: "forbidden.ts",
        line,
      })),
      ...[2, 3, 4, 5].map((line) => ({ file: "protocol.ts", line })),
    ]);
    expect(result.status).toBe(1);
  } finally {
    NodeFs.rmSync(directory, { recursive: true, force: true });
  }
});