mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-08 18:57:47 +08:00
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
92 lines
3.3 KiB
TypeScript
92 lines
3.3 KiB
TypeScript
/**
|
|
* Tests for the per-session .drawio auto-save (src/persistence.ts).
|
|
*/
|
|
|
|
import {
|
|
existsSync,
|
|
mkdtempSync,
|
|
readdirSync,
|
|
readFileSync,
|
|
utimesSync,
|
|
writeFileSync,
|
|
} from "node:fs"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
import { afterEach, describe, expect, it } from "vitest"
|
|
import { Autosaver, defaultDataDir } from "../src/persistence.ts"
|
|
|
|
const DIAGRAM = `<mxfile><diagram id="p" name="P"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="a" vertex="1" parent="1"/></root></mxGraphModel></diagram></mxfile>`
|
|
const BLANK = `<mxfile><diagram id="p" name="P"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel></diagram></mxfile>`
|
|
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
|
|
const tempDir = () => mkdtempSync(join(tmpdir(), "mcp-autosave-"))
|
|
|
|
describe("Autosaver", () => {
|
|
it("writes the latest XML once changes settle", async () => {
|
|
const dir = tempDir()
|
|
const saver = new Autosaver(dir, 30)
|
|
saver.schedule("mcp-a", DIAGRAM.replace('id="a"', 'id="first"'))
|
|
saver.schedule("mcp-a", DIAGRAM)
|
|
const path = saver.pathFor("mcp-a") as string
|
|
expect(existsSync(path)).toBe(false)
|
|
await sleep(80)
|
|
expect(readFileSync(path, "utf-8")).toBe(DIAGRAM)
|
|
expect(readdirSync(dir)).toEqual(["mcp-a.drawio"])
|
|
})
|
|
|
|
it("skips a blank page that was never saved, but saves clearing a diagram", async () => {
|
|
const dir = tempDir()
|
|
const saver = new Autosaver(dir, 10)
|
|
saver.schedule("mcp-blank", BLANK)
|
|
await sleep(40)
|
|
expect(existsSync(saver.pathFor("mcp-blank") as string)).toBe(false)
|
|
|
|
saver.schedule("mcp-b", DIAGRAM)
|
|
saver.flush()
|
|
saver.schedule("mcp-b", BLANK)
|
|
saver.flush()
|
|
expect(readFileSync(saver.pathFor("mcp-b") as string, "utf-8")).toBe(
|
|
BLANK,
|
|
)
|
|
})
|
|
|
|
it("keeps only the newest session files and never touches other files", () => {
|
|
const dir = tempDir()
|
|
const saver = new Autosaver(dir, 10, 2)
|
|
for (const [i, id] of ["mine", "mcp-old", "mcp-mid"].entries()) {
|
|
writeFileSync(join(dir, `${id}.drawio`), DIAGRAM)
|
|
utimesSync(join(dir, `${id}.drawio`), 1000 + i, 1000 + i)
|
|
}
|
|
saver.schedule("mcp-new", DIAGRAM)
|
|
saver.flush()
|
|
expect(readdirSync(dir).sort()).toEqual([
|
|
"mcp-mid.drawio",
|
|
"mcp-new.drawio",
|
|
"mine.drawio",
|
|
])
|
|
})
|
|
|
|
it("does nothing when saving is off", () => {
|
|
const saver = new Autosaver(null)
|
|
expect(saver.pathFor("mcp-x")).toBeNull()
|
|
saver.schedule("mcp-x", DIAGRAM)
|
|
saver.flush()
|
|
})
|
|
})
|
|
|
|
describe("defaultDataDir", () => {
|
|
const original = process.env.DRAWIO_DATA_DIR
|
|
afterEach(() => {
|
|
if (original === undefined) delete process.env.DRAWIO_DATA_DIR
|
|
else process.env.DRAWIO_DATA_DIR = original
|
|
})
|
|
|
|
it("reads DRAWIO_DATA_DIR, where off disables saving", () => {
|
|
process.env.DRAWIO_DATA_DIR = "off"
|
|
expect(defaultDataDir()).toBeNull()
|
|
process.env.DRAWIO_DATA_DIR = "/tmp/x"
|
|
expect(defaultDataDir()).toBe("/tmp/x")
|
|
delete process.env.DRAWIO_DATA_DIR
|
|
expect(defaultDataDir()).toMatch(/\.next-ai-drawio$/)
|
|
})
|
|
})
|