test(qa-matrix): stabilize sync timeout cursor

This commit is contained in:
Peter Steinberger
2026-04-23 01:20:56 +01:00
parent f72c97afca
commit bf132d6fb9
2 changed files with 20 additions and 12 deletions

View File

@@ -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,

View File

@@ -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) {