mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-09 03:07:46 +08:00
- edit_diagram applies nothing when any operation fails, rejects invalid or multi-cell new_xml, validates only the target page, and returns the current page XML on every rejection (including stale edits) - Fix get_diagram reading the old diagram right after an AI write: the preview pushed its sync reply with a newer version than it was taken at - Keep a user edit that loses the race with an AI write in history and tell the user in the preview - Autofix removes only exact foreign tags (a stray <mxGraph/> deleted <mxGraphModel>), fixes tag case, drops orphan <mxPoint>s, and rejects unknown element names in model XML - Edit empty and compressed pages; PNG exports use the page on screen; tag download exports; reload from the server after a page export - Expand ~ in paths, tell the model when the browser sync timed out, use registerPrompt, require SDK ^1.31.0
131 lines
4.2 KiB
TypeScript
131 lines
4.2 KiB
TypeScript
/**
|
|
* Tests for the all-or-nothing edit_diagram core (src/edit-diagram.ts).
|
|
*/
|
|
|
|
import { beforeAll, describe, expect, it } from "vitest"
|
|
import { installDomPolyfill } from "../src/dom.js"
|
|
|
|
beforeAll(() => {
|
|
installDomPolyfill()
|
|
})
|
|
|
|
import { editDiagram, targetPageXml } from "../src/edit-diagram.js"
|
|
import { validateMxCellStructure } from "../src/xml-validation.js"
|
|
|
|
const cell = (id: string, extra = "") =>
|
|
`<mxCell id="${id}" value="${id}" vertex="1" parent="1"${extra}><mxGeometry x="0" y="0" width="80" height="40" as="geometry"/></mxCell>`
|
|
|
|
const page = (id: string, cells: string) =>
|
|
`<diagram id="${id}" name="${id}"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/>${cells}</root></mxGraphModel></diagram>`
|
|
|
|
const DOC = `<mxfile>${page("p1", cell("a") + cell("b"))}</mxfile>`
|
|
|
|
describe("editDiagram", () => {
|
|
it("applies every operation and counts them", () => {
|
|
const out = editDiagram(
|
|
DOC,
|
|
[
|
|
{ operation: "add", cell_id: "c", new_xml: cell("c") },
|
|
{ operation: "delete", cell_id: "b" },
|
|
],
|
|
{},
|
|
)
|
|
expect(out.ok).toBe(true)
|
|
if (!out.ok) return
|
|
expect(out.applied).toBe(2)
|
|
expect(out.xml).toContain('id="c"')
|
|
expect(out.xml).not.toContain('id="b"')
|
|
})
|
|
|
|
it("applies nothing when one operation fails", () => {
|
|
const out = editDiagram(
|
|
DOC,
|
|
[
|
|
{ operation: "add", cell_id: "c", new_xml: cell("c") },
|
|
{ operation: "delete", cell_id: "missing" },
|
|
],
|
|
{},
|
|
)
|
|
expect(out.ok).toBe(false)
|
|
if (out.ok) return
|
|
expect(out.pageError).toBe(false)
|
|
expect(out.errors).toEqual([
|
|
'delete missing: Cell with id="missing" not found',
|
|
])
|
|
})
|
|
|
|
it("rejects new_xml that is still invalid after auto-fix", () => {
|
|
const out = editDiagram(
|
|
DOC,
|
|
[
|
|
{
|
|
operation: "update",
|
|
cell_id: "a",
|
|
new_xml: `<mxCell id="a" style="x" style="y" vertex="1" parent="1"/>`,
|
|
},
|
|
],
|
|
{},
|
|
)
|
|
expect(out.ok).toBe(false)
|
|
if (out.ok) return
|
|
expect(out.errors[0]).toMatch(/^update a: invalid new_xml: /)
|
|
})
|
|
|
|
it("rejects several cells in one new_xml", () => {
|
|
const out = editDiagram(
|
|
DOC,
|
|
[
|
|
{
|
|
operation: "add",
|
|
cell_id: "c",
|
|
new_xml: cell("c") + cell("d"),
|
|
},
|
|
],
|
|
{},
|
|
)
|
|
expect(out.ok).toBe(false)
|
|
if (out.ok) return
|
|
expect(out.errors[0]).toContain("exactly one cell")
|
|
})
|
|
|
|
it("accepts a UserObject that wraps one mxCell", () => {
|
|
const wrapped = `<UserObject id="u" label="U" link="https://example.com"><mxCell vertex="1" parent="1"><mxGeometry as="geometry"/></mxCell></UserObject>`
|
|
const out = editDiagram(
|
|
DOC,
|
|
[{ operation: "add", cell_id: "u", new_xml: wrapped }],
|
|
{},
|
|
)
|
|
expect(out.ok).toBe(true)
|
|
})
|
|
|
|
it("is not blocked by a problem on another page", () => {
|
|
// Page p2 has a duplicate cell id, which fails validation
|
|
const doc = `<mxfile>${page("p1", cell("a"))}${page("p2", cell("x") + cell("x"))}</mxfile>`
|
|
expect(validateMxCellStructure(doc)).not.toBeNull()
|
|
const out = editDiagram(
|
|
doc,
|
|
[{ operation: "add", cell_id: "c", new_xml: cell("c") }],
|
|
{ page_id: "p1" },
|
|
)
|
|
expect(out.ok).toBe(true)
|
|
})
|
|
|
|
it("reports a missing page as a page-level error", () => {
|
|
const out = editDiagram(DOC, [{ operation: "delete", cell_id: "a" }], {
|
|
page_id: "nope",
|
|
})
|
|
expect(out.ok).toBe(false)
|
|
if (out.ok) return
|
|
expect(out.pageError).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe("targetPageXml", () => {
|
|
it("returns only the selected page", () => {
|
|
const doc = `<mxfile>${page("p1", cell("a"))}${page("p2", cell("z"))}</mxfile>`
|
|
const xml = targetPageXml(doc, { page_id: "p2" })
|
|
expect(xml).toContain('id="z"')
|
|
expect(xml).not.toContain('id="a"')
|
|
})
|
|
})
|