test: cover shared installer flow helpers

This commit is contained in:
Peter Steinberger
2026-02-22 18:36:36 +00:00
parent 07888bee34
commit 12635de1c7
6 changed files with 452 additions and 156 deletions

View File

@@ -0,0 +1,122 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import * as archive from "./archive.js";
import { resolveExistingInstallPath, withExtractedArchiveRoot } from "./install-flow.js";
import * as installSource from "./install-source-utils.js";
describe("resolveExistingInstallPath", () => {
let fixtureRoot = "";
beforeEach(async () => {
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-install-flow-"));
});
afterEach(async () => {
if (fixtureRoot) {
await fs.rm(fixtureRoot, { recursive: true, force: true });
}
});
it("returns resolved path and stat for existing files", async () => {
const filePath = path.join(fixtureRoot, "plugin.tgz");
await fs.writeFile(filePath, "archive");
const result = await resolveExistingInstallPath(filePath);
expect(result.ok).toBe(true);
if (!result.ok) {
return;
}
expect(result.resolvedPath).toBe(filePath);
expect(result.stat.isFile()).toBe(true);
});
it("returns a path-not-found error for missing paths", async () => {
const missing = path.join(fixtureRoot, "missing.tgz");
const result = await resolveExistingInstallPath(missing);
expect(result).toEqual({
ok: false,
error: `path not found: ${missing}`,
});
});
});
describe("withExtractedArchiveRoot", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("extracts archive and passes root directory to callback", async () => {
const withTempDirSpy = vi
.spyOn(installSource, "withTempDir")
.mockImplementation(async (_prefix, fn) => await fn("/tmp/openclaw-install-flow"));
const extractSpy = vi.spyOn(archive, "extractArchive").mockResolvedValue(undefined);
const resolveRootSpy = vi
.spyOn(archive, "resolvePackedRootDir")
.mockResolvedValue("/tmp/openclaw-install-flow/extract/package");
const onExtracted = vi.fn(async (rootDir: string) => ({ ok: true as const, rootDir }));
const result = await withExtractedArchiveRoot({
archivePath: "/tmp/plugin.tgz",
tempDirPrefix: "openclaw-plugin-",
timeoutMs: 1000,
onExtracted,
});
expect(withTempDirSpy).toHaveBeenCalledWith("openclaw-plugin-", expect.any(Function));
expect(extractSpy).toHaveBeenCalledWith(
expect.objectContaining({
archivePath: "/tmp/plugin.tgz",
}),
);
expect(resolveRootSpy).toHaveBeenCalledWith("/tmp/openclaw-install-flow/extract");
expect(onExtracted).toHaveBeenCalledWith("/tmp/openclaw-install-flow/extract/package");
expect(result).toEqual({
ok: true,
rootDir: "/tmp/openclaw-install-flow/extract/package",
});
});
it("returns extract failure when extraction throws", async () => {
vi.spyOn(installSource, "withTempDir").mockImplementation(
async (_prefix, fn) => await fn("/tmp/openclaw-install-flow"),
);
vi.spyOn(archive, "extractArchive").mockRejectedValue(new Error("boom"));
const result = await withExtractedArchiveRoot({
archivePath: "/tmp/plugin.tgz",
tempDirPrefix: "openclaw-plugin-",
timeoutMs: 1000,
onExtracted: async () => ({ ok: true as const }),
});
expect(result).toEqual({
ok: false,
error: "failed to extract archive: Error: boom",
});
});
it("returns root-resolution failure when archive layout is invalid", async () => {
vi.spyOn(installSource, "withTempDir").mockImplementation(
async (_prefix, fn) => await fn("/tmp/openclaw-install-flow"),
);
vi.spyOn(archive, "extractArchive").mockResolvedValue(undefined);
vi.spyOn(archive, "resolvePackedRootDir").mockRejectedValue(new Error("invalid layout"));
const result = await withExtractedArchiveRoot({
archivePath: "/tmp/plugin.tgz",
tempDirPrefix: "openclaw-plugin-",
timeoutMs: 1000,
onExtracted: async () => ({ ok: true as const }),
});
expect(result).toEqual({
ok: false,
error: "Error: invalid layout",
});
});
});

View File

