Files
next-ai-draw-io/tests/unit/port-manager.test.ts
T
dayuan.jiang c75f74a5a0 fix: what the third round broke, and the first batch's review
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.
2026-10-05 17:33:36 +09:00

154 lines
4.8 KiB
TypeScript

// @vitest-environment node
import { mkdirSync, mkdtempSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { beforeEach, describe, expect, it, vi } from "vitest"
const userData = vi.hoisted(() => ({ dir: "" }))
vi.mock("electron", () => ({
app: { isPackaged: true, getPath: () => userData.dir },
}))
// Ports that fail to listen, with the error code
const busy = vi.hoisted(() => ({ ports: {} as Record<number, string> }))
vi.mock("node:net", () => ({
default: {
createServer: () => {
const handlers: Record<string, (arg?: unknown) => void> = {}
const server = {
once: (event: string, cb: (arg?: unknown) => void) => {
handlers[event] = cb
return server
},
listen: (port: number) => {
const code = busy.ports[port]
if (code) handlers.error?.({ code })
else handlers.listening?.()
},
close: () => {},
}
return server
},
},
}))
import {
findAvailablePort,
noteNoChats,
rememberChatPort,
resetAllocatedPort,
} from "@/electron/main/port-manager"
/** Chats saved under http://127.0.0.1:<port>, as Electron stores them */
const storeData = (port: number) =>
mkdirSync(
join(
userData.dir,
"IndexedDB",
`http_127.0.0.1_${port}.indexeddb.leveldb`,
),
{ recursive: true },
)
const launch = () => findAvailablePort(false)
beforeEach(() => {
userData.dir = mkdtempSync(join(tmpdir(), "port-manager-"))
busy.ports = {}
resetAllocatedPort()
})
describe("findAvailablePort", () => {
it("uses the legacy port first, as main does", async () => {
expect(await launch()).toBe(61337)
storeData(61337)
storeData(13370)
expect(await launch()).toBe(61337)
})
it("uses 13370 when only it has the user's chats", async () => {
// Windows reserved 61337 when they started using the app
storeData(13370)
expect(await launch()).toBe(13370)
})
it("goes back to the port with the chats once it is free", async () => {
// The previous version still quitting after an update
storeData(61337)
busy.ports[61337] = "EADDRINUSE"
expect(await launch()).toBe(13370)
storeData(13370)
busy.ports = {}
expect(await launch()).toBe(61337)
})
it("does not hide the chats for good after one reserved launch", async () => {
// Windows reserves port ranges per boot
storeData(61337)
busy.ports[61337] = "EACCES"
expect(await launch()).toBe(13370)
storeData(13370)
busy.ports = {}
expect(await launch()).toBe(61337)
})
it("falls back to the next ports", async () => {
storeData(13370)
busy.ports[13370] = "EADDRINUSE"
expect(await launch()).toBe(61337)
busy.ports[61337] = "EACCES"
expect(await launch()).toBe(13371)
})
})
describe("the port where chats were last saved", () => {
it("opens there first", async () => {
// Windows reserved 61337 for a while, and the user kept working
storeData(61337)
busy.ports[61337] = "EACCES"
expect(await launch()).toBe(13370)
storeData(13370)
rememberChatPort()
busy.ports = {}
expect(await launch()).toBe(13370)
})
it("does not move after a launch elsewhere that saved nothing", async () => {
storeData(61337)
expect(await launch()).toBe(61337)
rememberChatPort()
busy.ports[61337] = "EADDRINUSE"
expect(await launch()).toBe(13370)
storeData(13370)
busy.ports = {}
expect(await launch()).toBe(61337)
})
it("never stores a last-resort port, which changes between launches", async () => {
busy.ports[61337] = "EACCES"
busy.ports[13370] = "EADDRINUSE"
expect(await launch()).toBe(13371)
rememberChatPort()
busy.ports = {}
expect(await launch()).toBe(61337)
})
it("tries the other port after opening on one without chats", async () => {
// Split before this version: chats only on 13370, and a launch on
// 61337 created that origin's folder
storeData(13370)
storeData(61337)
expect(await launch()).toBe(61337)
noteNoChats()
expect(await launch()).toBe(13370)
// Once a choice is stored, an empty page changes nothing
noteNoChats()
expect(await launch()).toBe(13370)
})
it("stays put for a new user", async () => {
expect(await launch()).toBe(61337)
noteNoChats()
expect(await launch()).toBe(61337)
})
})