mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-12 20:49:53 +08:00
## Problem
Two audits of the repository (a workflow of finder, dedup and per-file verifier agents, run on 2026-10-11 against 1f4141e and again against 07f3e3c after a first round of refactoring) found dead code and features that were more machinery than the app needs: an Electron configuration-preset system with its own settings window, three copies of the about page, a hand-written `.env` parser, an "extended" system prompt nobody selected, two data migrations for formats from 2025-12 and 2026-01, a server-side example cache, type-to-confirm provider deletion, 81 unused dictionary keys, wrapper components around single call sites, and a long tail of unused exports, props, options and tests.
## Change
Every item below was either approved one by one or covered by "fix all of the others".
- **Electron:** the configuration-preset system is gone (settings window, preload, IPC, menu entry, dev watcher); `config-manager.ts` keeps only `userLocale`; `.env` files are parsed with `dotenv`. Entitlements file and a duplicate CI workflow removed; `test.yml` gets a type check and concurrency.
- **Server:** no extended prompt, no example cache, no `ENABLE_HISTORY_XML_REPLACE`, no message metadata or `hasConfig`; the connection test is one shared helper; EdgeOne loses its mock branch and model aliases; `DEBUG_LLM_PAYLOAD` logs message structure instead of payloads; Langfuse scoring helper; `apiKeyEnv` arrays kept and documented in `env.example` and the provider docs.
- **Pages:** one about page per language from a single component (old `/cn` and `/ja` URLs redirect with 308); layout uses `hasLocale`; the GitHub icon is a local component instead of `react-icons`; Cloudflare deploys without an R2 cache by default.
- **Chat and canvas:** the legacy `diagramHistory` conversion, the localStorage chat migration and the templates storage are gone; `chat-engine` reads the diagram through the context's refs; the diagram context has one tagged export path (`requestTaggedExport` / `fetchDiagramXml`); the screenshot check uses fetch + JSON and `generateText` with `Output.object`; `load-diagram` accepts `<svg:svg>` and strips the XML prolog.
- **Settings UI:** `model-selector` inlines its Dialog, Command and Tooltip; `ai-elements/model-selector`, `button-with-tooltip`, `error-toast` removed; no type-to-confirm, no footer, no `bedrockApiKey`; `use-model-config` loses the old-format migration; one reasoning component; the shimmer is CSS (no `motion`, no `@radix-ui/react-use-controllable-state`); 81 dictionary keys removed in all four languages.
- **MCP server:** `registerTool` / `ok` / `fail` replace per-tool try/catch; `syncFromBrowser`, `commitDocument`, `describePages`; the hand-written XML syntax check, unused result fields, `pickPageSelector`, `expandStyles` and the `host` attribute handling are gone; tests get a shared `setup.ts` and drive the server through the SDK client.
- **Prompt:** the web app's style rules come from the MCP drawing guide (`styleRules`), byte-identical to before.
- **Tests:** shared e2e helpers (`openApp`, `sse`, `toolCallEvents`, `chunkedReplies`); placeholder specs dropped; `settings-review-fixes` split by topic; unit tests use `vi.stubEnv`.
- **Dependencies removed:** `@xmldom/xmldom`, `remark-gfm`, `@anthropic-ai/tokenizer`, `@testing-library/user-event`, `concurrently`, `wait-on`, `eslint`, `eslint-config-next`, `@vitest/coverage-v8`, `shx`, `motion`, `@radix-ui/react-use-controllable-state`, `react-icons`.
Source lines: 74,983 before, 67,418 after (excluding lockfiles).
The last commit merges #984 (the canvas shell). Where the shell, the classic page or their tests depend on a wire format or signature this branch had changed, main's version wins (export fields of the poll response, `addHistory` signature); where main only extended code this branch restructured, main's change is carried into the new structure (`start_session`, `get_selection`, `prepareNewDiagram`, the diagram context's `requestExport`).
## Testing
- `tsc --noEmit` for the web app, `packages/mcp-server`, its shell and `electron`: clean
- `biome ci`: 0 errors (37 pre-existing warnings)
- Root `vitest run`: 719 passed; `packages/mcp-server` `vitest run`: 372 passed
- `next build`: clean
- Playwright, whole web suite on the dev server (4 workers): 280 tests, 279 passed; `language.spec.ts` "a stale offer to switch without saving does nothing once a message is being sent" failed under parallel load and passed alone, as it does on main
431 lines
16 KiB
TypeScript
431 lines
16 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 { describe, expect, it, vi } from "vitest"
|
|
import type { HistorySource } from "../src/history.ts"
|
|
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="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; source?: HistorySource }[],
|
|
) => 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([
|
|
{ xml: V1 },
|
|
{ xml: V2 },
|
|
])
|
|
expect(saver.loadHistory("mcp-h")).toEqual([{ xml: V1 }, { xml: 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([{ xml: 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([{ xml: 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([
|
|
{ xml: V1 },
|
|
{ xml: 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([
|
|
{ xml: V2 },
|
|
{ xml: 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"',
|
|
'[{"source":"user"}]',
|
|
]) {
|
|
writeFileSync(path, content)
|
|
expect(saver.loadHistory("mcp-bad")).toEqual([])
|
|
}
|
|
})
|
|
|
|
it("reads the History files of earlier versions, which hold the XML alone", () => {
|
|
const dir = tempDir()
|
|
const saver = new Autosaver(dir, 10)
|
|
writeFileSync(
|
|
saver.historyPathFor("mcp-old") as string,
|
|
JSON.stringify([V1, V2]),
|
|
)
|
|
expect(saver.loadHistory("mcp-old")).toEqual([{ xml: V1 }, { xml: V2 }])
|
|
})
|
|
|
|
it("keeps who made each entry, so the shell's cards look the same after a restart", () => {
|
|
// A clear of the canvas and a restore of it: without the source, the
|
|
// restored copy would show as a second clear
|
|
const dir = tempDir()
|
|
const saver = withHistory(dir, [
|
|
{ id: 0, xml: V1 },
|
|
{ id: 1, xml: BLANK, source: "user" },
|
|
{ id: 2, xml: V2, source: "restore" },
|
|
{ id: 3, xml: V1, source: "recover" },
|
|
])
|
|
saver.schedule("mcp-who", V1)
|
|
saver.flush()
|
|
expect(saver.loadHistory("mcp-who")).toEqual([
|
|
{ xml: V1 },
|
|
{ xml: BLANK, source: "user" },
|
|
{ xml: V2, source: "restore" },
|
|
{ xml: V1, source: "recover" },
|
|
])
|
|
// A source the server does not know is left out
|
|
writeFileSync(
|
|
saver.historyPathFor("mcp-who") as string,
|
|
JSON.stringify([{ xml: V1, source: "robot" }]),
|
|
)
|
|
expect(saver.loadHistory("mcp-who")).toEqual([{ xml: V1 }])
|
|
})
|
|
|
|
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([{ xml: V1 }])
|
|
})
|
|
})
|
|
|
|
describe("defaultDataDir", () => {
|
|
it("reads DRAWIO_DATA_DIR, where off disables saving", () => {
|
|
vi.stubEnv("DRAWIO_DATA_DIR", "off")
|
|
expect(defaultDataDir()).toBeNull()
|
|
vi.stubEnv("DRAWIO_DATA_DIR", "/tmp/x")
|
|
expect(defaultDataDir()).toBe("/tmp/x")
|
|
vi.stubEnv("DRAWIO_DATA_DIR", undefined)
|
|
expect(defaultDataDir()).toMatch(/\.next-ai-drawio$/)
|
|
})
|
|
|
|
it("expands ~, which JSON configs pass on as it is", () => {
|
|
vi.stubEnv("DRAWIO_DATA_DIR", "~/drawio-saves")
|
|
expect(defaultDataDir()).toBe(join(homedir(), "drawio-saves"))
|
|
})
|
|
})
|