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: {