Gate the "Shared from your device" label and the hidden download affordance on whether this device actually holds the file bytes, not on whether the current user uploaded it. uploaderPeerId is the user id, so the old check claimed ownership on every device of the uploader, blocking view/download on second devices that only synced metadata. Also include attachment metadata in the account_sync chat-sync-batch so sibling devices learn about synced attachments at all. Co-authored-by: Cursor <cursoragent@cursor.com>
70 lines
2.8 KiB
TypeScript
70 lines
2.8 KiB
TypeScript
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);
|
|
});
|
|
});
|
|
});
|