mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-02 15:50:22 +00:00
refactor(shared): derive requirements from metadata
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildConfigChecks,
|
||||
evaluateRequirementsFromMetadata,
|
||||
resolveMissingAnyBins,
|
||||
resolveMissingBins,
|
||||
resolveMissingEnv,
|
||||
@@ -60,4 +61,24 @@ describe("requirements helpers", () => {
|
||||
}),
|
||||
).toEqual([{ path: "a.b", value: 1, satisfied: true }]);
|
||||
});
|
||||
|
||||
it("evaluateRequirementsFromMetadata derives required+missing", () => {
|
||||
const res = evaluateRequirementsFromMetadata({
|
||||
always: false,
|
||||
metadata: {
|
||||
requires: { bins: ["a"], anyBins: ["b"], env: ["E"], config: ["cfg.value"] },
|
||||
os: ["darwin"],
|
||||
},
|
||||
hasLocalBin: (bin) => bin === "a",
|
||||
localPlatform: "linux",
|
||||
isEnvSatisfied: (name) => name === "E",
|
||||
resolveConfigValue: () => "x",
|
||||
isConfigSatisfied: () => false,
|
||||
});
|
||||
|
||||
expect(res.required.bins).toEqual(["a"]);
|
||||
expect(res.missing.config).toEqual(["cfg.value"]);
|
||||
expect(res.missing.os).toEqual(["darwin"]);
|
||||
expect(res.eligible).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,6 +12,11 @@ export type RequirementConfigCheck = {
|
||||
satisfied: boolean;
|
||||
};
|
||||
|
||||
export type RequirementsMetadata = {
|
||||
requires?: Partial<Pick<Requirements, "bins" | "anyBins" | "env" | "config">>;
|
||||
os?: string[];
|
||||
};
|
||||
|
||||
export function resolveMissingBins(params: {
|
||||
required: string[];
|
||||
hasLocalBin: (bin: string) => boolean;
|
||||
@@ -147,3 +152,43 @@ export function evaluateRequirements(params: {
|
||||
|
||||
return { missing, eligible, configChecks };
|
||||
}
|
||||
|
||||
export function evaluateRequirementsFromMetadata(params: {
|
||||
always: boolean;
|
||||
metadata?: RequirementsMetadata;
|
||||
hasLocalBin: (bin: string) => boolean;
|
||||
hasRemoteBin?: (bin: string) => boolean;
|
||||
hasRemoteAnyBin?: (bins: string[]) => boolean;
|
||||
localPlatform: string;
|
||||
remotePlatforms?: string[];
|
||||
isEnvSatisfied: (envName: string) => boolean;
|
||||
resolveConfigValue: (pathStr: string) => unknown;
|
||||
isConfigSatisfied: (pathStr: string) => boolean;
|
||||
}): {
|
||||
required: Requirements;
|
||||
missing: Requirements;
|
||||
eligible: boolean;
|
||||
configChecks: RequirementConfigCheck[];
|
||||
} {
|
||||
const required: Requirements = {
|
||||
bins: params.metadata?.requires?.bins ?? [],
|
||||
anyBins: params.metadata?.requires?.anyBins ?? [],
|
||||
env: params.metadata?.requires?.env ?? [],
|
||||
config: params.metadata?.requires?.config ?? [],
|
||||
os: params.metadata?.os ?? [],
|
||||
};
|
||||
|
||||
const result = evaluateRequirements({
|
||||
always: params.always,
|
||||
required,
|
||||
hasLocalBin: params.hasLocalBin,
|
||||
hasRemoteBin: params.hasRemoteBin,
|
||||
hasRemoteAnyBin: params.hasRemoteAnyBin,
|
||||
localPlatform: params.localPlatform,
|
||||
remotePlatforms: params.remotePlatforms,
|
||||
isEnvSatisfied: params.isEnvSatisfied,
|
||||
resolveConfigValue: params.resolveConfigValue,
|
||||
isConfigSatisfied: params.isConfigSatisfied,
|
||||
});
|
||||
return { required, ...result };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user