|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { withEnvAsync } from "../../test-utils/env.js"; |
| 6 | +import { withOpenClawTestState } from "../../test-utils/openclaw-test-state.js"; |
| 7 | +import type { SkillEntry, SkillInstallSpec } from "../types.js"; |
| 8 | +import { installSkill } from "./install.js"; |
| 9 | +import { skillsInstallTesting } from "./install.test-support.js"; |
| 10 | + |
| 11 | +const { runCommandWithTimeoutMock } = vi.hoisted(() => ({ |
| 12 | + runCommandWithTimeoutMock: vi.fn<typeof import("../../process/exec.js").runCommandWithTimeout>(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../../process/exec.js", () => ({ |
| 16 | + runCommandWithTimeout: runCommandWithTimeoutMock, |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("../../plugins/install-security-scan.js", () => ({ |
| 20 | + evaluateSkillInstallPolicy: vi.fn(async () => undefined), |
| 21 | +})); |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + skillsInstallTesting.setDepsForTest(); |
| 25 | + runCommandWithTimeoutMock.mockReset(); |
| 26 | + vi.restoreAllMocks(); |
| 27 | +}); |
| 28 | + |
| 29 | +describe.each(["uv", "go"] as const)("%s bootstrap cache freshness", (kind) => { |
| 30 | + it.each([0, 1])("reuses the prerequisite after the first recipe exits %i", async (firstCode) => { |
| 31 | + await withOpenClawTestState({ label: "skill-bootstrap-probe" }, async (state) => { |
| 32 | + vi.spyOn(os, "homedir").mockReturnValue(state.home); |
| 33 | + const prefix = path.join(state.home, ".local"); |
| 34 | + const binDir = path.join(prefix, "bin"); |
| 35 | + await fs.mkdir(binDir, { recursive: true }); |
| 36 | + const writeExecutable = async (name: string) => { |
| 37 | + const executable = path.join(binDir, name); |
| 38 | + await fs.writeFile(executable, "#!/bin/sh\nexit 0\n"); |
| 39 | + await fs.chmod(executable, 0o755); |
| 40 | + }; |
| 41 | + await writeExecutable("brew"); |
| 42 | + |
| 43 | + const entries: SkillEntry[] = ["first", "second"].map((name) => { |
| 44 | + const filePath = path.join(state.workspaceDir, name, "SKILL.md"); |
| 45 | + const spec: SkillInstallSpec = |
| 46 | + kind === "uv" |
| 47 | + ? { kind, package: `fixture-${name}` } |
| 48 | + : { kind, module: `example.com/fixture-${name}@latest` }; |
| 49 | + return { |
| 50 | + skill: { |
| 51 | + name, |
| 52 | + description: "Binary probe fixture", |
| 53 | + filePath, |
| 54 | + baseDir: path.dirname(filePath), |
| 55 | + source: "openclaw-workspace", |
| 56 | + sourceInfo: { path: filePath, source: "test", scope: "temporary", origin: "top-level" }, |
| 57 | + disableModelInvocation: false, |
| 58 | + }, |
| 59 | + frontmatter: {}, |
| 60 | + metadata: { install: [{ id: "deps", ...spec }] }, |
| 61 | + }; |
| 62 | + }); |
| 63 | + skillsInstallTesting.setDepsForTest({ loadWorkspaceSkills: () => entries }); |
| 64 | + |
| 65 | + let bootstraps = 0; |
| 66 | + let recipes = 0; |
| 67 | + await withEnvAsync({ PATH: binDir }, async () => { |
| 68 | + runCommandWithTimeoutMock.mockImplementation(async (argv, options) => { |
| 69 | + let stdout = ""; |
| 70 | + let code = 0; |
| 71 | + let stderr = ""; |
| 72 | + if (argv[0] === "brew" && argv[1] === "install" && argv[2] === kind) { |
| 73 | + bootstraps += 1; |
| 74 | + await writeExecutable(kind); |
| 75 | + } else if (argv[0] === "brew" && argv[1] === "--prefix") { |
| 76 | + stdout = prefix; |
| 77 | + } else { |
| 78 | + const name = recipes === 0 ? "first" : "second"; |
| 79 | + expect(argv).toEqual( |
| 80 | + kind === "uv" |
| 81 | + ? ["uv", "tool", "install", `fixture-${name}`] |
| 82 | + : ["go", "install", `example.com/fixture-${name}@latest`], |
| 83 | + ); |
| 84 | + await fs.access(path.join(binDir, kind), fs.constants.X_OK); |
| 85 | + if (kind === "go") { |
| 86 | + expect(options).toMatchObject({ env: { PATH: binDir, GOBIN: binDir } }); |
| 87 | + } |
| 88 | + code = recipes++ === 0 ? firstCode : 0; |
| 89 | + stdout = `recipe ${name}`; |
| 90 | + stderr = code === 0 ? "" : "fixture recipe failed"; |
| 91 | + } |
| 92 | + return { code, stdout, stderr, signal: null, killed: false, termination: "exit" }; |
| 93 | + }); |
| 94 | + |
| 95 | + for (const [index, skillName] of ["first", "second"].entries()) { |
| 96 | + const code = index === 0 ? firstCode : 0; |
| 97 | + const result = await installSkill({ |
| 98 | + workspaceDir: state.workspaceDir, |
| 99 | + skillName, |
| 100 | + installId: "deps", |
| 101 | + }); |
| 102 | + expect(result).toMatchObject({ |
| 103 | + ok: code === 0, |
| 104 | + code, |
| 105 | + stdout: `recipe ${skillName}`, |
| 106 | + stderr: code === 0 ? "" : "fixture recipe failed", |
| 107 | + }); |
| 108 | + expect(process.env.PATH).toBe(binDir); |
| 109 | + } |
| 110 | + expect(recipes).toBe(2); |
| 111 | + expect(bootstraps).toBe(1); |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments