mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 05:20:43 +00:00
test(qa-matrix): stabilize sync timeout cursor
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { MatrixQaObservedEvent } from "./events.js";
|
||||
import {
|
||||
createMatrixQaRoomObserver,
|
||||
@@ -50,16 +50,22 @@ describe("matrix sync helpers", () => {
|
||||
|
||||
const observedEvents: MatrixQaObservedEvent[] = [];
|
||||
|
||||
const result = await waitForOptionalMatrixQaRoomEvent({
|
||||
accessToken: "token",
|
||||
baseUrl: "http://127.0.0.1:28008/",
|
||||
fetchImpl,
|
||||
observedEvents,
|
||||
predicate: (event) => event.sender === "@sut:matrix-qa.test",
|
||||
roomId: "!room:matrix-qa.test",
|
||||
since: "start-batch",
|
||||
timeoutMs: 1_000,
|
||||
});
|
||||
const nowSpy = vi.spyOn(Date, "now").mockReturnValueOnce(0).mockReturnValue(1);
|
||||
let result: Awaited<ReturnType<typeof waitForOptionalMatrixQaRoomEvent>>;
|
||||
try {
|
||||
result = await waitForOptionalMatrixQaRoomEvent({
|
||||
accessToken: "token",
|
||||
baseUrl: "http://127.0.0.1:28008/",
|
||||
fetchImpl,
|
||||
observedEvents,
|
||||
predicate: (event) => event.sender === "@sut:matrix-qa.test",
|
||||
roomId: "!room:matrix-qa.test",
|
||||
since: "start-batch",
|
||||
timeoutMs: 1,
|
||||
});
|
||||
} finally {
|
||||
nowSpy.mockRestore();
|
||||
}
|
||||
|
||||
expect(result).toEqual({
|
||||
matched: false,
|
||||
|
||||
@@ -144,6 +144,7 @@ export function createMatrixQaRoomObserver(
|
||||
const startSince = await this.prime();
|
||||
const startedAt = Date.now();
|
||||
let cursorIndex = roomObserver.cursorIndex;
|
||||
let didPoll = false;
|
||||
while (true) {
|
||||
const matched = findMatrixQaObservedEventMatch({
|
||||
cursorIndex,
|
||||
@@ -161,7 +162,7 @@ export function createMatrixQaRoomObserver(
|
||||
}
|
||||
|
||||
const elapsedMs = Date.now() - startedAt;
|
||||
if (elapsedMs >= waitParams.timeoutMs) {
|
||||
if (elapsedMs >= waitParams.timeoutMs && (didPoll || waitParams.timeoutMs <= 0)) {
|
||||
roomObserver.cursorIndex = Math.max(roomObserver.cursorIndex, cursorIndex);
|
||||
return {
|
||||
matched: false,
|
||||
@@ -177,6 +178,7 @@ export function createMatrixQaRoomObserver(
|
||||
roomObserver,
|
||||
timeoutMs: remainingMs,
|
||||
});
|
||||
didPoll = true;
|
||||
}
|
||||
},
|
||||
async waitForRoomEvent(waitParams) {
|
||||
|
||||
Reference in New Issue
Block a user