mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-07 02:07:47 +08:00
MCP preview after the server lost a session (it expired, or the MCP process restarted): - Every server state has an id, made when the state is created. The tab notices a new id even when the version numbers happen to match, and every push names the state it was based on, so one based on a lost state is refused, also when it comes before the tab's first poll (the server recovers the saved file first). - The tab keeps the newest canvas XML, saved or not. When the server knows nothing (no file) or exactly what the tab last saved, the canvas wins and is saved, so edits made while the server was down are kept. Otherwise the server's diagram (an AI write the tab missed, a cleared document that was saved) is shown and the tab's copy goes to History. - Late answers to an old state's push or poll are dropped; a failed push says the server is unreachable; Download as .drawio saves the canvas. Settings and server: - Saved providers this version does not know stay in storage with their keys, and sending no longer trips over them. - The desktop "Ollama (Local)" preset with a key goes to local Ollama again; a server model's Ollama URL variable is read; the admin panel writes Ollama Cloud's URL for a key without one. - Provider error texts show again in the desktop app and for EdgeOne. - .env: a quoted value followed by a comment ending in a quote is read as dotenv reads it; unquoted values are unchanged. - Desktop app: the next launch opens the port where a chat was last saved; a launch elsewhere that saves nothing does not move it, and a page with no chats lets the next launch try the other port once. - The Test button no longer stays busy after another tab changed the key. - A completed append_diagram is no longer undone by an earlier failed edit's preview; a file read once in vain is saved again once it is read or gone. From the first batch's review: - The admin panel's Test of an entry without a URL now tests the server's <P>_BASE_URL, where chat sends the entry's key; chat is unchanged (the first fix rerouted working setups). - The model list ends downloads that are too large, accepts answers without a body, and keeps the "redirects are not allowed" explanation. - A test covers the preview's History rendering.
162 lines
5.9 KiB
TypeScript
162 lines
5.9 KiB
TypeScript
/**
|
|
* Tests for the per-session .drawio auto-save (src/persistence.ts).
|
|
*/
|
|
|
|
import {
|
|
chmodSync,
|
|
existsSync,
|
|
mkdtempSync,
|
|
readdirSync,
|
|
readFileSync,
|
|
rmSync,
|
|
utimesSync,
|
|
writeFileSync,
|
|
} from "node:fs"
|
|
import { homedir, 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)
|
|
// 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)
|
|
}
|
|
saver.schedule("mcp-mgd0a1b4-new789", DIAGRAM)
|
|
saver.flush()
|
|
expect(readdirSync(dir).sort()).toEqual([
|
|
"mcp-mgd0a1b3-mid456.drawio",
|
|
"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("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$/)
|
|
})
|
|
|
|
it("expands ~, which JSON configs pass on as it is", () => {
|
|
process.env.DRAWIO_DATA_DIR = "~/drawio-saves"
|
|
expect(defaultDataDir()).toBe(join(homedir(), "drawio-saves"))
|
|
})
|
|
})
|