mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-13 18:21:27 +00:00
refactor: dedupe repeated test helpers
This commit is contained in:
@@ -45,6 +45,14 @@ function createEnabledPluginConfig(pluginId: string): OpenClawConfig {
|
||||
} as OpenClawConfig;
|
||||
}
|
||||
|
||||
function createEmptyPluginConfig(): OpenClawConfig {
|
||||
return {
|
||||
plugins: {
|
||||
entries: {},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
}
|
||||
|
||||
function createClawHubInstalledConfig(params: {
|
||||
pluginId: string;
|
||||
install: Record<string, unknown>;
|
||||
@@ -86,6 +94,134 @@ function createClawHubInstallResult(params: {
|
||||
};
|
||||
}
|
||||
|
||||
function createNpmPluginInstallResult(
|
||||
pluginId = "demo",
|
||||
): Awaited<ReturnType<typeof installPluginFromNpmSpec>> {
|
||||
return {
|
||||
ok: true,
|
||||
pluginId,
|
||||
targetDir: cliInstallPath(pluginId),
|
||||
version: "1.2.3",
|
||||
npmResolution: {
|
||||
packageName: pluginId,
|
||||
resolvedVersion: "1.2.3",
|
||||
tarballUrl: `https://registry.npmjs.org/${pluginId}/-/${pluginId}-1.2.3.tgz`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function mockClawHubPackageNotFound(packageName: string) {
|
||||
installPluginFromClawHub.mockResolvedValue({
|
||||
ok: false,
|
||||
error: `ClawHub /api/v1/packages/${packageName} failed (404): Package not found`,
|
||||
code: "package_not_found",
|
||||
});
|
||||
}
|
||||
|
||||
function primeNpmPluginFallback(pluginId = "demo") {
|
||||
const cfg = createEmptyPluginConfig();
|
||||
const enabledCfg = createEnabledPluginConfig(pluginId);
|
||||
|
||||
loadConfig.mockReturnValue(cfg);
|
||||
mockClawHubPackageNotFound(pluginId);
|
||||
installPluginFromNpmSpec.mockResolvedValue(createNpmPluginInstallResult(pluginId));
|
||||
enablePluginInConfig.mockReturnValue({ config: enabledCfg });
|
||||
recordPluginInstall.mockReturnValue(enabledCfg);
|
||||
applyExclusiveSlotSelection.mockReturnValue({
|
||||
config: enabledCfg,
|
||||
warnings: [],
|
||||
});
|
||||
|
||||
return { cfg, enabledCfg };
|
||||
}
|
||||
|
||||
function createPathHookPackInstalledConfig(tmpRoot: string): OpenClawConfig {
|
||||
return {
|
||||
hooks: {
|
||||
internal: {
|
||||
installs: {
|
||||
"demo-hooks": {
|
||||
source: "path",
|
||||
sourcePath: tmpRoot,
|
||||
installPath: tmpRoot,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
}
|
||||
|
||||
function createNpmHookPackInstalledConfig(): OpenClawConfig {
|
||||
return {
|
||||
hooks: {
|
||||
internal: {
|
||||
installs: {
|
||||
"demo-hooks": {
|
||||
source: "npm",
|
||||
spec: "@acme/demo-hooks@1.2.3",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
}
|
||||
|
||||
function createHookPackInstallResult(targetDir: string): {
|
||||
ok: true;
|
||||
hookPackId: string;
|
||||
hooks: string[];
|
||||
targetDir: string;
|
||||
version: string;
|
||||
} {
|
||||
return {
|
||||
ok: true,
|
||||
hookPackId: "demo-hooks",
|
||||
hooks: ["command-audit"],
|
||||
targetDir,
|
||||
version: "1.2.3",
|
||||
};
|
||||
}
|
||||
|
||||
function primeHookPackNpmFallback() {
|
||||
const cfg = {} as OpenClawConfig;
|
||||
const installedCfg = createNpmHookPackInstalledConfig();
|
||||
|
||||
loadConfig.mockReturnValue(cfg);
|
||||
mockClawHubPackageNotFound("@acme/demo-hooks");
|
||||
installPluginFromNpmSpec.mockResolvedValue({
|
||||
ok: false,
|
||||
error: "package.json missing openclaw.plugin.json",
|
||||
});
|
||||
installHooksFromNpmSpec.mockResolvedValue({
|
||||
...createHookPackInstallResult("/tmp/hooks/demo-hooks"),
|
||||
npmResolution: {
|
||||
name: "@acme/demo-hooks",
|
||||
spec: "@acme/demo-hooks@1.2.3",
|
||||
integrity: "sha256-demo",
|
||||
},
|
||||
});
|
||||
recordHookInstall.mockReturnValue(installedCfg);
|
||||
|
||||
return { cfg, installedCfg };
|
||||
}
|
||||
|
||||
function primeHookPackPathFallback(params: {
|
||||
tmpRoot: string;
|
||||
pluginInstallError: string;
|
||||
}): OpenClawConfig {
|
||||
const installedCfg = createPathHookPackInstalledConfig(params.tmpRoot);
|
||||
|
||||
loadConfig.mockReturnValue({} as OpenClawConfig);
|
||||
installPluginFromPath.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
error: params.pluginInstallError,
|
||||
});
|
||||
installHooksFromPath.mockResolvedValueOnce(createHookPackInstallResult(params.tmpRoot));
|
||||
recordHookInstall.mockReturnValue(installedCfg);
|
||||
|
||||
return installedCfg;
|
||||
}
|
||||
|
||||
describe("plugins cli install", () => {
|
||||
beforeEach(() => {
|
||||
resetPluginsCliTestState();
|
||||
@@ -380,44 +516,7 @@ describe("plugins cli install", () => {
|
||||
});
|
||||
|
||||
it("falls back to npm when ClawHub does not have the package", async () => {
|
||||
const cfg = {
|
||||
plugins: {
|
||||
entries: {},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
const enabledCfg = {
|
||||
plugins: {
|
||||
entries: {
|
||||
demo: {
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
loadConfig.mockReturnValue(cfg);
|
||||
installPluginFromClawHub.mockResolvedValue({
|
||||
ok: false,
|
||||
error: "ClawHub /api/v1/packages/demo failed (404): Package not found",
|
||||
code: "package_not_found",
|
||||
});
|
||||
installPluginFromNpmSpec.mockResolvedValue({
|
||||
ok: true,
|
||||
pluginId: "demo",
|
||||
targetDir: cliInstallPath("demo"),
|
||||
version: "1.2.3",
|
||||
npmResolution: {
|
||||
packageName: "demo",
|
||||
resolvedVersion: "1.2.3",
|
||||
tarballUrl: "https://registry.npmjs.org/demo/-/demo-1.2.3.tgz",
|
||||
},
|
||||
});
|
||||
enablePluginInConfig.mockReturnValue({ config: enabledCfg });
|
||||
recordPluginInstall.mockReturnValue(enabledCfg);
|
||||
applyExclusiveSlotSelection.mockReturnValue({
|
||||
config: enabledCfg,
|
||||
warnings: [],
|
||||
});
|
||||
primeNpmPluginFallback();
|
||||
|
||||
await runPluginsCommand(["plugins", "install", "demo"]);
|
||||
|
||||
@@ -455,36 +554,7 @@ describe("plugins cli install", () => {
|
||||
});
|
||||
|
||||
it("passes dangerous force unsafe install to npm installs", async () => {
|
||||
const cfg = {
|
||||
plugins: {
|
||||
entries: {},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
const enabledCfg = createEnabledPluginConfig("demo");
|
||||
|
||||
loadConfig.mockReturnValue(cfg);
|
||||
installPluginFromClawHub.mockResolvedValue({
|
||||
ok: false,
|
||||
error: "ClawHub /api/v1/packages/demo failed (404): Package not found",
|
||||
code: "package_not_found",
|
||||
});
|
||||
installPluginFromNpmSpec.mockResolvedValue({
|
||||
ok: true,
|
||||
pluginId: "demo",
|
||||
targetDir: cliInstallPath("demo"),
|
||||
version: "1.2.3",
|
||||
npmResolution: {
|
||||
packageName: "demo",
|
||||
resolvedVersion: "1.2.3",
|
||||
tarballUrl: "https://registry.npmjs.org/demo/-/demo-1.2.3.tgz",
|
||||
},
|
||||
});
|
||||
enablePluginInConfig.mockReturnValue({ config: enabledCfg });
|
||||
recordPluginInstall.mockReturnValue(enabledCfg);
|
||||
applyExclusiveSlotSelection.mockReturnValue({
|
||||
config: enabledCfg,
|
||||
warnings: [],
|
||||
});
|
||||
primeNpmPluginFallback();
|
||||
|
||||
await runPluginsCommand(["plugins", "install", "demo", "--dangerously-force-unsafe-install"]);
|
||||
|
||||
@@ -542,35 +612,11 @@ describe("plugins cli install", () => {
|
||||
});
|
||||
|
||||
it("passes dangerous force unsafe install to linked hook-pack probe fallback", async () => {
|
||||
const cfg = {} as OpenClawConfig;
|
||||
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-hook-link-"));
|
||||
const installedCfg = {
|
||||
hooks: {
|
||||
internal: {
|
||||
installs: {
|
||||
"demo-hooks": {
|
||||
source: "path",
|
||||
sourcePath: tmpRoot,
|
||||
installPath: tmpRoot,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
loadConfig.mockReturnValue(cfg);
|
||||
installPluginFromPath.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
error: "plugin install probe failed",
|
||||
primeHookPackPathFallback({
|
||||
tmpRoot,
|
||||
pluginInstallError: "plugin install probe failed",
|
||||
});
|
||||
installHooksFromPath.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
hookPackId: "demo-hooks",
|
||||
hooks: ["command-audit"],
|
||||
targetDir: tmpRoot,
|
||||
version: "1.2.3",
|
||||
});
|
||||
recordHookInstall.mockReturnValue(installedCfg);
|
||||
|
||||
try {
|
||||
await runPluginsCommand([
|
||||
@@ -594,35 +640,11 @@ describe("plugins cli install", () => {
|
||||
});
|
||||
|
||||
it("passes dangerous force unsafe install to local hook-pack fallback installs", async () => {
|
||||
const cfg = {} as OpenClawConfig;
|
||||
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-hook-install-"));
|
||||
const installedCfg = {
|
||||
hooks: {
|
||||
internal: {
|
||||
installs: {
|
||||
"demo-hooks": {
|
||||
source: "path",
|
||||
sourcePath: tmpRoot,
|
||||
installPath: tmpRoot,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
loadConfig.mockReturnValue(cfg);
|
||||
installPluginFromPath.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
error: "plugin install failed",
|
||||
primeHookPackPathFallback({
|
||||
tmpRoot,
|
||||
pluginInstallError: "plugin install failed",
|
||||
});
|
||||
installHooksFromPath.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
hookPackId: "demo-hooks",
|
||||
hooks: ["command-audit"],
|
||||
targetDir: tmpRoot,
|
||||
version: "1.2.3",
|
||||
});
|
||||
recordHookInstall.mockReturnValue(installedCfg);
|
||||
|
||||
try {
|
||||
await runPluginsCommand([
|
||||
@@ -644,36 +666,7 @@ describe("plugins cli install", () => {
|
||||
);
|
||||
});
|
||||
it("passes force through as overwrite mode for npm installs", async () => {
|
||||
const cfg = {
|
||||
plugins: {
|
||||
entries: {},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
const enabledCfg = createEnabledPluginConfig("demo");
|
||||
|
||||
loadConfig.mockReturnValue(cfg);
|
||||
installPluginFromClawHub.mockResolvedValue({
|
||||
ok: false,
|
||||
error: "ClawHub /api/v1/packages/demo failed (404): Package not found",
|
||||
code: "package_not_found",
|
||||
});
|
||||
installPluginFromNpmSpec.mockResolvedValue({
|
||||
ok: true,
|
||||
pluginId: "demo",
|
||||
targetDir: cliInstallPath("demo"),
|
||||
version: "1.2.3",
|
||||
npmResolution: {
|
||||
packageName: "demo",
|
||||
resolvedVersion: "1.2.3",
|
||||
tarballUrl: "https://registry.npmjs.org/demo/-/demo-1.2.3.tgz",
|
||||
},
|
||||
});
|
||||
enablePluginInConfig.mockReturnValue({ config: enabledCfg });
|
||||
recordPluginInstall.mockReturnValue(enabledCfg);
|
||||
applyExclusiveSlotSelection.mockReturnValue({
|
||||
config: enabledCfg,
|
||||
warnings: [],
|
||||
});
|
||||
primeNpmPluginFallback();
|
||||
|
||||
await runPluginsCommand(["plugins", "install", "demo", "--force"]);
|
||||
|
||||
@@ -699,43 +692,7 @@ describe("plugins cli install", () => {
|
||||
});
|
||||
|
||||
it("falls back to installing hook packs from npm specs", async () => {
|
||||
const cfg = {} as OpenClawConfig;
|
||||
const installedCfg = {
|
||||
hooks: {
|
||||
internal: {
|
||||
installs: {
|
||||
"demo-hooks": {
|
||||
source: "npm",
|
||||
spec: "@acme/demo-hooks@1.2.3",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
loadConfig.mockReturnValue(cfg);
|
||||
installPluginFromClawHub.mockResolvedValue({
|
||||
ok: false,
|
||||
error: "ClawHub /api/v1/packages/@acme/demo-hooks failed (404): Package not found",
|
||||
code: "package_not_found",
|
||||
});
|
||||
installPluginFromNpmSpec.mockResolvedValue({
|
||||
ok: false,
|
||||
error: "package.json missing openclaw.plugin.json",
|
||||
});
|
||||
installHooksFromNpmSpec.mockResolvedValue({
|
||||
ok: true,
|
||||
hookPackId: "demo-hooks",
|
||||
hooks: ["command-audit"],
|
||||
targetDir: "/tmp/hooks/demo-hooks",
|
||||
version: "1.2.3",
|
||||
npmResolution: {
|
||||
name: "@acme/demo-hooks",
|
||||
spec: "@acme/demo-hooks@1.2.3",
|
||||
integrity: "sha256-demo",
|
||||
},
|
||||
});
|
||||
recordHookInstall.mockReturnValue(installedCfg);
|
||||
const { installedCfg } = primeHookPackNpmFallback();
|
||||
|
||||
await runPluginsCommand(["plugins", "install", "@acme/demo-hooks"]);
|
||||
|
||||
@@ -756,43 +713,7 @@ describe("plugins cli install", () => {
|
||||
});
|
||||
|
||||
it("passes force through as overwrite mode for hook-pack npm fallback installs", async () => {
|
||||
const cfg = {} as OpenClawConfig;
|
||||
const installedCfg = {
|
||||
hooks: {
|
||||
internal: {
|
||||
installs: {
|
||||
"demo-hooks": {
|
||||
source: "npm",
|
||||
spec: "@acme/demo-hooks@1.2.3",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
|
||||
loadConfig.mockReturnValue(cfg);
|
||||
installPluginFromClawHub.mockResolvedValue({
|
||||
ok: false,
|
||||
error: "ClawHub /api/v1/packages/@acme/demo-hooks failed (404): Package not found",
|
||||
code: "package_not_found",
|
||||
});
|
||||
installPluginFromNpmSpec.mockResolvedValue({
|
||||
ok: false,
|
||||
error: "package.json missing openclaw.plugin.json",
|
||||
});
|
||||
installHooksFromNpmSpec.mockResolvedValue({
|
||||
ok: true,
|
||||
hookPackId: "demo-hooks",
|
||||
hooks: ["command-audit"],
|
||||
targetDir: "/tmp/hooks/demo-hooks",
|
||||
version: "1.2.3",
|
||||
npmResolution: {
|
||||
name: "@acme/demo-hooks",
|
||||
spec: "@acme/demo-hooks@1.2.3",
|
||||
integrity: "sha256-demo",
|
||||
},
|
||||
});
|
||||
recordHookInstall.mockReturnValue(installedCfg);
|
||||
primeHookPackNpmFallback();
|
||||
|
||||
await runPluginsCommand(["plugins", "install", "@acme/demo-hooks", "--force"]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user