|
| 1 | +// Preserves read failures separately from absent or malformed plugin index state. |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { requireNodeSqlite } from "../infra/node-sqlite.js"; |
| 6 | +import * as stateDbReadOnly from "../state/openclaw-state-db-readonly.js"; |
| 7 | +import { |
| 8 | + closeOpenClawStateDatabaseForTest, |
| 9 | + runOpenClawStateWriteTransaction, |
| 10 | +} from "../state/openclaw-state-db.js"; |
| 11 | +import { inspectPersistedInstalledPluginIndexInstallRecordsSync } from "./installed-plugin-index-record-state.js"; |
| 12 | +import { readPersistedInstalledPluginIndexInstallRecords } from "./installed-plugin-index-records.js"; |
| 13 | +import { |
| 14 | + refreshPersistedInstalledPluginIndex, |
| 15 | + refreshPersistedInstalledPluginIndexWithLeaseSync, |
| 16 | +} from "./installed-plugin-index-store-write.js"; |
| 17 | +import { |
| 18 | + readPersistedInstalledPluginIndex, |
| 19 | + readPersistedInstalledPluginIndexSync, |
| 20 | + resolveInstalledPluginIndexStorePath, |
| 21 | +} from "./installed-plugin-index-store.js"; |
| 22 | +import { cleanupTrackedTempDirs, makeTrackedTempDir } from "./test-helpers/fs-fixtures.js"; |
| 23 | + |
| 24 | +const tempDirs: string[] = []; |
| 25 | +afterEach(() => { |
| 26 | + closeOpenClawStateDatabaseForTest(); |
| 27 | + cleanupTrackedTempDirs(tempDirs); |
| 28 | +}); |
| 29 | +function makeTempDir() { |
| 30 | + return makeTrackedTempDir("openclaw-installed-plugin-index-read-state", tempDirs); |
| 31 | +} |
| 32 | + |
| 33 | +function insertPersistedIndexRow(stateDir: string, valueJson: string): void { |
| 34 | + runOpenClawStateWriteTransaction( |
| 35 | + ({ db }) => { |
| 36 | + db.prepare( |
| 37 | + "INSERT INTO config_machine_state (state_key, value_json, updated_at_ms) VALUES ('plugins.installedIndex', ?, 123)", |
| 38 | + ).run(valueJson); |
| 39 | + }, |
| 40 | + { env: { OPENCLAW_STATE_DIR: stateDir } }, |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +function readPersistedIndexRow(filePath: string): { value_json: string; updated_at_ms: number } { |
| 45 | + const { DatabaseSync } = requireNodeSqlite(); |
| 46 | + const db = new DatabaseSync(filePath, { readOnly: true }); |
| 47 | + try { |
| 48 | + return db |
| 49 | + .prepare( |
| 50 | + "SELECT value_json, updated_at_ms FROM config_machine_state WHERE state_key = 'plugins.installedIndex'", |
| 51 | + ) |
| 52 | + .get() as { value_json: string; updated_at_ms: number }; |
| 53 | + } finally { |
| 54 | + db.close(); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +describe("installed plugin index read state", () => { |
| 59 | + it("preserves the original read error through both full-index readers", async () => { |
| 60 | + const stateDir = makeTempDir(); |
| 61 | + const error = Object.assign(new Error("plugin index read denied"), { code: "EACCES" }); |
| 62 | + const readSpy = vi.spyOn(stateDbReadOnly, "withExistingOpenClawStateDatabaseReadOnly"); |
| 63 | + for (const read of [readPersistedInstalledPluginIndexSync, readPersistedInstalledPluginIndex]) { |
| 64 | + readSpy.mockImplementationOnce(() => { |
| 65 | + throw error; |
| 66 | + }); |
| 67 | + await expect |
| 68 | + .soft( |
| 69 | + Promise.resolve().then(() => read({ stateDir })), |
| 70 | + read.name, |
| 71 | + ) |
| 72 | + .rejects.toBe(error); |
| 73 | + } |
| 74 | + readSpy.mockRestore(); |
| 75 | + }); |
| 76 | + |
| 77 | + it.each(["database", "table", "row"])( |
| 78 | + "preserves genuine missing %s without creating state", |
| 79 | + async (missing) => { |
| 80 | + const stateDir = makeTempDir(); |
| 81 | + const filePath = resolveInstalledPluginIndexStorePath({ stateDir }); |
| 82 | + if (missing !== "database") { |
| 83 | + fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 84 | + const { DatabaseSync } = requireNodeSqlite(); |
| 85 | + const db = new DatabaseSync(filePath); |
| 86 | + if (missing === "row") { |
| 87 | + db.exec( |
| 88 | + "CREATE TABLE config_machine_state (state_key TEXT PRIMARY KEY, value_json TEXT NOT NULL, updated_at_ms INTEGER NOT NULL) STRICT", |
| 89 | + ); |
| 90 | + } |
| 91 | + db.close(); |
| 92 | + } |
| 93 | + const before = missing === "database" ? undefined : fs.readFileSync(filePath); |
| 94 | + expect(inspectPersistedInstalledPluginIndexInstallRecordsSync({ stateDir })).toEqual({ |
| 95 | + status: "missing", |
| 96 | + }); |
| 97 | + await expect( |
| 98 | + readPersistedInstalledPluginIndexInstallRecords({ stateDir }), |
| 99 | + ).resolves.toBeNull(); |
| 100 | + await expect(readPersistedInstalledPluginIndex({ stateDir })).resolves.toBeNull(); |
| 101 | + expect(missing === "database" ? fs.existsSync(filePath) : fs.readFileSync(filePath)).toEqual( |
| 102 | + before ?? false, |
| 103 | + ); |
| 104 | + }, |
| 105 | + ); |
| 106 | + |
| 107 | + it.each([ |
| 108 | + { reason: "manual", leased: false }, |
| 109 | + { reason: "manual", leased: true }, |
| 110 | + { reason: "policy-changed", leased: false }, |
| 111 | + { reason: "policy-changed", leased: true }, |
| 112 | + ] as const)( |
| 113 | + "stops $reason refresh before mutation after one failed read (leased=$leased)", |
| 114 | + async ({ reason, leased }) => { |
| 115 | + const stateDir = makeTempDir(); |
| 116 | + const installRecords = { |
| 117 | + authoritative: { source: "npm", spec: "[email protected]" }, |
| 118 | + } as const; |
| 119 | + const valueJson = JSON.stringify({ |
| 120 | + revision: 123, |
| 121 | + index: { |
| 122 | + version: 1, |
| 123 | + hostContractVersion: "2026.4.25", |
| 124 | + compatRegistryVersion: "compat-v1", |
| 125 | + migrationVersion: 1, |
| 126 | + policyHash: "policy-hash", |
| 127 | + generatedAtMs: 123, |
| 128 | + installRecords, |
| 129 | + plugins: [], |
| 130 | + diagnostics: [], |
| 131 | + }, |
| 132 | + }); |
| 133 | + insertPersistedIndexRow(stateDir, valueJson); |
| 134 | + const filePath = resolveInstalledPluginIndexStorePath({ stateDir }); |
| 135 | + closeOpenClawStateDatabaseForTest(); |
| 136 | + const error = Object.assign(new Error("plugin index read denied"), { code: "EACCES" }); |
| 137 | + const readSpy = vi |
| 138 | + .spyOn(stateDbReadOnly, "withExistingOpenClawStateDatabaseReadOnly") |
| 139 | + .mockImplementationOnce(() => { |
| 140 | + throw error; |
| 141 | + }); |
| 142 | + const lease = { assertOwnedInTransaction: vi.fn() }; |
| 143 | + const params = { |
| 144 | + reason, |
| 145 | + stateDir, |
| 146 | + candidates: [], |
| 147 | + env: { OPENCLAW_VERSION: "2026.4.25", VITEST: "true" }, |
| 148 | + ...(reason === "policy-changed" ? { installRecords } : {}), |
| 149 | + }; |
| 150 | + const refresh = async () => |
| 151 | + leased |
| 152 | + ? refreshPersistedInstalledPluginIndexWithLeaseSync({ ...params, lease }) |
| 153 | + : refreshPersistedInstalledPluginIndex(params); |
| 154 | + await expect.soft(Promise.resolve().then(refresh)).rejects.toBe(error); |
| 155 | + expect.soft(lease.assertOwnedInTransaction).not.toHaveBeenCalled(); |
| 156 | + expect |
| 157 | + .soft(readPersistedIndexRow(filePath)) |
| 158 | + .toEqual({ value_json: valueJson, updated_at_ms: 123 }); |
| 159 | + readSpy.mockRestore(); |
| 160 | + await refresh(); |
| 161 | + expect((await readPersistedInstalledPluginIndex({ stateDir }))?.installRecords).toEqual( |
| 162 | + installRecords, |
| 163 | + ); |
| 164 | + const persisted = JSON.parse(readPersistedIndexRow(filePath).value_json) as { |
| 165 | + revision: number; |
| 166 | + }; |
| 167 | + expect(persisted.revision).toBeGreaterThan(123); |
| 168 | + }, |
| 169 | + ); |
| 170 | + |
| 171 | + it.each([ |
| 172 | + { label: "malformed JSON", valueJson: "{", recordStatus: "invalid" }, |
| 173 | + { label: "missing records", valueJson: '{"revision":123,"index":{}}', recordStatus: "invalid" }, |
| 174 | + { |
| 175 | + label: "malformed records", |
| 176 | + valueJson: '{"revision":123,"index":{"installRecords":{"demo":{"source":"bogus"}}}}', |
| 177 | + recordStatus: "invalid", |
| 178 | + }, |
| 179 | + { |
| 180 | + label: "invalid index metadata", |
| 181 | + valueJson: |
| 182 | + '{"revision":123,"index":{"version":999,"installRecords":{"demo":{"source":"npm"}}}}', |
| 183 | + recordStatus: "valid", |
| 184 | + }, |
| 185 | + { |
| 186 | + label: "missing revision", |
| 187 | + valueJson: '{"index":{"installRecords":{"demo":{"source":"npm"}}}}', |
| 188 | + recordStatus: "valid", |
| 189 | + }, |
| 190 | + ] as const)( |
| 191 | + "keeps record parsing independent of full-index parsing: $label", |
| 192 | + async ({ valueJson, recordStatus }) => { |
| 193 | + const stateDir = makeTempDir(); |
| 194 | + insertPersistedIndexRow(stateDir, valueJson); |
| 195 | + const records = recordStatus === "valid" ? { demo: { source: "npm" } } : null; |
| 196 | + expect(inspectPersistedInstalledPluginIndexInstallRecordsSync({ stateDir }).status).toBe( |
| 197 | + recordStatus, |
| 198 | + ); |
| 199 | + await expect(readPersistedInstalledPluginIndexInstallRecords({ stateDir })).resolves.toEqual( |
| 200 | + records, |
| 201 | + ); |
| 202 | + await expect(readPersistedInstalledPluginIndex({ stateDir })).resolves.toBeNull(); |
| 203 | + }, |
| 204 | + ); |
| 205 | +}); |
0 commit comments