Files
next-ai-draw-io/tests/unit/app-menu.test.ts
T
dayuan.jiang 50c7ad3ec4 fix(server): keep users' keys at their own endpoints, and more fixes from the third review
- Bedrock: a user's AWS keys no longer go to an endpoint the server sets
  in AWS_ENDPOINT_URL_BEDROCK_RUNTIME / AWS_ENDPOINT_URL (read by the
  upgraded SDK), and admin panel keys win over AWS_BEARER_TOKEN_BEDROCK,
  as the Test button checks them. Checked with Bedrock.
- Ollama: a server key without a base URL (admin panel, OLLAMA_API_KEY)
  goes to Ollama Cloud, as env.example says, instead of 127.0.0.1.
- Quota: EdgeOne counts whatever key header comes along, keyless Ollama at
  a private address counts, and their provider texts stay in the log.
- An EdgeOne server model (admin panel, ai-models.json) works: the route
  checked the raw provider header, which holds the name's slug.
- parse-url ends downloads it does not read (too large, PDF, errors).
- Desktop app: the port follows where the chats are (IndexedDB per
  origin) instead of a remembered port, which could hide them for good;
  a same-port restart tells the page to refetch the server models; a
  failed preset switch no longer undoes a newer choice; a presets file
  removed after a failed read can be saved again; .env values quoted from
  start to end keep their inner quotes, as dotenv reads them.
2026-10-05 13:04:51 +09:00

68 lines
2.0 KiB
TypeScript

// @vitest-environment node
import { beforeEach, describe, expect, it, vi } from "vitest"
vi.mock("electron", () => ({
app: {
isPackaged: true,
getName: () => "app",
getVersion: () => "1",
getLocale: () => "en",
},
BrowserWindow: { getFocusedWindow: () => null },
dialog: {},
Menu: { buildFromTemplate: () => ({}), setApplicationMenu: () => {} },
shell: {},
}))
// The saved current preset, and restarts that wait until the test ends them
const state = vi.hoisted(() => ({
current: "A" as string | null,
restarts: [] as Array<{ resolve: () => void; reject: (e: Error) => void }>,
}))
vi.mock("@/electron/main/config-manager", () => ({
applyPresetToEnv: (id: string) => {
state.current = id
return { AI_PROVIDER: id }
},
getAllPresets: () => [],
getCurrentPresetId: () => state.current,
setCurrentPreset: (id: string | null) => {
state.current = id
return true
},
}))
vi.mock("@/electron/main/next-server", () => ({
restartNextServer: () =>
new Promise<void>((resolve, reject) =>
state.restarts.push({ resolve, reject }),
),
}))
vi.mock("@/electron/main/menu-i18n", () => ({
getMenuTranslations: () => new Proxy({}, { get: () => "x" }),
getPreferredLocale: () => "en",
}))
vi.mock("@/electron/main/settings-window", () => ({
showSettingsWindow: () => {},
}))
import { switchPreset } from "@/electron/main/app-menu"
beforeEach(() => {
state.current = "A"
state.restarts = []
})
describe("switchPreset", () => {
it("keeps a preset chosen while a failed switch was restarting", async () => {
const toB = switchPreset("B").catch(() => {})
const toC = switchPreset("C")
// B's restart fails after the user already picked C
state.restarts[0].reject(new Error("timed out"))
await new Promise((r) => setTimeout(r, 0))
for (const r of state.restarts.slice(1)) r.resolve()
await toB
await toC
expect(state.current).toBe("C")
})
})