Files
velum-raycast/tests/rich-text.test.ts

90 lines
2.8 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { restoreRichHtml } from "../src/lib/rich-text";
const mapping = {
PERSON_01: { original: "Max Mustermann", type: "PERSON" },
ORG_01: { original: "Müller & Söhne", type: "ORG" },
};
describe("formatierte Textwiederherstellung", () => {
it("behält Formatierung bei und maskiert Originale HTML-sicher", () => {
const html = "<p>Hallo <strong>PERSON_01</strong> bei <em>ORG_01</em>.</p>";
expect(
restoreRichHtml(
html,
"Hallo PERSON_01 bei ORG_01.",
"Hallo Max Mustermann bei Müller & Söhne.",
mapping,
),
).toBe(
"<p>Hallo <strong>Max Mustermann</strong> bei <em>Müller &amp; Söhne</em>.</p>",
);
});
it("ändert weder Attribute noch unsichtbare Inhalte", () => {
const html =
'<div data-label="PERSON_01"><style>.PERSON_01{color:red}</style><b>PERSON_01</b></div>';
expect(restoreRichHtml(html, "PERSON_01", "Max Mustermann", mapping)).toBe(
'<div data-label="PERSON_01"><style>.PERSON_01{color:red}</style><b>Max Mustermann</b></div>',
);
});
it("übernimmt kodierte Sitzungsplatzhalter", () => {
const html = "<span>&lt;PERSON_01&gt;</span>";
expect(
restoreRichHtml(
html,
"<PERSON_01>",
"Max Mustermann",
mapping,
undefined,
true,
),
).toBe("<span>Max Mustermann</span>");
});
it("verwirft nicht passende oder über mehrere Textknoten verteilte HTML-Daten", () => {
expect(
restoreRichHtml(
"<b>PERSON_01</b>",
"anderer Text",
"Max Mustermann",
mapping,
),
).toBeUndefined();
expect(
restoreRichHtml(
"<b>PERSON_</b><i>01</i>",
"PERSON_01",
"Max Mustermann",
mapping,
),
).toBeUndefined();
});
it("beachtet projektbezogene Ersetzung innerhalb zusammengesetzter Begriffe", () => {
const html = "<b>abcPERSON_01xyz</b>";
expect(
restoreRichHtml(
html,
"abcPERSON_01xyz",
"abcMax Mustermannxyz",
mapping,
{ PERSON_01: "anywhere" },
),
).toBe("<b>abcMax Mustermannxyz</b>");
});
it("erhält HTML-Formatierung auch bei anders geschriebenen Platzhaltern", () => {
const html = "<b>person_01</b> und <i>Person_01</i>";
expect(restoreRichHtml(html, "person_01 und Person_01",
"max mustermann und Max Mustermann", mapping))
.toBe("<b>max mustermann</b> und <i>Max Mustermann</i>");
expect(restoreRichHtml("<b>Person_01</b> / <i>PERSON_01</i>",
"Person_01 / PERSON_01", "Person_01 / Eins", {
PERSON_01: { original: "Eins", type: "PERSON" },
person_01: { original: "Zwei", type: "PERSON" },
})).toBe("<b>Person_01</b> / <i>Eins</i>");
});
});