|
1 | 1 | // Plugin npm manifest tests validate generated plugin package manifests. |
2 | 2 | import { spawnSync } from "node:child_process"; |
3 | | -import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 3 | +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
4 | 4 | import { dirname, join, win32 } from "node:path"; |
5 | 5 | import { afterEach, describe, expect, it } from "vitest"; |
6 | 6 | import { |
7 | 7 | resolveAugmentedPluginNpmPackageJson, |
8 | 8 | resolveAugmentedPluginNpmManifest, |
9 | 9 | resolvePluginNpmCommand, |
| 10 | + runPluginNpmCiWithRetry, |
10 | 11 | withAugmentedPluginNpmManifestForPackage, |
11 | 12 | } from "../scripts/lib/plugin-npm-package-manifest.mjs"; |
12 | 13 | import { cleanupTempDirs, makeTempRepoRoot, writeJsonFile } from "./helpers/temp-repo.js"; |
@@ -188,6 +189,99 @@ describe("plugin npm package manifest staging", () => { |
188 | 189 | ).toThrow("OpenClaw refuses to shell out to bare npm on Windows"); |
189 | 190 | }); |
190 | 191 |
|
| 192 | + it("retries timed-out bundled dependency installs after cleaning partial output", () => { |
| 193 | + const timeoutError = Object.assign(new Error("timed out"), { code: "ETIMEDOUT" }); |
| 194 | + const spawnResults = [ |
| 195 | + { error: timeoutError, status: null }, |
| 196 | + { error: undefined, status: 0 }, |
| 197 | + ]; |
| 198 | + const spawnOptions: Array<Record<string, unknown>> = []; |
| 199 | + let cleanupCalls = 0; |
| 200 | + |
| 201 | + const result = runPluginNpmCiWithRetry( |
| 202 | + ["ci"], |
| 203 | + { cwd: "/tmp/plugin" }, |
| 204 | + { |
| 205 | + cleanupAttempt: () => { |
| 206 | + cleanupCalls += 1; |
| 207 | + }, |
| 208 | + pluginDir: "whatsapp", |
| 209 | + spawn: (_args: string[], options: Record<string, unknown>) => { |
| 210 | + spawnOptions.push(options); |
| 211 | + return spawnResults.shift(); |
| 212 | + }, |
| 213 | + timeoutMs: 1234, |
| 214 | + }, |
| 215 | + ) as { status: number | null }; |
| 216 | + |
| 217 | + expect(result.status).toBe(0); |
| 218 | + expect(cleanupCalls).toBe(1); |
| 219 | + expect(spawnOptions).toEqual([ |
| 220 | + { cwd: "/tmp/plugin", timeout: 1234 }, |
| 221 | + { cwd: "/tmp/plugin", timeout: 1234 }, |
| 222 | + ]); |
| 223 | + }); |
| 224 | + |
| 225 | + it("does not retry ordinary bundled dependency install failures", () => { |
| 226 | + let spawnCalls = 0; |
| 227 | + const result = runPluginNpmCiWithRetry( |
| 228 | + ["ci"], |
| 229 | + {}, |
| 230 | + { |
| 231 | + cleanupAttempt: () => { |
| 232 | + throw new Error("cleanup should not run"); |
| 233 | + }, |
| 234 | + spawn: () => { |
| 235 | + spawnCalls += 1; |
| 236 | + return { error: undefined, status: 1 }; |
| 237 | + }, |
| 238 | + }, |
| 239 | + ) as { status: number | null }; |
| 240 | + |
| 241 | + expect(result.status).toBe(1); |
| 242 | + expect(spawnCalls).toBe(1); |
| 243 | + }); |
| 244 | + |
| 245 | + it("cleans an exhausted timeout before reusing the same package directory", () => { |
| 246 | + const repoDir = makeTempRepoRoot(tempDirs, "openclaw-plugin-npm-timeout-"); |
| 247 | + const packageDir = join(repoDir, "extensions", "whatsapp"); |
| 248 | + const nodeModulesPath = join(packageDir, "node_modules"); |
| 249 | + const timeoutError = Object.assign(new Error("timed out"), { code: "ETIMEDOUT" }); |
| 250 | + mkdirSync(packageDir, { recursive: true }); |
| 251 | + |
| 252 | + const firstResult = runPluginNpmCiWithRetry( |
| 253 | + ["ci"], |
| 254 | + { cwd: packageDir }, |
| 255 | + { |
| 256 | + attempts: 3, |
| 257 | + cleanupAttempt: () => rmSync(nodeModulesPath, { recursive: true, force: true }), |
| 258 | + pluginDir: "whatsapp", |
| 259 | + spawn: () => { |
| 260 | + mkdirSync(nodeModulesPath, { recursive: true }); |
| 261 | + return { error: timeoutError, status: null }; |
| 262 | + }, |
| 263 | + }, |
| 264 | + ) as { error?: NodeJS.ErrnoException }; |
| 265 | + |
| 266 | + expect(firstResult.error?.code).toBe("ETIMEDOUT"); |
| 267 | + expect(existsSync(nodeModulesPath)).toBe(false); |
| 268 | + |
| 269 | + const secondResult = runPluginNpmCiWithRetry( |
| 270 | + ["ci"], |
| 271 | + { cwd: packageDir }, |
| 272 | + { |
| 273 | + cleanupAttempt: () => rmSync(nodeModulesPath, { recursive: true, force: true }), |
| 274 | + pluginDir: "whatsapp", |
| 275 | + spawn: () => { |
| 276 | + expect(existsSync(nodeModulesPath)).toBe(false); |
| 277 | + return { error: undefined, status: 0 }; |
| 278 | + }, |
| 279 | + }, |
| 280 | + ) as { status: number | null }; |
| 281 | + |
| 282 | + expect(secondResult.status).toBe(0); |
| 283 | + }); |
| 284 | + |
191 | 285 | it("overlays generated channel configs while packing and restores source manifest", () => { |
192 | 286 | const repoDir = makeTempRepoRoot(tempDirs, "openclaw-plugin-npm-package-manifest-"); |
193 | 287 | const packageDir = join(repoDir, "extensions", "twitch"); |
|
0 commit comments