|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js"; |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + listManagedPluginNpmRoots: vi.fn(), |
| 8 | + maybeRepairStaleManagedNpmBundledPlugins: vi.fn(), |
| 9 | + repairMissingConfiguredPluginInstalls: vi.fn(), |
| 10 | + relinkOpenClawPeerDependenciesInManagedNpmRoot: vi.fn(), |
| 11 | + runPluginPayloadSmokeCheck: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("../../commands/doctor/shared/missing-configured-plugin-install.js", () => ({ |
| 15 | + repairMissingConfiguredPluginInstalls: mocks.repairMissingConfiguredPluginInstalls, |
| 16 | +})); |
| 17 | +vi.mock("../../commands/doctor-plugin-registry.js", () => ({ |
| 18 | + maybeRepairStaleManagedNpmBundledPlugins: mocks.maybeRepairStaleManagedNpmBundledPlugins, |
| 19 | +})); |
| 20 | +vi.mock("../../plugins/plugin-peer-link.js", async (importOriginal) => { |
| 21 | + const actual = await importOriginal<typeof import("../../plugins/plugin-peer-link.js")>(); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + relinkOpenClawPeerDependenciesInManagedNpmRoot: |
| 25 | + mocks.relinkOpenClawPeerDependenciesInManagedNpmRoot, |
| 26 | + }; |
| 27 | +}); |
| 28 | +vi.mock("../../plugins/npm-project-roots.js", async (importOriginal) => { |
| 29 | + const actual = await importOriginal<typeof import("../../plugins/npm-project-roots.js")>(); |
| 30 | + return { |
| 31 | + ...actual, |
| 32 | + listManagedPluginNpmRoots: mocks.listManagedPluginNpmRoots, |
| 33 | + }; |
| 34 | +}); |
| 35 | +vi.mock("./plugin-payload-validation.js", () => ({ |
| 36 | + runPluginPayloadSmokeCheck: mocks.runPluginPayloadSmokeCheck, |
| 37 | +})); |
| 38 | + |
| 39 | +import { resolvePluginNpmGenerationProjectDir } from "../../plugins/install-paths.js"; |
| 40 | +import { |
| 41 | + loadInstalledPluginIndexInstallRecords, |
| 42 | + readPersistedInstalledPluginIndexInstallRecords, |
| 43 | + writePersistedInstalledPluginIndexInstallRecords, |
| 44 | +} from "../../plugins/installed-plugin-index-records.js"; |
| 45 | +import { VERSION } from "../../version.js"; |
| 46 | +import { runPostCorePluginConvergence } from "./post-core-plugin-convergence.js"; |
| 47 | + |
| 48 | +describe("post-core bundled plugin retirement", () => { |
| 49 | + const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + vi.clearAllMocks(); |
| 53 | + mocks.listManagedPluginNpmRoots.mockImplementation((npmRoot: string) => |
| 54 | + Promise.resolve([npmRoot]), |
| 55 | + ); |
| 56 | + mocks.relinkOpenClawPeerDependenciesInManagedNpmRoot.mockResolvedValue({ |
| 57 | + checked: 0, |
| 58 | + attempted: 0, |
| 59 | + repaired: 0, |
| 60 | + skipped: 0, |
| 61 | + }); |
| 62 | + mocks.runPluginPayloadSmokeCheck.mockResolvedValue({ checked: [], failures: [] }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("retires payload and record state before repair across two starts", async () => { |
| 66 | + const stateDir = tempDirs.make("openclaw-post-core-convergence-"); |
| 67 | + const bundledRoot = tempDirs.make("openclaw-post-core-bundled-"); |
| 68 | + const cfg = { |
| 69 | + update: { channel: "beta" as const }, |
| 70 | + plugins: { allow: ["codex"], entries: { codex: { enabled: true } } }, |
| 71 | + }; |
| 72 | + const env = { |
| 73 | + OPENCLAW_STATE_DIR: stateDir, |
| 74 | + OPENCLAW_BUNDLED_PLUGINS_DIR: bundledRoot, |
| 75 | + OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR: "1", |
| 76 | + VITEST: "true", |
| 77 | + }; |
| 78 | + const bundledDir = path.join(bundledRoot, "codex"); |
| 79 | + fs.mkdirSync(bundledDir, { recursive: true }); |
| 80 | + fs.writeFileSync(path.join(bundledDir, "index.js"), "export default {};\n", "utf8"); |
| 81 | + fs.writeFileSync( |
| 82 | + path.join(bundledDir, "openclaw.plugin.json"), |
| 83 | + JSON.stringify({ |
| 84 | + id: "codex", |
| 85 | + name: "codex", |
| 86 | + version: VERSION, |
| 87 | + configSchema: { type: "object" }, |
| 88 | + }), |
| 89 | + "utf8", |
| 90 | + ); |
| 91 | + fs.writeFileSync( |
| 92 | + path.join(bundledDir, "package.json"), |
| 93 | + JSON.stringify({ name: "@openclaw/codex", version: VERSION }), |
| 94 | + "utf8", |
| 95 | + ); |
| 96 | + const npmRoot = resolvePluginNpmGenerationProjectDir({ |
| 97 | + npmDir: path.join(stateDir, "npm"), |
| 98 | + packageName: "@openclaw/codex", |
| 99 | + generationKey: "@openclaw/[email protected]", |
| 100 | + }); |
| 101 | + const packageDir = path.join(npmRoot, "node_modules", "@openclaw", "codex"); |
| 102 | + fs.mkdirSync(packageDir, { recursive: true }); |
| 103 | + fs.writeFileSync( |
| 104 | + path.join(npmRoot, "package.json"), |
| 105 | + JSON.stringify({ dependencies: { "@openclaw/codex": "2026.7.2-beta.7" } }), |
| 106 | + "utf8", |
| 107 | + ); |
| 108 | + fs.writeFileSync( |
| 109 | + path.join(packageDir, "package.json"), |
| 110 | + JSON.stringify({ name: "@openclaw/codex", version: "2026.7.2-beta.7" }), |
| 111 | + "utf8", |
| 112 | + ); |
| 113 | + fs.writeFileSync( |
| 114 | + path.join(packageDir, "openclaw.plugin.json"), |
| 115 | + JSON.stringify({ id: "codex", name: "codex", configSchema: { type: "object" } }), |
| 116 | + "utf8", |
| 117 | + ); |
| 118 | + await writePersistedInstalledPluginIndexInstallRecords( |
| 119 | + { |
| 120 | + codex: { |
| 121 | + source: "npm", |
| 122 | + spec: "@openclaw/codex@beta", |
| 123 | + installPath: packageDir, |
| 124 | + version: "2026.7.2-beta.7", |
| 125 | + resolvedName: "@openclaw/codex", |
| 126 | + resolvedSpec: "@openclaw/[email protected]", |
| 127 | + resolvedVersion: "2026.7.2-beta.7", |
| 128 | + }, |
| 129 | + }, |
| 130 | + { config: cfg, env }, |
| 131 | + ); |
| 132 | + const actualDoctorRegistry = await vi.importActual< |
| 133 | + typeof import("../../commands/doctor-plugin-registry.js") |
| 134 | + >("../../commands/doctor-plugin-registry.js"); |
| 135 | + mocks.maybeRepairStaleManagedNpmBundledPlugins.mockImplementation( |
| 136 | + actualDoctorRegistry.maybeRepairStaleManagedNpmBundledPlugins, |
| 137 | + ); |
| 138 | + let installAttempts = 0; |
| 139 | + mocks.repairMissingConfiguredPluginInstalls.mockImplementation(async (params) => { |
| 140 | + const records = |
| 141 | + params.baselineRecords ?? |
| 142 | + (await loadInstalledPluginIndexInstallRecords({ env: params.env })); |
| 143 | + const codexRecord = records.codex; |
| 144 | + if (codexRecord) { |
| 145 | + installAttempts += 1; |
| 146 | + const retryRoot = resolvePluginNpmGenerationProjectDir({ |
| 147 | + npmDir: path.join(stateDir, "npm"), |
| 148 | + packageName: "@openclaw/codex", |
| 149 | + generationKey: `@openclaw/codex@retry-${installAttempts}`, |
| 150 | + }); |
| 151 | + const retryPackageDir = path.join(retryRoot, "node_modules", "@openclaw", "codex"); |
| 152 | + fs.mkdirSync(retryPackageDir, { recursive: true }); |
| 153 | + const nextRecords = { |
| 154 | + ...records, |
| 155 | + codex: { ...codexRecord, installPath: retryPackageDir }, |
| 156 | + }; |
| 157 | + await writePersistedInstalledPluginIndexInstallRecords(nextRecords, { |
| 158 | + config: cfg, |
| 159 | + env: params.env, |
| 160 | + }); |
| 161 | + return { |
| 162 | + changes: ['Refreshed stale configured plugin "codex" from @openclaw/codex@beta.'], |
| 163 | + warnings: [], |
| 164 | + records: nextRecords, |
| 165 | + }; |
| 166 | + } |
| 167 | + if (params.baselineRecords) { |
| 168 | + await writePersistedInstalledPluginIndexInstallRecords(records, { |
| 169 | + config: cfg, |
| 170 | + env: params.env, |
| 171 | + }); |
| 172 | + } |
| 173 | + return { changes: [], warnings: [], records }; |
| 174 | + }); |
| 175 | + |
| 176 | + const first = await runPostCorePluginConvergence({ cfg, env }); |
| 177 | + const projectsAfterFirst = fs.readdirSync(path.join(stateDir, "npm", "projects")); |
| 178 | + const second = await runPostCorePluginConvergence({ cfg, env }); |
| 179 | + |
| 180 | + expect(fs.existsSync(packageDir)).toBe(false); |
| 181 | + expect(installAttempts).toBe(0); |
| 182 | + expect(projectsAfterFirst).toHaveLength(1); |
| 183 | + expect(fs.readdirSync(path.join(stateDir, "npm", "projects"))).toEqual(projectsAfterFirst); |
| 184 | + expect(await readPersistedInstalledPluginIndexInstallRecords({ env })).not.toHaveProperty( |
| 185 | + "codex", |
| 186 | + ); |
| 187 | + expect(first.installRecords).not.toHaveProperty("codex"); |
| 188 | + expect(second.installRecords).not.toHaveProperty("codex"); |
| 189 | + expect(first.changes).toContain( |
| 190 | + 'Removed stale managed install record for bundled plugin "codex".', |
| 191 | + ); |
| 192 | + expect(second.changes).toEqual([]); |
| 193 | + }); |
| 194 | +}); |
0 commit comments