Skip to content

Commit 8b18d22

Browse files
steipeteampagentvincentkoc
authored
test(e2e): share canonical auth store reader (#126960)
* test(e2e): read shared auth store after onboarding * test(e2e): harden shared auth proof --------- Co-authored-by: Amp <[email protected]> Co-authored-by: Vincent Koc <[email protected]>
1 parent bbb6443 commit 8b18d22

9 files changed

Lines changed: 344 additions & 108 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export type AuthProfileStoreAssertionOptions = {
2+
missingMessage?: string;
3+
envRefMessage?: string;
4+
rawKeyMessage?: string;
5+
rawKeyNeedle?: string;
6+
};
7+
8+
export declare function readSharedAuthProfileStoreText(stateDir: string): string;
9+
10+
export declare function assertNoLegacyPrimaryAuthRows(stateDir: string): void;
11+
12+
export declare function assertOpenAiEnvAuthProfileStore(
13+
storeJson: string,
14+
options?: AuthProfileStoreAssertionOptions,
15+
): void;

‎scripts/e2e/lib/auth-profile-store-assertions.mjs‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,85 @@
11
// Shared auth profile store assertions for install/onboard E2E proof.
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
import { DatabaseSync } from "node:sqlite";
25
import { isRecord } from "../../lib/record-shared.mjs";
36

7+
export function readSharedAuthProfileStoreText(stateDir) {
8+
const dbPath = path.join(stateDir, "state", "openclaw.sqlite");
9+
if (!fs.existsSync(dbPath)) {
10+
return "";
11+
}
12+
let db;
13+
try {
14+
db = new DatabaseSync(dbPath, { readOnly: true });
15+
const schema = db
16+
.prepare("SELECT type FROM sqlite_schema WHERE name = ? LIMIT 1")
17+
.get("auth_profile_stores");
18+
if (!schema) {
19+
return "";
20+
}
21+
if (schema.type !== "table") {
22+
throw new Error(`auth_profile_stores is ${String(schema.type)}, not a table`);
23+
}
24+
const row = db
25+
.prepare("SELECT store_json FROM auth_profile_stores WHERE store_key = ?")
26+
.get("shared");
27+
return typeof row?.store_json === "string" ? row.store_json : "";
28+
} catch (error) {
29+
const detail = error instanceof Error ? error.message : String(error);
30+
throw new Error(`could not read the shared auth profile store: ${detail}`, {
31+
cause: error,
32+
});
33+
} finally {
34+
db?.close();
35+
}
36+
}
37+
38+
export function assertNoLegacyPrimaryAuthRows(stateDir) {
39+
const dbPath = path.join(stateDir, "agents", "main", "agent", "openclaw-agent.sqlite");
40+
if (!fs.existsSync(dbPath)) {
41+
return;
42+
}
43+
let db;
44+
try {
45+
db = new DatabaseSync(dbPath, { readOnly: true });
46+
const legacyRows = [
47+
{
48+
table: "auth_profile_store",
49+
query: "SELECT 1 AS present FROM auth_profile_store WHERE store_key = ? LIMIT 1",
50+
},
51+
{
52+
table: "auth_profile_state",
53+
query: "SELECT 1 AS present FROM auth_profile_state WHERE state_key = ? LIMIT 1",
54+
},
55+
];
56+
for (const entry of legacyRows) {
57+
const schema = db
58+
.prepare("SELECT type FROM sqlite_schema WHERE name = ? LIMIT 1")
59+
.get(entry.table);
60+
if (!schema) {
61+
continue;
62+
}
63+
if (schema.type !== "table") {
64+
throw new Error(`${entry.table} is ${String(schema.type)}, not a table`);
65+
}
66+
if (db.prepare(entry.query).get("primary")) {
67+
throw new Error(`onboard preserved a retired primary row in ${entry.table}`);
68+
}
69+
}
70+
} catch (error) {
71+
if (error instanceof Error && error.message.startsWith("onboard preserved a retired")) {
72+
throw error;
73+
}
74+
const detail = error instanceof Error ? error.message : String(error);
75+
throw new Error(`could not validate the main-agent auth database: ${detail}`, {
76+
cause: error,
77+
});
78+
} finally {
79+
db?.close();
80+
}
81+
}
82+
483
function hasExpectedOpenAiEnvRef(profile) {
584
if (!isRecord(profile)) {
685
return false;

‎scripts/e2e/lib/codex-on-demand/assertions.mjs‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
import { spawnSync } from "node:child_process";
33
import fs from "node:fs";
44
import path from "node:path";
5-
import { DatabaseSync } from "node:sqlite";
6-
import { assertOpenAiEnvAuthProfileStore } from "../auth-profile-store-assertions.mjs";
5+
import {
6+
assertNoLegacyPrimaryAuthRows,
7+
assertOpenAiEnvAuthProfileStore,
8+
readSharedAuthProfileStoreText,
9+
} from "../auth-profile-store-assertions.mjs";
710
import {
811
assertPathInside,
912
configPath,
@@ -125,24 +128,9 @@ if (providerRuntime && providerRuntime !== "codex") {
125128
throw new Error(`unexpected OpenAI provider runtime: ${providerRuntime}`);
126129
}
127130

128-
function readAuthProfileStoreText(agentDir) {
129-
const dbPath = path.join(agentDir, "openclaw-agent.sqlite");
130-
if (!fs.existsSync(dbPath)) {
131-
throw new Error("auth profile SQLite store was not persisted");
132-
}
133-
let db;
134-
try {
135-
db = new DatabaseSync(dbPath, { readOnly: true });
136-
const row = db
137-
.prepare("SELECT store_json FROM auth_profile_store WHERE store_key = ?")
138-
.get("primary");
139-
return typeof row?.store_json === "string" ? row.store_json : "";
140-
} finally {
141-
db?.close();
142-
}
143-
}
144-
145-
const authRaw = readAuthProfileStoreText(path.join(stateDir(), "agents", "main", "agent"));
131+
const openClawStateDir = stateDir();
132+
assertNoLegacyPrimaryAuthRows(openClawStateDir);
133+
const authRaw = readSharedAuthProfileStoreText(openClawStateDir);
146134
if (!authRaw) {
147135
throw new Error("auth profile SQLite store row was not persisted");
148136
}

‎scripts/e2e/lib/npm-onboard-channel-agent/assertions.mjs‎

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// Assertions for npm onboard channel-agent E2E scenarios.
22
import fs from "node:fs";
33
import path from "node:path";
4-
import { DatabaseSync } from "node:sqlite";
54
import {
65
assertAgentReplyContainsMarker,
76
assertOpenAiRequestLogUsed,
87
} from "../agent-turn-output.mjs";
9-
import { assertOpenAiEnvAuthProfileStore } from "../auth-profile-store-assertions.mjs";
8+
import {
9+
assertNoLegacyPrimaryAuthRows,
10+
assertOpenAiEnvAuthProfileStore,
11+
readSharedAuthProfileStoreText,
12+
} from "../auth-profile-store-assertions.mjs";
1013
import { readPositiveIntEnv } from "../env-limits.mjs";
1114
import {
1215
applyMockOpenAiModelConfig,
@@ -77,43 +80,15 @@ function extractStatusSection(text, title) {
7780
return stripAnsi(section.join("\n"));
7881
}
7982

80-
function readSharedAuthProfileStoreText(stateDir) {
81-
const dbPath = path.join(stateDir, "state", "openclaw.sqlite");
82-
if (!fs.existsSync(dbPath)) {
83-
return "";
84-
}
85-
let db;
86-
try {
87-
db = new DatabaseSync(dbPath, { readOnly: true });
88-
const row = db
89-
.prepare("SELECT store_json FROM auth_profile_stores WHERE store_key = ?")
90-
.get("shared");
91-
return typeof row?.store_json === "string" ? row.store_json : "";
92-
} catch {
93-
return "";
94-
} finally {
95-
db?.close();
96-
}
97-
}
98-
9983
function assertOnboardState() {
10084
const home = process.argv[3];
10185
const stateDir = path.join(home, ".openclaw");
10286
const configPath = path.join(stateDir, "openclaw.json");
103-
const legacyAuthDatabase = path.join(
104-
stateDir,
105-
"agents",
106-
"main",
107-
"agent",
108-
"openclaw-agent.sqlite",
109-
);
11087

11188
if (!fs.existsSync(configPath)) {
11289
throw new Error("onboard did not write openclaw.json");
11390
}
114-
if (fs.existsSync(legacyAuthDatabase)) {
115-
throw new Error("onboard created the retired main-agent auth database");
116-
}
91+
assertNoLegacyPrimaryAuthRows(stateDir);
11792
const authStoreText = readSharedAuthProfileStoreText(stateDir);
11893
if (!authStoreText) {
11994
throw new Error("onboard did not persist auth profile store");

‎scripts/e2e/lib/release-scenarios/assertions.mjs‎

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// Assertions for release scenario E2E packages and plugin state.
22
import fs from "node:fs";
33
import path from "node:path";
4-
import { DatabaseSync } from "node:sqlite";
54
import {
65
assertAgentReplyContainsMarker,
76
assertOpenAiRequestLogUsed,
87
} from "../agent-turn-output.mjs";
9-
import { assertOpenAiEnvAuthProfileStore } from "../auth-profile-store-assertions.mjs";
8+
import {
9+
assertNoLegacyPrimaryAuthRows,
10+
assertOpenAiEnvAuthProfileStore,
11+
readSharedAuthProfileStoreText,
12+
} from "../auth-profile-store-assertions.mjs";
1013
import {
1114
applyMockOpenAiModelConfig,
1215
parseMockOpenAiPort,
@@ -49,39 +52,16 @@ function authProfilesPath() {
4952
);
5053
}
5154

52-
function authProfilesDatabasePath() {
53-
return path.join(
54-
process.env.HOME ?? "",
55-
".openclaw",
56-
"agents",
57-
"main",
58-
"agent",
59-
"openclaw-agent.sqlite",
60-
);
61-
}
62-
63-
function readAuthProfileStoreSqliteText() {
64-
const dbPath = authProfilesDatabasePath();
65-
if (!fs.existsSync(dbPath)) {
66-
return "";
67-
}
68-
let db;
69-
try {
70-
db = new DatabaseSync(dbPath, { readOnly: true });
71-
const row = db
72-
.prepare("SELECT store_json FROM auth_profile_store WHERE store_key = ?")
73-
.get("primary");
74-
return typeof row?.store_json === "string" ? row.store_json : "";
75-
} catch {
76-
return "";
77-
} finally {
78-
db?.close();
79-
}
55+
function stateDir() {
56+
return process.env.OPENCLAW_STATE_DIR ?? path.dirname(configPath());
8057
}
8158

8259
function readStateText() {
8360
const paths = [configPath(), authProfilesPath()].filter((file) => fs.existsSync(file));
84-
return [...paths.map((file) => fs.readFileSync(file, "utf8")), readAuthProfileStoreSqliteText()]
61+
return [
62+
...paths.map((file) => fs.readFileSync(file, "utf8")),
63+
readSharedAuthProfileStoreText(stateDir()),
64+
]
8565
.filter(Boolean)
8666
.join("\n");
8767
}
@@ -96,7 +76,8 @@ function configureMockOpenAi() {
9676
function assertOpenAiEnvRef() {
9777
const rawKey = process.argv[3];
9878
assert(fs.existsSync(configPath()), "openclaw.json missing");
99-
assertOpenAiEnvAuthProfileStore(readAuthProfileStoreSqliteText(), {
79+
assertNoLegacyPrimaryAuthRows(stateDir());
80+
assertOpenAiEnvAuthProfileStore(readSharedAuthProfileStoreText(stateDir()), {
10081
missingMessage: "OpenAI env ref was not persisted",
10182
envRefMessage: "OpenAI env ref was not persisted",
10283
rawKeyMessage: "raw OpenAI key was persisted",

0 commit comments

Comments
 (0)