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.

4.0 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */
import { describe, expect, it } from "vitest";
import { parseContent } from "../url-parser";

describe("parseContent", () => {
  it("handles data URIs", () => {
    const parts = parseContent("data:image/png;base64,iVBOR");
    expect(parts).toEqual([
      {
        type: "image",
        url: "data:image/png;base64,iVBOR",
      },
    ]);
  });

  it("handles complete URLs", () => {
    const parts = parseContent("https://marimo.io/path?query=value");
    expect(parts).toEqual([
      { type: "url", url: "https://marimo.io/path?query=value" },
    ]);
  });

  it("handles multiple URLs with text", () => {
    const parts = parseContent(
      "Visit https://marimo.io and https://github.com/marimo-team",
    );
    expect(parts).toEqual([
      { type: "text", value: "Visit " },
      { type: "url", url: "https://marimo.io" },
      { type: "text", value: " and " },
      { type: "url", url: "https://github.com/marimo-team" },
    ]);
  });

  it("handles text with data URIs", () => {
    // Currently doesn't detect mixed content
    const parts = parseContent("Image: data:image/png;base64,iVBOR");
    expect(parts).toEqual([
      { type: "text", value: "Image: data:image/png;base64,iVBOR" },
    ]);
  });

  it("handles data URIs, text and images", () => {
    const parts = parseContent(
      "this is a picture: https://avatars.githubusercontent.com/u/123 and data:image/png;base64,iVBOR",
    );
    expect(parts).toEqual([
      { type: "text", value: "this is a picture: " },
      { type: "image", url: "https://avatars.githubusercontent.com/u/123" },
      { type: "text", value: " and data:image/png;base64,iVBOR" },
    ]);
  });

  it("handles plain text without URLs", () => {
    const parts = parseContent("Hello world");
    expect(parts).toEqual([{ type: "text", value: "Hello world" }]);
  });

  it("handles image URLs with various extensions", () => {
    const extensions = ["png", "jpg", "jpeg", "gif", "webp", "svg", "ico"];
    extensions.forEach((ext) => {
      const parts = parseContent(`Image: https://example.com/image.${ext}`);
      expect(parts).toContainEqual({
        type: "image",
        url: `https://example.com/image.${ext}`,
      });
    });
  });

  it("handles known image domains", () => {
    const parts = parseContent(
      "Avatar: https://avatars.githubusercontent.com/u/123",
    );
    expect(parts).toContainEqual({
      type: "image",
      url: "https://avatars.githubusercontent.com/u/123",
    });
  });

  it("preserves newlines between URLs", () => {
    const parts = parseContent("https://marimo.io\nhttps://github.com\n");
    expect(parts).toEqual([
      { type: "url", url: "https://marimo.io" },
      { type: "text", value: "\n" },
      { type: "url", url: "https://github.com" },
      { type: "text", value: "\n" },
    ]);
  });

  it("preserves whitespace in mixed content", () => {
    const parts = parseContent(
      "Line 1: https://marimo.io\nLine 2: https://github.com",
    );
    expect(parts).toEqual([
      { type: "text", value: "Line 1: " },
      { type: "url", url: "https://marimo.io" },
      { type: "text", value: "\nLine 2: " },
      { type: "url", url: "https://github.com" },
    ]);
  });

  it("stops at JSON delimiters instead of swallowing them (#10567)", () => {
    const parts = parseContent(
      '[{"name":"inv.pdf","url":"https://example.com/p/AAA"}]',
    );
    expect(parts).toEqual([
      { type: "text", value: '[{"name":"inv.pdf","url":"' },
      { type: "url", url: "https://example.com/p/AAA" },
      { type: "text", value: '"}]' },
    ]);
  });

  it("does not include a trailing double-quote in the href", () => {
    const parts = parseContent('"https://example.com/x"');
    expect(parts).toContainEqual({
      type: "url",
      url: "https://example.com/x",
    });
  });

  it("keeps apostrophes, which are valid in URLs", () => {
    const parts = parseContent("https://example.com/O'Reilly");
    expect(parts).toEqual([
      { type: "url", url: "https://example.com/O'Reilly" },
    ]);
  });
});