1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

Revert "Revert "Factor out utils in redaction tests""

This reverts commit 2525c82049.
This commit is contained in:
Andy Balaam
2023-11-07 13:44:50 +00:00
parent a3762c8e22
commit 304c2b12bf

View File

@ -72,13 +72,7 @@ describe("MatrixEvent", () => {
describe("redaction", () => { describe("redaction", () => {
it("should prune clearEvent when being redacted", () => { it("should prune clearEvent when being redacted", () => {
const ev = new MatrixEvent({ const ev = createEvent("$event1:server", "Test");
type: "m.room.message",
content: {
body: "Test",
},
event_id: "$event1:server",
});
expect(ev.getContent().body).toBe("Test"); expect(ev.getContent().body).toBe("Test");
expect(ev.getWireContent().body).toBe("Test"); expect(ev.getWireContent().body).toBe("Test");
@ -89,10 +83,7 @@ describe("MatrixEvent", () => {
const mockClient = {} as unknown as MockedObject<MatrixClient>; const mockClient = {} as unknown as MockedObject<MatrixClient>;
const room = new Room("!roomid:e.xyz", mockClient, "myname"); const room = new Room("!roomid:e.xyz", mockClient, "myname");
const redaction = new MatrixEvent({ const redaction = createRedaction(ev.getId()!);
type: "m.room.redaction",
redacts: ev.getId(),
});
ev.makeRedacted(redaction, room); ev.makeRedacted(redaction, room);
expect(ev.getContent().body).toBeUndefined(); expect(ev.getContent().body).toBeUndefined();
@ -102,82 +93,85 @@ describe("MatrixEvent", () => {
it("should remain in the main timeline when redacted", async () => { it("should remain in the main timeline when redacted", async () => {
// Given an event in the main timeline // Given an event in the main timeline
const mockClient = { const mockClient = createMockClient();
supportsThreads: jest.fn().mockReturnValue(true),
decryptEventIfNeeded: jest.fn().mockReturnThis(),
getUserId: jest.fn().mockReturnValue("@user:server"),
} as unknown as MockedObject<MatrixClient>;
const room = new Room("!roomid:e.xyz", mockClient, "myname"); const room = new Room("!roomid:e.xyz", mockClient, "myname");
const ev = new MatrixEvent({ const ev = createEvent("$event1:server");
type: "m.room.message",
content: {
body: "Test",
},
event_id: "$event1:server",
});
await room.addLiveEvents([ev]); await room.addLiveEvents([ev]);
await room.createThreadsTimelineSets(); await room.createThreadsTimelineSets();
expect(ev.threadRootId).toBeUndefined(); expect(ev.threadRootId).toBeUndefined();
expect(mainTimelineLiveEventIds(room)).toEqual(["$event1:server"]); expect(mainTimelineLiveEventIds(room)).toEqual([ev.getId()]);
// When I redact it // When I redact it
const redaction = new MatrixEvent({ const redaction = createRedaction(ev.getId()!);
type: "m.room.redaction",
redacts: ev.getId(),
});
ev.makeRedacted(redaction, room); ev.makeRedacted(redaction, room);
// Then it remains in the main timeline // Then it remains in the main timeline
expect(ev.threadRootId).toBeUndefined(); expect(ev.threadRootId).toBeUndefined();
expect(mainTimelineLiveEventIds(room)).toEqual(["$event1:server"]); expect(mainTimelineLiveEventIds(room)).toEqual([ev.getId()]);
}); });
it("should move into the main timeline when redacted", async () => { it("should move into the main timeline when redacted", async () => {
// Given an event in a thread // Given an event in a thread
const mockClient = { const mockClient = createMockClient();
supportsThreads: jest.fn().mockReturnValue(true),
decryptEventIfNeeded: jest.fn().mockReturnThis(),
getUserId: jest.fn().mockReturnValue("@user:server"),
} as unknown as MockedObject<MatrixClient>;
const room = new Room("!roomid:e.xyz", mockClient, "myname"); const room = new Room("!roomid:e.xyz", mockClient, "myname");
const threadRoot = new MatrixEvent({ const threadRoot = createEvent("$threadroot:server");
type: "m.room.message", const ev = createThreadedEvent("$event1:server", threadRoot.getId()!);
content: {
body: "threadRoot",
},
event_id: "$threadroot:server",
});
const ev = new MatrixEvent({
type: "m.room.message",
content: {
"body": "Test",
"m.relates_to": {
rel_type: THREAD_RELATION_TYPE.name,
event_id: "$threadroot:server",
},
},
event_id: "$event1:server",
});
await room.addLiveEvents([threadRoot, ev]); await room.addLiveEvents([threadRoot, ev]);
await room.createThreadsTimelineSets(); await room.createThreadsTimelineSets();
expect(ev.threadRootId).toEqual("$threadroot:server"); expect(ev.threadRootId).toEqual(threadRoot.getId());
expect(mainTimelineLiveEventIds(room)).toEqual(["$threadroot:server"]); expect(mainTimelineLiveEventIds(room)).toEqual([threadRoot.getId()]);
expect(threadLiveEventIds(room, 0)).toEqual(["$threadroot:server", "$event1:server"]); expect(threadLiveEventIds(room, 0)).toEqual([threadRoot.getId(), ev.getId()]);
// When I redact it // When I redact it
const redaction = new MatrixEvent({ const redaction = createRedaction(ev.getId()!);
type: "m.room.redaction",
redacts: ev.getId(),
});
ev.makeRedacted(redaction, room); ev.makeRedacted(redaction, room);
// Then it disappears from the thread and appears in the main timeline // Then it disappears from the thread and appears in the main timeline
expect(ev.threadRootId).toBeUndefined(); expect(ev.threadRootId).toBeUndefined();
expect(mainTimelineLiveEventIds(room)).toEqual(["$threadroot:server", "$event1:server"]); expect(mainTimelineLiveEventIds(room)).toEqual([threadRoot.getId(), ev.getId()]);
expect(threadLiveEventIds(room, 0)).not.toContain("$event1:server"); expect(threadLiveEventIds(room, 0)).not.toContain(ev.getId());
}); });
function createMockClient(): MatrixClient {
return {
supportsThreads: jest.fn().mockReturnValue(true),
decryptEventIfNeeded: jest.fn().mockReturnThis(),
getUserId: jest.fn().mockReturnValue("@user:server"),
} as unknown as MockedObject<MatrixClient>;
}
function createEvent(eventId: string, body?: string): MatrixEvent {
return new MatrixEvent({
type: "m.room.message",
content: {
body: body ?? eventId,
},
event_id: eventId,
});
}
function createThreadedEvent(eventId: string, threadRootId: string): MatrixEvent {
return new MatrixEvent({
type: "m.room.message",
content: {
"body": eventId,
"m.relates_to": {
rel_type: THREAD_RELATION_TYPE.name,
event_id: threadRootId,
},
},
event_id: eventId,
});
}
function createRedaction(redactedEventid: string): MatrixEvent {
return new MatrixEvent({
type: "m.room.redaction",
redacts: redactedEventid,
});
}
}); });
describe("applyVisibilityEvent", () => { describe("applyVisibilityEvent", () => {