test(matrix): stabilize file sync store persistence checks

This commit is contained in:
Vincent Koc
2026-03-30 08:39:12 +09:00
parent d6a3580347
commit 408f6a5b0b

View File

@@ -2,8 +2,8 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import type { ISyncResponse } from "matrix-js-sdk";
import * as jsonFiles from "openclaw/plugin-sdk/infra-runtime";
import { afterEach, describe, expect, it, vi } from "vitest";
import * as runtimeApi from "../../runtime-api.js";
import { FileBackedMatrixSyncStore } from "./file-sync-store.js";
function createSyncResponse(nextBatch: string): ISyncResponse {
@@ -142,7 +142,7 @@ describe("FileBackedMatrixSyncStore", () => {
it("coalesces background persistence until the debounce window elapses", async () => {
vi.useFakeTimers();
const storagePath = createStoragePath();
const writeSpy = vi.spyOn(jsonFiles, "writeJsonAtomic").mockResolvedValue();
const writeSpy = vi.spyOn(runtimeApi, "writeJsonFileAtomically").mockResolvedValue();
const store = new FileBackedMatrixSyncStore(storagePath);
await store.setSyncData(createSyncResponse("s111"));
@@ -155,6 +155,7 @@ describe("FileBackedMatrixSyncStore", () => {
expect(writeSpy).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(1);
await Promise.resolve();
expect(writeSpy).toHaveBeenCalledTimes(1);
expect(writeSpy).toHaveBeenCalledWith(
storagePath,
@@ -166,8 +167,9 @@ describe("FileBackedMatrixSyncStore", () => {
lazyLoadMembers: true,
},
}),
expect.any(Object),
);
await store.flush();
});
it("waits for an in-flight persist when shutdown flush runs", async () => {
@@ -175,7 +177,7 @@ describe("FileBackedMatrixSyncStore", () => {
const storagePath = createStoragePath();
const writeDeferred = createDeferred();
const writeSpy = vi
.spyOn(jsonFiles, "writeJsonAtomic")
.spyOn(runtimeApi, "writeJsonFileAtomically")
.mockImplementation(async () => writeDeferred.promise);
const store = new FileBackedMatrixSyncStore(storagePath);