Files
openclaw/src/commitments/store.test-utils.ts
morluto 1a34950d9c fix(commitments): keep heartbeats responsive with large queues (#105780)
* fix(commitments): migrate store to sqlite

Replace steady-state JSON persistence with typed shared-state rows and a doctor-only verified import for shipped legacy data.

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>

* fix(commitments): satisfy migration gates

* fix(commitments): type sqlite counts defensively

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-07-15 03:58:03 -07:00

47 lines
1.5 KiB
TypeScript

// Test-only helpers for seeding and inspecting canonical commitment rows.
import { executeSqliteQuerySync, getNodeSqliteKysely } from "../infra/kysely-sync.js";
import {
openOpenClawStateDatabase,
runOpenClawStateWriteTransaction,
} from "../state/openclaw-state-db.js";
import {
commitmentRecordFromRow,
commitmentRecordToRow,
type CommitmentsDatabase,
} from "./store-record.js";
import type { CommitmentRecord } from "./types.js";
function assertTestRuntime(): void {
if (!process.env.VITEST && process.env.NODE_ENV !== "test") {
throw new Error("commitment store test helpers are unavailable outside tests");
}
}
export function seedCommitmentsForTest(records: CommitmentRecord[]): void {
assertTestRuntime();
runOpenClawStateWriteTransaction(({ db }) => {
const commitmentsDb = getNodeSqliteKysely<CommitmentsDatabase>(db);
executeSqliteQuerySync(db, commitmentsDb.deleteFrom("commitments"));
for (let offset = 0; offset < records.length; offset += 500) {
executeSqliteQuerySync(
db,
commitmentsDb
.insertInto("commitments")
.values(records.slice(offset, offset + 500).map(commitmentRecordToRow)),
);
}
});
}
export function readCommitmentsForTest(): CommitmentRecord[] {
assertTestRuntime();
const database = openOpenClawStateDatabase();
return executeSqliteQuerySync(
database.db,
getNodeSqliteKysely<CommitmentsDatabase>(database.db)
.selectFrom("commitments")
.selectAll()
.orderBy("id", "asc"),
).rows.map(commitmentRecordFromRow);
}