


























@@ -163,18 +163,36 @@ type MockCallSource = {
163163};
164164};
165165166+function mockCall(source: MockCallSource, label: string, callIndex = 0) {
167+const calls = source.mock.calls;
168+const resolvedIndex = callIndex < 0 ? calls.length + callIndex : callIndex;
169+const call = calls[resolvedIndex];
170+if (!call) {
171+throw new Error(`expected ${label} call ${callIndex}`);
172+}
173+return call;
174+}
175+166176function mockObjectArg(source: MockCallSource, label: string, callIndex = 0, argIndex = 0) {
167-const arg = source.mock.calls[callIndex]?.[argIndex];
177+const arg = mockCall(source, label, callIndex)[argIndex];
168178if (!arg || typeof arg !== "object") {
169179throw new Error(`expected ${label} object arg`);
170180}
171181return arg as Record<string, unknown>;
172182}
173183184+function lastMockObjectArg(source: MockCallSource, label: string, argIndex = 0) {
185+return mockObjectArg(source, label, -1, argIndex);
186+}
187+174188function mockMessageBody(source: MockCallSource, label: string, callIndex = 0) {
175189return String(mockObjectArg(source, label, callIndex).body);
176190}
177191192+function lastMockMessageBody(source: MockCallSource, label: string) {
193+return mockMessageBody(source, label, -1);
194+}
195+178196function expectSentTextMessage(
179197source: MockCallSource,
180198expected: {
@@ -640,7 +658,7 @@ describe("matrix live qa scenarios", () => {
640658expect(artifacts.reactionEventId).toBe("$driver-approval-reaction");
641659expect(artifacts.reactionTargetEventId).toBe(approvalEventId);
642660expect(waitForRoomEvent).toHaveBeenCalledTimes(3);
643-expect(gatewayCall.mock.calls.at(-1)?.[0]).toBe("exec.approval.waitDecision");
661+expect(mockCall(gatewayCall, "gatewayCall", -1)[0]).toBe("exec.approval.waitDecision");
644662});
645663646664it("reuses observed Matrix approval events across channel and DM target=both waits", async () => {
@@ -741,10 +759,9 @@ describe("matrix live qa scenarios", () => {
741759expect(artifacts.approvals?.[1]?.roomId).toBe("!driver-shared-dm:matrix-qa.test");
742760743761expect(waitForRoomEvent).toHaveBeenCalledTimes(1);
744-expect(gatewayCall.mock.calls.at(-1)?.[0]).toBe("exec.approval.resolve");
745-expect((gatewayCall.mock.calls.at(-1)?.[2] as { expectFinal?: unknown }).expectFinal).toBe(
746-false,
747-);
762+const finalGatewayCall = mockCall(gatewayCall, "gatewayCall", -1);
763+expect(finalGatewayCall[0]).toBe("exec.approval.resolve");
764+expect((finalGatewayCall[2] as { expectFinal?: unknown }).expectFinal).toBe(false);
748765expect(createMatrixQaClient).toHaveBeenCalledTimes(3);
749766});
750767@@ -1421,7 +1438,7 @@ describe("matrix live qa scenarios", () => {
14211438since: `${params.roomId}:no-reply`,
14221439}));
14231440const waitForRoomEvent = vi.fn().mockImplementation(async (params) => {
1424-const sentBody = String(sendTextMessage.mock.calls.at(-1)?.[0]?.body ?? "");
1441+const sentBody = lastMockMessageBody(sendTextMessage, "sendTextMessage");
14251442const token = sentBody
14261443.replace("@sut:matrix-qa.test reply with only this exact marker: ", "")
14271444.replace("reply with only this exact marker: ", "");
@@ -1503,8 +1520,12 @@ describe("matrix live qa scenarios", () => {
15031520"@sut:matrix-qa.test",
15041521]);
15051522expect(mockObjectArg(sendTextMessage, "sendTextMessage").roomId).toBe("!main:matrix-qa.test");
1506-expect(sendTextMessage.mock.calls.at(1)?.[0]?.mentionUserIds).toEqual(["@sut:matrix-qa.test"]);
1507-expect(sendTextMessage.mock.calls.at(1)?.[0]?.roomId).toBe("!main:matrix-qa.test");
1523+expect(mockObjectArg(sendTextMessage, "sendTextMessage", 1).mentionUserIds).toEqual([
1524+"@sut:matrix-qa.test",
1525+]);
1526+expect(mockObjectArg(sendTextMessage, "sendTextMessage", 1).roomId).toBe(
1527+"!main:matrix-qa.test",
1528+);
15081529});
1509153015101531it("queues a Matrix trigger during restart before proving incremental sync continues", async () => {
@@ -1515,7 +1536,7 @@ describe("matrix live qa scenarios", () => {
15151536return String(params.body).includes("CATCHUP") ? "$catchup-trigger" : "$incremental-trigger";
15161537});
15171538const waitForRoomEvent = vi.fn().mockImplementation(async () => {
1518-const sentBody = String(sendTextMessage.mock.calls.at(-1)?.[0]?.body ?? "");
1539+const sentBody = lastMockMessageBody(sendTextMessage, "sendTextMessage");
15191540const token = sentBody.replace("@sut:matrix-qa.test reply with only this exact marker: ", "");
15201541callOrder.push(`wait:${token.includes("CATCHUP") ? "catchup" : "incremental"}`);
15211542return {
@@ -1612,7 +1633,7 @@ describe("matrix live qa scenarios", () => {
16121633return kind === "fresh" ? "$fresh-trigger" : "$first-trigger";
16131634});
16141635const waitForRoomEvent = vi.fn().mockImplementation(async () => {
1615-const sentBody = String(sendTextMessage.mock.calls.at(-1)?.[0]?.body ?? "");
1636+const sentBody = lastMockMessageBody(sendTextMessage, "sendTextMessage");
16161637const token = sentBody.replace("@sut:matrix-qa.test reply with only this exact marker: ", "");
16171638const kind = token.includes("REPLAY_DEDUPE_FRESH") ? "fresh" : "first";
16181639callOrder.push(`wait:${kind}`);
@@ -1722,7 +1743,7 @@ describe("matrix live qa scenarios", () => {
17221743return kind === "fresh" ? "$fresh-trigger" : "$first-trigger";
17231744});
17241745const waitForRoomEvent = vi.fn().mockImplementation(async () => {
1725-const sentBody = String(sendTextMessage.mock.calls.at(-1)?.[0]?.body ?? "");
1746+const sentBody = lastMockMessageBody(sendTextMessage, "sendTextMessage");
17261747const token = sentBody.replace(
17271748"@sut:matrix-qa.test reply with only this exact marker: ",
17281749"",
@@ -2155,7 +2176,7 @@ describe("matrix live qa scenarios", () => {
21552176return "$after-trigger";
21562177});
21572178const waitForRoomEvent = vi.fn().mockImplementation(async (params) => {
2158-const body = String(sendTextMessage.mock.calls.at(-1)?.[0]?.body ?? "");
2179+const body = lastMockMessageBody(sendTextMessage, "sendTextMessage");
21592180const token = body.replace("@sut:matrix-qa.test reply with only this exact marker: ", "");
21602181return {
21612182event: {
@@ -3271,7 +3292,9 @@ describe("matrix live qa scenarios", () => {
32713292expect(body).toMatch(
32723293/reply with exactly this two-line body and no extra text:\nMATRIX_QA_BLOCK_ONE_[A-F0-9]{8}\nMATRIX_QA_BLOCK_TWO_[A-F0-9]{8}$/,
32733294);
3274-expect(waitForRoomEvent.mock.calls.at(1)?.[0]?.since).toBe("driver-sync-block-one");
3295+expect(mockObjectArg(waitForRoomEvent, "waitForRoomEvent", 1).since).toBe(
3296+"driver-sync-block-one",
3297+);
32753298});
3276329932773300it("sends a real Matrix image attachment for image-understanding prompts", async () => {
@@ -3574,7 +3597,9 @@ describe("matrix live qa scenarios", () => {
35743597expect(mediaMessage?.kind).toBe(mediaCase.kind);
35753598expect(mediaMessage?.mentionUserIds).toEqual(["@sut:matrix-qa.test"]);
35763599}
3577-const firstReplyWait = waitForRoomEvent.mock.calls.at(1)?.[0];
3600+const firstReplyWait = mockObjectArg(waitForRoomEvent, "waitForRoomEvent", 1) as {
3601+predicate: (event: MatrixQaObservedEvent) => boolean;
3602+};
35783603const firstToken =
35793604mockMessageBody(sendMediaMessage, "sendMediaMessage").match(
35803605/MATRIX_QA_MEDIA_[A-Z]+_[A-Z0-9]+/,
@@ -4484,7 +4509,10 @@ describe("matrix live qa scenarios", () => {
44844509search: "",
44854510}),
44864511).toBe(false);
4487-const recoveryClientOptions = createMatrixQaE2eeScenarioClient.mock.calls.at(-1)?.[0];
4512+const recoveryClientOptions = lastMockObjectArg(
4513+createMatrixQaE2eeScenarioClient,
4514+"createMatrixQaE2eeScenarioClient",
4515+);
44884516expect(recoveryClientOptions?.accessToken).toBe("recovery-token");
44894517expect(recoveryClientOptions?.baseUrl).toBe("http://127.0.0.1:39877");
44904518expect(recoveryClientOptions?.deviceId).toBe("RECOVERYDEVICE");
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。