Files
next-ai-draw-io/packages/mcp-server/tests/wrap-cells.test.ts
T
dayuan.jiang b7c543ca70 refactor(mcp-server): make the diagram modules usable from the web app
The web app will reuse the MCP server's XML engine instead of its own
copy in lib/utils.ts, so these modules now run in the browser too.

- Relative imports end in .ts, rewritten to .js by tsc
  (rewriteRelativeImportExtensions); Next.js resolves them directly
- Every module uses the global DOMParser/XMLSerializer: native in the
  browser, linkedom in Node via installDomPolyfill. pages.ts parsed with
  linkedom but serialized with the global serializer, which throws in
  the browser
- The saxes syntax check moves to xml-syntax.ts, so the browser does not
  pull in linkedom; it now also rejects undeclared prefixes such as
  xlink:, as the browser does
- Page decompression uses pako and atob instead of node:zlib and Buffer
- hasCells moves to pages.ts, away from the file system code
- The duplicate cell id check counts UserObject/object ids
- wrapCellsInModel drops comments and text before the first cell, which
  the web app accepts today
- validateAndFixXml takes { strict: false } for diagrams with user content
- Web tests run these modules with a browser DOM (jsdom)
- saxes becomes a direct dependency of the web app
2026-10-04 12:25:53 +09:00

69 lines
2.8 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 { 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>`,
)
})
})