mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-13 04:59:51 +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
306 lines
12 KiB
TypeScript
306 lines
12 KiB
TypeScript
/**
|
|
* Unit tests for load_diagram's file parsing (load-diagram.ts).
|
|
*
|
|
* A .drawio file stores each page's <mxGraphModel> either as plain XML or
|
|
* as draw.io's compressed default (encodeURIComponent → raw deflate →
|
|
* base64 text content). The loader must produce the canonical session
|
|
* shape: an <mxfile> whose every page is plain XML.
|
|
*/
|
|
|
|
import { deflateRawSync } from "node:zlib"
|
|
import { DOMParser } from "linkedom"
|
|
import { describe, expect, it } from "vitest"
|
|
|
|
import {
|
|
decompressPageContent,
|
|
extractSvgEmbeddedXml,
|
|
parseDrawioFileContent,
|
|
} from "../src/load-diagram.ts"
|
|
|
|
const MODEL_XML = `<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>`
|
|
|
|
/** Compress a page body exactly the way draw.io does when saving. */
|
|
function drawioCompress(xml: string): string {
|
|
return deflateRawSync(
|
|
Buffer.from(encodeURIComponent(xml), "utf-8"),
|
|
).toString("base64")
|
|
}
|
|
|
|
const PLAIN_MXFILE = `<mxfile host="app.diagrams.net"><diagram id="p1" name="Page-1">${MODEL_XML}</diagram></mxfile>`
|
|
const COMPRESSED_MXFILE = `<mxfile host="app.diagrams.net" compressed="true"><diagram id="p1" name="Page-1">${drawioCompress(MODEL_XML)}</diagram></mxfile>`
|
|
|
|
describe("decompressPageContent", () => {
|
|
it("round-trips draw.io's compressed format", () => {
|
|
expect(decompressPageContent(drawioCompress(MODEL_XML))).toBe(MODEL_XML)
|
|
})
|
|
|
|
it("handles non-URI-encoded legacy payloads", () => {
|
|
const legacy = deflateRawSync(Buffer.from(MODEL_XML, "utf-8")).toString(
|
|
"base64",
|
|
)
|
|
expect(decompressPageContent(legacy)).toBe(MODEL_XML)
|
|
})
|
|
|
|
it("returns null for garbage", () => {
|
|
expect(decompressPageContent("not base64 deflate")).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe("parseDrawioFileContent", () => {
|
|
it("passes a plain-XML mxfile through unchanged", () => {
|
|
const r = parseDrawioFileContent(PLAIN_MXFILE)
|
|
expect(r).toEqual({
|
|
ok: true,
|
|
xml: PLAIN_MXFILE,
|
|
hadCompressedPages: false,
|
|
})
|
|
})
|
|
|
|
it("wraps a bare mxGraphModel into a one-page mxfile", () => {
|
|
const r = parseDrawioFileContent(MODEL_XML)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.xml).toContain("<mxfile")
|
|
expect(r.xml).toContain('value="Hello"')
|
|
}
|
|
})
|
|
|
|
it("rejects a bare mxGraphModel that is not closed", () => {
|
|
const r = parseDrawioFileContent(
|
|
MODEL_XML.replace("</root></mxGraphModel>", ""),
|
|
)
|
|
expect(r.ok).toBe(false)
|
|
})
|
|
|
|
it("decompresses a compressed mxfile into plain XML pages", () => {
|
|
const r = parseDrawioFileContent(COMPRESSED_MXFILE)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.xml).toContain("<mxGraphModel")
|
|
expect(r.xml).toContain('value="Hello"')
|
|
// The compressed blob must be gone.
|
|
expect(r.xml).not.toContain(drawioCompress(MODEL_XML))
|
|
}
|
|
})
|
|
|
|
it("decompresses only the compressed pages of a mixed file", () => {
|
|
const mixed = `<mxfile><diagram id="a" name="Plain">${MODEL_XML}</diagram><diagram id="b" name="Squeezed">${drawioCompress(MODEL_XML)}</diagram></mxfile>`
|
|
const r = parseDrawioFileContent(mixed)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
const doc = new DOMParser().parseFromString(r.xml, "text/xml")
|
|
const diagrams = Array.from(
|
|
doc.querySelectorAll("diagram"),
|
|
) as Element[]
|
|
expect(diagrams).toHaveLength(2)
|
|
for (const d of diagrams) {
|
|
expect(d.querySelector("mxGraphModel")).not.toBeNull()
|
|
}
|
|
}
|
|
})
|
|
|
|
it("keeps empty pages as-is", () => {
|
|
const withEmpty = `<mxfile><diagram id="a" name="Page-1">${MODEL_XML}</diagram><diagram id="b" name="Empty"></diagram></mxfile>`
|
|
const r = parseDrawioFileContent(withEmpty)
|
|
expect(r).toEqual({
|
|
ok: true,
|
|
xml: withEmpty,
|
|
hadCompressedPages: false,
|
|
})
|
|
})
|
|
|
|
it("rejects empty files", () => {
|
|
const r = parseDrawioFileContent(" ")
|
|
expect(r.ok).toBe(false)
|
|
})
|
|
|
|
it("rejects non-drawio content", () => {
|
|
const r = parseDrawioFileContent("<html><body/></html>")
|
|
expect(r.ok).toBe(false)
|
|
if (!r.ok) expect(r.error).toContain("Not a draw.io file")
|
|
})
|
|
|
|
it("rejects an SVG without an embedded diagram", () => {
|
|
const r = parseDrawioFileContent("<svg><rect/></svg>")
|
|
expect(r.ok).toBe(false)
|
|
if (!r.ok) expect(r.error).toContain("embedded diagram")
|
|
})
|
|
|
|
it("rejects a page whose content is neither XML nor compressed", () => {
|
|
const bad = `<mxfile><diagram id="a" name="Broken">!!! not a diagram !!!</diagram></mxfile>`
|
|
const r = parseDrawioFileContent(bad)
|
|
expect(r.ok).toBe(false)
|
|
if (!r.ok) expect(r.error).toContain('"Broken"')
|
|
})
|
|
})
|
|
|
|
describe("hadCompressedPages", () => {
|
|
it("is false for a plain-XML mxfile", () => {
|
|
const r = parseDrawioFileContent(PLAIN_MXFILE)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) expect(r.hadCompressedPages).toBe(false)
|
|
})
|
|
|
|
it("is false for a bare mxGraphModel", () => {
|
|
const r = parseDrawioFileContent(MODEL_XML)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) expect(r.hadCompressedPages).toBe(false)
|
|
})
|
|
|
|
it("is true for a fully compressed mxfile", () => {
|
|
const r = parseDrawioFileContent(COMPRESSED_MXFILE)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) expect(r.hadCompressedPages).toBe(true)
|
|
})
|
|
|
|
it("is true for a mixed plain/compressed file", () => {
|
|
const mixed = `<mxfile><diagram id="a" name="Plain">${MODEL_XML}</diagram><diagram id="b" name="Squeezed">${drawioCompress(MODEL_XML)}</diagram></mxfile>`
|
|
const r = parseDrawioFileContent(mixed)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) expect(r.hadCompressedPages).toBe(true)
|
|
})
|
|
})
|
|
|
|
/**
|
|
* Escape text for an XML attribute the way draw.io/Chromium serialise the
|
|
* diagram into the SVG root's content attribute.
|
|
*/
|
|
function escapeAttr(text: string): string {
|
|
return text
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """)
|
|
.replace(/\n/g, "
")
|
|
}
|
|
|
|
/** Build a .drawio.svg (Editable SVG) with the given mxfile embedded. */
|
|
function drawioSvg(mxfileXml: string): string {
|
|
return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="200px" height="100px" viewBox="-0.5 -0.5 200 100" content="${escapeAttr(mxfileXml)}"><defs/><g><rect x="40" y="40" width="120" height="60" fill="#ffffff" stroke="#000000"/></g></svg>`
|
|
}
|
|
|
|
const SVG_PROLOG = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<!-- Do not edit this file with editors other than draw.io -->
|
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
|
`
|
|
|
|
describe("parseDrawioFileContent with a prolog before the root", () => {
|
|
it("accepts a declaration, a comment and a DOCTYPE before <mxfile>", () => {
|
|
const prolog = `<?xml version="1.0" encoding="UTF-8"?>\n<!-- Architecture, v2 -->\n<!DOCTYPE mxfile>\n`
|
|
expect(parseDrawioFileContent(prolog + PLAIN_MXFILE)).toEqual({
|
|
ok: true,
|
|
xml: PLAIN_MXFILE,
|
|
hadCompressedPages: false,
|
|
})
|
|
})
|
|
|
|
it("accepts a comment before a bare <mxGraphModel>", () => {
|
|
const r = parseDrawioFileContent(`<!-- exported -->\n${MODEL_XML}`)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) expect(r.xml).toContain('value="Hello"')
|
|
})
|
|
|
|
it("treats a file with nothing after its prolog as empty", () => {
|
|
expect(parseDrawioFileContent(`<?xml version="1.0"?>\n`)).toEqual({
|
|
ok: false,
|
|
error: "File is empty.",
|
|
})
|
|
})
|
|
})
|
|
|
|
describe("extractSvgEmbeddedXml", () => {
|
|
it("returns undefined for non-SVG text", () => {
|
|
expect(extractSvgEmbeddedXml(PLAIN_MXFILE)).toBeUndefined()
|
|
expect(extractSvgEmbeddedXml("hello")).toBeUndefined()
|
|
})
|
|
|
|
it("accepts a root element with a namespace prefix", () => {
|
|
const svg = `<svg:svg xmlns:svg="http://www.w3.org/2000/svg" content="${escapeAttr(PLAIN_MXFILE)}"></svg:svg>`
|
|
expect(extractSvgEmbeddedXml(svg)).toBe(PLAIN_MXFILE)
|
|
expect(parseDrawioFileContent(svg)).toEqual({
|
|
ok: true,
|
|
xml: PLAIN_MXFILE,
|
|
hadCompressedPages: false,
|
|
})
|
|
})
|
|
|
|
it("returns an empty string for an SVG without content", () => {
|
|
expect(extractSvgEmbeddedXml("<svg><rect/></svg>")).toBe("")
|
|
})
|
|
|
|
it("returns the decoded content attribute", () => {
|
|
expect(extractSvgEmbeddedXml(drawioSvg(PLAIN_MXFILE))).toBe(
|
|
PLAIN_MXFILE,
|
|
)
|
|
})
|
|
})
|
|
|
|
describe("parseDrawioFileContent with .drawio.svg", () => {
|
|
it("loads a compressed one-page mxfile embedded in an SVG", () => {
|
|
const r = parseDrawioFileContent(drawioSvg(COMPRESSED_MXFILE))
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.xml).toContain("<mxfile")
|
|
expect(r.xml).toContain("<mxGraphModel")
|
|
expect(r.xml).toContain('value="Hello"')
|
|
expect(r.xml).not.toContain("<svg")
|
|
}
|
|
})
|
|
|
|
it("accepts draw.io's XML declaration, comment and DOCTYPE prolog", () => {
|
|
const r = parseDrawioFileContent(
|
|
SVG_PROLOG + drawioSvg(COMPRESSED_MXFILE),
|
|
)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) expect(r.xml).toContain('value="Hello"')
|
|
})
|
|
|
|
it("round-trips " and 
 in an uncompressed embedded label", () => {
|
|
const model = MODEL_XML.replace(
|
|
'value="Hello"',
|
|
'value="say "hi"
next"',
|
|
)
|
|
// A pretty-printed file: the newline between tags becomes 
 in
|
|
// the SVG attribute, the label's entities become &quot; etc.
|
|
const mxfile = `<mxfile host="app.diagrams.net">\n<diagram id="p1" name="Page-1">${model}</diagram>\n</mxfile>`
|
|
const svg = drawioSvg(mxfile)
|
|
expect(svg).toContain("&quot;")
|
|
expect(svg).toContain("&#xa;")
|
|
expect(svg).toContain(">
<diagram")
|
|
|
|
const r = parseDrawioFileContent(svg)
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
expect(r.xml).toContain('value="say "hi"
next"')
|
|
const doc = new DOMParser().parseFromString(r.xml, "text/xml")
|
|
const cell = doc.querySelector('mxCell[id="box1"]') as Element
|
|
expect(cell.getAttributeNode("value")?.value).toBe('say "hi"\nnext')
|
|
}
|
|
})
|
|
|
|
it("loads a two-page embedded mxfile as two pages", () => {
|
|
const twoPages = `<mxfile compressed="true"><diagram id="a" name="First">${drawioCompress(MODEL_XML)}</diagram><diagram id="b" name="Second">${drawioCompress(MODEL_XML.replace("Hello", "World"))}</diagram></mxfile>`
|
|
const r = parseDrawioFileContent(drawioSvg(twoPages))
|
|
expect(r.ok).toBe(true)
|
|
if (r.ok) {
|
|
const doc = new DOMParser().parseFromString(r.xml, "text/xml")
|
|
const diagrams = Array.from(
|
|
doc.querySelectorAll("diagram"),
|
|
) as Element[]
|
|
expect(diagrams).toHaveLength(2)
|
|
for (const d of diagrams) {
|
|
expect(d.querySelector("mxGraphModel")).not.toBeNull()
|
|
}
|
|
expect(r.xml).toContain('value="World"')
|
|
}
|
|
})
|
|
|
|
it("rejects an SVG whose content is not draw.io XML", () => {
|
|
const r = parseDrawioFileContent(
|
|
'<svg xmlns="http://www.w3.org/2000/svg" content="hello"><rect/></svg>',
|
|
)
|
|
expect(r.ok).toBe(false)
|
|
if (!r.ok) expect(r.error).toContain("not draw.io XML")
|
|
})
|
|
})
|