Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

5.6 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import assert from "node:assert/strict";
import test from "node:test";

import { markdownToSpeechText } from "../src/features/chat/utils/speech-text.ts";

test("read-aloud does not speak emphasis markers (#12547)", () => {
  assert.equal(
    markdownToSpeechText(
      "This is **bold**, *italic*, __also bold__ and ~~gone~~.",
    ),
    "This is bold, italic, also bold and gone.",
  );
});

test("headings, bullets and numbered items lose their symbols but keep a pause", () => {
  const spoken = markdownToSpeechText(
    "## Steps\n\n- Install the package\n- Run `unsloth studio`\n\n1. First\n2. Second!",
  );
  assert.equal(
    spoken,
    "Steps.\nInstall the package.\nRun unsloth studio.\nFirst.\nSecond!",
  );
  assert.doesNotMatch(spoken, /[#*`]|^\s*[-\d]/m);
});

test("links and images read as their text, not their URLs", () => {
  assert.equal(
    markdownToSpeechText(
      "See [the docs](https://unsloth.ai/docs) and ![a cat](cat.png).",
    ),
    "See the docs and a cat.",
  );
});

test("code keeps its content but drops the fences", () => {
  assert.equal(
    markdownToSpeechText("Run this:\n\n```bash\npip install unsloth\n```"),
    "Run this:\npip install unsloth",
  );
});

test("blockquotes, rules, tables and html are read as plain text", () => {
  assert.equal(
    markdownToSpeechText(
      "> quoted **line**\n\n---\n\n| Model | Size |\n| --- | --- |\n| Qwen | 7B |\n\n<br>",
    ),
    "quoted line\nModel, Size.\nQwen, 7B.",
  );
});

test("math is read from its source and plain text passes through unchanged", () => {
  assert.equal(
    markdownToSpeechText("Euler: $e^{i\\pi}+1=0$"),
    "Euler: e^{i\\pi}+1=0",
  );
  assert.equal(markdownToSpeechText("Just a sentence."), "Just a sentence.");
  assert.equal(
    markdownToSpeechText("It costs $5 and $10 today."),
    "It costs $5 and $10 today.",
  );
  assert.equal(markdownToSpeechText("Inline \\(x^2\\)"), "Inline x^2");
  assert.equal(markdownToSpeechText(""), "");
});

test("text inside raw HTML and footnotes is read, as the page shows it", () => {
  assert.equal(
    markdownToSpeechText(
      "<details><summary>More</summary>\n\nHidden <b>text</b>\n\n</details>\n\nSee note[^1].\n\n[^1]: The note.\n\nEnd.",
    ),
    "More\nHidden text\nSee note.\nEnd.\nThe note.",
  );
  assert.equal(
    markdownToSpeechText("<div>Visible answer</div>\n<!-- note -->"),
    "Visible answer",
  );
});

test("tags the page shows as text are read, and entities read as their characters", () => {
  assert.equal(
    markdownToSpeechText("Use <placeholder> here, a Vec<T>, and <b>bold</b>."),
    "Use <placeholder> here, a Vec<T>, and bold.",
  );
  assert.equal(
    markdownToSpeechText("<div>AT&amp;T &lt; 5 &#33; &#x41;</div>"),
    "AT&T < 5 ! A",
  );
});

test("raw HTML reads as the chat shows it: quoted attributes, literal tags, named entities", () => {
  assert.equal(
    markdownToSpeechText('<div aria-label="a > b">visible</div>'),
    "visible",
  );
  assert.equal(
    markdownToSpeechText(
      "<div>The <small> tag and <script>alert(1)</script></div>",
    ),
    "The <small> tag and <script>alert(1)</script>",
  );
  assert.equal(
    markdownToSpeechText("<div>caf&eacute; &copy; &hellip;</div>"),
    "café © …",
  );
});

test("HTML tags keep the word boundaries the page shows", () => {
  assert.equal(markdownToSpeechText("hello<br>world"), "hello\nworld");
  assert.equal(
    markdownToSpeechText("| a<br>b | c |\n|---|---|\n| 1 | 2 |"),
    "a\nb, c.\n1, 2.",
  );
  assert.equal(markdownToSpeechText("<div>H<sub>2</sub>O</div>"), "H2O");
});

test("read-aloud skips emoji sequences but preserves surrounding words", () => {
  const spoken = markdownToSpeechText(
    "Japan 🇯🇵 developer 👩‍💻 thumbs 👍🏽 heart ❤️",
  );

  assert.match(spoken, /Japan\s+developer\s+thumbs\s+heart/);

  for (const emoji of ["🇯🇵", "👩‍💻", "👍🏽", "❤️"]) {
    assert.equal(spoken.includes(emoji), false);
  }
});

test("read-aloud handles emoji-only messages", () => {
  assert.equal(markdownToSpeechText("😀 👨‍💻 🇯🇵").trim(), "");
  for (const input of [
    "# 😀",
    "- 😀\n- 👍",
    "1. 😀",
    "| 😀 | 😘 |\n| --- | --- |\n| 🥰 | ❤️ |",
  ]) {
    assert.equal(markdownToSpeechText(input), "", input);
  }
});

test("read-aloud preserves word boundaries around emojis", () => {
  for (const input of [
    "Hello😀world",
    "Hello😀😀world",
    "Hello👩‍💻👍🏽world",
  ]) {
    assert.equal(markdownToSpeechText(input), "Hello world", input);
  }
});

test("read-aloud cleans the reported German and English replies", () => {
  assert.equal(
    markdownToSpeechText("Hallo! 🥰❤️ Schön, dass du da bist. 😘"),
    "Hallo! Schön, dass du da bist.",
  );
  assert.equal(
    markdownToSpeechText("Hello! 🥰❤️ It's wonderful to see you. 😘"),
    "Hello! It's wonderful to see you.",
  );
});

test("read-aloud preserves ordinary symbols and whitespace without emojis", () => {
  assert.equal(
    markdownToSpeechText("© 2026 ® ™ ℹ 123 # *"),
    "© 2026 ® ™ ℹ 123 # *",
  );
  assert.equal(markdownToSpeechText("`two  spaces`"), "two  spaces");
  assert.equal(markdownToSpeechText("word🥰❤️, next"), "word, next");
  assert.equal(markdownToSpeechText("$A ↔ B$"), "A ↔ B");
  assert.equal(markdownToSpeechText("Sex: ♀ / ♂"), "Sex: ♀ / ♂");
  assert.equal(markdownToSpeechText("Watch ⌚\uFE0E"), "Watch ⌚\uFE0E");
  assert.equal(markdownToSpeechText("©️ ®️ ™️ ℹ️"), "");
});