Files
next-ai-draw-io/packages/mcp-server/tests/persistence.test.ts
T
Dayuan Jiang 77be43d996 feat(mcp-server): saved sessions, restore_version and History on disk (#981)
* feat(mcp-server): list and resume saved diagram sessions

start_session accepts an optional session_id to continue a saved diagram
under the same preview URL and auto-save file, reporting the restored
pages. New read-only tool list_saved_diagrams prints every auto-saved
file newest first with its pages. Autosaver gains list() and dataDir().
INSTRUCTIONS and both READMEs mention the new tool.

* feat(mcp-server): add restore_version tool for undo and redo from History

Add the restore_version write tool so the model can put an earlier
version from History back on the canvas (steps_back, default 1). The
current canvas is kept in History unless it is the blank page, so a redo
is possible. http-server.ts exports restoreHistoryEntry, shared by the
HTTP restore endpoint and the tool; history.ts exports HistoryEntry and
otherVersions (distinct versions other than the canvas, newest first).
The drawing guide tells the model to call restore_version when the user
asks to undo, and both READMEs document the tool.

* feat(mcp-server): save the diagram History next to the auto-saved file

The Autosaver writes each session's History (the XML of its last 20 versions, without thumbnails) to <session-id>.history.json beside the .drawio file, after the same 1 second delay and only when the entries changed. When a saved session is resumed after an idle timeout or a process restart, the saved History is loaded back before the diagram, so the History panel and restore_version keep working. Removing the oldest files also removes their History files.

* fix(mcp-server): review fixes for saved sessions, undo tool, history on disk

* fix(mcp-server): Codex review fixes for saved sessions, undo and History on disk

- start_session resumes every saved file, also a cleared one or one of
  empty named pages, and decompresses pages draw.io compressed
- History kept by a lost user edit or a recovering tab is saved too
- A diagram cleared before its first save keeps its History on disk
- The History file is written again after the 50-file cap removed it
- restore_version compares versions like the edit gate (a bare
  mxGraphModel from the browser has no page name), checks the restore
  result, and says "none" when no version is left
- The preview closes an open History list when the server state was
  recreated, since the entries got new ids
- start_session's description mentions session_id

* fix(mcp-server): compare History versions pairwise and tie restores to the server state

Second Codex review of the History fixes:
- One bare mxGraphModel in History no longer hides page renames in every
  comparison: names are left out only when one of the two is bare
- A History list asked for before the server recreated the session is
  dropped, and a restore names the state its list belongs to; the server
  refuses one from a lost state (History ids change after a restart)

* fix(mcp-server): restore from a History list with the state it was taken in

Third Codex review: a restore sent the tab's current state, so an old
list shown after a failed refresh passed the server's check. The list now
keeps the state it came from, and the server refuses a missing (null)
state too; only a tab of an older version, which sends none, is let
through.
2026-10-11 20:23:23 +09:00

388 lines
14 KiB
TypeScript

/**
* Tests for the per-session .drawio auto-save (src/persistence.ts).
*/
import {
chmodSync,
existsSync,
mkdtempSync,
readdirSync,
readFileSync,
rmSync,
statSync,
utimesSync,
writeFileSync,
} from "node:fs"
import { homedir, tmpdir } from "node:os"
import { join } from "node:path"
import { afterEach, beforeAll, describe, expect, it } from "vitest"
import { installDomPolyfill } from "../src/dom.ts"
import { Autosaver, defaultDataDir } from "../src/persistence.ts"
beforeAll(() => {
installDomPolyfill()
})
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="page-1" name="Page-1"><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("saves a new document of empty pages the user named", async () => {
const dir = tempDir()
const saver = new Autosaver(dir, 10)
const emptyPage = (name: string) =>
`<diagram id="${name}" name="${name}"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel></diagram>`
const pages = `<mxfile>${emptyPage("Planning")}${emptyPage("Notes")}</mxfile>`
saver.schedule("mcp-pages", pages)
saver.flush()
expect(
readFileSync(saver.pathFor("mcp-pages") as string, "utf-8"),
).toBe(pages)
})
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)
// Session ids look like mcp-<time in base 36>-<random>
const ids = [
"mine",
"mcp-notes",
"mcp-system-design-v2",
"mcp-mgd0a1b2-old123",
"mcp-mgd0a1b3-mid456",
]
for (const [i, id] of ids.entries()) {
writeFileSync(join(dir, `${id}.drawio`), DIAGRAM)
utimesSync(join(dir, `${id}.drawio`), 1000 + i, 1000 + i)
}
// A session's History file goes with its diagram
writeFileSync(join(dir, "mcp-mgd0a1b2-old123.history.json"), "[]")
writeFileSync(join(dir, "mcp-mgd0a1b3-mid456.history.json"), "[]")
saver.schedule("mcp-mgd0a1b4-new789", DIAGRAM)
saver.flush()
expect(readdirSync(dir).sort()).toEqual([
"mcp-mgd0a1b3-mid456.drawio",
"mcp-mgd0a1b3-mid456.history.json",
"mcp-mgd0a1b4-new789.drawio",
"mcp-notes.drawio",
"mcp-system-design-v2.drawio",
"mine.drawio",
])
})
it("reads a session's saved diagram back", () => {
const saver = new Autosaver(tempDir(), 10)
expect(saver.load("mcp-none")).toBeNull()
saver.schedule("mcp-back", DIAGRAM)
saver.flush()
expect(saver.load("mcp-back")).toBe(DIAGRAM)
expect(new Autosaver(null).load("mcp-back")).toBeNull()
})
it("never replaces a saved file it could not read", () => {
const saver = new Autosaver(tempDir(), 10)
saver.schedule("mcp-locked", DIAGRAM)
saver.flush()
const path = saver.pathFor("mcp-locked") as string
chmodSync(path, 0o000)
try {
expect(saver.load("mcp-locked")).toBeNull()
// The blank page shown instead must not overwrite the diagram
saver.schedule("mcp-locked", BLANK)
saver.flush()
} finally {
chmodSync(path, 0o644)
}
expect(readFileSync(path, "utf-8")).toBe(DIAGRAM)
})
it("saves again once the file was read, or is gone", () => {
const saver = new Autosaver(tempDir(), 10)
saver.schedule("mcp-fixed", DIAGRAM)
saver.flush()
const path = saver.pathFor("mcp-fixed") as string
chmodSync(path, 0o000)
expect(saver.load("mcp-fixed")).toBeNull()
// Permissions fixed; the session is recreated and reads the file
chmodSync(path, 0o644)
expect(saver.load("mcp-fixed")).toBe(DIAGRAM)
const edited = DIAGRAM.replace('id="a"', 'id="b"')
saver.schedule("mcp-fixed", edited)
saver.flush()
expect(readFileSync(path, "utf-8")).toBe(edited)
// A file that could not be read and was then deleted protects
// nothing any more
chmodSync(path, 0o000)
expect(saver.load("mcp-fixed")).toBeNull()
chmodSync(path, 0o644)
rmSync(path)
expect(saver.load("mcp-fixed")).toBeNull()
saver.schedule("mcp-fixed", DIAGRAM)
saver.flush()
expect(readFileSync(path, "utf-8")).toBe(DIAGRAM)
})
it("saves again when the unreadable file is deleted during the session", () => {
const saver = new Autosaver(tempDir(), 10)
saver.schedule("mcp-live", DIAGRAM)
saver.flush()
const path = saver.pathFor("mcp-live") as string
chmodSync(path, 0o000)
expect(saver.load("mcp-live")).toBeNull()
// The session goes on (load is not called again); the user removes
// the broken file
chmodSync(path, 0o644)
rmSync(path)
saver.schedule("mcp-live", DIAGRAM)
saver.flush()
expect(readFileSync(path, "utf-8")).toBe(DIAGRAM)
})
it("keeps protecting a file it cannot even look at", () => {
// A folder without permission makes the file look missing; it is not
const dir = tempDir()
const saver = new Autosaver(dir, 10)
saver.schedule("mcp-hidden", DIAGRAM)
saver.flush()
const path = saver.pathFor("mcp-hidden") as string
chmodSync(path, 0o000)
expect(saver.load("mcp-hidden")).toBeNull()
chmodSync(dir, 0o000)
try {
expect(saver.load("mcp-hidden")).toBeNull()
} finally {
chmodSync(dir, 0o755)
}
chmodSync(path, 0o644)
saver.schedule("mcp-hidden", BLANK)
saver.flush()
expect(readFileSync(path, "utf-8")).toBe(DIAGRAM)
})
it("does nothing when saving is off", () => {
const saver = new Autosaver(null)
expect(saver.pathFor("mcp-x")).toBeNull()
expect(saver.historyPathFor("mcp-x")).toBeNull()
expect(saver.loadHistory("mcp-x")).toEqual([])
saver.schedule("mcp-x", DIAGRAM)
saver.flush()
})
it("lists every session file newest first, hand-named ones included", () => {
const dir = tempDir()
const saver = new Autosaver(dir, 10)
const names = [
"mcp-mgd0a1b2-old123.drawio",
"mine.drawio",
"mcp-notes.drawio",
"mcp-readme.txt",
"mcp-mgd0a1b3-new456.drawio",
]
for (const [i, name] of names.entries()) {
writeFileSync(join(dir, name), DIAGRAM)
utimesSync(join(dir, name), 1000 + i, 1000 + i)
}
expect(saver.dataDir()).toBe(dir)
expect(saver.list()).toEqual([
{
sessionId: "mcp-mgd0a1b3-new456",
path: join(dir, "mcp-mgd0a1b3-new456.drawio"),
savedAt: new Date(1004 * 1000),
},
{
sessionId: "mcp-notes",
path: join(dir, "mcp-notes.drawio"),
savedAt: new Date(1002 * 1000),
},
{
sessionId: "mcp-mgd0a1b2-old123",
path: join(dir, "mcp-mgd0a1b2-old123.drawio"),
savedAt: new Date(1000 * 1000),
},
])
})
it("lists nothing when saving is off or the folder does not exist", () => {
const off = new Autosaver(null)
expect(off.dataDir()).toBeNull()
expect(off.list()).toEqual([])
expect(new Autosaver(join(tempDir(), "missing")).list()).toEqual([])
})
})
describe("Autosaver History", () => {
const V1 = DIAGRAM.replace('id="a"', 'id="v1"')
const V2 = DIAGRAM.replace('id="a"', 'id="v2"')
const V3 = DIAGRAM.replace('id="a"', 'id="v3"')
// One session's History entries, as history.ts keeps them (the saver
// leaves the thumbnails out)
const withHistory = (dir: string, entries: { id: number; xml: string }[]) =>
new Autosaver(dir, 30, 50, () => entries)
it("saves the XML of the History entries next to the diagram", async () => {
const dir = tempDir()
const entries = [
{ id: 0, xml: V1 },
{ id: 1, xml: V2 },
]
const saver = withHistory(dir, entries)
const path = saver.historyPathFor("mcp-h") as string
expect(path).toBe(join(dir, "mcp-h.history.json"))
saver.schedule("mcp-h", V2)
expect(existsSync(path)).toBe(false)
await sleep(80)
expect(JSON.parse(readFileSync(path, "utf-8"))).toEqual([V1, V2])
expect(saver.loadHistory("mcp-h")).toEqual([V1, V2])
expect(readdirSync(dir).sort()).toEqual([
"mcp-h.drawio",
"mcp-h.history.json",
])
})
it("saves a diagram cleared before its first save, with its History", () => {
const dir = tempDir()
const saver = withHistory(dir, [{ id: 0, xml: V1 }])
saver.schedule("mcp-cleared", BLANK)
saver.flush()
expect(
readFileSync(saver.pathFor("mcp-cleared") as string, "utf-8"),
).toBe(BLANK)
expect(saver.loadHistory("mcp-cleared")).toEqual([V1])
})
it("still skips a blank page whose History holds only blank pages", () => {
const dir = tempDir()
const saver = withHistory(dir, [{ id: 0, xml: BLANK }])
saver.schedule("mcp-empty", BLANK)
saver.flush()
expect(readdirSync(dir)).toEqual([])
})
it("writes the History again after the cap removed it", () => {
const dir = tempDir()
const entries = [{ id: 0, xml: V1 }]
const saver = new Autosaver(dir, 10, 1, () => entries)
const old = "mcp-aaaaaaaa-old"
saver.schedule(old, V1)
saver.flush()
utimesSync(saver.pathFor(old) as string, 1000, 1000)
// A newer session pushes the old one out
saver.schedule("mcp-aaaaaaab-new", V1)
saver.flush()
expect(existsSync(saver.historyPathFor(old) as string)).toBe(false)
// The old session is still open and saves again, History unchanged
saver.schedule(old, V1)
saver.flush()
expect(saver.loadHistory(old)).toEqual([V1])
})
it("rewrites the History file only when the entries changed", () => {
const dir = tempDir()
const entries = [{ id: 0, xml: V1 }]
const saver = withHistory(dir, entries)
saver.schedule("mcp-same", V1)
saver.flush()
const path = saver.historyPathFor("mcp-same") as string
utimesSync(path, 1000, 1000)
const old = statSync(path).mtimeMs
// draw.io's own autosave of an unchanged diagram
saver.schedule("mcp-same", V1)
saver.flush()
expect(statSync(path).mtimeMs).toBe(old)
entries.push({ id: 1, xml: V2 })
saver.schedule("mcp-same", V2)
saver.flush()
expect(statSync(path).mtimeMs).not.toBe(old)
expect(saver.loadHistory("mcp-same")).toEqual([V1, V2])
// The circular buffer dropped the oldest entry: same length, new id
entries.shift()
entries.push({ id: 2, xml: V3 })
saver.schedule("mcp-same", V3)
saver.flush()
expect(saver.loadHistory("mcp-same")).toEqual([V2, V3])
})
it("writes no History file while there are no entries", () => {
const dir = tempDir()
const saver = withHistory(dir, [])
saver.schedule("mcp-empty", DIAGRAM)
saver.flush()
expect(readdirSync(dir)).toEqual(["mcp-empty.drawio"])
})
it("reads [] for a missing or malformed History file", () => {
const dir = tempDir()
const saver = new Autosaver(dir, 10)
const path = saver.historyPathFor("mcp-bad") as string
expect(saver.loadHistory("mcp-bad")).toEqual([])
for (const content of ["{not json", '{"a":1}', "[1, 2]", '"x"']) {
writeFileSync(path, content)
expect(saver.loadHistory("mcp-bad")).toEqual([])
}
})
it("flush() writes the History on shutdown", () => {
const dir = tempDir()
const saver = withHistory(dir, [{ id: 0, xml: V1 }])
saver.schedule("mcp-exit", V1)
saver.flush()
expect(saver.loadHistory("mcp-exit")).toEqual([V1])
})
})
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$/)
})
it("expands ~, which JSON configs pass on as it is", () => {
process.env.DRAWIO_DATA_DIR = "~/drawio-saves"
expect(defaultDataDir()).toBe(join(homedir(), "drawio-saves"))
})
})