@@ -0,0 +1,51 @@
import { describe, expect, it } from "vitest";
import {
resolveInstallModeOptions,
resolveTimedInstallModeOptions,
} from "./install-mode-options.js";
describe("install mode option helpers", () => {
it("applies logger, mode, and dryRun defaults", () => {
const logger = { warn: (_message: string) => {} };
const result = resolveInstallModeOptions({}, logger);
expect(result).toEqual({
logger,
mode: "install",
dryRun: false,
});
});
it("preserves explicit mode and dryRun values", () => {
const logger = { warn: (_message: string) => {} };
const result = resolveInstallModeOptions(
{
logger,
mode: "update",
dryRun: true,
},
{ warn: () => {} },
);
expect(result).toEqual({
logger,
mode: "update",
dryRun: true,
});
});
it("uses default timeout when not provided", () => {
const logger = { warn: (_message: string) => {} };
const result = resolveTimedInstallModeOptions({}, logger);
expect(result.timeoutMs).toBe(120_000);
expect(result.mode).toBe("install");
expect(result.dryRun).toBe(false);
});
it("honors custom timeout default override", () => {
const result = resolveTimedInstallModeOptions({}, { warn: () => {} }, 5000);
expect(result.timeoutMs).toBe(5000);
});
});

View File

@@ -1,7 +1,11 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { packNpmSpecToArchive, withTempDir } from "./install-source-utils.js";
import type { NpmIntegrityDriftPayload } from "./npm-integrity.js";
import { installFromNpmSpecArchive } from "./npm-pack-install.js";
import {
finalizeNpmSpecArchiveInstall,
installFromNpmSpecArchive,
installFromNpmSpecArchiveWithInstaller,
} from "./npm-pack-install.js";
vi.mock("./install-source-utils.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("./install-source-utils.js")>();
@@ -173,3 +177,99 @@ describe("installFromNpmSpecArchive", () => {
expect(okResult.integrityDrift).toBeUndefined();
});
});
describe("installFromNpmSpecArchiveWithInstaller", () => {
beforeEach(() => {
vi.mocked(packNpmSpecToArchive).mockClear();
});
it("passes archive path and installer params to installFromArchive", async () => {
vi.mocked(packNpmSpecToArchive).mockResolvedValue({
ok: true,
archivePath: "/tmp/openclaw-plugin.tgz",
metadata: {
resolvedSpec: "@openclaw/voice-call@1.0.0",
integrity: "sha512-same",
},
});
const installFromArchive = vi.fn(
async (_params: { archivePath: string; pluginId: string }) =>
({ ok: true as const, pluginId: "voice-call" }) as const,
);
const result = await installFromNpmSpecArchiveWithInstaller({
tempDirPrefix: "openclaw-test-",
spec: "@openclaw/voice-call@1.0.0",
timeoutMs: 1000,
installFromArchive,
archiveInstallParams: { pluginId: "voice-call" },
});
expect(result.ok).toBe(true);
if (!result.ok) {
return;
}
expect(installFromArchive).toHaveBeenCalledWith({
archivePath: "/tmp/openclaw-plugin.tgz",
pluginId: "voice-call",
});
expect(result.installResult).toEqual({ ok: true, pluginId: "voice-call" });
});
});
describe("finalizeNpmSpecArchiveInstall", () => {
it("returns top-level flow errors unchanged", () => {
const result = finalizeNpmSpecArchiveInstall<{ ok: true } | { ok: false; error: string }>({
ok: false,
error: "pack failed",
});
expect(result).toEqual({ ok: false, error: "pack failed" });
});
it("returns install errors unchanged", () => {
const result = finalizeNpmSpecArchiveInstall<{ ok: true } | { ok: false; error: string }>({
ok: true,
installResult: { ok: false, error: "install failed" },
npmResolution: {
resolvedSpec: "@openclaw/test@1.0.0",
integrity: "sha512-same",
resolvedAt: "2026-01-01T00:00:00.000Z",
},
});
expect(result).toEqual({ ok: false, error: "install failed" });
});
it("attaches npm metadata to successful install results", () => {
const result = finalizeNpmSpecArchiveInstall<
{ ok: true; pluginId: string } | { ok: false; error: string }
>({
ok: true,
installResult: { ok: true, pluginId: "voice-call" },
npmResolution: {
resolvedSpec: "@openclaw/voice-call@1.0.0",
integrity: "sha512-same",
resolvedAt: "2026-01-01T00:00:00.000Z",
},
integrityDrift: {
expectedIntegrity: "sha512-old",
actualIntegrity: "sha512-same",
},
});
expect(result).toEqual({
ok: true,
pluginId: "voice-call",
npmResolution: {
resolvedSpec: "@openclaw/voice-call@1.0.0",
integrity: "sha512-same",
resolvedAt: "2026-01-01T00:00:00.000Z",
},
integrityDrift: {
expectedIntegrity: "sha512-old",
actualIntegrity: "sha512-same",
},
});
});
});