mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-09-01 17:10:24 +08:00
* fix(mcp): keep diagram context valid during edits Closes #885 * fix(mcp): replace edit_diagram time gate with content comparison The 30s wall-clock gate rejected slow-but-correct clients (#885). Instead of a timeout, remember the exact state-store XML the model last saw (get_diagram / create_new_diagram / edit_diagram / page CRUD) and reject edit_diagram only when the live browser state differs - i.e. the user made edits the model hasn't seen yet. Slow reasoning no longer trips the gate, while unseen manual edits still do. * docs(mcp): align edit_diagram/get_diagram descriptions with content-based gate The 'You MUST call get_diagram BEFORE this tool' requirement and the 'Skipping get_diagram WILL cause user's changes to be LOST' warning no longer match server behavior: a stale edit is rejected with no side effects, never silently applied. Describe the freshness check instead, and direct get_diagram usage at its real purpose - learning the current diagram content when the model doesn't already know it. * fix(mcp): compare diagram content structurally in the edit gate draw.io re-serialises the document when pushing state back (attribute order, pretty-printing, regenerated diagram ids, viewport attributes, mxfile host), so byte comparison could flag an unchanged diagram as stale. Fingerprint what a user can actually change instead - page set, page names, and each page's root cell tree with sorted attributes - keeping byte equality as the fast path. A bare mxGraphModel now also fingerprints identically to its single-page mxfile wrapping. * fix(mcp): don't compare page names against bare mxGraphModel pushes A bare <mxGraphModel> pushed by the embed/sync path carries no page name, so normalizeToMxfile invents "Page-1" — falsely reading any custom page name as a content change and re-triggering the stale rejection on every edit. When either side of the gate comparison is a bare mxGraphModel, fingerprint cell trees only; full-mxfile comparisons still detect renames. * chore(mcp): bump version to 0.2.2 * chore(mcp): sync package-lock.json version to 0.2.2 --------- Co-authored-by: dayuan.jiang <jdy.toh@gmail.com>
133 lines
5.3 KiB
TypeScript
133 lines
5.3 KiB
TypeScript
/**
|
|
* Unit tests for the edit_diagram workflow gate (edit-gate.ts).
|
|
*
|
|
* The gate replaced the old 30-second wall-clock rule (#885): an edit is
|
|
* allowed when the model has seen the current browser state, no matter how
|
|
* long ago — and rejected when the browser state moved since. "Seen" is
|
|
* judged structurally, so draw.io's re-serialisation of the same content
|
|
* (attribute order, whitespace, viewport attributes, wrapper shape) never
|
|
* reads as a user edit.
|
|
*/
|
|
|
|
import { DOMParser } from "linkedom"
|
|
import { beforeAll, describe, expect, it } from "vitest"
|
|
|
|
beforeAll(() => {
|
|
;(globalThis as any).DOMParser = DOMParser
|
|
})
|
|
|
|
import { checkEditGate, contentFingerprint } from "../src/edit-gate.js"
|
|
|
|
const XML_A = `<mxfile host="app.diagrams.net"><diagram id="p1" name="Page-1"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="box1" value="Hello" style="rounded=0;" vertex="1" parent="1"><mxGeometry x="40" y="40" width="120" height="60" as="geometry"/></mxCell></root></mxGraphModel></diagram></mxfile>`
|
|
|
|
// The same document as draw.io re-serialises it on autosave: different host,
|
|
// regenerated diagram id, viewport attributes on mxGraphModel, re-ordered
|
|
// cell attributes, pretty-printed whitespace.
|
|
const XML_A_RESERIALIZED = `<mxfile host="embed.diagrams.net">
|
|
<diagram id="regenerated-id" name="Page-1">
|
|
<mxGraphModel dx="1596" dy="743" grid="1" pageWidth="827" pageHeight="1169">
|
|
<root>
|
|
<mxCell id="0" />
|
|
<mxCell id="1" parent="0" />
|
|
<mxCell id="box1" parent="1" style="rounded=0;" value="Hello" vertex="1">
|
|
<mxGeometry height="60" width="120" x="40" y="40" as="geometry" />
|
|
</mxCell>
|
|
</root>
|
|
</mxGraphModel>
|
|
</diagram>
|
|
</mxfile>`
|
|
|
|
// A real user edit: box1 moved to a different position.
|
|
const XML_B = XML_A.replace('x="40" y="40"', 'x="300" y="200"')
|
|
|
|
// Bare mxGraphModel with identical page content to XML_A.
|
|
const XML_A_BARE = `<mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="box1" value="Hello" style="rounded=0;" vertex="1" parent="1"><mxGeometry x="40" y="40" width="120" height="60" as="geometry"/></mxCell></root></mxGraphModel>`
|
|
|
|
describe("checkEditGate", () => {
|
|
it("rejects when no diagram context was ever established", () => {
|
|
expect(checkEditGate("", XML_A)).toEqual({
|
|
ok: false,
|
|
reason: "no-context",
|
|
})
|
|
})
|
|
|
|
it("allows when the browser state is exactly what the model saw", () => {
|
|
expect(checkEditGate(XML_A, XML_A)).toEqual({ ok: true })
|
|
})
|
|
|
|
it("allows when the browser state is a re-serialisation of the same content", () => {
|
|
expect(checkEditGate(XML_A, XML_A_RESERIALIZED)).toEqual({ ok: true })
|
|
})
|
|
|
|
it("rejects when a cell actually changed", () => {
|
|
expect(checkEditGate(XML_A, XML_B)).toEqual({
|
|
ok: false,
|
|
reason: "stale",
|
|
})
|
|
})
|
|
|
|
it("rejects a real edit even when wrapped in re-serialisation noise", () => {
|
|
const movedAndReserialized = XML_A_RESERIALIZED.replace(
|
|
'x="40" y="40"',
|
|
'x="300" y="200"',
|
|
)
|
|
expect(checkEditGate(XML_A, movedAndReserialized)).toEqual({
|
|
ok: false,
|
|
reason: "stale",
|
|
})
|
|
})
|
|
|
|
it("allows when the store has no live entry to compare against", () => {
|
|
expect(checkEditGate(XML_A, "")).toEqual({ ok: true })
|
|
})
|
|
|
|
// A bare <mxGraphModel> push carries no page name, so the gate must not
|
|
// compare the invented "Page-1" wrapper name against the real one.
|
|
it("allows a bare mxGraphModel push when the page has a custom name", () => {
|
|
const seenRenamed = XML_A.replace('name="Page-1"', 'name="Arch"')
|
|
expect(checkEditGate(seenRenamed, XML_A_BARE)).toEqual({ ok: true })
|
|
})
|
|
|
|
it("still rejects a bare mxGraphModel push whose cells changed", () => {
|
|
const seenRenamed = XML_A.replace('name="Page-1"', 'name="Arch"')
|
|
const bareMoved = XML_A_BARE.replace('x="40" y="40"', 'x="300" y="200"')
|
|
expect(checkEditGate(seenRenamed, bareMoved)).toEqual({
|
|
ok: false,
|
|
reason: "stale",
|
|
})
|
|
})
|
|
})
|
|
|
|
describe("contentFingerprint", () => {
|
|
it("is invariant under draw.io re-serialisation", () => {
|
|
expect(contentFingerprint(XML_A)).toBe(
|
|
contentFingerprint(XML_A_RESERIALIZED),
|
|
)
|
|
})
|
|
|
|
it("treats a bare mxGraphModel like its one-page mxfile wrapping", () => {
|
|
expect(contentFingerprint(XML_A_BARE)).toBe(contentFingerprint(XML_A))
|
|
})
|
|
|
|
it("changes when a cell attribute changes", () => {
|
|
expect(contentFingerprint(XML_A)).not.toBe(contentFingerprint(XML_B))
|
|
})
|
|
|
|
it("changes when a page is renamed", () => {
|
|
const renamed = XML_A.replace('name="Page-1"', 'name="Renamed"')
|
|
expect(contentFingerprint(XML_A)).not.toBe(contentFingerprint(renamed))
|
|
})
|
|
|
|
it("changes when a page is added", () => {
|
|
const twoPages = XML_A.replace(
|
|
"</mxfile>",
|
|
`<diagram id="p2" name="Page-2"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel></diagram></mxfile>`,
|
|
)
|
|
expect(contentFingerprint(XML_A)).not.toBe(contentFingerprint(twoPages))
|
|
})
|
|
|
|
it("falls back to the raw string for unparseable input", () => {
|
|
expect(contentFingerprint("not xml at all")).toBe("not xml at all")
|
|
})
|
|
})
|