test: assert e2e auth profiles use sqlite

This commit is contained in:
Peter Steinberger
2026-05-10 00:09:43 +01:00
parent 0698894e8b
commit 46dff29e02
2 changed files with 50 additions and 15 deletions

View File

@@ -1,5 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import { DatabaseSync } from "node:sqlite";
import { readInstalledPluginRecords } from "../installed-plugin-index.mjs";
const readJson = (file) => JSON.parse(fs.readFileSync(file, "utf8"));
@@ -28,12 +30,6 @@ function assertPathInside(parentPath, childPath, label) {
}
}
function installRecords() {
const indexPath = path.join(stateDir(), "plugins", "installs.json");
const index = fs.existsSync(indexPath) ? readJson(indexPath) : {};
return index.installRecords || index.records || cfg.plugins?.installs || {};
}
function findPackageJson(packageName, roots) {
const packagePath = packageName.startsWith("@")
? path.join(...packageName.split("/"), "package.json")
@@ -42,9 +38,27 @@ function findPackageJson(packageName, roots) {
return candidates.find((candidate) => fs.existsSync(candidate));
}
function stateDatabasePath() {
return path.join(stateDir(), "state", "openclaw.sqlite");
}
function readOpenClawStateKvJson(scope, key) {
const dbPath = stateDatabasePath();
if (!fs.existsSync(dbPath)) {
throw new Error(`missing OpenClaw state database: ${dbPath}`);
}
const db = new DatabaseSync(dbPath, { readOnly: true });
try {
const row = db.prepare("SELECT value_json FROM kv WHERE scope = ? AND key = ?").get(scope, key);
return typeof row?.value_json === "string" ? JSON.parse(row.value_json) : undefined;
} finally {
db.close();
}
}
const cfg = readJson(configPath());
const inspect = readJson("/tmp/openclaw-codex-inspect.json");
const records = installRecords();
const records = readInstalledPluginRecords();
const codexRecord = records.codex || inspect.install;
if (!codexRecord) {
throw new Error(`missing codex install record: ${JSON.stringify(records)}`);
@@ -107,11 +121,16 @@ if (providerRuntime && providerRuntime !== "codex") {
throw new Error(`unexpected OpenAI provider runtime: ${providerRuntime}`);
}
const authPath = path.join(stateDir(), "agents", "main", "agent", "auth-profiles.json");
const authRaw = fs.readFileSync(authPath, "utf8");
if (!authRaw.includes("OPENAI_API_KEY")) {
const authAgentDir = path.join(stateDir(), "agents", "main", "agent");
const authStore = readOpenClawStateKvJson("auth-profiles", authAgentDir);
const authRaw = JSON.stringify(authStore ?? {});
if (!authStore || !authRaw.includes("OPENAI_API_KEY")) {
throw new Error("auth profile did not persist OPENAI_API_KEY env ref");
}
if (authRaw.includes("sk-openclaw-codex-on-demand-e2e")) {
throw new Error("auth profile persisted the raw OpenAI test key");
}
const authPath = path.join(authAgentDir, "auth-profiles.json");
if (fs.existsSync(authPath)) {
throw new Error(`auth profile should be SQLite-backed, found legacy file: ${authPath}`);
}

View File

@@ -1,9 +1,24 @@
import fs from "node:fs";
import path from "node:path";
import { DatabaseSync } from "node:sqlite";
const command = process.argv[2];
const readJson = (file) => JSON.parse(fs.readFileSync(file, "utf8"));
function readOpenClawStateKvJson(stateDir, scope, key) {
const dbPath = path.join(stateDir, "state", "openclaw.sqlite");
if (!fs.existsSync(dbPath)) {
throw new Error(`missing OpenClaw state database: ${dbPath}`);
}
const db = new DatabaseSync(dbPath, { readOnly: true });
try {
const row = db.prepare("SELECT value_json FROM kv WHERE scope = ? AND key = ?").get(scope, key);
return typeof row?.value_json === "string" ? JSON.parse(row.value_json) : undefined;
} finally {
db.close();
}
}
function assertOnboardState() {
const home = process.argv[3];
const stateDir = path.join(home, ".openclaw");
@@ -17,16 +32,17 @@ function assertOnboardState() {
if (!fs.existsSync(agentDir)) {
throw new Error("onboard did not create main agent dir");
}
if (!fs.existsSync(authPath)) {
throw new Error("onboard did not create auth-profiles.json");
}
const authRaw = fs.readFileSync(authPath, "utf8");
if (!authRaw.includes("OPENAI_API_KEY")) {
const authStore = readOpenClawStateKvJson(stateDir, "auth-profiles", agentDir);
const authRaw = JSON.stringify(authStore ?? {});
if (!authStore || !authRaw.includes("OPENAI_API_KEY")) {
throw new Error("auth profile did not persist OPENAI_API_KEY env ref");
}
if (authRaw.includes("sk-openclaw-npm-onboard-e2e")) {
throw new Error("auth profile persisted the raw OpenAI test key");
}
if (fs.existsSync(authPath)) {
throw new Error(`auth profile should be SQLite-backed, found legacy file: ${authPath}`);
}
}
function configureMockModel() {