import { deviceHasLocalCopy, isSharingFromThisDevice, isUploaderUser } from './attachment-sharing.rules'; describe('attachment sharing rules', () => { describe('isUploaderUser', () => { it('is true when the attachment uploader matches the current user', () => { expect(isUploaderUser({ uploaderPeerId: 'user-1' }, 'user-1')).toBe(true); }); it('is false when the uploader is a different user', () => { expect(isUploaderUser({ uploaderPeerId: 'user-2' }, 'user-1')).toBe(false); }); it('is false when either id is missing', () => { expect(isUploaderUser({ uploaderPeerId: undefined }, 'user-1')).toBe(false); expect(isUploaderUser({ uploaderPeerId: 'user-1' }, null)).toBe(false); expect(isUploaderUser({ uploaderPeerId: 'user-1' }, '')).toBe(false); }); }); describe('deviceHasLocalCopy', () => { it('is true when an available blob object url is present', () => { expect(deviceHasLocalCopy({ available: true, objectUrl: 'blob:abc' })).toBe(true); }); it('is true when a savedPath or filePath is present', () => { expect(deviceHasLocalCopy({ available: false, savedPath: '/appdata/file.bin' })).toBe(true); expect(deviceHasLocalCopy({ available: false, filePath: '/home/me/file.bin' })).toBe(true); }); it('is false when only metadata exists (no bytes, no paths)', () => { expect(deviceHasLocalCopy({ available: false })).toBe(false); expect(deviceHasLocalCopy({ available: false, savedPath: ' ', filePath: '' })).toBe(false); }); it('is false when marked available but no object url is present yet', () => { expect(deviceHasLocalCopy({ available: true })).toBe(false); }); }); describe('isSharingFromThisDevice', () => { it('is true for the uploader device that still holds the file locally', () => { expect( isSharingFromThisDevice({ uploaderPeerId: 'user-1', available: true, objectUrl: 'blob:abc' }, 'user-1') ).toBe(true); expect( isSharingFromThisDevice({ uploaderPeerId: 'user-1', available: false, savedPath: '/appdata/file.bin' }, 'user-1') ).toBe(true); }); it('is false on a second device of the same user that only synced metadata', () => { // This is the regression: the user uploaded from another device, so the metadata // carries uploaderPeerId === currentUserId, but this device holds no local bytes. expect( isSharingFromThisDevice({ uploaderPeerId: 'user-1', available: false }, 'user-1') ).toBe(false); }); it('is false for a different user even if they downloaded the file locally', () => { expect( isSharingFromThisDevice({ uploaderPeerId: 'user-1', available: true, objectUrl: 'blob:abc' }, 'user-2') ).toBe(false); }); }); });