|
| 1 | +import { mkdirSync, writeFileSync } from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { DatabaseSync } from "node:sqlite"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { |
| 6 | + assertNoLegacyPrimaryAuthRows, |
| 7 | + readSharedAuthProfileStoreText, |
| 8 | +} from "../../scripts/e2e/lib/auth-profile-store-assertions.mjs"; |
| 9 | +import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js"; |
| 10 | + |
| 11 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 12 | + |
| 13 | +function makeStateDir(): string { |
| 14 | + const root = tempDirs.make("openclaw-auth-profile-assertions-"); |
| 15 | + return path.join(root, ".openclaw"); |
| 16 | +} |
| 17 | + |
| 18 | +function writeSharedDatabase( |
| 19 | + stateDir: string, |
| 20 | + options: { asView?: boolean; storeJson?: string } = {}, |
| 21 | +): string { |
| 22 | + const dbPath = path.join(stateDir, "state", "openclaw.sqlite"); |
| 23 | + mkdirSync(path.dirname(dbPath), { recursive: true }); |
| 24 | + const db = new DatabaseSync(dbPath); |
| 25 | + try { |
| 26 | + if (options.asView) { |
| 27 | + db.exec(` |
| 28 | + CREATE VIEW auth_profile_stores AS |
| 29 | + SELECT 'shared' AS store_key, '{}' AS store_json, 1 AS updated_at; |
| 30 | + `); |
| 31 | + } else { |
| 32 | + db.exec(` |
| 33 | + CREATE TABLE auth_profile_stores ( |
| 34 | + store_key TEXT NOT NULL PRIMARY KEY, |
| 35 | + store_json TEXT NOT NULL, |
| 36 | + updated_at INTEGER NOT NULL |
| 37 | + ) STRICT; |
| 38 | + `); |
| 39 | + db.prepare("INSERT INTO auth_profile_stores VALUES (?, ?, ?)").run( |
| 40 | + "shared", |
| 41 | + options.storeJson ?? "{}", |
| 42 | + Date.now(), |
| 43 | + ); |
| 44 | + } |
| 45 | + } finally { |
| 46 | + db.close(); |
| 47 | + } |
| 48 | + return dbPath; |
| 49 | +} |
| 50 | + |
| 51 | +function writeAgentDatabase( |
| 52 | + stateDir: string, |
| 53 | + options: { |
| 54 | + stateKeys?: string[]; |
| 55 | + storeKeys?: string[]; |
| 56 | + storeAsView?: boolean; |
| 57 | + } = {}, |
| 58 | +): string { |
| 59 | + const dbPath = path.join(stateDir, "agents", "main", "agent", "openclaw-agent.sqlite"); |
| 60 | + mkdirSync(path.dirname(dbPath), { recursive: true }); |
| 61 | + const db = new DatabaseSync(dbPath); |
| 62 | + try { |
| 63 | + if (options.storeAsView) { |
| 64 | + db.exec(` |
| 65 | + CREATE VIEW auth_profile_store AS |
| 66 | + SELECT 'primary' AS store_key, '{}' AS store_json, 1 AS updated_at; |
| 67 | + `); |
| 68 | + } else { |
| 69 | + db.exec(` |
| 70 | + CREATE TABLE auth_profile_store ( |
| 71 | + store_key TEXT NOT NULL PRIMARY KEY, |
| 72 | + store_json TEXT NOT NULL, |
| 73 | + updated_at INTEGER NOT NULL |
| 74 | + ) STRICT; |
| 75 | + `); |
| 76 | + for (const key of options.storeKeys ?? []) { |
| 77 | + db.prepare("INSERT INTO auth_profile_store VALUES (?, '{}', ?)").run(key, Date.now()); |
| 78 | + } |
| 79 | + } |
| 80 | + db.exec(` |
| 81 | + CREATE TABLE auth_profile_state ( |
| 82 | + state_key TEXT NOT NULL PRIMARY KEY, |
| 83 | + state_json TEXT NOT NULL, |
| 84 | + updated_at INTEGER NOT NULL |
| 85 | + ) STRICT; |
| 86 | + `); |
| 87 | + for (const key of options.stateKeys ?? []) { |
| 88 | + db.prepare("INSERT INTO auth_profile_state VALUES (?, '{}', ?)").run(key, Date.now()); |
| 89 | + } |
| 90 | + } finally { |
| 91 | + db.close(); |
| 92 | + } |
| 93 | + return dbPath; |
| 94 | +} |
| 95 | + |
| 96 | +describe("auth profile store E2E assertions", () => { |
| 97 | + it("reads the canonical shared row", () => { |
| 98 | + const stateDir = makeStateDir(); |
| 99 | + writeSharedDatabase(stateDir, { storeJson: '{"version":1}' }); |
| 100 | + |
| 101 | + expect(readSharedAuthProfileStoreText(stateDir)).toBe('{"version":1}'); |
| 102 | + }); |
| 103 | + |
| 104 | + it("returns empty when the shared database or table is absent", () => { |
| 105 | + const stateDir = makeStateDir(); |
| 106 | + |
| 107 | + expect(readSharedAuthProfileStoreText(stateDir)).toBe(""); |
| 108 | + const dbPath = path.join(stateDir, "state", "openclaw.sqlite"); |
| 109 | + mkdirSync(path.dirname(dbPath), { recursive: true }); |
| 110 | + new DatabaseSync(dbPath).close(); |
| 111 | + expect(readSharedAuthProfileStoreText(stateDir)).toBe(""); |
| 112 | + }); |
| 113 | + |
| 114 | + it("fails closed for a corrupt shared database", () => { |
| 115 | + const stateDir = makeStateDir(); |
| 116 | + const dbPath = path.join(stateDir, "state", "openclaw.sqlite"); |
| 117 | + mkdirSync(path.dirname(dbPath), { recursive: true }); |
| 118 | + writeFileSync(dbPath, "not sqlite"); |
| 119 | + |
| 120 | + expect(() => readSharedAuthProfileStoreText(stateDir)).toThrow( |
| 121 | + "could not read the shared auth profile store", |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + it("fails closed when the shared auth table is replaced by a view", () => { |
| 126 | + const stateDir = makeStateDir(); |
| 127 | + writeSharedDatabase(stateDir, { asView: true }); |
| 128 | + |
| 129 | + expect(() => readSharedAuthProfileStoreText(stateDir)).toThrow( |
| 130 | + "auth_profile_stores is view, not a table", |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it("permits unrelated main-agent auth rows", () => { |
| 135 | + const stateDir = makeStateDir(); |
| 136 | + writeAgentDatabase(stateDir, { |
| 137 | + stateKeys: ["last-good"], |
| 138 | + storeKeys: ["workspace"], |
| 139 | + }); |
| 140 | + |
| 141 | + expect(() => assertNoLegacyPrimaryAuthRows(stateDir)).not.toThrow(); |
| 142 | + }); |
| 143 | + |
| 144 | + it.each(["auth_profile_store", "auth_profile_state"] as const)( |
| 145 | + "rejects a retired primary row in %s", |
| 146 | + (table) => { |
| 147 | + const stateDir = makeStateDir(); |
| 148 | + writeAgentDatabase(stateDir, { |
| 149 | + stateKeys: table === "auth_profile_state" ? ["primary"] : [], |
| 150 | + storeKeys: table === "auth_profile_store" ? ["primary"] : [], |
| 151 | + }); |
| 152 | + |
| 153 | + expect(() => assertNoLegacyPrimaryAuthRows(stateDir)).toThrow( |
| 154 | + `onboard preserved a retired primary row in ${table}`, |
| 155 | + ); |
| 156 | + }, |
| 157 | + ); |
| 158 | + |
| 159 | + it("fails closed for a corrupt main-agent database", () => { |
| 160 | + const stateDir = makeStateDir(); |
| 161 | + const dbPath = path.join(stateDir, "agents", "main", "agent", "openclaw-agent.sqlite"); |
| 162 | + mkdirSync(path.dirname(dbPath), { recursive: true }); |
| 163 | + writeFileSync(dbPath, "not sqlite"); |
| 164 | + |
| 165 | + expect(() => assertNoLegacyPrimaryAuthRows(stateDir)).toThrow( |
| 166 | + "could not validate the main-agent auth database", |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + it("fails closed when a retired auth table is replaced by a view", () => { |
| 171 | + const stateDir = makeStateDir(); |
| 172 | + writeAgentDatabase(stateDir, { storeAsView: true }); |
| 173 | + |
| 174 | + expect(() => assertNoLegacyPrimaryAuthRows(stateDir)).toThrow( |
| 175 | + "auth_profile_store is view, not a table", |
| 176 | + ); |
| 177 | + }); |
| 178 | +}); |
0 commit comments