refactor(core): dedupe gateway runtime and config tests

This commit is contained in:
Peter Steinberger
2026-02-22 07:37:11 +00:00
parent ad1c07e7c0
commit b109fa53ea
20 changed files with 699 additions and 561 deletions

View File

@@ -14,6 +14,62 @@ vi.mock("./install-source-utils.js", async (importOriginal) => {
});
describe("installFromNpmSpecArchive", () => {
const baseSpec = "@openclaw/test@1.0.0";
const baseArchivePath = "/tmp/openclaw-test.tgz";
const mockPackedSuccess = (overrides?: {
resolvedSpec?: string;
integrity?: string;
name?: string;
version?: string;
}) => {
vi.mocked(packNpmSpecToArchive).mockResolvedValue({
ok: true,
archivePath: baseArchivePath,
metadata: {
resolvedSpec: overrides?.resolvedSpec ?? baseSpec,
integrity: overrides?.integrity ?? "sha512-same",
...(overrides?.name ? { name: overrides.name } : {}),
...(overrides?.version ? { version: overrides.version } : {}),
},
});
};
const runInstall = async (overrides: {
expectedIntegrity?: string;
onIntegrityDrift?: (payload: {
spec: string;
expectedIntegrity: string;
actualIntegrity: string;
resolvedSpec: string;
}) => boolean | Promise<boolean>;
warn?: (message: string) => void;
installFromArchive: (params: {
archivePath: string;
}) => Promise<{ ok: boolean; [k: string]: unknown }>;
}) =>
await installFromNpmSpecArchive({
tempDirPrefix: "openclaw-test-",
spec: baseSpec,
timeoutMs: 1000,
expectedIntegrity: overrides.expectedIntegrity,
onIntegrityDrift: overrides.onIntegrityDrift,
warn: overrides.warn,
installFromArchive: overrides.installFromArchive,
});
const expectWrappedOkResult = (
result: Awaited<ReturnType<typeof runInstall>>,
installResult: Record<string, unknown>,
) => {
expect(result.ok).toBe(true);
if (!result.ok) {
throw new Error("expected ok result");
}
expect(result.installResult).toEqual(installResult);
return result;
};
beforeEach(() => {
vi.mocked(packNpmSpecToArchive).mockReset();
vi.mocked(withTempDir).mockClear();
@@ -36,52 +92,45 @@ describe("installFromNpmSpecArchive", () => {
});
it("returns resolution metadata and installer result on success", async () => {
vi.mocked(packNpmSpecToArchive).mockResolvedValue({
ok: true,
archivePath: "/tmp/openclaw-test.tgz",
metadata: {
name: "@openclaw/test",
version: "1.0.0",
resolvedSpec: "@openclaw/test@1.0.0",
integrity: "sha512-same",
},
});
mockPackedSuccess({ name: "@openclaw/test", version: "1.0.0" });
const installFromArchive = vi.fn(async () => ({ ok: true as const, target: "done" }));
const result = await installFromNpmSpecArchive({
tempDirPrefix: "openclaw-test-",
spec: "@openclaw/test@1.0.0",
timeoutMs: 1000,
const result = await runInstall({
expectedIntegrity: "sha512-same",
installFromArchive,
});
expect(result.ok).toBe(true);
if (!result.ok) {
return;
}
expect(result.installResult).toEqual({ ok: true, target: "done" });
expect(result.integrityDrift).toBeUndefined();
expect(result.npmResolution.resolvedSpec).toBe("@openclaw/test@1.0.0");
expect(result.npmResolution.resolvedAt).toBeTruthy();
const okResult = expectWrappedOkResult(result, { ok: true, target: "done" });
expect(okResult.integrityDrift).toBeUndefined();
expect(okResult.npmResolution.resolvedSpec).toBe("@openclaw/test@1.0.0");
expect(okResult.npmResolution.resolvedAt).toBeTruthy();
expect(installFromArchive).toHaveBeenCalledWith({ archivePath: "/tmp/openclaw-test.tgz" });
});
it("aborts when integrity drift callback rejects drift", async () => {
vi.mocked(packNpmSpecToArchive).mockResolvedValue({
ok: true,
archivePath: "/tmp/openclaw-test.tgz",
metadata: {
resolvedSpec: "@openclaw/test@1.0.0",
integrity: "sha512-new",
},
it("proceeds when integrity drift callback accepts drift", async () => {
mockPackedSuccess({ integrity: "sha512-new" });
const onIntegrityDrift = vi.fn(async () => true);
const installFromArchive = vi.fn(async () => ({ ok: true as const, id: "plugin-accept" }));
const result = await runInstall({
expectedIntegrity: "sha512-old",
onIntegrityDrift,
installFromArchive,
});
const okResult = expectWrappedOkResult(result, { ok: true, id: "plugin-accept" });
expect(okResult.integrityDrift).toEqual({
expectedIntegrity: "sha512-old",
actualIntegrity: "sha512-new",
});
expect(onIntegrityDrift).toHaveBeenCalledTimes(1);
});
it("aborts when integrity drift callback rejects drift", async () => {
mockPackedSuccess({ integrity: "sha512-new" });
const installFromArchive = vi.fn(async () => ({ ok: true as const }));
const result = await installFromNpmSpecArchive({
tempDirPrefix: "openclaw-test-",
spec: "@openclaw/test@1.0.0",
timeoutMs: 1000,
const result = await runInstall({
expectedIntegrity: "sha512-old",
onIntegrityDrift: async () => false,
installFromArchive,
@@ -95,32 +144,18 @@ describe("installFromNpmSpecArchive", () => {
});
it("warns and proceeds on drift when no callback is configured", async () => {
vi.mocked(packNpmSpecToArchive).mockResolvedValue({
ok: true,
archivePath: "/tmp/openclaw-test.tgz",
metadata: {
resolvedSpec: "@openclaw/test@1.0.0",
integrity: "sha512-new",
},
});
mockPackedSuccess({ integrity: "sha512-new" });
const warn = vi.fn();
const installFromArchive = vi.fn(async () => ({ ok: true as const, id: "plugin-1" }));
const result = await installFromNpmSpecArchive({
tempDirPrefix: "openclaw-test-",
spec: "@openclaw/test@1.0.0",
timeoutMs: 1000,
const result = await runInstall({
expectedIntegrity: "sha512-old",
warn,
installFromArchive,
});
expect(result.ok).toBe(true);
if (!result.ok) {
return;
}
expect(result.installResult).toEqual({ ok: true, id: "plugin-1" });
expect(result.integrityDrift).toEqual({
const okResult = expectWrappedOkResult(result, { ok: true, id: "plugin-1" });
expect(okResult.integrityDrift).toEqual({
expectedIntegrity: "sha512-old",
actualIntegrity: "sha512-new",
});
@@ -130,26 +165,15 @@ describe("installFromNpmSpecArchive", () => {
});
it("returns installer failures to callers for domain-specific handling", async () => {
vi.mocked(packNpmSpecToArchive).mockResolvedValue({
ok: true,
archivePath: "/tmp/openclaw-test.tgz",
metadata: { resolvedSpec: "@openclaw/test@1.0.0", integrity: "sha512-same" },
});
mockPackedSuccess({ integrity: "sha512-same" });
const installFromArchive = vi.fn(async () => ({ ok: false as const, error: "install failed" }));
const result = await installFromNpmSpecArchive({
tempDirPrefix: "openclaw-test-",
spec: "@openclaw/test@1.0.0",
timeoutMs: 1000,
const result = await runInstall({
expectedIntegrity: "sha512-same",
installFromArchive,
});
expect(result.ok).toBe(true);
if (!result.ok) {
return;
}
expect(result.installResult).toEqual({ ok: false, error: "install failed" });
expect(result.integrityDrift).toBeUndefined();
const okResult = expectWrappedOkResult(result, { ok: false, error: "install failed" });
expect(okResult.integrityDrift).toBeUndefined();
});
});