Skip to content

Commit be891d2

Browse files
authored
fix(e2e): follow shared auth ownership in onboarding proof (#126958)
1 parent 630175d commit be891d2

2 files changed

Lines changed: 54 additions & 21 deletions

File tree

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ function extractStatusSection(text, title) {
7777
return stripAnsi(section.join("\n"));
7878
}
7979

80-
function readAuthProfileStoreText(agentDir) {
81-
const dbPath = path.join(agentDir, "openclaw-agent.sqlite");
80+
function readSharedAuthProfileStoreText(stateDir) {
81+
const dbPath = path.join(stateDir, "state", "openclaw.sqlite");
8282
if (!fs.existsSync(dbPath)) {
8383
return "";
8484
}
8585
let db;
8686
try {
8787
db = new DatabaseSync(dbPath, { readOnly: true });
8888
const row = db
89-
.prepare("SELECT store_json FROM auth_profile_store WHERE store_key = ?")
90-
.get("primary");
89+
.prepare("SELECT store_json FROM auth_profile_stores WHERE store_key = ?")
90+
.get("shared");
9191
return typeof row?.store_json === "string" ? row.store_json : "";
9292
} catch {
9393
return "";
@@ -100,15 +100,21 @@ function assertOnboardState() {
100100
const home = process.argv[3];
101101
const stateDir = path.join(home, ".openclaw");
102102
const configPath = path.join(stateDir, "openclaw.json");
103-
const agentDir = path.join(stateDir, "agents", "main", "agent");
103+
const legacyAuthDatabase = path.join(
104+
stateDir,
105+
"agents",
106+
"main",
107+
"agent",
108+
"openclaw-agent.sqlite",
109+
);
104110

105111
if (!fs.existsSync(configPath)) {
106112
throw new Error("onboard did not write openclaw.json");
107113
}
108-
if (!fs.existsSync(agentDir)) {
109-
throw new Error("onboard did not create main agent dir");
114+
if (fs.existsSync(legacyAuthDatabase)) {
115+
throw new Error("onboard created the retired main-agent auth database");
110116
}
111-
const authStoreText = readAuthProfileStoreText(agentDir);
117+
const authStoreText = readSharedAuthProfileStoreText(stateDir);
112118
if (!authStoreText) {
113119
throw new Error("onboard did not persist auth profile store");
114120
}

‎test/scripts/npm-onboard-channel-agent-assertions.test.ts‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,24 @@ function writeOnboardConfig(home: string): void {
3737
);
3838
}
3939

40-
function writeAuthProfileStoreSqlite(agentDir: string, store: unknown): void {
41-
fs.mkdirSync(agentDir, { recursive: true });
42-
const db = new DatabaseSync(path.join(agentDir, "openclaw-agent.sqlite"));
40+
function writeSharedAuthProfileStoreSqlite(home: string, store: unknown): void {
41+
const stateDir = path.join(home, ".openclaw", "state");
42+
fs.mkdirSync(stateDir, { recursive: true });
43+
const db = new DatabaseSync(path.join(stateDir, "openclaw.sqlite"));
4344
try {
4445
db.exec(`
45-
CREATE TABLE IF NOT EXISTS auth_profile_store (
46+
CREATE TABLE IF NOT EXISTS auth_profile_stores (
4647
store_key TEXT NOT NULL PRIMARY KEY,
4748
store_json TEXT NOT NULL,
4849
updated_at INTEGER NOT NULL
4950
);
5051
`);
5152
db.prepare(
5253
`
53-
INSERT INTO auth_profile_store (store_key, store_json, updated_at)
54+
INSERT INTO auth_profile_stores (store_key, store_json, updated_at)
5455
VALUES (?, ?, ?)
5556
`,
56-
).run("primary", JSON.stringify(store), Date.now());
57+
).run("shared", JSON.stringify(store), Date.now());
5758
} finally {
5859
db.close();
5960
}
@@ -217,13 +218,13 @@ describe("npm onboard channel agent assertions", () => {
217218
}
218219
});
219220

220-
it("validates OpenAI env refs from the SQLite auth profile store", () => {
221+
it("validates OpenAI env refs from the shared SQLite auth profile store", () => {
221222
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-onboard-assertions-"));
222223
const agentDir = path.join(tempDir, ".openclaw", "agents", "main", "agent");
223224

224225
try {
225226
writeOnboardConfig(tempDir);
226-
writeAuthProfileStoreSqlite(agentDir, {
227+
writeSharedAuthProfileStoreSqlite(tempDir, {
227228
version: 1,
228229
profiles: {
229230
"openai:api-key": {
@@ -238,6 +239,7 @@ describe("npm onboard channel agent assertions", () => {
238239

239240
expect(result.status).toBe(0);
240241
expect(result.stderr).toBe("");
242+
expect(fs.existsSync(agentDir)).toBe(false);
241243
expect(fs.existsSync(path.join(agentDir, "auth-profiles.json"))).toBe(false);
242244
} finally {
243245
fs.rmSync(tempDir, { force: true, recursive: true });
@@ -257,11 +259,10 @@ describe("npm onboard channel agent assertions", () => {
257259

258260
for (const store of cases) {
259261
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-onboard-assertions-"));
260-
const agentDir = path.join(tempDir, ".openclaw", "agents", "main", "agent");
261262

262263
try {
263264
writeOnboardConfig(tempDir);
264-
writeAuthProfileStoreSqlite(agentDir, store);
265+
writeSharedAuthProfileStoreSqlite(tempDir, store);
265266

266267
const result = runOnboardAssert(tempDir);
267268

@@ -275,11 +276,9 @@ describe("npm onboard channel agent assertions", () => {
275276

276277
it("rejects inline OpenAI keys in the SQLite auth profile store", () => {
277278
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-onboard-assertions-"));
278-
const agentDir = path.join(tempDir, ".openclaw", "agents", "main", "agent");
279-
280279
try {
281280
writeOnboardConfig(tempDir);
282-
writeAuthProfileStoreSqlite(agentDir, {
281+
writeSharedAuthProfileStoreSqlite(tempDir, {
283282
version: 1,
284283
profiles: {
285284
"openai:api-key": {
@@ -299,6 +298,34 @@ describe("npm onboard channel agent assertions", () => {
299298
}
300299
});
301300

301+
it("rejects a fresh install that recreates the retired main-agent auth database", () => {
302+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-onboard-assertions-"));
303+
const legacyAgentDir = path.join(tempDir, ".openclaw", "agents", "main", "agent");
304+
305+
try {
306+
writeOnboardConfig(tempDir);
307+
writeSharedAuthProfileStoreSqlite(tempDir, {
308+
version: 1,
309+
profiles: {
310+
"openai:api-key": {
311+
type: "api_key",
312+
provider: "openai",
313+
keyRef: { source: "env", provider: "default", id: "OPENAI_API_KEY" },
314+
},
315+
},
316+
});
317+
fs.mkdirSync(legacyAgentDir, { recursive: true });
318+
new DatabaseSync(path.join(legacyAgentDir, "openclaw-agent.sqlite")).close();
319+
320+
const result = runOnboardAssert(tempDir);
321+
322+
expect(result.status).not.toBe(0);
323+
expect(result.stderr).toContain("onboard created the retired main-agent auth database");
324+
} finally {
325+
fs.rmSync(tempDir, { force: true, recursive: true });
326+
}
327+
});
328+
302329
it("validates channel tokens in their canonical config fields", () => {
303330
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-channel-assertions-"));
304331
try {

0 commit comments

Comments
 (0)