Files
openclaw/scripts/e2e/lib/codex-on-demand/assertions.mjs
Peter Steinberger 877fad25ed feat(codex): upgrade app-server integration to 0.149.0
Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-08-21 17:27:41 -07:00

127 lines
4.8 KiB
JavaScript

// Assertions for Codex on-demand plugin E2E scenarios.
import fs from "node:fs";
import path from "node:path";
import { DatabaseSync } from "node:sqlite";
import { assertOpenAiEnvAuthProfileStore } from "../auth-profile-store-assertions.mjs";
import {
assertPathInside,
configPath,
findPackageJson,
managedNpmRoot,
npmProjectRootForInstalledPackage,
readInstallRecords,
readJson,
stateDir,
} from "../codex-install-utils.mjs";
import { assertCodexReleasePackageContract } from "../codex-release-package-assertions.mjs";
const cfg = readJson(configPath());
const onboard = readJson("/tmp/openclaw-onboard.json");
const inspect = readJson("/tmp/openclaw-codex-inspect.json");
const records = readInstallRecords();
const codexRecord = records.codex;
if (onboard.ok !== true || onboard.mode !== "local" || onboard.authChoice !== "openai-api-key") {
throw new Error(`unexpected onboarding terminal result: ${JSON.stringify(onboard)}`);
}
if (cfg.plugins?.installs !== undefined) {
throw new Error("codex install record remained in config instead of the canonical SQLite index");
}
if (!codexRecord) {
throw new Error(`missing codex install record: ${JSON.stringify(records)}`);
}
if (codexRecord.source !== "npm") {
throw new Error(`expected npm codex install record, got ${codexRecord.source}`);
}
if (!codexRecord.spec?.includes("@openclaw/codex")) {
throw new Error(`expected @openclaw/codex install spec, got ${codexRecord.spec}`);
}
const npmRoot = managedNpmRoot();
const installPath = (codexRecord.installPath || "").replace(/^~(?=$|\/)/u, process.env.HOME);
if (!installPath) {
throw new Error(`missing codex installPath: ${JSON.stringify(codexRecord)}`);
}
assertPathInside(npmRoot, installPath, "codex install path");
const codexPackageJson = path.join(installPath, "package.json");
if (!fs.existsSync(codexPackageJson)) {
throw new Error(`missing npm-installed @openclaw/codex package: ${codexPackageJson}`);
}
const codexPackage = readJson(codexPackageJson);
if (codexPackage.name !== "@openclaw/codex") {
throw new Error(`unexpected codex package name: ${codexPackage.name}`);
}
const npmProjectRoot = npmProjectRootForInstalledPackage(installPath, "@openclaw/codex");
const openAiCodexPackageJson = findPackageJson("@openai/codex", [
installPath,
npmProjectRoot,
npmRoot,
]);
if (!openAiCodexPackageJson) {
throw new Error("missing @openai/codex dependency under managed npm root");
}
assertPathInside(npmRoot, openAiCodexPackageJson, "@openai/codex dependency");
assertCodexReleasePackageContract({
pluginPackageJson: codexPackageJson,
codexPackageJson: openAiCodexPackageJson,
packageRoots: [installPath, npmProjectRoot, npmRoot],
managedRoot: npmRoot,
});
const list = readJson("/tmp/openclaw-plugins-list.json");
const plugin = (list.plugins || []).find((entry) => entry.id === "codex");
if (!plugin || plugin.enabled !== true || plugin.status !== "loaded") {
throw new Error(`codex plugin was not enabled+loaded: ${JSON.stringify(plugin)}`);
}
if (inspect.plugin?.id !== "codex" || inspect.plugin?.status !== "loaded") {
throw new Error(`unexpected codex inspect state: ${JSON.stringify(inspect.plugin)}`);
}
const hasHarness =
(Array.isArray(inspect.plugin?.agentHarnessIds) &&
inspect.plugin.agentHarnessIds.includes("codex")) ||
(Array.isArray(inspect.capabilities) &&
inspect.capabilities.some(
(entry) => entry?.kind === "agent-harness" && entry.ids?.includes("codex"),
));
if (!hasHarness) {
throw new Error(`codex harness was not registered: ${JSON.stringify(inspect.plugin)}`);
}
const primaryModel = cfg.agents?.defaults?.model?.primary;
if (primaryModel !== "openai/gpt-5.6-sol") {
throw new Error(`expected OpenAI onboarding model openai/gpt-5.6-sol, got ${primaryModel}`);
}
const providerRuntime = cfg.models?.providers?.openai?.agentRuntime?.id;
if (providerRuntime && providerRuntime !== "codex") {
throw new Error(`unexpected OpenAI provider runtime: ${providerRuntime}`);
}
function readAuthProfileStoreText(agentDir) {
const dbPath = path.join(agentDir, "openclaw-agent.sqlite");
if (!fs.existsSync(dbPath)) {
throw new Error("auth profile SQLite store was not persisted");
}
let db;
try {
db = new DatabaseSync(dbPath, { readOnly: true });
const row = db
.prepare("SELECT store_json FROM auth_profile_store WHERE store_key = ?")
.get("primary");
return typeof row?.store_json === "string" ? row.store_json : "";
} finally {
db?.close();
}
}
const authRaw = readAuthProfileStoreText(path.join(stateDir(), "agents", "main", "agent"));
if (!authRaw) {
throw new Error("auth profile SQLite store row was not persisted");
}
assertOpenAiEnvAuthProfileStore(authRaw, {
envRefMessage: "auth profile did not persist OPENAI_API_KEY env ref",
rawKeyMessage: "auth profile persisted the raw OpenAI test key",
rawKeyNeedle: "sk-openclaw-codex-on-demand-e2e",
});