|
2 | 2 | import { spawnSync } from "node:child_process"; |
3 | 3 | import fs from "node:fs/promises"; |
4 | 4 | import path from "node:path"; |
| 5 | +import { DatabaseSync } from "node:sqlite"; |
5 | 6 | import { withTempHome } from "openclaw/plugin-sdk/test-env"; |
6 | 7 | import { describe, expect, it } from "vitest"; |
7 | 8 |
|
@@ -229,6 +230,179 @@ describe("cli json stdout contract", () => { |
229 | 230 | ); |
230 | 231 | }); |
231 | 232 |
|
| 233 | + it.each([ |
| 234 | + { name: "leaf JSON", args: ["models", "refresh", "--json"] }, |
| 235 | + { name: "parent JSON", args: ["models", "--json", "refresh"] }, |
| 236 | + { name: "human output", args: ["models", "refresh"], human: true }, |
| 237 | + { |
| 238 | + name: "forced Commander JSON", |
| 239 | + args: ["models", "refresh", "--json"], |
| 240 | + commander: true, |
| 241 | + }, |
| 242 | + { |
| 243 | + name: "dual-TTY JSON", |
| 244 | + args: ["models", "refresh", "--json"], |
| 245 | + tty: true, |
| 246 | + }, |
| 247 | + ])("renders model catalog refresh failures canonically for $name", async (testCase) => { |
| 248 | + await withTempHome( |
| 249 | + async (tempHome) => { |
| 250 | + const preload = Buffer.from( |
| 251 | + [ |
| 252 | + 'import net from "node:net";', |
| 253 | + 'net.Socket.prototype.connect = function () { throw new Error("AUTOQA_NETWORK_FORBIDDEN"); };', |
| 254 | + 'globalThis.fetch = async () => { throw new Error("offline fixture"); };', |
| 255 | + "globalThis.fetch.mock = {};", |
| 256 | + ...("tty" in testCase |
| 257 | + ? [ |
| 258 | + 'Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true });', |
| 259 | + 'Object.defineProperty(process.stderr, "isTTY", { value: true, configurable: true });', |
| 260 | + ] |
| 261 | + : []), |
| 262 | + ].join("\n"), |
| 263 | + ).toString("base64"); |
| 264 | + const result = runBuiltCli(tempHome, testCase.args, { |
| 265 | + NODE_OPTIONS: `--import=data:text/javascript;base64,${preload}`, |
| 266 | + OPENCLAW_CONFIG_PATH: path.join(tempHome, "missing-openclaw.json"), |
| 267 | + OPENCLAW_STATE_DIR: path.join(tempHome, "isolated-state"), |
| 268 | + ...("commander" in testCase ? { OPENCLAW_DISABLE_ROUTE_FIRST: "1" } : {}), |
| 269 | + ...("tty" in testCase ? { FORCE_COLOR: "1" } : {}), |
| 270 | + }); |
| 271 | + const message = "Remote catalog refresh failed: Error: offline fixture"; |
| 272 | + |
| 273 | + expect(result.status, result.stderr).toBe(1); |
| 274 | + expect(result.stdout, result.stderr).not.toMatch(/[\u001B\u0007]/u); |
| 275 | + if ("human" in testCase) { |
| 276 | + expect(result.stdout).toBe(""); |
| 277 | + } else { |
| 278 | + expect(JSON.parse(result.stdout)).toEqual({ |
| 279 | + ok: false, |
| 280 | + error: { type: "cli_error", message }, |
| 281 | + }); |
| 282 | + } |
| 283 | + expect(result.stderr).toContain(message); |
| 284 | + expect(result.stderr.split(message)).toHaveLength(2); |
| 285 | + expect(result.stderr).not.toContain("AUTOQA_NETWORK_FORBIDDEN"); |
| 286 | + if ("tty" in testCase) { |
| 287 | + expect(result.stderr).toContain("\u001B[?25h"); |
| 288 | + } |
| 289 | + }, |
| 290 | + { prefix: "openclaw-models-refresh-json-failure-e2e-" }, |
| 291 | + ); |
| 292 | + }); |
| 293 | + |
| 294 | + it("preserves model catalog refresh success payloads and persisted rows", async () => { |
| 295 | + await withTempHome( |
| 296 | + async (tempHome) => { |
| 297 | + const stateDir = path.join(tempHome, "isolated-state"); |
| 298 | + const configPath = path.join(tempHome, "openclaw.json"); |
| 299 | + const databasePath = path.join(stateDir, "state", "openclaw.sqlite"); |
| 300 | + const generatedAt = Date.now() + 60_000; |
| 301 | + const bundle = { |
| 302 | + schemaVersion: 1, |
| 303 | + generatedAt, |
| 304 | + sourceCommit: "autoqa-model-catalog-fixture", |
| 305 | + providers: { openai: { models: [{ id: "gpt-5.6-luna" }] } }, |
| 306 | + }; |
| 307 | + const updatedBundle = { ...bundle, generatedAt: generatedAt + 1_000 }; |
| 308 | + const preloadFor = (response: "initial" | "updated" | "unchanged" | "failure") => { |
| 309 | + const fixture = response === "initial" ? bundle : updatedBundle; |
| 310 | + const fetchResponse = |
| 311 | + response === "failure" |
| 312 | + ? 'throw new Error("offline fixture");' |
| 313 | + : response === "unchanged" |
| 314 | + ? "return new Response(null, { status: 304 });" |
| 315 | + : `return new Response(JSON.stringify(${JSON.stringify(fixture)}), { headers: { etag: '\"fixture\"' } });`; |
| 316 | + return Buffer.from( |
| 317 | + [ |
| 318 | + 'import net from "node:net";', |
| 319 | + 'import module from "node:module";', |
| 320 | + 'net.Socket.prototype.connect = function () { throw new Error("AUTOQA_NETWORK_FORBIDDEN"); };', |
| 321 | + "const createRequire = module.createRequire;", |
| 322 | + 'module.createRequire = (...args) => new Proxy(createRequire(...args), { apply(target, receiver, request) { return String(request[0]).endsWith("/build-info.json") ? { builtAt: "2020-01-01T00:00:00.000Z" } : Reflect.apply(target, receiver, request); } });', |
| 323 | + "module.syncBuiltinESMExports();", |
| 324 | + `globalThis.fetch = async () => { ${fetchResponse} };`, |
| 325 | + "globalThis.fetch.mock = {};", |
| 326 | + ].join("\n"), |
| 327 | + ).toString("base64"); |
| 328 | + }; |
| 329 | + const runRefresh = ( |
| 330 | + args: string[], |
| 331 | + response: "initial" | "updated" | "unchanged" | "failure", |
| 332 | + ) => |
| 333 | + runBuiltCli(tempHome, ["models", ...args], { |
| 334 | + NODE_OPTIONS: `--import=data:text/javascript;base64,${preloadFor(response)}`, |
| 335 | + OPENCLAW_CONFIG_PATH: configPath, |
| 336 | + OPENCLAW_STATE_DIR: stateDir, |
| 337 | + }); |
| 338 | + const readCatalogRow = () => { |
| 339 | + const database = new DatabaseSync(databasePath, { readOnly: true }); |
| 340 | + try { |
| 341 | + return database.prepare("SELECT * FROM model_catalog_remote WHERE id = 1").get(); |
| 342 | + } finally { |
| 343 | + database.close(); |
| 344 | + } |
| 345 | + }; |
| 346 | + |
| 347 | + const human = runRefresh(["refresh"], "initial"); |
| 348 | + expect(human.status, human.stderr).toBe(0); |
| 349 | + expect(human.stdout).toContain("Remote catalog refresh: updated (1 providers, 1 models;"); |
| 350 | + expect(human.stdout).toContain( |
| 351 | + "A running Gateway applies the updated catalog after its next restart.", |
| 352 | + ); |
| 353 | + |
| 354 | + const updated = runRefresh(["refresh", "--json"], "updated"); |
| 355 | + expect(updated.status, updated.stderr).toBe(0); |
| 356 | + expect(JSON.parse(updated.stdout)).toEqual({ |
| 357 | + status: "updated", |
| 358 | + generatedAt: updatedBundle.generatedAt, |
| 359 | + providers: 1, |
| 360 | + models: 1, |
| 361 | + }); |
| 362 | + |
| 363 | + const unchanged = runRefresh(["--json", "refresh"], "unchanged"); |
| 364 | + expect(unchanged.status, unchanged.stderr).toBe(0); |
| 365 | + expect(JSON.parse(unchanged.stdout)).toEqual({ |
| 366 | + status: "unchanged", |
| 367 | + generatedAt: updatedBundle.generatedAt, |
| 368 | + providers: 1, |
| 369 | + models: 1, |
| 370 | + }); |
| 371 | + const persistedRow = readCatalogRow(); |
| 372 | + expect(persistedRow).toMatchObject({ |
| 373 | + generated_at: updatedBundle.generatedAt, |
| 374 | + bundle_json: JSON.stringify(updatedBundle), |
| 375 | + }); |
| 376 | + |
| 377 | + const failure = runRefresh(["refresh", "--json"], "failure"); |
| 378 | + expect(failure.status, failure.stderr).toBe(1); |
| 379 | + expect(JSON.parse(failure.stdout)).toEqual({ |
| 380 | + ok: false, |
| 381 | + error: { |
| 382 | + type: "cli_error", |
| 383 | + message: "Remote catalog refresh failed: Error: offline fixture", |
| 384 | + }, |
| 385 | + }); |
| 386 | + expect(readCatalogRow()).toEqual(persistedRow); |
| 387 | + |
| 388 | + await fs.writeFile( |
| 389 | + configPath, |
| 390 | + `${JSON.stringify({ models: { catalogRefresh: { enabled: false } } })}\n`, |
| 391 | + "utf8", |
| 392 | + ); |
| 393 | + const disabled = runRefresh(["refresh", "--json"], "failure"); |
| 394 | + expect(disabled.status, disabled.stderr).toBe(0); |
| 395 | + expect(JSON.parse(disabled.stdout)).toEqual({ |
| 396 | + status: "disabled", |
| 397 | + providers: 0, |
| 398 | + models: 0, |
| 399 | + }); |
| 400 | + expect(readCatalogRow()).toEqual(persistedRow); |
| 401 | + }, |
| 402 | + { prefix: "openclaw-models-refresh-persistence-e2e-" }, |
| 403 | + ); |
| 404 | + }); |
| 405 | + |
232 | 406 | it.each([ |
233 | 407 | { |
234 | 408 | name: "add without an interactive terminal in human mode", |
|
0 commit comments