diff --git a/ts/test-node/types/Attachment_test.ts b/ts/test-node/types/Attachment_test.ts index 3e13701d66..9a69a87ceb 100644 --- a/ts/test-node/types/Attachment_test.ts +++ b/ts/test-node/types/Attachment_test.ts @@ -62,7 +62,7 @@ describe('Attachment', () => { }); }); context('for attachment with index', () => { - it('should use filename if provided', () => { + it('should use filename if it is provided', () => { const attachment: Attachment.AttachmentType = fakeAttachment({ fileName: 'funny-cat.mov', data: Bytes.fromString('foo'), @@ -74,12 +74,47 @@ describe('Attachment', () => { const actual = Attachment.getSuggestedFilename({ attachment, timestamp, - index: 3, }); const expected = 'funny-cat.mov'; assert.strictEqual(actual, expected); }); + it('should use filename if it is provided and index is 1', () => { + const attachment: Attachment.AttachmentType = fakeAttachment({ + fileName: 'funny-cat.mov', + data: Bytes.fromString('foo'), + contentType: MIME.VIDEO_QUICKTIME, + }); + const timestamp = new Date( + DAY + new Date(DAY).getTimezoneOffset() * 60 * 1000 + ); + const actual = Attachment.getSuggestedFilename({ + attachment, + timestamp, + index: 1, + }); + const expected = 'funny-cat.mov'; + assert.strictEqual(actual, expected); + }); + + it('should use filename if it is provided and index is >1', () => { + const attachment: Attachment.AttachmentType = fakeAttachment({ + fileName: 'funny-cat.mov', + data: Bytes.fromString('foo'), + contentType: MIME.VIDEO_QUICKTIME, + }); + const timestamp = new Date( + DAY + new Date(DAY).getTimezoneOffset() * 60 * 1000 + ); + const actual = Attachment.getSuggestedFilename({ + attachment, + timestamp, + index: 2, + }); + const expected = 'signal-1970-01-02-000000_002.mov'; + assert.strictEqual(actual, expected); + }); + it('should use provided index if > 1 and filename not provided', () => { const attachment: Attachment.AttachmentType = fakeAttachment({ data: Bytes.fromString('foo'), diff --git a/ts/types/Attachment.ts b/ts/types/Attachment.ts index 526e507b86..352397236a 100644 --- a/ts/types/Attachment.ts +++ b/ts/types/Attachment.ts @@ -1001,7 +1001,7 @@ export const getSuggestedFilename = ({ index?: number; }): string => { const { fileName } = attachment; - if (fileName) { + if (fileName && (!isNumber(index) || index === 1)) { return fileName; }