Files
next-ai-draw-io/tests/unit/chat-route-edgeone.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

80 lines
2.6 KiB
TypeScript

// @vitest-environment node
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
vi.mock("@/lib/dynamo-quota-manager", () => ({
isQuotaEnabled: () => false,
checkAndIncrementRequest: async () => ({ allowed: true }),
recordTokenUsage: async () => {},
}))
import { POST as chat } from "@/app/api/chat/route"
const ENV = ["AI_MODELS_CONFIG", "AI_PROVIDER", "AI_MODEL"]
const saved: Record<string, string | undefined> = {}
const calls: Array<{ url: string; headers: Headers }> = []
beforeEach(() => {
for (const k of ENV) saved[k] = process.env[k]
delete process.env.AI_PROVIDER
delete process.env.AI_MODEL
calls.length = 0
vi.stubGlobal(
"fetch",
vi.fn(async (url: string, init: RequestInit) => {
calls.push({ url: String(url), headers: new Headers(init.headers) })
throw new Error("no network in tests")
}),
)
})
afterEach(() => {
for (const k of ENV) {
if (saved[k] === undefined) delete process.env[k]
else process.env[k] = saved[k]
}
vi.unstubAllGlobals()
})
describe("EdgeOne as a server model", () => {
it("calls the site's Edge AI function with the cookies", async () => {
// An admin panel or ai-models.json provider: the client sends the
// provider name's slug, not "edgeone"
process.env.AI_MODELS_CONFIG = JSON.stringify({
providers: [
{
name: "Edge Pages",
provider: "edgeone",
models: ["@tx/deepseek-ai/deepseek-v3-0324"],
},
],
})
const res = await chat(
new Request("http://localhost/api/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-ai-provider": "edge-pages",
"x-selected-model-id":
"server:edge-pages:@tx/deepseek-ai/deepseek-v3-0324",
cookie: "eo_token=t; eo_time=1",
},
body: JSON.stringify({
messages: [
{
id: "u1",
role: "user",
parts: [{ type: "text", text: "Draw two boxes" }],
},
],
xml: "",
}),
}),
)
await res.text()
expect(calls[0]?.url).toBe(
"http://localhost/api/edgeai/chat/completions",
)
expect(calls[0]?.headers.get("cookie")).toBe("eo_token=t; eo_time=1")
})
})