unbreak jest tests

pull/28788/head^2
Kegan Dougal 2023-01-19 11:15:08 +00:00
parent 186938d32a
commit 7c2dd7224f
3 changed files with 22 additions and 61 deletions

View File

@ -119,12 +119,10 @@ export class SlidingSyncManager {
public slidingSync: SlidingSync; public slidingSync: SlidingSync;
private client: MatrixClient; private client: MatrixClient;
private listIdToIndex: Record<string, number>;
private configureDefer: IDeferred<void>; private configureDefer: IDeferred<void>;
public constructor() { public constructor() {
this.listIdToIndex = {};
this.configureDefer = defer<void>(); this.configureDefer = defer<void>();
} }
@ -134,7 +132,6 @@ export class SlidingSyncManager {
public configure(client: MatrixClient, proxyUrl: string): SlidingSync { public configure(client: MatrixClient, proxyUrl: string): SlidingSync {
this.client = client; this.client = client;
this.listIdToIndex = {};
// by default use the encrypted subscription as that gets everything, which is a safer // by default use the encrypted subscription as that gets everything, which is a safer
// default than potentially missing member events. // default than potentially missing member events.
this.slidingSync = new SlidingSync( this.slidingSync = new SlidingSync(
@ -179,15 +176,6 @@ export class SlidingSyncManager {
return this.slidingSync; return this.slidingSync;
} }
public listIdForIndex(index: number): string | null {
for (const listId in this.listIdToIndex) {
if (this.listIdToIndex[listId] === index) {
return listId;
}
}
return null;
}
/** /**
* Ensure that this list is registered. * Ensure that this list is registered.
* @param listKey The list key to register * @param listKey The list key to register

View File

@ -84,7 +84,7 @@ describe("SlidingSyncManager", () => {
const batchSize = 10; const batchSize = 10;
mocked(slidingSync.setList).mockResolvedValue("yep"); mocked(slidingSync.setList).mockResolvedValue("yep");
mocked(slidingSync.setListRanges).mockResolvedValue("yep"); mocked(slidingSync.setListRanges).mockResolvedValue("yep");
mocked(slidingSync.getListData).mockImplementation((i) => { mocked(slidingSync.getListData).mockImplementation((key) => {
return { return {
joinedCount: 64, joinedCount: 64,
roomIndexToRoomId: {}, roomIndexToRoomId: {},
@ -106,7 +106,7 @@ describe("SlidingSyncManager", () => {
wantWindows.forEach((range, i) => { wantWindows.forEach((range, i) => {
if (i === 0) { if (i === 0) {
expect(slidingSync.setList).toBeCalledWith( expect(slidingSync.setList).toBeCalledWith(
manager.getOrAllocateListIndex(SlidingSyncManager.ListSearch), SlidingSyncManager.ListSearch,
expect.objectContaining({ expect.objectContaining({
ranges: [[0, batchSize - 1], range], ranges: [[0, batchSize - 1], range],
}), }),
@ -114,7 +114,7 @@ describe("SlidingSyncManager", () => {
return; return;
} }
expect(slidingSync.setListRanges).toBeCalledWith( expect(slidingSync.setListRanges).toBeCalledWith(
manager.getOrAllocateListIndex(SlidingSyncManager.ListSearch), SlidingSyncManager.ListSearch,
[[0, batchSize - 1], range], [[0, batchSize - 1], range],
); );
}); });
@ -123,7 +123,7 @@ describe("SlidingSyncManager", () => {
const gapMs = 1; const gapMs = 1;
const batchSize = 10; const batchSize = 10;
mocked(slidingSync.setList).mockResolvedValue("yep"); mocked(slidingSync.setList).mockResolvedValue("yep");
mocked(slidingSync.getListData).mockImplementation((i) => { mocked(slidingSync.getListData).mockImplementation((key) => {
return { return {
joinedCount: 0, joinedCount: 0,
roomIndexToRoomId: {}, roomIndexToRoomId: {},
@ -133,7 +133,7 @@ describe("SlidingSyncManager", () => {
expect(slidingSync.getListData).toBeCalledTimes(1); expect(slidingSync.getListData).toBeCalledTimes(1);
expect(slidingSync.setList).toBeCalledTimes(1); expect(slidingSync.setList).toBeCalledTimes(1);
expect(slidingSync.setList).toBeCalledWith( expect(slidingSync.setList).toBeCalledWith(
manager.getOrAllocateListIndex(SlidingSyncManager.ListSearch), SlidingSyncManager.ListSearch,
expect.objectContaining({ expect.objectContaining({
ranges: [ ranges: [
[0, batchSize - 1], [0, batchSize - 1],
@ -146,7 +146,7 @@ describe("SlidingSyncManager", () => {
const gapMs = 1; const gapMs = 1;
const batchSize = 10; const batchSize = 10;
mocked(slidingSync.setList).mockRejectedValue("narp"); mocked(slidingSync.setList).mockRejectedValue("narp");
mocked(slidingSync.getListData).mockImplementation((i) => { mocked(slidingSync.getListData).mockImplementation((key) => {
return { return {
joinedCount: 0, joinedCount: 0,
roomIndexToRoomId: {}, roomIndexToRoomId: {},
@ -156,7 +156,7 @@ describe("SlidingSyncManager", () => {
expect(slidingSync.getListData).toBeCalledTimes(1); expect(slidingSync.getListData).toBeCalledTimes(1);
expect(slidingSync.setList).toBeCalledTimes(1); expect(slidingSync.setList).toBeCalledTimes(1);
expect(slidingSync.setList).toBeCalledWith( expect(slidingSync.setList).toBeCalledWith(
manager.getOrAllocateListIndex(SlidingSyncManager.ListSearch), SlidingSyncManager.ListSearch,
expect.objectContaining({ expect.objectContaining({
ranges: [ ranges: [
[0, batchSize - 1], [0, batchSize - 1],

View File

@ -42,7 +42,6 @@ describe("SlidingRoomListStore", () => {
let context: TestSdkContext; let context: TestSdkContext;
let dis: MatrixDispatcher; let dis: MatrixDispatcher;
let activeSpace: string; let activeSpace: string;
let tagIdToIndex = {};
beforeEach(async () => { beforeEach(async () => {
context = new TestSdkContext(); context = new TestSdkContext();
@ -64,27 +63,6 @@ describe("SlidingRoomListStore", () => {
getRoomId: jest.fn(), getRoomId: jest.fn(),
}) as unknown as RoomViewStore, }) as unknown as RoomViewStore,
); );
// mock implementations to allow the store to map tag IDs to sliding sync list indexes and vice versa
let index = 0;
tagIdToIndex = {};
mocked(context._SlidingSyncManager.getOrAllocateListIndex).mockImplementation((listId: string): number => {
if (tagIdToIndex[listId] != null) {
return tagIdToIndex[listId];
}
tagIdToIndex[listId] = index;
index++;
return index;
});
mocked(context.slidingSyncManager.listIdForIndex).mockImplementation((i) => {
for (const tagId in tagIdToIndex) {
const j = tagIdToIndex[tagId];
if (i === j) {
return tagId;
}
}
return null;
});
mocked(context._SlidingSyncManager.ensureListRegistered).mockResolvedValue({ mocked(context._SlidingSyncManager.ensureListRegistered).mockResolvedValue({
ranges: [[0, 10]], ranges: [[0, 10]],
}); });
@ -108,7 +86,7 @@ describe("SlidingRoomListStore", () => {
await p; await p;
expect(context._SlidingSyncManager.ensureListRegistered).toHaveBeenCalledWith( expect(context._SlidingSyncManager.ensureListRegistered).toHaveBeenCalledWith(
tagIdToIndex[DefaultTagID.Untagged], DefaultTagID.Untagged,
{ {
filters: expect.objectContaining({ filters: expect.objectContaining({
spaces: [spaceRoomId], spaces: [spaceRoomId],
@ -127,7 +105,7 @@ describe("SlidingRoomListStore", () => {
await store.start(); // call onReady await store.start(); // call onReady
await p; await p;
expect(context._SlidingSyncManager.ensureListRegistered).toHaveBeenCalledWith( expect(context._SlidingSyncManager.ensureListRegistered).toHaveBeenCalledWith(
tagIdToIndex[DefaultTagID.Untagged], DefaultTagID.Untagged,
expect.objectContaining({ expect.objectContaining({
filters: expect.objectContaining({ filters: expect.objectContaining({
spaces: [spaceRoomId], spaces: [spaceRoomId],
@ -161,7 +139,7 @@ describe("SlidingRoomListStore", () => {
await p; await p;
expect(context._SlidingSyncManager.ensureListRegistered).toHaveBeenCalledWith( expect(context._SlidingSyncManager.ensureListRegistered).toHaveBeenCalledWith(
tagIdToIndex[DefaultTagID.Untagged], DefaultTagID.Untagged,
{ {
filters: expect.objectContaining({ filters: expect.objectContaining({
spaces: [spaceRoomId, subSpace1, subSpace2], spaces: [spaceRoomId, subSpace1, subSpace2],
@ -172,16 +150,15 @@ describe("SlidingRoomListStore", () => {
}); });
it("setTagSorting alters the 'sort' option in the list", async () => { it("setTagSorting alters the 'sort' option in the list", async () => {
mocked(context._SlidingSyncManager.getOrAllocateListIndex).mockReturnValue(0);
const tagId: TagID = "foo"; const tagId: TagID = "foo";
await store.setTagSorting(tagId, SortAlgorithm.Alphabetic); await store.setTagSorting(tagId, SortAlgorithm.Alphabetic);
expect(context._SlidingSyncManager.ensureListRegistered).toBeCalledWith(0, { expect(context._SlidingSyncManager.ensureListRegistered).toBeCalledWith(tagId, {
sort: SlidingSyncSortToFilter[SortAlgorithm.Alphabetic], sort: SlidingSyncSortToFilter[SortAlgorithm.Alphabetic],
}); });
expect(store.getTagSorting(tagId)).toEqual(SortAlgorithm.Alphabetic); expect(store.getTagSorting(tagId)).toEqual(SortAlgorithm.Alphabetic);
await store.setTagSorting(tagId, SortAlgorithm.Recent); await store.setTagSorting(tagId, SortAlgorithm.Recent);
expect(context._SlidingSyncManager.ensureListRegistered).toBeCalledWith(0, { expect(context._SlidingSyncManager.ensureListRegistered).toBeCalledWith(tagId, {
sort: SlidingSyncSortToFilter[SortAlgorithm.Recent], sort: SlidingSyncSortToFilter[SortAlgorithm.Recent],
}); });
expect(store.getTagSorting(tagId)).toEqual(SortAlgorithm.Recent); expect(store.getTagSorting(tagId)).toEqual(SortAlgorithm.Recent);
@ -189,27 +166,25 @@ describe("SlidingRoomListStore", () => {
it("getTagsForRoom gets the tags for the room", async () => { it("getTagsForRoom gets the tags for the room", async () => {
await store.start(); await store.start();
const untaggedIndex = context._SlidingSyncManager.getOrAllocateListIndex(DefaultTagID.Untagged);
const favIndex = context._SlidingSyncManager.getOrAllocateListIndex(DefaultTagID.Favourite);
const roomA = "!a:localhost"; const roomA = "!a:localhost";
const roomB = "!b:localhost"; const roomB = "!b:localhost";
const indexToListData = { const keyToListData = {
[untaggedIndex]: { [DefaultTagID.Untagged]: {
joinedCount: 10, joinedCount: 10,
roomIndexToRoomId: { roomIndexToRoomId: {
0: roomA, 0: roomA,
1: roomB, 1: roomB,
}, },
}, },
[favIndex]: { [DefaultTagID.Favourite]: {
joinedCount: 2, joinedCount: 2,
roomIndexToRoomId: { roomIndexToRoomId: {
0: roomB, 0: roomB,
}, },
}, },
}; };
mocked(context._SlidingSyncManager.slidingSync.getListData).mockImplementation((i: number) => { mocked(context._SlidingSyncManager.slidingSync.getListData).mockImplementation((key: string) => {
return indexToListData[i] || null; return keyToListData[key] || null;
}); });
expect(store.getTagsForRoom(new Room(roomA, context.client, context.client.getUserId()))).toEqual([ expect(store.getTagsForRoom(new Room(roomA, context.client, context.client.getUserId()))).toEqual([
@ -227,7 +202,6 @@ describe("SlidingRoomListStore", () => {
const roomB = "!b:localhost"; const roomB = "!b:localhost";
const roomC = "!c:localhost"; const roomC = "!c:localhost";
const tagId = DefaultTagID.Favourite; const tagId = DefaultTagID.Favourite;
const listIndex = context.slidingSyncManager.getOrAllocateListIndex(tagId);
const joinCount = 10; const joinCount = 10;
const roomIndexToRoomId = { const roomIndexToRoomId = {
// mixed to ensure we sort // mixed to ensure we sort
@ -252,7 +226,7 @@ describe("SlidingRoomListStore", () => {
return null; return null;
}); });
const p = untilEmission(store, LISTS_UPDATE_EVENT); const p = untilEmission(store, LISTS_UPDATE_EVENT);
context.slidingSyncManager.slidingSync.emit(SlidingSyncEvent.List, listIndex, joinCount, roomIndexToRoomId); context.slidingSyncManager.slidingSync.emit(SlidingSyncEvent.List, tagId, joinCount, roomIndexToRoomId);
await p; await p;
expect(store.getCount(tagId)).toEqual(joinCount); expect(store.getCount(tagId)).toEqual(joinCount);
expect(store.orderedLists[tagId]).toEqual(rooms); expect(store.orderedLists[tagId]).toEqual(rooms);
@ -265,7 +239,6 @@ describe("SlidingRoomListStore", () => {
const roomIdB = "!b:localhost"; const roomIdB = "!b:localhost";
const roomIdC = "!c:localhost"; const roomIdC = "!c:localhost";
const tagId = DefaultTagID.Favourite; const tagId = DefaultTagID.Favourite;
const listIndex = context.slidingSyncManager.getOrAllocateListIndex(tagId);
const joinCount = 10; const joinCount = 10;
const roomIndexToRoomId = { const roomIndexToRoomId = {
// mixed to ensure we sort // mixed to ensure we sort
@ -287,8 +260,8 @@ describe("SlidingRoomListStore", () => {
} }
return null; return null;
}); });
mocked(context._SlidingSyncManager.slidingSync.getListData).mockImplementation((i: number) => { mocked(context._SlidingSyncManager.slidingSync.getListData).mockImplementation((key: string) => {
if (i !== listIndex) { if (key !== tagId) {
return null; return null;
} }
return { return {
@ -297,7 +270,7 @@ describe("SlidingRoomListStore", () => {
}; };
}); });
let p = untilEmission(store, LISTS_UPDATE_EVENT); let p = untilEmission(store, LISTS_UPDATE_EVENT);
context.slidingSyncManager.slidingSync.emit(SlidingSyncEvent.List, listIndex, joinCount, roomIndexToRoomId); context.slidingSyncManager.slidingSync.emit(SlidingSyncEvent.List, tagId, joinCount, roomIndexToRoomId);
await p; await p;
expect(store.orderedLists[tagId]).toEqual([roomA, roomB, roomC]); expect(store.orderedLists[tagId]).toEqual([roomA, roomB, roomC]);
@ -310,7 +283,7 @@ describe("SlidingRoomListStore", () => {
roomIndexToRoomId[1] = roomIdA; roomIndexToRoomId[1] = roomIdA;
roomIndexToRoomId[2] = roomIdB; roomIndexToRoomId[2] = roomIdB;
p = untilEmission(store, LISTS_UPDATE_EVENT); p = untilEmission(store, LISTS_UPDATE_EVENT);
context.slidingSyncManager.slidingSync.emit(SlidingSyncEvent.List, listIndex, joinCount, roomIndexToRoomId); context.slidingSyncManager.slidingSync.emit(SlidingSyncEvent.List, tagId, joinCount, roomIndexToRoomId);
await p; await p;
// check that B didn't move and that A was put below B // check that B didn't move and that A was put below B