From 8bfa301f2b28cd2f64dbc0074d7a5a21089af2aa Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Wed, 24 May 2023 23:27:08 +0100 Subject: [PATCH 1/9] Fix an existing test for editing messages in threads While attempting to test a new change, I discovered that the test "should allow edits to be added to thread timeline" did not actually fail if its assertions failed. Further, those assertions were incorrect. So this change fixes the test to create the thread, wait for it to be initialised, and then add events to it. This simplifies the flow and ensures the test fails if something unexpected happens. --- spec/unit/event-timeline-set.spec.ts | 34 +++++++++++++++++----------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/spec/unit/event-timeline-set.spec.ts b/spec/unit/event-timeline-set.spec.ts index a099aba0370..32f1a7d22e9 100644 --- a/spec/unit/event-timeline-set.spec.ts +++ b/spec/unit/event-timeline-set.spec.ts @@ -273,19 +273,27 @@ describe("EventTimelineSet", () => { sender, }); - jest.spyOn(client, "paginateEventTimeline").mockImplementation(async () => { - thread.timelineSet.getLiveTimeline().addEvent(threadReply, { toStartOfTimeline: true }); - return true; - }); - jest.spyOn(client, "relations").mockResolvedValue({ - events: [], - }); - - const thread = room.createThread(root.getId()!, root, [threadReply, editToThreadReply], false); - thread.once(RoomEvent.TimelineReset, () => { - const lastEvent = thread.timeline.at(-1)!; - expect(lastEvent.getContent().body).toBe(" * edit"); - }); + // Mock methods that call out to HTTP endpoints + jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); + jest.spyOn(client, "relations").mockResolvedValue({ events: [] }); + jest.spyOn(client, "fetchRoomEvent").mockResolvedValue({}); + + // Create a thread and wait for it to be initialised + const thread = room.createThread(root.getId()!, root, [], false); + await new Promise((res) => thread.once(RoomEvent.TimelineReset, () => res())); + + // When a message and an edit are added to the thread + await thread.addEvent(threadReply, false); + await thread.addEvent(editToThreadReply, false); + + // Then both events end up in the timeline + const lastEvent = thread.timeline.at(-1)!; + const secondLastEvent = thread.timeline.at(-2)!; + expect(lastEvent).toBe(editToThreadReply); + expect(secondLastEvent).toBe(threadReply); + + // And the first message has been edited + expect(secondLastEvent.getContent().body).toEqual("edit"); }); describe("non-room timeline", () => { From 35a8c5f10d921de4f5a1c0b65431f33a00896db6 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Wed, 24 May 2023 23:50:13 +0100 Subject: [PATCH 2/9] Move editing test into thread.spec.ts --- spec/unit/event-timeline-set.spec.ts | 95 +----------------------- spec/unit/models/thread.spec.ts | 104 ++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 97 deletions(-) diff --git a/spec/unit/event-timeline-set.spec.ts b/spec/unit/event-timeline-set.spec.ts index 32f1a7d22e9..7afee718967 100644 --- a/spec/unit/event-timeline-set.spec.ts +++ b/spec/unit/event-timeline-set.spec.ts @@ -24,13 +24,10 @@ import { MatrixClient, MatrixEvent, MatrixEventEvent, - RelationType, Room, - RoomEvent, } from "../../src"; -import { FeatureSupport, Thread } from "../../src/models/thread"; +import { Thread } from "../../src/models/thread"; import { ReEmitter } from "../../src/ReEmitter"; -import { eventMapperFor } from "../../src/event-mapper"; describe("EventTimelineSet", () => { const roomId = "!foo:bar"; @@ -206,96 +203,6 @@ describe("EventTimelineSet", () => { expect(liveTimeline.getEvents().length).toStrictEqual(0); }); - it("should allow edits to be added to thread timeline", async () => { - jest.spyOn(client, "supportsThreads").mockReturnValue(true); - jest.spyOn(client, "getEventMapper").mockReturnValue(eventMapperFor(client, {})); - Thread.hasServerSideSupport = FeatureSupport.Stable; - - const sender = "@alice:matrix.org"; - - const root = utils.mkEvent({ - event: true, - content: { - body: "Thread root", - }, - type: EventType.RoomMessage, - sender, - }); - room.addLiveEvents([root]); - - const threadReply = utils.mkEvent({ - event: true, - content: { - "body": "Thread reply", - "m.relates_to": { - event_id: root.getId()!, - rel_type: RelationType.Thread, - }, - }, - type: EventType.RoomMessage, - sender, - }); - - root.setUnsigned({ - "m.relations": { - [RelationType.Thread]: { - count: 1, - latest_event: { - content: threadReply.getContent(), - origin_server_ts: 5, - room_id: room.roomId, - sender, - type: EventType.RoomMessage, - event_id: threadReply.getId()!, - user_id: sender, - age: 1, - }, - current_user_participated: true, - }, - }, - }); - - const editToThreadReply = utils.mkEvent({ - event: true, - content: { - "body": " * edit", - "m.new_content": { - "body": "edit", - "msgtype": "m.text", - "org.matrix.msc1767.text": "edit", - }, - "m.relates_to": { - event_id: threadReply.getId()!, - rel_type: RelationType.Replace, - }, - }, - type: EventType.RoomMessage, - sender, - }); - - // Mock methods that call out to HTTP endpoints - jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); - jest.spyOn(client, "relations").mockResolvedValue({ events: [] }); - jest.spyOn(client, "fetchRoomEvent").mockResolvedValue({}); - - // Create a thread and wait for it to be initialised - const thread = room.createThread(root.getId()!, root, [], false); - await new Promise((res) => thread.once(RoomEvent.TimelineReset, () => res())); - - // When a message and an edit are added to the thread - await thread.addEvent(threadReply, false); - await thread.addEvent(editToThreadReply, false); - - // Then both events end up in the timeline - const lastEvent = thread.timeline.at(-1)!; - const secondLastEvent = thread.timeline.at(-2)!; - expect(lastEvent).toBe(editToThreadReply); - expect(secondLastEvent).toBe(threadReply); - - // And the first message has been edited - expect(secondLastEvent.getContent().body).toEqual("edit"); - }); - describe("non-room timeline", () => { it("Adds event to timeline", () => { const nonRoomEventTimelineSet = new EventTimelineSet( diff --git a/spec/unit/models/thread.spec.ts b/spec/unit/models/thread.spec.ts index bd17f00c8aa..cd0340cb199 100644 --- a/spec/unit/models/thread.spec.ts +++ b/spec/unit/models/thread.spec.ts @@ -17,12 +17,12 @@ limitations under the License. import { mocked } from "jest-mock"; import { MatrixClient, PendingEventOrdering } from "../../../src/client"; -import { Room } from "../../../src/models/room"; +import { Room, RoomEvent } from "../../../src/models/room"; import { Thread, THREAD_RELATION_TYPE, ThreadEvent, FeatureSupport } from "../../../src/models/thread"; import { mkThread } from "../../test-utils/thread"; import { TestClient } from "../../TestClient"; -import { emitPromise, mkMessage, mkReaction, mock } from "../../test-utils/test-utils"; -import { Direction, EventStatus, MatrixEvent } from "../../../src"; +import { emitPromise, mkEvent, mkMessage, mkReaction, mock } from "../../test-utils/test-utils"; +import { Direction, EventStatus, EventType, MatrixEvent, RelationType } from "../../../src"; import { ReceiptType } from "../../../src/@types/read_receipts"; import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../test-utils/client"; import { ReEmitter } from "../../../src/ReEmitter"; @@ -474,4 +474,102 @@ describe("Thread", () => { return client; } }); + + describe("Editing events", () => { + it("should allow edits to be added to thread timeline", async () => { + const roomId = "!foo:bar"; + const userA = "@alice:bar"; + const client = mock(MatrixClient, "MatrixClient"); + client.reEmitter = mock(ReEmitter, "ReEmitter"); + client.canSupport = new Map(); + const room = new Room(roomId, client, userA); + jest.spyOn(client, "supportsThreads").mockReturnValue(true); + jest.spyOn(client, "getEventMapper").mockReturnValue(eventMapperFor(client, {})); + Thread.hasServerSideSupport = FeatureSupport.Stable; + + const sender = "@alice:matrix.org"; + + const root = mkEvent({ + event: true, + content: { + body: "Thread root", + }, + type: EventType.RoomMessage, + sender, + }); + room.addLiveEvents([root]); + + const threadReply = mkEvent({ + event: true, + content: { + "body": "Thread reply", + "m.relates_to": { + event_id: root.getId()!, + rel_type: RelationType.Thread, + }, + }, + type: EventType.RoomMessage, + sender, + }); + + root.setUnsigned({ + "m.relations": { + [RelationType.Thread]: { + count: 1, + latest_event: { + content: threadReply.getContent(), + origin_server_ts: 5, + room_id: room.roomId, + sender, + type: EventType.RoomMessage, + event_id: threadReply.getId()!, + user_id: sender, + age: 1, + }, + current_user_participated: true, + }, + }, + }); + + const editToThreadReply = mkEvent({ + event: true, + content: { + "body": " * edit", + "m.new_content": { + "body": "edit", + "msgtype": "m.text", + "org.matrix.msc1767.text": "edit", + }, + "m.relates_to": { + event_id: threadReply.getId()!, + rel_type: RelationType.Replace, + }, + }, + type: EventType.RoomMessage, + sender, + }); + + // Mock methods that call out to HTTP endpoints + jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); + jest.spyOn(client, "relations").mockResolvedValue({ events: [] }); + jest.spyOn(client, "fetchRoomEvent").mockResolvedValue({}); + + // Create a thread and wait for it to be initialised + const thread = room.createThread(root.getId()!, root, [], false); + await new Promise((res) => thread.once(RoomEvent.TimelineReset, () => res())); + + // When a message and an edit are added to the thread + await thread.addEvent(threadReply, false); + await thread.addEvent(editToThreadReply, false); + + // Then both events end up in the timeline + const lastEvent = thread.timeline.at(-1)!; + const secondLastEvent = thread.timeline.at(-2)!; + expect(lastEvent).toBe(editToThreadReply); + expect(secondLastEvent).toBe(threadReply); + + // And the first message has been edited + expect(secondLastEvent.getContent().body).toEqual("edit"); + }); + }); }); From c27c56f1dddd701b303b3baac650dfd63a74ef57 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Wed, 24 May 2023 23:55:56 +0100 Subject: [PATCH 3/9] Isolate Thread global modification in beforeAll() --- spec/unit/models/thread.spec.ts | 180 +++++++++++++++++--------------- 1 file changed, 96 insertions(+), 84 deletions(-) diff --git a/spec/unit/models/thread.spec.ts b/spec/unit/models/thread.spec.ts index cd0340cb199..547a4dc7f02 100644 --- a/spec/unit/models/thread.spec.ts +++ b/spec/unit/models/thread.spec.ts @@ -476,100 +476,112 @@ describe("Thread", () => { }); describe("Editing events", () => { - it("should allow edits to be added to thread timeline", async () => { - const roomId = "!foo:bar"; - const userA = "@alice:bar"; - const client = mock(MatrixClient, "MatrixClient"); - client.reEmitter = mock(ReEmitter, "ReEmitter"); - client.canSupport = new Map(); - const room = new Room(roomId, client, userA); - jest.spyOn(client, "supportsThreads").mockReturnValue(true); - jest.spyOn(client, "getEventMapper").mockReturnValue(eventMapperFor(client, {})); - Thread.hasServerSideSupport = FeatureSupport.Stable; + describe("Given server support for threads", () => { + let previousThreadHasServerSideSupport: FeatureSupport; - const sender = "@alice:matrix.org"; - - const root = mkEvent({ - event: true, - content: { - body: "Thread root", - }, - type: EventType.RoomMessage, - sender, + beforeAll(() => { + previousThreadHasServerSideSupport = Thread.hasServerSideSupport; + Thread.hasServerSideSupport = FeatureSupport.Stable; }); - room.addLiveEvents([root]); - const threadReply = mkEvent({ - event: true, - content: { - "body": "Thread reply", - "m.relates_to": { - event_id: root.getId()!, - rel_type: RelationType.Thread, - }, - }, - type: EventType.RoomMessage, - sender, + afterAll(() => { + Thread.hasServerSideSupport = previousThreadHasServerSideSupport; }); - root.setUnsigned({ - "m.relations": { - [RelationType.Thread]: { - count: 1, - latest_event: { - content: threadReply.getContent(), - origin_server_ts: 5, - room_id: room.roomId, - sender, - type: EventType.RoomMessage, - event_id: threadReply.getId()!, - user_id: sender, - age: 1, + it("should allow edits to be added to thread timeline", async () => { + const roomId = "!foo:bar"; + const userA = "@alice:bar"; + const client = mock(MatrixClient, "MatrixClient"); + client.reEmitter = mock(ReEmitter, "ReEmitter"); + client.canSupport = new Map(); + const room = new Room(roomId, client, userA); + jest.spyOn(client, "supportsThreads").mockReturnValue(true); + jest.spyOn(client, "getEventMapper").mockReturnValue(eventMapperFor(client, {})); + + const sender = "@alice:matrix.org"; + + const root = mkEvent({ + event: true, + content: { + body: "Thread root", + }, + type: EventType.RoomMessage, + sender, + }); + room.addLiveEvents([root]); + + const threadReply = mkEvent({ + event: true, + content: { + "body": "Thread reply", + "m.relates_to": { + event_id: root.getId()!, + rel_type: RelationType.Thread, }, - current_user_participated: true, }, - }, - }); - - const editToThreadReply = mkEvent({ - event: true, - content: { - "body": " * edit", - "m.new_content": { - "body": "edit", - "msgtype": "m.text", - "org.matrix.msc1767.text": "edit", + type: EventType.RoomMessage, + sender, + }); + + root.setUnsigned({ + "m.relations": { + [RelationType.Thread]: { + count: 1, + latest_event: { + content: threadReply.getContent(), + origin_server_ts: 5, + room_id: room.roomId, + sender, + type: EventType.RoomMessage, + event_id: threadReply.getId()!, + user_id: sender, + age: 1, + }, + current_user_participated: true, + }, }, - "m.relates_to": { - event_id: threadReply.getId()!, - rel_type: RelationType.Replace, + }); + + const editToThreadReply = mkEvent({ + event: true, + content: { + "body": " * edit", + "m.new_content": { + "body": "edit", + "msgtype": "m.text", + "org.matrix.msc1767.text": "edit", + }, + "m.relates_to": { + event_id: threadReply.getId()!, + rel_type: RelationType.Replace, + }, }, - }, - type: EventType.RoomMessage, - sender, + type: EventType.RoomMessage, + sender, + }); + + // Mock methods that call out to HTTP endpoints + jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); + jest.spyOn(client, "relations").mockResolvedValue({ events: [] }); + jest.spyOn(client, "fetchRoomEvent").mockResolvedValue({}); + + // Create a thread and wait for it to be initialised + const thread = room.createThread(root.getId()!, root, [], false); + await new Promise((res) => thread.once(RoomEvent.TimelineReset, () => res())); + + // When a message and an edit are added to the thread + await thread.addEvent(threadReply, false); + await thread.addEvent(editToThreadReply, false); + + // Then both events end up in the timeline + const lastEvent = thread.timeline.at(-1)!; + const secondLastEvent = thread.timeline.at(-2)!; + expect(lastEvent).toBe(editToThreadReply); + expect(secondLastEvent).toBe(threadReply); + + // And the first message has been edited + expect(secondLastEvent.getContent().body).toEqual("edit"); }); - - // Mock methods that call out to HTTP endpoints - jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); - jest.spyOn(client, "relations").mockResolvedValue({ events: [] }); - jest.spyOn(client, "fetchRoomEvent").mockResolvedValue({}); - - // Create a thread and wait for it to be initialised - const thread = room.createThread(root.getId()!, root, [], false); - await new Promise((res) => thread.once(RoomEvent.TimelineReset, () => res())); - - // When a message and an edit are added to the thread - await thread.addEvent(threadReply, false); - await thread.addEvent(editToThreadReply, false); - - // Then both events end up in the timeline - const lastEvent = thread.timeline.at(-1)!; - const secondLastEvent = thread.timeline.at(-2)!; - expect(lastEvent).toBe(editToThreadReply); - expect(secondLastEvent).toBe(threadReply); - - // And the first message has been edited - expect(secondLastEvent.getContent().body).toEqual("edit"); }); }); }); From 22504d598322d5702d66bea03cf6798e2b86110b Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Wed, 24 May 2023 23:58:12 +0100 Subject: [PATCH 4/9] Delete unneeded setUnsigned call --- spec/unit/models/thread.spec.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/spec/unit/models/thread.spec.ts b/spec/unit/models/thread.spec.ts index 547a4dc7f02..1cb3c6c3c08 100644 --- a/spec/unit/models/thread.spec.ts +++ b/spec/unit/models/thread.spec.ts @@ -523,25 +523,6 @@ describe("Thread", () => { sender, }); - root.setUnsigned({ - "m.relations": { - [RelationType.Thread]: { - count: 1, - latest_event: { - content: threadReply.getContent(), - origin_server_ts: 5, - room_id: room.roomId, - sender, - type: EventType.RoomMessage, - event_id: threadReply.getId()!, - user_id: sender, - age: 1, - }, - current_user_participated: true, - }, - }, - }); - const editToThreadReply = mkEvent({ event: true, content: { From 5dd1cba8d72bce716cc8112f24b47d4bb433ad82 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Thu, 25 May 2023 00:22:56 +0100 Subject: [PATCH 5/9] Use standard message-creation methods --- spec/test-utils/test-utils.ts | 31 +++++++++++++++++++++ spec/unit/models/thread.spec.ts | 48 +++++++-------------------------- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/spec/test-utils/test-utils.ts b/spec/test-utils/test-utils.ts index ff41b35960d..dab7dfc9b85 100644 --- a/spec/test-utils/test-utils.ts +++ b/spec/test-utils/test-utils.ts @@ -370,6 +370,37 @@ export function mkReaction( ); } +export function mkEdit( + target: MatrixEvent, + client: MatrixClient, + userId: string, + roomId: string, + msg?: string, + ts?: number, +) { + msg = msg ?? `Edit of ${target.getId()}`; + return mkEvent( + { + event: true, + type: EventType.RoomMessage, + user: userId, + room: roomId, + content: { + "body": `* ${msg}`, + "m.new_content": { + body: msg, + }, + "m.relates_to": { + rel_type: RelationType.Replace, + event_id: target.getId()!, + }, + }, + ts, + }, + client, + ); +} + /** * A mock implementation of webstorage */ diff --git a/spec/unit/models/thread.spec.ts b/spec/unit/models/thread.spec.ts index 1cb3c6c3c08..b41b326f684 100644 --- a/spec/unit/models/thread.spec.ts +++ b/spec/unit/models/thread.spec.ts @@ -19,10 +19,10 @@ import { mocked } from "jest-mock"; import { MatrixClient, PendingEventOrdering } from "../../../src/client"; import { Room, RoomEvent } from "../../../src/models/room"; import { Thread, THREAD_RELATION_TYPE, ThreadEvent, FeatureSupport } from "../../../src/models/thread"; -import { mkThread } from "../../test-utils/thread"; +import { makeThreadEvent, mkThread } from "../../test-utils/thread"; import { TestClient } from "../../TestClient"; -import { emitPromise, mkEvent, mkMessage, mkReaction, mock } from "../../test-utils/test-utils"; -import { Direction, EventStatus, EventType, MatrixEvent, RelationType } from "../../../src"; +import { emitPromise, mkEdit, mkMessage, mkReaction, mock } from "../../test-utils/test-utils"; +import { Direction, EventStatus, MatrixEvent } from "../../../src"; import { ReceiptType } from "../../../src/@types/read_receipts"; import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../test-utils/client"; import { ReEmitter } from "../../../src/ReEmitter"; @@ -500,46 +500,18 @@ describe("Thread", () => { const sender = "@alice:matrix.org"; - const root = mkEvent({ - event: true, - content: { - body: "Thread root", - }, - type: EventType.RoomMessage, - sender, - }); + const root = mkMessage({ event: true, user: sender, msg: "Thread root" }); room.addLiveEvents([root]); - const threadReply = mkEvent({ + const threadReply = makeThreadEvent({ event: true, - content: { - "body": "Thread reply", - "m.relates_to": { - event_id: root.getId()!, - rel_type: RelationType.Thread, - }, - }, - type: EventType.RoomMessage, - sender, + user: sender, + msg: "Thread reply", + rootEventId: root.getId(), + replyToEventId: root.getId(), }); - const editToThreadReply = mkEvent({ - event: true, - content: { - "body": " * edit", - "m.new_content": { - "body": "edit", - "msgtype": "m.text", - "org.matrix.msc1767.text": "edit", - }, - "m.relates_to": { - event_id: threadReply.getId()!, - rel_type: RelationType.Replace, - }, - }, - type: EventType.RoomMessage, - sender, - }); + const editToThreadReply = mkEdit(threadReply, client, sender, room.roomId, "edit"); // Mock methods that call out to HTTP endpoints jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); From 6fd1f172603b2e6615f29e4a6c8f02eef03cf670 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Thu, 25 May 2023 00:24:36 +0100 Subject: [PATCH 6/9] Rename event variables --- spec/unit/models/thread.spec.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/unit/models/thread.spec.ts b/spec/unit/models/thread.spec.ts index b41b326f684..9d690b0f6b3 100644 --- a/spec/unit/models/thread.spec.ts +++ b/spec/unit/models/thread.spec.ts @@ -503,7 +503,7 @@ describe("Thread", () => { const root = mkMessage({ event: true, user: sender, msg: "Thread root" }); room.addLiveEvents([root]); - const threadReply = makeThreadEvent({ + const messageToEdit = makeThreadEvent({ event: true, user: sender, msg: "Thread reply", @@ -511,7 +511,7 @@ describe("Thread", () => { replyToEventId: root.getId(), }); - const editToThreadReply = mkEdit(threadReply, client, sender, room.roomId, "edit"); + const editEvent = mkEdit(messageToEdit, client, sender, room.roomId, "edit"); // Mock methods that call out to HTTP endpoints jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); @@ -523,14 +523,14 @@ describe("Thread", () => { await new Promise((res) => thread.once(RoomEvent.TimelineReset, () => res())); // When a message and an edit are added to the thread - await thread.addEvent(threadReply, false); - await thread.addEvent(editToThreadReply, false); + await thread.addEvent(messageToEdit, false); + await thread.addEvent(editEvent, false); // Then both events end up in the timeline const lastEvent = thread.timeline.at(-1)!; const secondLastEvent = thread.timeline.at(-2)!; - expect(lastEvent).toBe(editToThreadReply); - expect(secondLastEvent).toBe(threadReply); + expect(lastEvent).toBe(editEvent); + expect(secondLastEvent).toBe(messageToEdit); // And the first message has been edited expect(secondLastEvent.getContent().body).toEqual("edit"); From 4539a21f95e1599482b61d3d795c28d0a74a2540 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Thu, 25 May 2023 00:25:50 +0100 Subject: [PATCH 7/9] Rename sender->user --- spec/unit/models/thread.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/unit/models/thread.spec.ts b/spec/unit/models/thread.spec.ts index 9d690b0f6b3..25c7a9c99e7 100644 --- a/spec/unit/models/thread.spec.ts +++ b/spec/unit/models/thread.spec.ts @@ -498,20 +498,20 @@ describe("Thread", () => { jest.spyOn(client, "supportsThreads").mockReturnValue(true); jest.spyOn(client, "getEventMapper").mockReturnValue(eventMapperFor(client, {})); - const sender = "@alice:matrix.org"; + const user = "@alice:matrix.org"; - const root = mkMessage({ event: true, user: sender, msg: "Thread root" }); + const root = mkMessage({ event: true, user, msg: "Thread root" }); room.addLiveEvents([root]); const messageToEdit = makeThreadEvent({ event: true, - user: sender, + user, msg: "Thread reply", rootEventId: root.getId(), replyToEventId: root.getId(), }); - const editEvent = mkEdit(messageToEdit, client, sender, room.roomId, "edit"); + const editEvent = mkEdit(messageToEdit, client, user, room.roomId, "edit"); // Mock methods that call out to HTTP endpoints jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); From b451063b723eb52e861545233adb5b11a6e136dd Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Thu, 25 May 2023 00:27:06 +0100 Subject: [PATCH 8/9] Remove unneeded variables --- spec/unit/models/thread.spec.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/spec/unit/models/thread.spec.ts b/spec/unit/models/thread.spec.ts index 25c7a9c99e7..d3a345df388 100644 --- a/spec/unit/models/thread.spec.ts +++ b/spec/unit/models/thread.spec.ts @@ -489,17 +489,14 @@ describe("Thread", () => { }); it("should allow edits to be added to thread timeline", async () => { - const roomId = "!foo:bar"; - const userA = "@alice:bar"; + const user = "@alice:matrix.org"; const client = mock(MatrixClient, "MatrixClient"); client.reEmitter = mock(ReEmitter, "ReEmitter"); client.canSupport = new Map(); - const room = new Room(roomId, client, userA); + const room = new Room("!foo:bar", client, user); jest.spyOn(client, "supportsThreads").mockReturnValue(true); jest.spyOn(client, "getEventMapper").mockReturnValue(eventMapperFor(client, {})); - const user = "@alice:matrix.org"; - const root = mkMessage({ event: true, user, msg: "Thread root" }); room.addLiveEvents([root]); From 920df86055062e267ee55030834f8201994ef917 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Thu, 25 May 2023 00:45:03 +0100 Subject: [PATCH 9/9] Extract distractions into functions --- spec/unit/models/thread.spec.ts | 85 ++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 28 deletions(-) diff --git a/spec/unit/models/thread.spec.ts b/spec/unit/models/thread.spec.ts index d3a345df388..b5037a5d2e9 100644 --- a/spec/unit/models/thread.spec.ts +++ b/spec/unit/models/thread.spec.ts @@ -489,37 +489,15 @@ describe("Thread", () => { }); it("should allow edits to be added to thread timeline", async () => { + // Given a thread + const client = createClient(); const user = "@alice:matrix.org"; - const client = mock(MatrixClient, "MatrixClient"); - client.reEmitter = mock(ReEmitter, "ReEmitter"); - client.canSupport = new Map(); - const room = new Room("!foo:bar", client, user); - jest.spyOn(client, "supportsThreads").mockReturnValue(true); - jest.spyOn(client, "getEventMapper").mockReturnValue(eventMapperFor(client, {})); - - const root = mkMessage({ event: true, user, msg: "Thread root" }); - room.addLiveEvents([root]); - - const messageToEdit = makeThreadEvent({ - event: true, - user, - msg: "Thread reply", - rootEventId: root.getId(), - replyToEventId: root.getId(), - }); - - const editEvent = mkEdit(messageToEdit, client, user, room.roomId, "edit"); - - // Mock methods that call out to HTTP endpoints - jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); - jest.spyOn(client, "relations").mockResolvedValue({ events: [] }); - jest.spyOn(client, "fetchRoomEvent").mockResolvedValue({}); - - // Create a thread and wait for it to be initialised - const thread = room.createThread(root.getId()!, root, [], false); - await new Promise((res) => thread.once(RoomEvent.TimelineReset, () => res())); + const room = "!room:z"; + const thread = await createThread(client, user, room); // When a message and an edit are added to the thread + const messageToEdit = createThreadMessage(thread.id, user, room, "Thread reply"); + const editEvent = mkEdit(messageToEdit, client, user, room, "edit"); await thread.addEvent(messageToEdit, false); await thread.addEvent(editEvent, false); @@ -535,3 +513,54 @@ describe("Thread", () => { }); }); }); + +/** + * Create a message event that lives in a thread + */ +function createThreadMessage(threadId: string, user: string, room: string, msg: string): MatrixEvent { + return makeThreadEvent({ + event: true, + user, + room, + msg, + rootEventId: threadId, + replyToEventId: threadId, + }); +} + +/** + * Create a thread and wait for it to be properly initialised (so you can safely + * add events to it and expect them to appear in the timeline. + */ +async function createThread(client: MatrixClient, user: string, roomId: string): Promise { + const root = mkMessage({ event: true, user, room: roomId, msg: "Thread root" }); + + const room = new Room(roomId, client, "@roomcreator:x"); + room.addLiveEvents([root]); + + // Create the thread and wait for it to be initialised + const thread = room.createThread(root.getId()!, root, [], false); + await new Promise((res) => thread.once(RoomEvent.TimelineReset, () => res())); + + return thread; +} + +/** + * Create a MatrixClient that supports threads and has all the methods used when + * creating a thread that call out to HTTP endpoints mocked out. + */ +function createClient(): MatrixClient { + const client = mock(MatrixClient, "MatrixClient"); + client.reEmitter = mock(ReEmitter, "ReEmitter"); + client.canSupport = new Map(); + + jest.spyOn(client, "supportsThreads").mockReturnValue(true); + jest.spyOn(client, "getEventMapper").mockReturnValue(eventMapperFor(client, {})); + + // Mock methods that call out to HTTP endpoints + jest.spyOn(client, "paginateEventTimeline").mockResolvedValue(true); + jest.spyOn(client, "relations").mockResolvedValue({ events: [] }); + jest.spyOn(client, "fetchRoomEvent").mockResolvedValue({}); + + return client; +}