mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-09 19:19:50 +08:00
Chats: - New Chat right after an answer saves that chat once. Saves run one at a time and read the chat on screen when their turn comes; a save scheduled for a chat that is no longer on screen is dropped. A chat whose id was still on its way to the URL no longer comes back after New Chat (the next answer went into it). - Crossing the 768 px breakpoint keeps the chat panel: a streaming answer, unsaved messages and attachments stay. The panel gets the sizes of each side, and a panel collapsed on desktop opens on mobile. - The chat's export waits for its own reply: an edit's history export still on its way no longer answers it with the older diagram, and two file saves at once no longer swap results. - A second edit in one answer is previewed on the first edit's result. - Stop also ends a running screenshot check; a chat that cannot be saved (storage full) can be left with "Continue without saving". - Small diagrams with shapes count as diagrams; the tool card no longer crashes on malformed operations. Quota and providers: - Requests that reach the server's own endpoints count toward the quota: EdgeOne (always its own endpoint now), a private base URL whatever key header is sent, keyless Ollama without a URL. With the quota on, a redirect is followed only to a public address. The output cap applies to these requests too. - Stop records the tokens of the steps that finished; the screenshot check counts its tokens without counting a request. - EdgeOne configured only by AI_PROVIDER works, also in the admin Test, which forwards the access code. Azure set up only in the admin panel works in chat. The Test sends a Bedrock session token. - The admin panel's Test of an entry without a URL uses the server's URL as the server does (no private address check for it); the admin panel no longer writes an Ollama URL. MCP server: - Write tools and start_session run one at a time, so two at once never drop each other's change; a cancelled call waiting its turn is skipped. get_diagram and export_diagram keep the session they started with. - Export to .drawio first gets the user's latest edits from the browser. - History thumbnails: one that arrives after the next AI write is dropped; a sync reply keeps the image; a version that changed only page settings is its own entry. - A diagram over the 10 MB limit is saved without its image, or the user is told to download it (the server now answers 413 instead of cutting the connection). - Labels holding text like id='1' or parent='1' are no longer read as attributes (a layer or a parent was deleted). A broken bare <mxGraphModel> file is refused. - After a sync reply the tab no longer sends its autosave copy again. Desktop and files: - A newer switch of the same preset is not rolled back by an older one that failed. .env values with escaped quotes are read whole. - MCP saved files: a file that could not be read stays protected while a folder without permission hides it, and is saved again once deleted. - The desktop app reports "no chats" only when the count was read and no model settings are stored.
199 lines
7.2 KiB
TypeScript
199 lines
7.2 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("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()
|
|
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"))
|
|
})
|
|
})
|