mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-09 19:19:50 +08:00
Found by the PR review, each with a test that failed first:
- get_diagram during a page export returned the one-page projection on
screen as the whole document (6 of 6 times when timed so). The preview
page no longer answers a sync while a projection shows, and syncs after
reloading, so the poll that restores the real document exports it.
- Exports are numbered on the server too: a late result of an export that
timed out was saved as the next export's file.
- In Chrome, a new_xml with a syntax error counted the <parsererror>
element as a second cell, so the web app rejected edits that auto-fix
repairs ("must contain exactly one cell").
- hasCells missed single-quoted ids, so screenshot_diagram called such a
diagram empty and auto-save never created its file.
- A literal \n directly under a <diagram> that has a model passed
validation; only text-only pages are compressed data.
- A wrapped mxCell repeating its UserObject's id took the wrapper's place
in edits, so delete and update left an empty or nested wrapper.
- Bare cells with a shape or edge id of "0" or "1" are rejected with a
clear message instead of being renamed, which broke their edges.
- DRAWIO_DATA_DIR expands ~, which JSON configs pass on as it is.
100 lines
4.0 KiB
TypeScript
100 lines
4.0 KiB
TypeScript
/**
|
|
* Tests for wrapCellsInModel: the model may send only the mxCell elements of
|
|
* a page, like in the web app, and the server adds the wrapper and root cells.
|
|
*/
|
|
|
|
import { beforeAll, describe, expect, it } from "vitest"
|
|
import { installDomPolyfill } from "../src/dom.ts"
|
|
|
|
beforeAll(() => {
|
|
installDomPolyfill()
|
|
})
|
|
|
|
import { prepareNewDiagram } from "../src/new-diagram.ts"
|
|
import { hasCells, wrapCellsInModel } from "../src/pages.ts"
|
|
import { validateAndFixXml } from "../src/xml-validation.ts"
|
|
|
|
const A = `<mxCell id="2" value="A" vertex="1" parent="1"><mxGeometry x="0" y="0" width="80" height="40" as="geometry"/></mxCell>`
|
|
const B = `<mxCell id="3" value="B" vertex="1" parent="1"><mxGeometry x="200" y="0" width="80" height="40" as="geometry"/></mxCell>`
|
|
const ROOTS = `<mxCell id="0"/><mxCell id="1" parent="0"/>`
|
|
|
|
describe("wrapCellsInModel", () => {
|
|
it("wraps sibling cells so they pass validation", () => {
|
|
expect(validateAndFixXml(A + B).valid).toBe(false)
|
|
const wrapped = wrapCellsInModel(A + B)
|
|
expect(wrapped).toBe(
|
|
`<mxGraphModel><root>${ROOTS}${A}${B}</root></mxGraphModel>`,
|
|
)
|
|
expect(validateAndFixXml(wrapped).valid).toBe(true)
|
|
})
|
|
|
|
it("replaces root cells the model wrote itself", () => {
|
|
const wrapped = wrapCellsInModel(
|
|
`<mxCell id="0"></mxCell><mxCell id="1" parent="0"/>${A}`,
|
|
)
|
|
expect(wrapped.match(/id="0"/g)).toHaveLength(1)
|
|
expect(wrapped.match(/id="1"/g)).toHaveLength(1)
|
|
expect(wrapped).toContain(A)
|
|
})
|
|
|
|
it("unwraps a <root> and drops trailing provider tags", () => {
|
|
const wrapped = wrapCellsInModel(
|
|
`<root>${A}</root></invoke></function_calls>`,
|
|
)
|
|
expect(wrapped).toBe(
|
|
`<mxGraphModel><root>${ROOTS}${A}</root></mxGraphModel>`,
|
|
)
|
|
})
|
|
|
|
it("drops comments and text before the first cell", () => {
|
|
const expected = `<mxGraphModel><root>${ROOTS}${A}</root></mxGraphModel>`
|
|
expect(wrapCellsInModel(`Here is the diagram: ${A}`)).toBe(expected)
|
|
expect(wrapCellsInModel(`<!-- boxes -->\n${A}`)).toBe(expected)
|
|
})
|
|
|
|
it("leaves <mxGraphModel> and <mxfile> input unchanged", () => {
|
|
const model = `<mxGraphModel><root>${ROOTS}${A}</root></mxGraphModel>`
|
|
const file = `<mxfile><diagram id="p" name="P">${model}</diagram></mxfile>`
|
|
expect(wrapCellsInModel(model)).toBe(model)
|
|
expect(wrapCellsInModel(file)).toBe(file)
|
|
})
|
|
|
|
it("keeps a UserObject cell at the end", () => {
|
|
const wrapped = `<UserObject id="u" label="U"><mxCell vertex="1" parent="1"><mxGeometry as="geometry"/></mxCell></UserObject>`
|
|
expect(wrapCellsInModel(A + wrapped)).toContain(wrapped)
|
|
expect(wrapCellsInModel(`${wrapped}</invoke>`)).toBe(
|
|
`<mxGraphModel><root>${ROOTS}${wrapped}</root></mxGraphModel>`,
|
|
)
|
|
})
|
|
})
|
|
|
|
describe("hasCells", () => {
|
|
it("counts cells written with single quotes", () => {
|
|
expect(hasCells(`<mxCell id='2' vertex='1' parent='1'/>`)).toBe(true)
|
|
expect(hasCells(`<mxCell id='0'/><mxCell id='1' parent='0'/>`)).toBe(
|
|
false,
|
|
)
|
|
})
|
|
})
|
|
|
|
describe("prepareNewDiagram", () => {
|
|
it("rejects a shape that uses a root cell id", () => {
|
|
// It would clash with the added root cell "1" and be renamed,
|
|
// which breaks the edges that point to it
|
|
const cells =
|
|
`<mxCell id="1" value="Start" vertex="1" parent="1"><mxGeometry x="0" y="0" width="80" height="40" as="geometry"/></mxCell>` +
|
|
`<mxCell id="3" edge="1" parent="1" source="1" target="2"><mxGeometry relative="1" as="geometry"/></mxCell>`
|
|
const out = prepareNewDiagram(cells)
|
|
expect(out.ok).toBe(false)
|
|
if (out.ok) return
|
|
expect(out.error).toMatch(/"0" and "1"/)
|
|
})
|
|
|
|
it("still accepts the root cells sent along with the shapes", () => {
|
|
const out = prepareNewDiagram(
|
|
`<mxCell id="0"/><mxCell id="1" parent="0"/>${A}`,
|
|
)
|
|
expect(out.ok).toBe(true)
|
|
})
|
|
})
|