Skip to content

Commit

Permalink
BC-8452 - Add worker test (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax authored Dec 23, 2024
1 parent ed87499 commit a9b0669
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 66 deletions.
5 changes: 4 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ TLDRAW_WEBSOCKET_PORT=3399

X_API_ALLOWED_KEYS=randomString

NOT_AUTHENTICATED_REDIRECT_URL=http://localhost:4000/login
WORKER_TASK_DEBOUNCE=500
WORKER_MIN_MESSAGE_LIFETIME=1000

NOT_AUTHENTICATED_REDIRECT_URL=http://localhost:4000/login
118 changes: 53 additions & 65 deletions src/modules/server/api/test/websocket.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ describe('Websocket Api Test', () => {
return isSynced;
});

/* const waitUntilDocValueMatches = (ydoc: Doc, key: string, value: number): Promise<void> =>
promise.until(0, () => {
const result = ydoc.getMap().get(key);
const isMatch = result === value;
return isMatch;
}); */

describe('when clients have permission for room', () => {
describe('when two clients connect to the same doc before any changes', () => {
const setup = () => {
Expand All @@ -83,25 +75,30 @@ describe('Websocket Api Test', () => {
error: null,
});

const { ydoc: client1Doc } = createWsClient(room);
const { ydoc: client2Doc } = createWsClient(room);
const { ydoc: client1Doc, provider: provider1 } = createWsClient(room);
const { ydoc: client2Doc, provider: provider2 } = createWsClient(room);

return { client1Doc, client2Doc };
return { client1Doc, client2Doc, provider1, provider2 };
};

it('syncs doc changes of first client to second client', async () => {
const { client1Doc, client2Doc } = setup();
const { client1Doc, client2Doc, provider1, provider2 } = setup();

client1Doc.getMap().set('a', 1);

await waitUntilDocsEqual(client1Doc, client2Doc);

const result = client2Doc.getMap().get('a');
expect(result).toBe(1);

provider1.awareness.destroy();
provider2.awareness.destroy();
provider1.destroy();
provider2.destroy();
});

it('syncs subsequent doc changes of second client to first client', async () => {
const { client1Doc, client2Doc } = setup();
const { client1Doc, client2Doc, provider1, provider2 } = setup();

client1Doc.getMap().set('a', 1);
await waitUntilDocsEqual(client1Doc, client2Doc);
Expand All @@ -111,85 +108,74 @@ describe('Websocket Api Test', () => {

const result = client1Doc.getMap().get('a');
expect(result).toBe(2);
});

/* it('syncs nearly parallel doc changes of second client to first client', async () => {
// This test is instable
const { client1Doc, client2Doc } = setup();
client1Doc.getMap().set('a', 1);
client2Doc.getMap().set('a', 2);
await waitUntilDocValueMatches(client1Doc, 'a', 2);

const result = client1Doc.getMap().get('a');
expect(result).toBe(2);
}); */
provider1.awareness.destroy();
provider2.awareness.destroy();
provider1.destroy();
provider2.destroy();
});
});

describe('when two clients connect to the same doc one before and one after the changes', () => {
const setup = () => {
const randomString = Math.random().toString(36).substring(7);
const room = randomString;

authorizationService.hasPermission.mockResolvedValue({
authorizationService.hasPermission.mockResolvedValueOnce({
hasWriteAccess: true,
room: randomString,
userid: 'userId1',
error: null,
});
authorizationService.hasPermission.mockResolvedValueOnce({
hasWriteAccess: true,
room: randomString,
userid: 'userId',
userid: 'userId2',
error: null,
});

const { ydoc: client1Doc } = createWsClient(room);
const { ydoc: client1Doc, provider } = createWsClient(room);

return { client1Doc, room };
return { client1Doc, room, provider };
};

it('syncs doc changes of first client to second client', async () => {
const { client1Doc, room } = setup();
const { client1Doc, room, provider } = setup();

client1Doc.getMap().set('a', 1);

const { ydoc: client2Doc } = createWsClient(room);
const { ydoc: client2Doc, provider: provider2 } = createWsClient(room);
await waitUntilDocsEqual(client1Doc, client2Doc);

const result = client2Doc.getMap().get('a');
expect(result).toBe(1);

provider.awareness.destroy();
provider.destroy();
provider2.awareness.destroy();
provider2.destroy();
});

it('syncs subsequent doc changes of second client to first client', async () => {
const { client1Doc, room } = setup();
const { client1Doc, room, provider } = setup();

client1Doc.getMap().set('a', 1);

const { ydoc: client2Doc } = createWsClient(room);
const { ydoc: client2Doc, provider: provider2 } = createWsClient(room);
await waitUntilDocsEqual(client1Doc, client2Doc);

client2Doc.getMap().set('a', 2);
await waitUntilDocsEqual(client1Doc, client2Doc);

const result = client1Doc.getMap().get('a');
expect(result).toBe(2);
});

/* it('syncs nearly parallel doc changes of second client to first client', async () => {
// This test is instable
const { client1Doc, room } = setup();
client1Doc.getMap().set('a', 1);
const { ydoc: client2Doc } = createWsClient(room);
client2Doc.getMap().set('a', 2);
await waitUntilDocValueMatches(client1Doc, 'a', 2);

const result = client1Doc.getMap().get('a');
expect(result).toBe(2);
}); */
provider.awareness.destroy();
provider.destroy();
provider2.awareness.destroy();
provider2.destroy();
});
});

/* describe('when doc is only pesisted in storage and not in redis', () => {
// Need to implement this test
}); */
});

describe('when client has no permission for room', () => {
Expand All @@ -199,14 +185,14 @@ describe('Websocket Api Test', () => {
const room = randomString;

const errorResponse = ResponsePayloadBuilder.buildWithError(4401, 'Unauthorized');
authorizationService.hasPermission.mockResolvedValue(errorResponse);
authorizationService.hasPermission.mockResolvedValueOnce(errorResponse);

const { ydoc: client1Doc, provider } = createWsClient(room);

return { client1Doc, provider };
};

it('syncs doc changes of first client to second client', async () => {
it('returns unauthorized error', async () => {
const { provider } = setup();

let error: CloseEvent;
Expand All @@ -224,6 +210,9 @@ describe('Websocket Api Test', () => {
expect(error.reason).toBe('Unauthorized');
// @ts-ignore
expect(error.code).toBe(4401);

provider.awareness.destroy();
provider.destroy();
});
});

Expand All @@ -233,14 +222,14 @@ describe('Websocket Api Test', () => {
const room = randomString;

const response = ResponsePayloadBuilder.build(null, 'userId');
authorizationService.hasPermission.mockResolvedValue(response);
authorizationService.hasPermission.mockResolvedValueOnce(response);

const { ydoc: client1Doc, provider } = createWsClient(room);

return { client1Doc, provider };
};

it('syncs doc changes of first client to second client', async () => {
it('returns error', async () => {
const { provider } = setup();

let error: CloseEvent;
Expand All @@ -258,24 +247,24 @@ describe('Websocket Api Test', () => {
expect(error.reason).toBe('Missing room or userid');
// @ts-ignore
expect(error.code).toBe(1008);

provider.awareness.destroy();
provider.destroy();
});
});
});

/*describe('when openCallback catch an error', () => {
describe('when openCallback catch an error', () => {
const setup = () => {
const randomString = Math.random().toString(36).substring(7);
const room = randomString;

const response = ResponsePayloadBuilder.build(room, 'userId');
authorizationService.hasPermission.mockResolvedValue(response);
const { ydoc: client1Doc, provider } = createWsClient(room);

return { client1Doc, provider };
};

it('syncs doc changes of first client to second client', async () => {
it('returns internal server error', async () => {
const { provider } = setup();

let error: CloseEvent;
Expand All @@ -284,16 +273,15 @@ describe('Websocket Api Test', () => {
error = event as CloseEvent;
};
}
//spyOn(provider.ws, 'end').and.callThrough();

await promise.until(0, () => {
return error as unknown as boolean;
});

// @ts-ignore
//expect(error.reason).toBe('Internal Server Error');
expect(error.reason).toBe('Internal Server Error');
// @ts-ignore
expect(error.code).toBe(1011);
});
});*/
});
});
Loading

0 comments on commit a9b0669

Please sign in to comment.