/**
* Tests for prepareNewDiagram's truncation check: output that stops inside an
* mxCell gets a clear error instead of one about the wrapper tags the model
* never sent.
*/
import { beforeAll, describe, expect, it } from "vitest"
import { installDomPolyfill } from "../src/dom.ts"
beforeAll(() => {
installDomPolyfill()
})
import {
isMxCellXmlComplete,
prepareNewDiagram,
truncatedCellError,
} from "../src/new-diagram.ts"
describe("isMxCellXmlComplete", () => {
it("returns false for empty/null input", () => {
expect(isMxCellXmlComplete("")).toBe(false)
expect(isMxCellXmlComplete(null)).toBe(false)
expect(isMxCellXmlComplete(undefined)).toBe(false)
})
it("returns true for self-closing mxCell", () => {
const xml =
''
expect(isMxCellXmlComplete(xml)).toBe(true)
// A raw "<" in a value (escaped later by the auto-fix)
expect(
isMxCellXmlComplete(
'',
),
).toBe(true)
})
it("returns true for mxCell with closing tag", () => {
const xml = `
`
expect(isMxCellXmlComplete(xml)).toBe(true)
})
it("returns false for truncated mxCell", () => {
const xml =
' {
const xml = `
{
const xml = `
`
expect(isMxCellXmlComplete(xml)).toBe(false)
})
it("returns false when output stops after of an open mxCell", () => {
const xml = `
`
expect(isMxCellXmlComplete(xml)).toBe(false)
})
it("returns true for a self-closing last mxCell with > in its value", () => {
const xml = `
`
expect(isMxCellXmlComplete(xml)).toBe(true)
})
it("returns true for multiple complete mxCells", () => {
const xml = `
`
expect(isMxCellXmlComplete(xml)).toBe(true)
})
})
describe("prepareNewDiagram with cut-off output", () => {
const A = ``
it("rejects a cell cut off inside an attribute", () => {
const cut = `${A} {
const cut = `${A}`
const out = prepareNewDiagram(cut)
expect(out.ok).toBe(false)
if (out.ok) return
expect(out.error).toContain("cut off")
expect(out.error).not.toContain("")
})
it("quotes at most the last 300 characters", () => {
const cut = `${A} {
const cells = `${A}`
expect(truncatedCellError(cells)).toBeNull()
expect(prepareNewDiagram(cells).ok).toBe(true)
})
it("leaves a closing tag the auto-fix repairs to the auto-fix", () => {
// is a typo, not a cut
const typo = A.replace("", "")
expect(truncatedCellError(typo)).toBeNull()
expect(prepareNewDiagram(typo).ok).toBe(true)
})
it("leaves input without any cell to the validator", () => {
for (const input of ["", "Here is the diagram"]) {
expect(truncatedCellError(input)).toBeNull()
const out = prepareNewDiagram(input)
expect(out.ok).toBe(false)
if (!out.ok) expect(out.error).not.toContain("cut off")
}
})
it("never checks a full ", () => {
const cut = ` {
const BLUE =
'\n'
const A =
'\n'
it("accepts complete compact cells after the definitions and expands them", () => {
const out = prepareNewDiagram(
`${BLUE}${A}`,
)
expect(out.ok).toBe(true)
if (!out.ok) return
expect(out.xml).toContain("fillColor=#dae8fc")
expect(out.xml).toContain(" {
const cut = `${BLUE}${A} {
const cut = `${BLUE}${A} {
const slip = `${A} {
expect(truncatedCellError(BLUE)).toBeNull()
const out = prepareNewDiagram(BLUE)
expect(out.ok).toBe(false)
if (out.ok) return
expect(out.error).not.toContain("cut off")
expect(out.error).toContain("no cells")
})
it("leaves an unclosed definition to the style check", () => {
const cut = `${BLUE}