From edc9be1b7f97bc0fc5731aa9ecfcbfac79d3b0cb Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 3 Jun 2026 16:34:32 -0700 Subject: [PATCH] test(release): read auth refs from sqlite store --- .../e2e/lib/release-scenarios/assertions.mjs | 35 +++++++++- .../release-scenarios-assertions.test.ts | 64 ++++++++++++++++++- 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/scripts/e2e/lib/release-scenarios/assertions.mjs b/scripts/e2e/lib/release-scenarios/assertions.mjs index be98e3fecbe..9cca7081798 100644 --- a/scripts/e2e/lib/release-scenarios/assertions.mjs +++ b/scripts/e2e/lib/release-scenarios/assertions.mjs @@ -1,5 +1,6 @@ import fs from "node:fs"; import path from "node:path"; +import { DatabaseSync } from "node:sqlite"; import { assertAgentReplyContainsMarker, assertOpenAiRequestLogUsed, @@ -81,9 +82,41 @@ function authProfilesPath() { ); } +function authProfilesDatabasePath() { + return path.join( + process.env.HOME ?? "", + ".openclaw", + "agents", + "main", + "agent", + "openclaw-agent.sqlite", + ); +} + +function readAuthProfileStoreSqliteText() { + const dbPath = authProfilesDatabasePath(); + if (!fs.existsSync(dbPath)) { + return ""; + } + 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 : ""; + } catch { + return ""; + } finally { + db?.close(); + } +} + function readStateText() { const paths = [configPath(), authProfilesPath()].filter((file) => fs.existsSync(file)); - return paths.map((file) => fs.readFileSync(file, "utf8")).join("\n"); + return [...paths.map((file) => fs.readFileSync(file, "utf8")), readAuthProfileStoreSqliteText()] + .filter(Boolean) + .join("\n"); } function configureMockOpenAi() { diff --git a/test/scripts/release-scenarios-assertions.test.ts b/test/scripts/release-scenarios-assertions.test.ts index fd98396df88..cbf2f32ab2b 100644 --- a/test/scripts/release-scenarios-assertions.test.ts +++ b/test/scripts/release-scenarios-assertions.test.ts @@ -2,6 +2,7 @@ import { spawnSync } from "node:child_process"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; +import { DatabaseSync } from "node:sqlite"; import { describe, expect, it } from "vitest"; const ASSERTIONS_SCRIPT = "scripts/e2e/lib/release-scenarios/assertions.mjs"; @@ -19,16 +20,39 @@ function writeJson(filePath: string, value: unknown) { writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8"); } -function runAssertion(args: string[]) { +function runAssertion(args: string[], env?: NodeJS.ProcessEnv) { return spawnSync(process.execPath, [ASSERTIONS_SCRIPT, ...args], { encoding: "utf8", env: { ...process.env, + ...env, NODE_OPTIONS: nodeOptionsWithoutExperimentalWarnings(), }, }); } +function writeAuthProfileStoreSqlite(agentDir: string, store: unknown) { + mkdirSync(agentDir, { recursive: true }); + const db = new DatabaseSync(path.join(agentDir, "openclaw-agent.sqlite")); + try { + db.exec(` + CREATE TABLE IF NOT EXISTS auth_profile_store ( + store_key TEXT NOT NULL PRIMARY KEY, + store_json TEXT NOT NULL, + updated_at INTEGER NOT NULL + ); + `); + db.prepare( + ` + INSERT INTO auth_profile_store (store_key, store_json, updated_at) + VALUES (?, ?, ?) + `, + ).run("primary", JSON.stringify(store), Date.now()); + } finally { + db.close(); + } +} + describe("release scenario assertions", () => { it("scans large files when checking release scenario output text", () => { const root = mkdtempSync(path.join(tmpdir(), "openclaw-release-scenarios-")); @@ -134,6 +158,44 @@ describe("release scenario assertions", () => { } }); + it("accepts OpenAI env refs from the SQLite auth profile store", () => { + const root = mkdtempSync(path.join(tmpdir(), "openclaw-release-scenarios-")); + const home = path.join(root, "home"); + const stateDir = path.join(home, ".openclaw"); + const agentDir = path.join(stateDir, "agents", "main", "agent"); + const configPath = path.join(stateDir, "openclaw.json"); + + try { + writeJson(configPath, { + auth: { + profiles: { + "openai:api-key": { provider: "openai", mode: "api_key" }, + }, + }, + }); + writeAuthProfileStoreSqlite(agentDir, { + version: 1, + profiles: { + "openai:api-key": { + type: "api_key", + provider: "openai", + keyRef: { source: "env", provider: "default", id: "OPENAI_API_KEY" }, + }, + }, + }); + + const result = runAssertion(["assert-openai-env-ref", "sk-test-raw-key"], { + HOME: home, + OPENCLAW_CONFIG_PATH: configPath, + }); + + expect(result.status).toBe(0); + expect(result.stderr).toBe(""); + } finally { + rmSync(root, { force: true, recursive: true }); + } + }); + it("passes when the installed package version matches the candidate version", () => { const root = mkdtempSync(path.join(tmpdir(), "openclaw-release-scenarios-")); const packageRoot = path.join(root, "openclaw");