|
| 1 | +/** Isolated Zhipu BigModel provider UI proof. Saves a fixture key and never calls a model service. |
| 2 | + * Start creation_server.py on 8029 and Vite on 5189 with its API proxy on 8029. |
| 3 | + * CREATION_WEB_URL, CREATION_API_URL and ZHIPU_SCREENSHOT_DIR override defaults. |
| 4 | + */ |
| 5 | +import assert from "node:assert/strict"; |
| 6 | +import { mkdir } from "node:fs/promises"; |
| 7 | +import { join } from "node:path"; |
| 8 | +import { launchBrowser } from "./engine.mjs"; |
| 9 | + |
| 10 | +const base = process.env.CREATION_WEB_URL ?? "http://127.0.0.1:5189"; |
| 11 | +const api = process.env.CREATION_API_URL ?? "http://127.0.0.1:8029"; |
| 12 | +const output = process.env.ZHIPU_SCREENSHOT_DIR ?? "/tmp/opendirector-zhipu-evidence"; |
| 13 | +const modelIds = ["glm-5.3", "glm-5.3-flash", "glm-5.2"]; |
| 14 | +const canonicalURL = "https://open.bigmodel.cn/api/paas/v4"; |
| 15 | +const fixtureKey = "zhipu-browser-fixture-only-no-real-service"; |
| 16 | +const headers = { "X-OpenDirector-Request": "1", "Content-Type": "application/json" }; |
| 17 | +async function call(path, { method = "GET", body } = {}) { |
| 18 | + const response = await fetch(api + path, { |
| 19 | + method, |
| 20 | + headers, |
| 21 | + ...(body ? { body: JSON.stringify(body) } : {}), |
| 22 | + }); |
| 23 | + assert.ok(response.ok, `Fixture API ${method} ${path}: ${response.status}`); |
| 24 | + return response.status === 204 ? null : response.json(); |
| 25 | +} |
| 26 | +const health = await call("/api/creation-test-health"); |
| 27 | +assert.equal(health.isolated, true, "Run only against creation_server.py"); |
| 28 | +assert.equal(health.provider, "fixture"); |
| 29 | +const webHealth = await (await fetch(base + "/api/creation-test-health", { headers })).json(); |
| 30 | +assert.equal(webHealth.root, health.root, "Web proxy must use the same isolated API"); |
| 31 | +const removedServices = new Set(); |
| 32 | +for (const config of await call("/api/model-configs")) { |
| 33 | + const serviceId = config.providerId ?? config.id; |
| 34 | + if (config.url.replace(/\/+$/, "") === canonicalURL && modelIds.includes(config.modelId) && !removedServices.has(serviceId)) { |
| 35 | + await call(`/api/model-services/${config.id}`, { method: "DELETE" }); |
| 36 | + removedServices.add(serviceId); |
| 37 | + } |
| 38 | +} |
| 39 | +await mkdir(output, { recursive: true }); |
| 40 | +const projectId = crypto.randomUUID(); |
| 41 | +await call(`/api/creation/projects/${projectId}/commands`, { |
| 42 | + method: "POST", |
| 43 | + body: { commandId: crypto.randomUUID(), projectId, type: "project.create", |
| 44 | + payload: { name: `Zhipu provider UI fixture ${projectId.slice(0, 8)}` } }, |
| 45 | +}); |
| 46 | + |
| 47 | +const copy = { |
| 48 | + "zh-CN": { |
| 49 | + settings: "设置", models: "模型接入", agentOverview: "文本模型", chat: "对话", |
| 50 | + add: "添加服务", provider: "提供商", zhipu: "智谱 BigModel", url: "服务地址(Base URL)", |
| 51 | + reload: "载入支持的模型", save: "保存服务", cancel: "取消", manage: "管理", close: "关闭设置", |
| 52 | + chatEntry: "与 AI 对话", manual: "手动添加", manualAdd: "添加", manualId: "Model ID(服务的 served-model-name)", |
| 53 | + }, |
| 54 | + "en-US": { |
| 55 | + settings: "Settings", models: "Model connections", agentOverview: "Text model", chat: "Chat", |
| 56 | + add: "Add service", provider: "Provider", zhipu: "Zhipu BigModel", url: "Service URL (Base URL)", |
| 57 | + reload: "Load supported models", save: "Save service", cancel: "Cancel", manage: "Manage", close: "Close settings", |
| 58 | + chatEntry: "Chat with AI", manual: "Add manually", manualAdd: "Add", manualId: "Model ID (the service's served-model-name)", |
| 59 | + }, |
| 60 | +}; |
| 61 | +const browser = await launchBrowser(); |
| 62 | +const page = await browser.newPage({ viewport: { width: 1500, height: 1000 } }); |
| 63 | +page.setDefaultTimeout(15000); |
| 64 | +await page.addInitScript(() => { |
| 65 | + localStorage.setItem("opendirector.modelSetup.v1", "dismissed"); |
| 66 | + localStorage.setItem("opendirector.locale.v1", "zh-CN"); |
| 67 | +}); |
| 68 | +const requests = []; |
| 69 | +const errors = []; |
| 70 | +page.on("request", request => requests.push(request.url())); |
| 71 | +page.on("pageerror", error => errors.push(error.message)); |
| 72 | +const shot = name => page.screenshot({ path: join(output, name), fullPage: true }); |
| 73 | +const provider = labels => page.getByRole("combobox", { name: labels.provider, exact: true }); |
| 74 | +// Filter an option card by its checkbox so glm-5.3 does not substring-match glm-5.3-flash. |
| 75 | +const optionRow = modelId => |
| 76 | + page.locator(".cw-model-option").filter({ has: page.getByRole("checkbox", { name: modelId, exact: true }) }); |
| 77 | +const pickerRow = page.getByRole("menuitemradio", { name: "glm-5.3", exact: true }); |
| 78 | +async function brandLogo(image) { |
| 79 | + await image.waitFor(); |
| 80 | + await image.evaluate(element => element.decode()); |
| 81 | + assert.ok(await image.evaluate(element => element.naturalWidth > 0)); |
| 82 | + const source = await image.getAttribute("src"); |
| 83 | + assert.ok(source.startsWith("data:image/svg+xml") || new URL(source, base).origin === new URL(base).origin); |
| 84 | +} |
| 85 | +async function openAgentCategory(labels) { |
| 86 | + await page.locator(".cw-model-setup-row") |
| 87 | + .filter({ has: page.locator("strong", { hasText: new RegExp(`^${labels.agentOverview}$`) }) }) |
| 88 | + .getByRole("button").click(); |
| 89 | +} |
| 90 | +async function provePreset(labels, locale) { |
| 91 | + await page.getByRole("button", { name: labels.add, exact: true }).click(); |
| 92 | + const start = requests.length; |
| 93 | + await provider(labels).click(); |
| 94 | + const option = page.getByRole("option", { name: labels.zhipu, exact: true }); |
| 95 | + await brandLogo(option.locator("img.cw-model-logo")); |
| 96 | + await option.click(); |
| 97 | + assert.equal(await provider(labels).innerText(), labels.zhipu); |
| 98 | + assert.equal(await page.getByLabel(labels.url, { exact: true }).inputValue(), canonicalURL); |
| 99 | + assert.equal(await page.locator(".cw-model-option").count(), modelIds.length); |
| 100 | + await brandLogo(provider(labels).locator("img.cw-model-logo")); |
| 101 | + for (const modelId of modelIds) { |
| 102 | + assert.equal(await page.getByRole("checkbox", { name: modelId, exact: true }).isChecked(), true); |
| 103 | + await brandLogo(optionRow(modelId).locator("img.cw-model-logo")); |
| 104 | + } |
| 105 | + await page.getByLabel("API Key", { exact: true }).fill(fixtureKey); |
| 106 | + await page.getByRole("button", { name: labels.reload, exact: true }).click(); |
| 107 | + for (const modelId of modelIds) |
| 108 | + assert.equal(await page.getByRole("checkbox", { name: modelId, exact: true }).isChecked(), true); |
| 109 | + assert.ok(!requests.slice(start).some(url => url.includes("/model-configs/discover")), "Preset selection or reload must not discover remote models"); |
| 110 | + await shot(`${locale}-mock-zhipu-provider.png`); |
| 111 | +} |
| 112 | +async function proveReopen(labels, locale) { |
| 113 | + const service = page.locator(".cw-model-service").filter({ hasText: canonicalURL }); |
| 114 | + assert.equal(await service.count(), 1); |
| 115 | + assert.ok((await service.innerText()).includes(labels.zhipu)); |
| 116 | + for (const logo of await service.locator("img.cw-model-logo").all()) |
| 117 | + await brandLogo(logo); |
| 118 | + await service.getByRole("button", { name: labels.manage, exact: true }).click(); |
| 119 | + assert.equal(await provider(labels).innerText(), labels.zhipu); |
| 120 | + for (const modelId of modelIds) |
| 121 | + assert.equal(await page.getByRole("checkbox", { name: modelId, exact: true }).isChecked(), true); |
| 122 | + assert.equal(await page.getByLabel("API Key", { exact: true }).inputValue(), "", "Saved key stays private"); |
| 123 | + await brandLogo(provider(labels).locator("img.cw-model-logo")); |
| 124 | + await shot(`${locale}-mock-zhipu-reopened.png`); |
| 125 | + await page.getByRole("button", { name: labels.cancel, exact: true }).click(); |
| 126 | +} |
| 127 | +async function proveManualAdd(labels, locale) { |
| 128 | + // The preset ships a curated list, but manual model IDs stay available like DeepSeek. |
| 129 | + await page.getByRole("button", { name: labels.add, exact: true }).click(); |
| 130 | + await provider(labels).click(); |
| 131 | + await page.getByRole("option", { name: labels.zhipu, exact: true }).click(); |
| 132 | + const start = requests.length; |
| 133 | + await page.getByRole("button", { name: labels.manual, exact: true }).click(); |
| 134 | + await page.getByLabel(labels.manualId, { exact: true }).fill("glm-4.7"); |
| 135 | + await page.locator(".cw-model-manual").getByRole("button", { name: labels.manualAdd, exact: true }).click(); |
| 136 | + assert.equal(await page.locator(".cw-model-option").count(), modelIds.length + 1); |
| 137 | + assert.equal(await page.getByRole("checkbox", { name: "glm-4.7", exact: true }).isChecked(), true); |
| 138 | + assert.ok(!requests.slice(start).some(url => url.includes("/model-configs/discover")), "Manual add must not discover remote models"); |
| 139 | + await shot(`${locale}-mock-zhipu-manual-add.png`); |
| 140 | + await page.getByRole("button", { name: labels.cancel, exact: true }).click(); |
| 141 | + await page.getByRole("button", { name: labels.add, exact: true }).waitFor(); |
| 142 | +} |
| 143 | +async function proveAgentPicker() { |
| 144 | + // The picker runtime inventory is a response fixture. The Default group lists the |
| 145 | + // saved agent model configs from the real API, so no provider endpoint is contacted. |
| 146 | + await page.route(/\/assistant\/runtimes$/, route => route.fulfill({ |
| 147 | + json: [{ |
| 148 | + id: "default", label: "Default", group: "default", modelSource: "settings", |
| 149 | + available: true, status: "ready", modelOverride: false, |
| 150 | + }], |
| 151 | + })); |
| 152 | + await page.getByRole("button", { name: copy["zh-CN"].chatEntry, exact: true }).click(); |
| 153 | + await page.getByRole("button", { name: /^Agent:/ }).click(); |
| 154 | + await brandLogo(pickerRow.locator("img.cw-model-logo")); |
| 155 | + await pickerRow.click(); |
| 156 | + await page.getByRole("button", { name: /^Agent:/ }).click(); |
| 157 | + assert.equal( |
| 158 | + await pickerRow.getAttribute("aria-checked"), |
| 159 | + "true", "Saved Zhipu model stays selected in the assistant picker", |
| 160 | + ); |
| 161 | + await shot("zh-CN-mock-zhipu-agent-picker.png"); |
| 162 | + await page.keyboard.press("Escape"); |
| 163 | +} |
| 164 | + |
| 165 | +try { |
| 166 | + await page.goto(`${base}/ai-tool/ai-canvas/${projectId}?creation=unified`); |
| 167 | + const zh = copy["zh-CN"]; |
| 168 | + await page.getByRole("button", { name: zh.settings, exact: true }).click(); |
| 169 | + await page.getByRole("button", { name: zh.models, exact: true }).click(); |
| 170 | + await openAgentCategory(zh); |
| 171 | + await provePreset(zh, "zh-CN"); |
| 172 | + await page.getByRole("button", { name: zh.save, exact: true }).click(); |
| 173 | + await page.getByRole("button", { name: zh.add, exact: true }).waitFor(); |
| 174 | + const saved = (await call("/api/model-configs")).filter(config => config.url === canonicalURL); |
| 175 | + assert.deepEqual(saved.map(config => config.modelId).sort(), [...modelIds].sort()); |
| 176 | + assert.equal(new Set(saved.map(config => config.providerId)).size, 1); |
| 177 | + for (const model of saved) { |
| 178 | + assert.equal(model.kind, "agent"); |
| 179 | + assert.ok(!JSON.stringify(model).includes(fixtureKey)); |
| 180 | + assert.equal(Object.hasOwn(model, "apiKey"), false); |
| 181 | + } |
| 182 | + await proveReopen(zh, "zh-CN"); |
| 183 | + await proveManualAdd(zh, "zh-CN"); |
| 184 | + await page.getByRole("button", { name: zh.close, exact: true }).click(); |
| 185 | + await proveAgentPicker(); |
| 186 | + |
| 187 | + await page.getByRole("button", { name: zh.settings, exact: true }).click(); |
| 188 | + await page.getByRole("button", { name: zh.models, exact: true }).click(); |
| 189 | + await page.getByRole("button", { name: "通用", exact: true }).click(); |
| 190 | + await page.locator("#settings-language").selectOption("en-US"); |
| 191 | + const en = copy["en-US"]; |
| 192 | + await page.getByRole("button", { name: en.models, exact: true }).click(); |
| 193 | + await openAgentCategory(en); |
| 194 | + await provePreset(en, "en-US"); |
| 195 | + await page.getByRole("button", { name: en.cancel, exact: true }).click(); |
| 196 | + await proveReopen(en, "en-US"); |
| 197 | + await proveManualAdd(en, "en-US"); |
| 198 | + assert.ok(!requests.some(url => new URL(url).hostname === "open.bigmodel.cn"), "Browser must not contact the model provider"); |
| 199 | + assert.deepEqual(errors, []); |
| 200 | + console.log(JSON.stringify({ passed: true, fixture: true, generation: "not submitted", base, api, |
| 201 | + projectURL: `${base}/ai-tool/ai-canvas/${projectId}?creation=unified`, screenshotDirectory: output, |
| 202 | + modelConfigIds: saved.map(config => config.id), checks: ["Chinese and English", "Z.ai brand logo", |
| 203 | + "canonical URL", "GLM models preselected without discovery", "save and reopen", "public models have no key", |
| 204 | + "manual model IDs stay available", "assistant picker lists and keeps the Zhipu model", "no provider request from the browser"] })); |
| 205 | +} catch (error) { |
| 206 | + await shot("mock-zhipu-provider-failure.png").catch(() => {}); |
| 207 | + throw error; |
| 208 | +} finally { |
| 209 | + await browser.close(); |
| 210 | +} |
0 commit comments