mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-18 19:04:45 +00:00
test: speed up plugin state seed helper
This commit is contained in:
@@ -59,6 +59,15 @@ type PluginStateDatabase = {
|
||||
walMaintenance: SqliteWalMaintenance;
|
||||
};
|
||||
|
||||
type PluginStateSeedEntryForTests = {
|
||||
pluginId: string;
|
||||
namespace: string;
|
||||
key: string;
|
||||
valueJson: string;
|
||||
createdAt?: number;
|
||||
expiresAt?: number | null;
|
||||
};
|
||||
|
||||
let cachedDatabase: PluginStateDatabase | null = null;
|
||||
|
||||
function normalizeNumber(value: number | bigint | null): number | undefined {
|
||||
@@ -638,6 +647,29 @@ export function clearPluginStateSqliteStoreForTests(): void {
|
||||
store.db.exec("DELETE FROM plugin_state_entries;");
|
||||
}
|
||||
|
||||
export function seedPluginStateSqliteEntriesForTests(
|
||||
entries: readonly PluginStateSeedEntryForTests[],
|
||||
): void {
|
||||
if (entries.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
runWriteTransaction("register", (store) => {
|
||||
for (let index = 0; index < entries.length; index += 1) {
|
||||
const entry = entries[index];
|
||||
store.statements.upsertEntry.run({
|
||||
plugin_id: entry.pluginId,
|
||||
namespace: entry.namespace,
|
||||
entry_key: entry.key,
|
||||
value_json: entry.valueJson,
|
||||
created_at: entry.createdAt ?? now + index,
|
||||
expires_at: entry.expiresAt ?? null,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function probePluginStateStore(): PluginStateStoreProbeResult {
|
||||
const dbPath = resolvePluginStateSqlitePath(process.env);
|
||||
const steps: PluginStateStoreProbeStep[] = [];
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { requireNodeSqlite } from "../infra/node-sqlite.js";
|
||||
import { resolvePluginStateSqlitePath } from "./plugin-state-store.paths.js";
|
||||
import { closePluginStateSqliteStore, probePluginStateStore } from "./plugin-state-store.sqlite.js";
|
||||
import { seedPluginStateSqliteEntriesForTests } from "./plugin-state-store.sqlite.js";
|
||||
|
||||
export type PluginStateSeedEntry = {
|
||||
pluginId: string;
|
||||
@@ -16,52 +14,20 @@ export function seedPluginStateEntriesForTests(entries: PluginStateSeedEntry[]):
|
||||
return;
|
||||
}
|
||||
|
||||
probePluginStateStore();
|
||||
closePluginStateSqliteStore();
|
||||
|
||||
const { DatabaseSync } = requireNodeSqlite();
|
||||
const db = new DatabaseSync(resolvePluginStateSqlitePath());
|
||||
const insertEntry = db.prepare(`
|
||||
INSERT INTO plugin_state_entries (
|
||||
plugin_id,
|
||||
namespace,
|
||||
entry_key,
|
||||
value_json,
|
||||
created_at,
|
||||
expires_at
|
||||
) VALUES (
|
||||
@plugin_id,
|
||||
@namespace,
|
||||
@entry_key,
|
||||
@value_json,
|
||||
@created_at,
|
||||
@expires_at
|
||||
)
|
||||
`);
|
||||
const now = Date.now();
|
||||
|
||||
db.exec("BEGIN IMMEDIATE");
|
||||
try {
|
||||
for (let index = 0; index < entries.length; index += 1) {
|
||||
const entry = entries[index];
|
||||
seedPluginStateSqliteEntriesForTests(
|
||||
entries.map((entry) => {
|
||||
const valueJson = JSON.stringify(entry.value);
|
||||
if (valueJson == null) {
|
||||
throw new Error("plugin state seed value must be JSON serializable");
|
||||
}
|
||||
insertEntry.run({
|
||||
plugin_id: entry.pluginId,
|
||||
return {
|
||||
pluginId: entry.pluginId,
|
||||
namespace: entry.namespace,
|
||||
entry_key: entry.key,
|
||||
value_json: valueJson,
|
||||
created_at: entry.createdAt ?? now + index,
|
||||
expires_at: entry.expiresAt ?? null,
|
||||
});
|
||||
}
|
||||
db.exec("COMMIT");
|
||||
} catch (error) {
|
||||
db.exec("ROLLBACK");
|
||||
throw error;
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
key: entry.key,
|
||||
valueJson,
|
||||
...(entry.createdAt != null ? { createdAt: entry.createdAt } : {}),
|
||||
...(entry.expiresAt !== undefined ? { expiresAt: entry.expiresAt } : {}),
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user