From 80014e8fb8a0321819a220819ed0a9f93b0dc495 Mon Sep 17 00:00:00 2001 From: Enmanuel Date: Sun, 8 Mar 2026 18:06:05 +0000 Subject: [PATCH] fix(e2e): obtener room ID desde URL en startThreadOnLastMessage y waitForThreadReplyViaSdk Corrige dos bugs en los fixtures de Playwright: 1. startThreadOnLastMessage usaba client.getRooms()[0] (el primer room unido) en lugar del room activo, causando que el mensaje threaded se enviara al room equivocado. Ahora lee el room ID de window.location.hash (#/room/!xxx:server), con resolucion de alias si la URL contiene un alias en lugar de un ID. 2. waitForThreadReplyViaSdk iteraba todos los rooms unidos, lo que podia devolver falsos positivos de otros rooms. Ahora esta acotado al room de la URL actual, con logica de fallback para aliases canonicos y alternativos. --- e2e/fixtures/matrix-room.ts | 47 ++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/e2e/fixtures/matrix-room.ts b/e2e/fixtures/matrix-room.ts index 616f223..e1a6ee4 100644 --- a/e2e/fixtures/matrix-room.ts +++ b/e2e/fixtures/matrix-room.ts @@ -301,17 +301,26 @@ export async function startThreadOnLastMessage(page: Page) { await dismissToasts(page); // Obtener el event ID del ultimo mensaje y el room ID via el SDK de Element - const threadInfo = await page.evaluate(() => { + const threadInfo = await page.evaluate(async () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const client = (window as any).mxMatrixClientPeg?.get?.(); if (!client) throw new Error("Matrix client no disponible en window"); - // Obtener el room actual visible - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const dis = (window as any).dis; - const roomId = client.getRooms() - .filter((r: { getMyMembership: () => string }) => r.getMyMembership() === "join") - .map((r: { roomId: string }) => r.roomId)[0]; + // Obtener el room actual desde la URL (mas fiable que getRooms()[0]) + const hash = window.location.hash; // e.g. "#/room/!xxx:server" or "#/room/#alias:server" + const match = hash.match(/#\/room\/([^?/]+)/); + if (!match) throw new Error(`No se pudo obtener room ID de la URL: ${hash}`); + const roomIdOrAlias = decodeURIComponent(match[1]); + + let roomId: string; + if (roomIdOrAlias.startsWith("!")) { + roomId = roomIdOrAlias; + } else { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const resolved = await (client as any).getRoomIdForAlias(roomIdOrAlias); + if (!resolved?.room_id) throw new Error(`No se pudo resolver alias: ${roomIdOrAlias}`); + roomId = resolved.room_id; + } if (!roomId) throw new Error("No hay room activo"); @@ -526,11 +535,33 @@ export async function waitForThreadReplyViaSdk( const client = (window as any).mxMatrixClientPeg?.get?.(); if (!client) return null; + // Scoped to current room only (via URL) to avoid false positives + const hash = window.location.hash; + const match = hash.match(/#\/room\/([^?/]+)/); + const roomIdOrAlias = match ? decodeURIComponent(match[1]) : null; + const rooms = client.getRooms().filter( - (r: { getMyMembership: () => string }) => r.getMyMembership() === "join" + (r: { getMyMembership: () => string; roomId: string }) => { + if (r.getMyMembership() !== "join") return false; + if (roomIdOrAlias) { + return r.roomId === roomIdOrAlias || + r.roomId === roomIdOrAlias; // alias resolution handled below + } + return true; + } ); for (const room of rooms) { + // Skip rooms that don't match the current URL room + if (roomIdOrAlias && !roomIdOrAlias.startsWith("!")) { + // For aliases, check if the room has this alias + const aliases = room.getAltAliases?.() || []; + const canonicalAlias = room.getCanonicalAlias?.(); + if (canonicalAlias !== roomIdOrAlias && !aliases.includes(roomIdOrAlias)) { + continue; + } + } + const timeline = room.getLiveTimeline().getEvents(); // Buscar eventos que sean respuestas de thread (m.relates_to.rel_type === "m.thread") const threadReplies = timeline.filter((e: {