|
| 1 | +// Proves startup update discovery through the real extended-stable registry resolver. |
| 2 | +import http from "node:http"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js"; |
| 5 | +import { |
| 6 | + createOpenClawTestState, |
| 7 | + type OpenClawTestState, |
| 8 | +} from "../test-utils/openclaw-test-state.js"; |
| 9 | +import type { UpdateCheckResult } from "./update-check.js"; |
| 10 | + |
| 11 | +vi.mock("./openclaw-root.js", async () => { |
| 12 | + const actual = await vi.importActual<typeof import("./openclaw-root.js")>("./openclaw-root.js"); |
| 13 | + return { |
| 14 | + ...actual, |
| 15 | + resolveOpenClawPackageRoot: vi.fn(async () => "/opt/openclaw"), |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +vi.mock("./update-check.js", async () => { |
| 20 | + const actual = await vi.importActual<typeof import("./update-check.js")>("./update-check.js"); |
| 21 | + return { |
| 22 | + ...actual, |
| 23 | + checkUpdateStatus: vi.fn(async () => ({ |
| 24 | + root: "/opt/openclaw", |
| 25 | + installKind: "package", |
| 26 | + packageManager: "npm", |
| 27 | + }) satisfies UpdateCheckResult), |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +vi.mock("../version.js", () => ({ |
| 32 | + VERSION: "1.0.0", |
| 33 | +})); |
| 34 | + |
| 35 | +describe("extended-stable startup update integration", () => { |
| 36 | + let testState: OpenClawTestState; |
| 37 | + let server: http.Server | undefined; |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + server = undefined; |
| 41 | + testState = await createOpenClawTestState({ |
| 42 | + layout: "state-only", |
| 43 | + prefix: "openclaw-update-startup-integration-", |
| 44 | + env: { |
| 45 | + NODE_ENV: "test", |
| 46 | + NPM_CONFIG_REGISTRY: undefined, |
| 47 | + OPENCLAW_UPDATE_PACKAGE_SPEC: undefined, |
| 48 | + VITEST: undefined, |
| 49 | + }, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(async () => { |
| 54 | + const activeServer = server; |
| 55 | + if (activeServer) { |
| 56 | + await new Promise<void>((resolve, reject) => { |
| 57 | + activeServer.close((error) => (error ? reject(error) : resolve())); |
| 58 | + }); |
| 59 | + } |
| 60 | + closeOpenClawStateDatabaseForTest(); |
| 61 | + await testState.cleanup(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("emits a read-only hint after verifying a newer exact loopback package", async () => { |
| 65 | + const requests: string[] = []; |
| 66 | + const registryServer = http.createServer((request, response) => { |
| 67 | + requests.push(request.url ?? ""); |
| 68 | + response.writeHead(200, { "content-type": "application/json" }); |
| 69 | + response.end(JSON.stringify({ version: "2.0.0" })); |
| 70 | + }); |
| 71 | + server = registryServer; |
| 72 | + await new Promise<void>((resolve) => { |
| 73 | + registryServer.listen(0, "127.0.0.1", resolve); |
| 74 | + }); |
| 75 | + const address = registryServer.address(); |
| 76 | + if (!address || typeof address === "string") { |
| 77 | + throw new Error("expected loopback registry address"); |
| 78 | + } |
| 79 | + process.env.OPENCLAW_UPDATE_PACKAGE_SPEC = "openclaw"; |
| 80 | + process.env.NPM_CONFIG_REGISTRY = `http://127.0.0.1:${address.port}/`; |
| 81 | + |
| 82 | + const { runGatewayUpdateCheck, resetUpdateAvailableStateForTest } = |
| 83 | + await import("./update-startup.js"); |
| 84 | + resetUpdateAvailableStateForTest(); |
| 85 | + const log = { info: vi.fn() }; |
| 86 | + const onUpdateAvailableChange = vi.fn(); |
| 87 | + const runAutoUpdate = vi.fn(); |
| 88 | + |
| 89 | + await runGatewayUpdateCheck({ |
| 90 | + cfg: { update: { channel: "extended-stable", auto: { enabled: true } } }, |
| 91 | + log, |
| 92 | + isNixMode: false, |
| 93 | + allowInTests: true, |
| 94 | + onUpdateAvailableChange, |
| 95 | + runAutoUpdate, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(requests).toEqual(["/openclaw/extended-stable", "/openclaw/2.0.0"]); |
| 99 | + expect(onUpdateAvailableChange).toHaveBeenCalledWith({ |
| 100 | + currentVersion: "1.0.0", |
| 101 | + latestVersion: "2.0.0", |
| 102 | + channel: "extended-stable", |
| 103 | + }); |
| 104 | + expect(log.info).toHaveBeenCalledWith( |
| 105 | + "update available (extended-stable): v2.0.0 (current v1.0.0). Run: openclaw update", |
| 106 | + ); |
| 107 | + expect(runAutoUpdate).not.toHaveBeenCalled(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments