Skip to content

High performance self-hosted photo and video management solution.

2.1 KB
Raw Blame
import { Kysely } from 'kysely';
import { DateTime } from 'luxon';
import { v7 } from 'uuid';
import { SyncEntityType, SyncRequestType } from 'src/enum.js';
import { SyncCheckpointRepository } from 'src/repositories/sync-checkpoint.repository.js';
import { DB } from 'src/schema/index.js';
import { toAck } from 'src/utils/sync.js';
import { SyncTestContext } from 'test/medium.factory.js';
import { getKyselyDB } from 'test/utils.js';

let defaultDatabase: Kysely<DB>;

// eslint-disable-next-line unicorn/no-unnecessary-parameters
const setup = async (db?: Kysely<DB>) => {
  const ctx = new SyncTestContext(db || defaultDatabase);
  const { auth, user, session } = await ctx.newSyncAuthUser();
  return { auth, user, session, ctx };
};

beforeAll(async () => {
  defaultDatabase = await getKyselyDB();
});

describe(SyncEntityType.SyncCompleteV1, () => {
  it('should work', async () => {
    const { auth, ctx } = await setup();

    await ctx.assertSyncIsComplete(auth, [SyncRequestType.AssetsV2]);
  });

  it('should detect an old checkpoint and send back a reset', async () => {
    const { auth, session, ctx } = await setup();
    const updateId = v7({ msecs: DateTime.now().minus({ days: 60 }).toMillis() });

    await ctx.get(SyncCheckpointRepository).upsertAll([
      {
        type: SyncEntityType.SyncCompleteV1,
        sessionId: session.id,
        ack: toAck({ type: SyncEntityType.SyncCompleteV1, updateId }),
      },
    ]);

    const response = await ctx.syncStream(auth, [SyncRequestType.AssetsV2]);
    expect(response).toEqual([{ type: SyncEntityType.SyncResetV1, data: {}, ack: 'SyncResetV1|reset' }]);
  });

  it('should not send back a reset if the checkpoint is recent', async () => {
    const { auth, session, ctx } = await setup();
    const updateId = v7({ msecs: DateTime.now().minus({ days: 7 }).toMillis() });

    await ctx.get(SyncCheckpointRepository).upsertAll([
      {
        type: SyncEntityType.SyncCompleteV1,
        sessionId: session.id,
        ack: toAck({ type: SyncEntityType.SyncCompleteV1, updateId }),
      },
    ]);

    await ctx.assertSyncIsComplete(auth, [SyncRequestType.AssetsV2]);
  });
});