-
Notifications
You must be signed in to change notification settings - Fork 97
fix: allow same-name staged attachments #1950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ap4sh
wants to merge
1
commit into
session-foundation:dev
Choose a base branch
from
Ap4sh:fix-staged-attachments-same-name
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
ts/test/session/unit/staged_attachments/StagedAttachments_test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| import { expect } from 'chai'; | ||
| import Sinon from 'sinon'; | ||
|
|
||
| import { | ||
| addStagedAttachmentsInConversation, | ||
| getEmptyStagedAttachmentsState, | ||
| reducer, | ||
| removeStagedAttachmentInConversation, | ||
| } from '../../../../state/ducks/stagedAttachments'; | ||
| import type { StagedAttachmentType } from '../../../../components/conversation/composition/CompositionBox'; | ||
|
|
||
| const conversationKey = 'conversation-key'; | ||
|
|
||
| function makeAttachment({ | ||
| stagedAttachmentId, | ||
| fileName, | ||
| url = '', | ||
| videoUrl, | ||
| isVoiceMessage = false, | ||
| }: { | ||
| stagedAttachmentId: string; | ||
| fileName: string; | ||
| url?: string; | ||
| videoUrl?: string; | ||
| isVoiceMessage?: boolean; | ||
| }): StagedAttachmentType { | ||
| return { | ||
| stagedAttachmentId, | ||
| file: {} as File, | ||
| contentType: 'image/jpeg', | ||
| fileName, | ||
| url, | ||
| videoUrl, | ||
| fileSize: null, | ||
| isVoiceMessage, | ||
| screenshot: null, | ||
| thumbnail: null, | ||
| }; | ||
| } | ||
|
|
||
| describe('state/ducks/stagedAttachments', () => { | ||
| beforeEach(() => { | ||
| (global as any).window = { | ||
| log: { | ||
| warn: Sinon.stub(), | ||
| }, | ||
| }; | ||
|
|
||
| if (!URL.revokeObjectURL) { | ||
| URL.revokeObjectURL = () => undefined; | ||
| } | ||
|
|
||
| Sinon.stub(URL, 'revokeObjectURL'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| Sinon.restore(); | ||
| delete (global as any).window; | ||
| }); | ||
|
|
||
| it('keeps staged attachments with the same filename when their staged ids differ', () => { | ||
| const first = makeAttachment({ | ||
| stagedAttachmentId: 'first', | ||
| fileName: 'image.jpg', | ||
| url: 'blob:first', | ||
| }); | ||
| const second = makeAttachment({ | ||
| stagedAttachmentId: 'second', | ||
| fileName: 'image.jpg', | ||
| url: 'blob:second', | ||
| }); | ||
|
|
||
| const state = reducer( | ||
| getEmptyStagedAttachmentsState(), | ||
| addStagedAttachmentsInConversation({ | ||
| conversationKey, | ||
| newAttachments: [first, second], | ||
| }) | ||
| ); | ||
|
|
||
| expect(state.stagedAttachments[conversationKey].map(attachment => attachment.url)).to.deep.eq([ | ||
| 'blob:first', | ||
| 'blob:second', | ||
| ]); | ||
| }); | ||
|
|
||
| it('removes only the staged attachment matching the staged id', () => { | ||
| const first = makeAttachment({ | ||
| stagedAttachmentId: 'first', | ||
| fileName: 'image.jpg', | ||
| url: 'blob:first', | ||
| }); | ||
| const second = makeAttachment({ | ||
| stagedAttachmentId: 'second', | ||
| fileName: 'image.jpg', | ||
| url: 'blob:second', | ||
| videoUrl: 'blob:second-video', | ||
| }); | ||
|
|
||
| const stateWithAttachments = reducer( | ||
| getEmptyStagedAttachmentsState(), | ||
| addStagedAttachmentsInConversation({ | ||
| conversationKey, | ||
| newAttachments: [first, second], | ||
| }) | ||
| ); | ||
|
|
||
| const state = reducer( | ||
| stateWithAttachments, | ||
| removeStagedAttachmentInConversation({ | ||
| conversationKey, | ||
| stagedAttachmentId: 'second', | ||
| }) | ||
| ); | ||
|
|
||
| const revokedUrls = (URL.revokeObjectURL as Sinon.SinonStub) | ||
| .getCalls() | ||
| .map(call => call.args[0]); | ||
|
|
||
| expect( | ||
| state.stagedAttachments[conversationKey].map(attachment => attachment.stagedAttachmentId) | ||
| ).to.deep.eq(['first']); | ||
| expect(revokedUrls).to.deep.eq(['blob:second', 'blob:second-video']); | ||
| }); | ||
|
|
||
| it('does not add a voice message with another staged attachment', () => { | ||
| const currentAttachment = makeAttachment({ | ||
| stagedAttachmentId: 'current', | ||
| fileName: 'image.jpg', | ||
| }); | ||
| const voiceAttachment = makeAttachment({ | ||
| stagedAttachmentId: 'voice', | ||
| fileName: 'session-audio-message', | ||
| isVoiceMessage: true, | ||
| }); | ||
|
|
||
| const stateWithAttachment = reducer( | ||
| getEmptyStagedAttachmentsState(), | ||
| addStagedAttachmentsInConversation({ | ||
| conversationKey, | ||
| newAttachments: [currentAttachment], | ||
| }) | ||
| ); | ||
|
|
||
| const state = reducer( | ||
| stateWithAttachment, | ||
| addStagedAttachmentsInConversation({ | ||
| conversationKey, | ||
| newAttachments: [voiceAttachment], | ||
| }) | ||
| ); | ||
|
|
||
| expect( | ||
| state.stagedAttachments[conversationKey].map(attachment => attachment.stagedAttachmentId) | ||
| ).to.deep.eq(['current']); | ||
| expect((global as any).window.log.warn.calledOnce).to.eq(true); | ||
| }); | ||
|
|
||
| it('does not add another attachment when a voice message is already staged', () => { | ||
| const voiceAttachment = makeAttachment({ | ||
| stagedAttachmentId: 'voice', | ||
| fileName: 'session-audio-message', | ||
| isVoiceMessage: true, | ||
| }); | ||
| const nextAttachment = makeAttachment({ | ||
| stagedAttachmentId: 'next', | ||
| fileName: 'image.jpg', | ||
| }); | ||
|
|
||
| const stateWithVoiceMessage = reducer( | ||
| getEmptyStagedAttachmentsState(), | ||
| addStagedAttachmentsInConversation({ | ||
| conversationKey, | ||
| newAttachments: [voiceAttachment], | ||
| }) | ||
| ); | ||
|
|
||
| const state = reducer( | ||
| stateWithVoiceMessage, | ||
| addStagedAttachmentsInConversation({ | ||
| conversationKey, | ||
| newAttachments: [nextAttachment], | ||
| }) | ||
| ); | ||
|
|
||
| expect( | ||
| state.stagedAttachments[conversationKey].map(attachment => attachment.stagedAttachmentId) | ||
| ).to.deep.eq(['voice']); | ||
| expect((global as any).window.log.warn.calledOnce).to.eq(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This path does not go through staged attachment state
sendVoiceMessagebuilds aStagedAttachmentImportedTypeand sends it directly throughsendMessage, whilestagedAttachmentIdis only required onStagedAttachmentTypebeforegetFileAndStoreLocallyKeeping it omitted from
StagedAttachmentImportedTypeis intentional so the local staging id does not leak into outgoing attachment payloads