openclaw/scripts/e2e/lib/plugin-update/probe.mjs

326 lines
10 KiB
JavaScript

// Probe script for plugin update E2E scenarios.
import fs from "node:fs";
import http from "node:http";
import os from "node:os";
import path from "node:path";
import {
readPluginInstallRecords,
writePluginInstallIndexForE2E,
} from "../plugin-index-sqlite.mjs";
import { runConsentScenario } from "./consent-scenario.mjs";
const home = os.homedir();
const OUTPUT_TAIL_BYTES = 64 * 1024;
const OUTPUT_TAIL_LINES = 120;
const OUTPUT_SCAN_WINDOW_BYTES = 8 * 1024;
const readJson = (file) => {
try {
return JSON.parse(fs.readFileSync(file, "utf8"));
} catch {
return {};
}
};
const pluginRecordSnapshot = () => {
const records = readPluginInstallRecords({ fallbackRecords: {} });
const record = records["lossless-claw"] ?? records["@example/lossless-claw"];
if (!record) {
throw new Error("missing plugin install record");
}
const { source, spec, resolvedName, resolvedVersion, resolvedSpec, integrity, shasum } = record;
return { source, spec, resolvedName, resolvedVersion, resolvedSpec, integrity, shasum };
};
function openclawPath(...parts) {
return path.join(home, ".openclaw", ...parts);
}
function writeJson(file, value) {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`);
}
function seedInstallState() {
const pluginRoot = openclawPath("extensions", "lossless-claw");
const pluginSource = path.join(pluginRoot, "index.js");
const pluginManifest = path.join(pluginRoot, "openclaw.plugin.json");
writeJson(path.join(pluginRoot, "package.json"), {
name: "@example/lossless-claw",
version: "0.9.0",
type: "module",
openclaw: {
extensions: ["./index.js"],
},
});
fs.writeFileSync(
pluginSource,
'export default { id: "lossless-claw", register() {} };\n',
"utf8",
);
writeJson(pluginManifest, {
id: "lossless-claw",
configSchema: { type: "object" },
});
writeJson(process.env.OPENCLAW_CONFIG_PATH, { plugins: {} });
const index = {
version: 1,
warning: "DO NOT EDIT. This file is generated by OpenClaw plugin registry commands.",
hostContractVersion: "docker-e2e",
compatRegistryVersion: "docker-e2e",
migrationVersion: 1,
policyHash: "docker-e2e",
generatedAtMs: 1777118400000,
installRecords: {
"lossless-claw": {
source: "npm",
spec: "@example/lossless-claw@0.9.0",
installPath: "~/.openclaw/extensions/lossless-claw",
resolvedName: "@example/lossless-claw",
resolvedVersion: "0.9.0",
resolvedSpec: "@example/lossless-claw@0.9.0",
integrity: "sha512-same",
shasum: "same",
},
},
plugins: [
{
pluginId: "lossless-claw",
manifestPath: pluginManifest,
manifestHash: "docker-e2e",
source: pluginSource,
rootDir: pluginRoot,
origin: "global",
enabled: true,
startup: {
sidecar: false,
memory: false,
agentHarnesses: [],
configPaths: [],
},
contributions: {
channels: [],
channelConfigs: [],
providers: [],
modelCatalogProviders: [],
modelSupportPrefixes: [],
modelSupportPatterns: [],
autoEnableProviderIds: [],
commandAliases: [],
contracts: {},
},
compat: [],
installOwner: "lossless-claw",
},
],
diagnostics: [],
};
// Package installation may already initialize current state. Unversioned
// fixtures retain the pre-v13 bootstrap path for frozen older candidates.
writePluginInstallIndexForE2E(index, { storageMode: "existing-schema" });
}
async function waitRegistry() {
for (let attempt = 0; attempt < 50; attempt += 1) {
if (await registryHealthy()) {
return;
}
await new Promise((resolve) => {
setTimeout(resolve, 100);
});
}
throw new Error("Local npm metadata registry failed to start");
}
function registryHealthy() {
return new Promise((resolve) => {
const registry = process.env.NPM_CONFIG_REGISTRY ?? "http://127.0.0.1:4873";
const req = http.get(`${registry.replace(/\/$/u, "")}/@example%2flossless-claw`, (res) => {
resolve(res.statusCode === 200);
res.resume();
});
req.on("error", () => resolve(false));
req.setTimeout(200, () => {
req.destroy();
resolve(false);
});
});
}
function assertSnapshot(beforePath) {
const before = readJson(beforePath);
const after = pluginRecordSnapshot();
if (JSON.stringify(before) !== JSON.stringify(after)) {
throw new Error(
`plugin install record changed unexpectedly: ${JSON.stringify({ before, after })}`,
);
}
}
function appendBufferTail(tail, chunk, maxBytes) {
if (chunk.length >= maxBytes) {
return chunk.subarray(chunk.length - maxBytes);
}
if (tail.length + chunk.length <= maxBytes) {
return Buffer.concat([tail, chunk]);
}
return Buffer.concat([tail, chunk]).subarray(tail.length + chunk.length - maxBytes);
}
async function readOutputEvidence(logPath) {
let outputTail = Buffer.alloc(0);
let scanWindow = "";
let sawDownload = false;
let sawUpToDate = false;
for await (const chunk of fs.createReadStream(logPath)) {
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
const text = buffer.toString("utf8");
const searchable = `${scanWindow}${text}`;
outputTail = appendBufferTail(outputTail, buffer, OUTPUT_TAIL_BYTES);
sawDownload ||= searchable.includes("Downloading @example/lossless-claw");
sawUpToDate ||= searchable.includes("lossless-claw is up to date (0.9.0).");
scanWindow = searchable.slice(-OUTPUT_SCAN_WINDOW_BYTES);
}
return {
outputTail: outputTail
.toString("utf8")
.split(/\r?\n/u)
.slice(-OUTPUT_TAIL_LINES)
.join("\n")
.trimEnd(),
sawDownload,
sawUpToDate,
};
}
async function assertOutput(logPath) {
const evidence = await readOutputEvidence(logPath);
const failure = evidence.sawDownload
? "Unexpected npm download/reinstall path"
: !evidence.sawUpToDate
? "Expected up-to-date output missing"
: "";
if (failure) {
throw new Error(`${failure}\nOutput tail:\n${evidence.outputTail}`);
}
}
function assertCorruptTargetUnavailable(updateJsonPath, pluginId) {
const result = readJson(updateJsonPath);
const plugins = result.postUpdate?.plugins;
const hasRepairNotice = (plugins?.warnings ?? []).some(
(warning) =>
warning?.pluginId === pluginId &&
typeof warning.message === "string" &&
warning.message.includes(pluginId) &&
(warning.guidance ?? []).some(
(command) =>
typeof command === "string" &&
command.startsWith("openclaw ") &&
warning.message.includes(command),
),
);
if (
result.status !== "ok" ||
result.recovery?.serviceRestartSafe === false ||
plugins?.status !== "warning" ||
!hasRepairNotice
) {
throw new Error(
`expected successful core update with a named plugin repair notice: ${JSON.stringify(result)}`,
);
}
}
function assertCorruptUpdate(updateJsonPath, pluginId) {
const payload = readJson(updateJsonPath);
if (payload.status !== "ok") {
throw new Error(`expected core update status ok, got ${JSON.stringify(payload.status)}`);
}
const plugins = payload.postUpdate?.plugins;
if (!plugins) {
throw new Error(`missing postUpdate.plugins in update output: ${JSON.stringify(payload)}`);
}
assertCorruptPluginRestored(plugins, pluginId);
}
function assertCorruptPluginResult(pluginJsonPath, pluginId) {
const plugins = readJson(pluginJsonPath);
assertCorruptPluginRestored(plugins, pluginId);
}
function assertCorruptPluginRestored(plugins, pluginId) {
const evidence = collectPluginEvidence(plugins, pluginId);
const outcome = evidence.outcome;
if (
!["ok", "warning"].includes(plugins.status) ||
(outcome && !["updated", "unchanged"].includes(outcome.status)) ||
evidence.warning ||
evidence.integrityDrift ||
evidence.syncErrors.length > 0
) {
throw new Error(
`expected ${pluginId} restored without unresolved plugin errors or warnings: ${JSON.stringify(evidence)}`,
);
}
}
function collectPluginEvidence(plugins, pluginId) {
const outcomes = plugins.npm?.outcomes ?? [];
const warnings = plugins.warnings ?? [];
const integrityDrifts = plugins.integrityDrifts ?? [];
const syncErrors = (plugins.sync?.errors ?? []).filter((message) =>
String(message).includes(pluginId),
);
return {
outcome: outcomes.findLast((entry) => entry?.pluginId === pluginId),
warning: warnings.find((entry) => entry?.pluginId === pluginId),
integrityDrift: integrityDrifts.find((entry) => entry?.pluginId === pluginId),
syncErrors,
};
}
function assertCorruptPluginPolicyPreserved(configPath, pluginId) {
const config = readJson(configPath);
const allow = config.plugins?.allow;
if (!Array.isArray(allow)) {
throw new Error(`expected plugins.allow to be an array, got ${JSON.stringify(allow)}`);
}
const pluginMembershipCount = allow.filter((entry) => entry === pluginId).length;
if (pluginMembershipCount !== 1) {
throw new Error(
`expected plugins.allow to contain ${pluginId} exactly once, got ${JSON.stringify(allow)}`,
);
}
const codexEnabled = config.plugins?.entries?.codex?.enabled;
if (codexEnabled !== false) {
throw new Error(
`expected the corrupt plugin fixture's explicit Codex opt-out to survive, got ${JSON.stringify(codexEnabled)}`,
);
}
console.log(JSON.stringify({ allow, codexEnabled: false }));
}
const [command, arg, arg2] = process.argv.slice(2);
const commands = {
consent: () =>
runConsentScenario(arg, arg2, {
coreUpdateConsent: process.env.OPENCLAW_E2E_CORE_UPDATE_CONSENT !== "0",
}),
seed: seedInstallState,
"wait-registry": waitRegistry,
snapshot: () => process.stdout.write(JSON.stringify(pluginRecordSnapshot(), null, 2)),
"assert-snapshot": () => assertSnapshot(arg),
"assert-output": () => assertOutput(arg),
"assert-corrupt-unavailable": () => assertCorruptTargetUnavailable(arg, arg2),
"assert-corrupt-update": () => assertCorruptUpdate(arg, arg2),
"assert-corrupt-plugin-result": () => assertCorruptPluginResult(arg, arg2),
"assert-corrupt-policy-preserved": () => assertCorruptPluginPolicyPreserved(arg, arg2),
};
const run = commands[command];
await (
run ??
(() => {
throw new Error(`Unknown plugin update probe command: ${command || "(missing)"}`);
})
)();