import { test } from "node:test"; import assert from "node:assert/strict"; import { headingEdit } from "../../../../src/lib/features/pm/model/description-format.ts"; function apply(value: string, start: number, end = start) { const edit = headingEdit(value, start, end); return { value: value.slice(0, edit.start) + edit.text + value.slice(edit.end), start: edit.selectionStart, end: edit.selectionEnd }; } test("Heading formats the caret line and preserves the caret inside its text", () => { assert.deepEqual(apply("Before\nTyped text\nAfter", 12), { value: "Before\n## Typed text\nAfter", start: 15, end: 15 }); assert.deepEqual(apply("Typed text", 0), { value: "## Typed text", start: 3, end: 3 }); assert.deepEqual(apply("Typed text", 10), { value: "## Typed text", start: 13, end: 13 }); }); test("Heading preserves selected text and formats all selected lines", () => { assert.deepEqual(apply("One line\nTwo lines\nLast", 2, 12), { value: "## One line\n## Two lines\nLast", start: 5, end: 18 }); assert.deepEqual(apply("One\nTwo", 0, 4), { value: "## One\nTwo", start: 3, end: 7 }); }); test("Heading changes an existing heading without duplicate markers", () => { assert.deepEqual(apply("# Title", 7), { value: "## Title", start: 8, end: 8 }); assert.deepEqual(apply("## Title", 5), { value: "## Title", start: 5, end: 5 }); assert.deepEqual(apply("### Title", 1), { value: "## Title", start: 3, end: 3 }); }); test("Heading leaves the caret after the marker on empty lines", () => { assert.deepEqual(apply("", 0), { value: "## ", start: 3, end: 3 }); assert.deepEqual(apply("Text\n", 5), { value: "Text\n## ", start: 8, end: 8 }); assert.deepEqual(apply("\nText", 0), { value: "## \nText", start: 3, end: 3 }); });