import { test } from "node:test"; import assert from "node:assert/strict"; import { apiUrl, apiOrigin } from "../../shared/api-origin.ts"; import { modelResourceUrl } from "../src/scene/assembly-plan.ts"; import { DemandApi } from "../src/ai/demand-api.ts"; import { clearAuthSession, setAuthSession } from "../src/auth/auth.ts"; import fixture from "./demand-fixtures.json" with { type: "json" }; test("development and production website domains select their explicit API", () => { for (const host of [ "cadworld.robotquan.com", "cadworld-admin.robotquan.com", ]) { assert.equal( apiOrigin(`https://${host}`), "https://cadworld-api.robotquan.com", ); assert.equal( apiOrigin(`https://dev-${host}`), "https://dev-cadworld-api.robotquan.com", ); } for (const host of [ "localhost:8091", "127.0.0.1:8091", "192.168.1.10:8091", "172.16.2.3:8092", "[::1]:8091", ]) assert.equal( apiUrl("/api/robot-conversations", `http://${host}`), "/api/robot-conversations", ); assert.equal( apiUrl("/api/auth/captcha", "http://localhost:8091"), "/api/auth/captcha", ); assert.equal( apiUrl("/api/users/me", "https://dev-cadworld.robotquan.com"), "https://dev-cadworld-api.robotquan.com/api/users/me", ); assert.throws(() => apiOrigin("https://unknown.example"), /尚未配置/); }); test("models use storage while private images use the selected API environment", () => { const page = "https://cadworld.robotquan.com"; assert.equal( modelResourceUrl( "https://dev-cadworld-admin.robotquan.com/admin-api/component-assets/CW/meshes/%E5%A4%B4.STL?v=2", page, ), "https://linkerbot.oss-cn-beijing.aliyuncs.com/CW/meshes/%E5%A4%B4.STL?v=2", ); assert.equal( apiUrl("/api/robot-conversations/DC-1/attachments/DA-1?t=2", page), "https://cadworld-api.robotquan.com/api/robot-conversations/DC-1/attachments/DA-1?t=2", ); assert.equal( apiUrl("https://bucket.example/a.stl?signature=123", page), "https://bucket.example/a.stl?signature=123", ); }); test("online JSON, multipart, private image preview and SSE all use direct API with bearer auth", async (t) => { const old = globalThis.window; globalThis.window = { location: { origin: "https://dev-cadworld.robotquan.com" }, }; setAuthSession({ token: "test-token", tokenType: "Bearer", expiresAt: Date.now() + 60_000, user: { userCode: "U1", nickname: "测试用户", email: "u@example.com" }, }); t.after(() => old === undefined ? delete globalThis.window : (globalThis.window = old), ); const calls = []; const api = new DemandApi(async (url, options) => { calls.push({ url, options }); if (url.endsWith("/attachments")) return Response.json({ code: "OK", data: { attachmentId: "DA-1", previewUrl: "/api/robot-conversations/DC-1/attachments/DA-1", }, }); if (url.endsWith("/attachments/DA-1")) return new Response("image-data", { headers: { "Content-Type": "image/png" }, }); if (url.endsWith("/messages")) return new Response( `event:analysis.completed\ndata:${JSON.stringify({ code: "OK", data: { ...fixture.analysis, match: fixture.match } })}\n\n`, { headers: { "Content-Type": "text/event-stream" } }, ); return Response.json({ code: "OK", data: { conversationId: "DC-1", revision: 0 }, }); }); await api.createConversation(); const image = await api.upload("DC-1", new File(["image"], "draft.png")); assert.equal( image.previewUrl.startsWith("blob:"), true, ); await api.stream( "/api/robot-conversations/DC-1/messages", { text: "躯干和头" }, new AbortController().signal, () => {}, ); assert.equal(calls.length, 4); for (const call of calls) { assert.ok( call.url.startsWith("https://dev-cadworld-api.robotquan.com/api/"), ); assert.equal(call.options.credentials, "omit"); assert.equal(call.options.headers.Authorization, "Bearer test-token"); } assert.ok(calls[1].options.body instanceof FormData); assert.equal(calls.at(-1).options.headers.Accept, "text/event-stream"); });