@@ -884,6 +884,90 @@ describe('Public file shares against PostgreSQL and private storage', () => {
884884 }
885885 )
886886
887+ check (
888+ 'public share content saved during an object read is delivered from a fresh snapshot, never mixed' ,
889+ async ( ) => {
890+ const { updateProjectFileShare } = await import ( '@/lib/projects/files/application/shares' )
891+ const {
892+ authorizePublicFileShare,
893+ readPublicFileShareContent,
894+ retryOnSharedFileRevisionChange,
895+ } = await import ( '@/lib/public-shares/application' )
896+ // A Markdown file streams from storage; a page is rendered whole. Both see the save.
897+ const saved = 'Saved while read'
898+ const shared = {
899+ streamed : { input : createInput , source : '' } ,
900+ rendered : {
901+ input : ( projectId : string ) => ( {
902+ ...createInput ( projectId , '---\ntitle: Architecture\n---\n\nShared page' ) ,
903+ name : 'Architecture' ,
904+ contentType : 'text/x-sim-page' ,
905+ } ) ,
906+ source : '---\ntitle: Architecture\n---\n\n' ,
907+ } ,
908+ }
909+ for ( const { input, source } of Object . values ( shared ) )
910+ for ( const saves of [ 'once' , 'every read' ] as const ) {
911+ const f = await fixture ( )
912+ const { file } = await createProjectFile . execute ( {
913+ principal : f . principal ,
914+ input : input ( f . projectId ) ,
915+ } )
916+ const { share } = await updateProjectFileShare . execute ( {
917+ principal : f . principal ,
918+ input : { projectId : f . projectId , fileId : file . id , isActive : true } ,
919+ } )
920+ const access = await authorizePublicFileShare ( {
921+ token : share . token ,
922+ credential : { method : 'GET' } ,
923+ } )
924+ if ( ! access . authorized ) throw new Error ( 'Public fixture rejected' )
925+ // A collaborative save commits while the object is being read, buffered or streamed alike.
926+ let written = 0
927+ let saving = false
928+ const save = async ( ) => {
929+ if ( saving || ( saves === 'once' && written > 0 ) ) return
930+ saving = true
931+ written ++
932+ try {
933+ await updateProjectFileContent . execute ( {
934+ principal : f . principal ,
935+ input : {
936+ projectId : f . projectId ,
937+ fileId : file . id ,
938+ content : `${ source } ${ saved } ${ written } ` ,
939+ encoding : 'utf-8' ,
940+ } ,
941+ } )
942+ } finally {
943+ saving = false
944+ }
945+ }
946+ const download = storage . downloadFile
947+ const downloadStream = storage . downloadFileStream
948+ const spies = [
949+ vi . spyOn ( storage , 'downloadFile' ) . mockImplementation ( async ( options ) => {
950+ const result = await download ( options )
951+ await save ( )
952+ return result
953+ } ) ,
954+ vi . spyOn ( storage , 'downloadFileStream' ) . mockImplementation ( async ( options ) => {
955+ const result = await downloadStream ( options )
956+ await save ( )
957+ return result
958+ } ) ,
959+ ]
960+ const delivered = retryOnSharedFileRevisionChange ( ( ) =>
961+ sharedBytes ( readPublicFileShareContent ( { grant : access . grant } ) )
962+ )
963+ if ( saves === 'once' ) expect ( ( await delivered ) . toString ( ) ) . toContain ( `${ saved } 1` )
964+ else await expect ( delivered ) . rejects . toMatchObject ( { code : 'conflict' } )
965+ expect ( written ) . toBe ( saves === 'once' ? 1 : 3 )
966+ for ( const spy of spies ) spy . mockRestore ( )
967+ }
968+ }
969+ )
970+
887971 check (
888972 'public share grants cannot be cloned or reused after Project archive and preserve workspace links' ,
889973 async ( ) => {
0 commit comments