1
0
mirror of https://github.com/immich-app/immich.git synced 2025-08-08 23:07:06 +02:00

feat: pending sync reset flag (#19861)

This commit is contained in:
Jason Rasmussen
2025-07-11 09:38:02 -04:00
committed by GitHub
parent 34f0f6c813
commit 4b3a4725c6
28 changed files with 499 additions and 27 deletions

View File

@ -234,11 +234,11 @@ export class SyncTestContext extends MediumTestContext<SyncService> {
});
}
async syncStream(auth: AuthDto, types: SyncRequestType[]) {
async syncStream(auth: AuthDto, types: SyncRequestType[], reset?: boolean) {
const stream = mediumFactory.syncStream();
// Wait for 2ms to ensure all updates are available and account for setTimeout inaccuracy
await wait(2);
await this.sut.stream(auth, stream, { types });
await this.sut.stream(auth, stream, { types, reset });
return stream.getResponse();
}
@ -481,6 +481,7 @@ const sessionInsert = ({
const defaults: Insertable<SessionTable> = {
id,
userId,
isPendingSyncReset: false,
token: sha256(id),
};

View File

@ -0,0 +1,63 @@
import { Kysely } from 'kysely';
import { SyncEntityType, SyncRequestType } from 'src/enum';
import { DB } from 'src/schema';
import { SyncTestContext } from 'test/medium.factory';
import { getKyselyDB } from 'test/utils';
let defaultDatabase: Kysely<DB>;
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.SyncResetV1, () => {
it('should work', async () => {
const { auth, ctx } = await setup();
const response = await ctx.syncStream(auth, [SyncRequestType.AssetsV1]);
expect(response).toEqual([]);
});
it('should detect a pending sync reset', async () => {
const { auth, ctx } = await setup();
auth.session!.isPendingSyncReset = true;
const response = await ctx.syncStream(auth, [SyncRequestType.AssetsV1]);
expect(response).toEqual([{ type: SyncEntityType.SyncResetV1, data: {} }]);
});
it('should not send other dtos when a reset is pending', async () => {
const { auth, user, ctx } = await setup();
await ctx.newAsset({ ownerId: user.id });
await expect(ctx.syncStream(auth, [SyncRequestType.AssetsV1])).resolves.toHaveLength(1);
auth.session!.isPendingSyncReset = true;
await expect(ctx.syncStream(auth, [SyncRequestType.AssetsV1])).resolves.toEqual([
{ type: SyncEntityType.SyncResetV1, data: {} },
]);
});
it('should allow resetting a pending reset when requesting changes ', async () => {
const { auth, user, ctx } = await setup();
await ctx.newAsset({ ownerId: user.id });
auth.session!.isPendingSyncReset = true;
await expect(ctx.syncStream(auth, [SyncRequestType.AssetsV1], true)).resolves.toEqual([
expect.objectContaining({
type: SyncEntityType.AssetV1,
}),
]);
});
});

View File

@ -58,7 +58,11 @@ const authFactory = ({
}
if (session) {
auth.session = { id: session.id, hasElevatedPermission: false };
auth.session = {
id: session.id,
isPendingSyncReset: false,
hasElevatedPermission: false,
};
}
if (sharedLink) {
@ -131,6 +135,7 @@ const sessionFactory = (session: Partial<Session> = {}) => ({
expiresAt: null,
userId: newUuid(),
pinExpiresAt: newDate(),
isPendingSyncReset: false,
...session,
});