All files / rpc-server/src/__tests__/json-crdt-server presence.ts

98.48% Statements 65/66
66.66% Branches 2/3
94.73% Functions 18/19
98.27% Lines 57/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 1321x     4x   1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x             1x 1x                         1x     1x 1x 1x 1x 1x 1x   1x             1x 1x 1x 1x   1x 1x                         1x 1x     1x 1x 1x 1x 1x 2x   1x             1x 1x 1x 1x 1x     1x 1x 1x     1x 1x 1x 1x             1x 1x 2x   1x 1x 1x           1x          
import {tick, until} from 'thingies';
import type {ApiTestSetup, JsonCrdtTestSetup} from '../../__demos__/json-crdt-server/__tests__/setup';
 
const roomId = () => 'room-' + Math.random().toString(36).slice(2);
 
export const runPresenceTests = (_setup: ApiTestSetup, params: {staticOnly?: true} = {}) => {
  const setup = _setup as JsonCrdtTestSetup;
 
  describe('presence', () => {
    Eif (!params.staticOnly) {
      test('can subscribe and receive published presence entries', async () => {
        const {call, call$, stop} = await setup();
        const emits: any[] = [];
        const room = roomId();
        call$('presence.listen', {room}).subscribe((res) => {
          emits.push(res);
        });
        await call('presence.update', {
          room,
          id: 'user-1',
          data: {
            hello: 'world',
          },
        });
        await until(() => emits.length === 1);
        expect(emits[0]).toMatchObject({
          time: expect.any(Number),
          entries: [
            {
              id: 'user-1',
              lastSeen: expect.any(Number),
              validUntil: expect.any(Number),
              data: {
                hello: 'world',
              },
            },
          ],
        });
        stop();
      });
 
      test('can receive an existing record when subscribing after it was created', async () => {
        const {call, call$, stop} = await setup();
        const emits: any[] = [];
        const room = roomId();
        const subscription = call$('presence.listen', {room}).subscribe((res) => {
          emits.push(res);
        });
        await call('presence.update', {
          room,
          id: 'user-1',
          data: {
            hello: 'world',
          },
        });
        await until(() => emits.length === 1);
        const emits2: any[] = [];
        call$('presence.listen', {room}).subscribe((res) => {
          emits2.push(res);
        });
        await until(() => emits2.length === 1);
        expect(emits2[0]).toMatchObject({
          time: expect.any(Number),
          entries: [
            {
              id: 'user-1',
              lastSeen: expect.any(Number),
              validUntil: expect.any(Number),
              data: {
                hello: 'world',
              },
            },
          ],
        });
        subscription.unsubscribe();
        stop();
      });
 
      test('can remove existing entries', async () => {
        const {call, call$, stop} = await setup();
        const emits: any[] = [];
        const room = roomId();
        call$('presence.listen', {room}).subscribe((res) => {
          emits.push(res);
        });
        await call('presence.update', {
          room,
          id: 'user-1',
          data: {
            hello: 'world',
          },
        });
        await until(() => emits.length === 1);
        await call('presence.remove', {room, id: 'user-1'});
        await until(() => emits.length === 2);
        const emits2: any[] = [];
        call$('presence.listen', {room}).subscribe((res) => {
          emits2.push(res);
        });
        await tick(50);
        expect(emits2.length).toBe(0);
        stop();
      });
 
      test('emits entry deletion messages', async () => {
        const {call, call$, stop} = await setup();
        const room = roomId();
        await call('presence.update', {
          room,
          id: 'user-1',
          data: {
            hello: 'world',
          },
        });
        const emits: any[] = [];
        call$('presence.listen', {room}).subscribe((res) => {
          emits.push(res);
        });
        await call('presence.remove', {room, id: 'user-1'});
        await until(() => emits.length === 2);
        expect(emits[1].entries[0]).toMatchObject({
          id: 'user-1',
          lastSeen: expect.any(Number),
          validUntil: 0,
          data: expect.any(Object),
        });
        stop();
      });
    }
  });
};