diff --git a/extensions/qa-matrix/src/substrate/sync.test.ts b/extensions/qa-matrix/src/substrate/sync.test.ts index abea4b9c058..dbaacf3f2da 100644 --- a/extensions/qa-matrix/src/substrate/sync.test.ts +++ b/extensions/qa-matrix/src/substrate/sync.test.ts @@ -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>; + 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, diff --git a/extensions/qa-matrix/src/substrate/sync.ts b/extensions/qa-matrix/src/substrate/sync.ts index 9d9983eaeae..aafcf4d3536 100644 --- a/extensions/qa-matrix/src/substrate/sync.ts +++ b/extensions/qa-matrix/src/substrate/sync.ts @@ -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) {