@@ -350,10 +350,10 @@ def test_create_external_session(self):
350350 files_param = call_args [1 ]["files" ]
351351 self .assertEqual (len (files_param ), 3 )
352352 self .assertEqual (files_param [0 ][0 ], "files" )
353- self .assertEqual (files_param [0 ][1 ][0 ], u"annex4 .pdf" )
353+ self .assertEqual (files_param [0 ][1 ][0 ], u"annex1-1 .pdf" )
354354 self .assertEqual (files_param [1 ][0 ], "files" )
355- self .assertEqual (files_param [1 ][1 ][0 ], u"annex1 .pdf" )
356- self .assertEqual (files_param [2 ][1 ][0 ], u"annex1-1 .pdf" )
355+ self .assertEqual (files_param [1 ][1 ][0 ], u"annex4 .pdf" )
356+ self .assertEqual (files_param [2 ][1 ][0 ], u"annex1.pdf" )
357357
358358 # Case 6: session without files to send => returns "_no_files_", no HTTP call
359359 for i in range (len (session3 ["files" ])):
@@ -422,6 +422,41 @@ def test_session_size_discrimination(self):
422422 self .assertIs (session3 , session2 )
423423 self .assertEqual (session3 ["size" ], 7014 + 6968 )
424424
425+ def test_add_files_ordering_by_context (self ):
426+ """Files added to a session are ordered by their position within their context."""
427+ def reset_annotation ():
428+ annot = get_session_annotation ()
429+ annot ["sessions" ].clear ()
430+ annot ["uids" ].clear ()
431+ annot ["c_uids" ].clear ()
432+ annot ["numbering" ] = 0
433+
434+ signers = [("user1" , "user1@sign.com" , "User 1" , "Position 1" )]
435+
436+ # Case 1: uid[0] then uid[1] (different context) then uid[2] (same context as uid[0])
437+ # uid[2] must land immediately after uid[0], not after uid[1]
438+ reset_annotation ()
439+ sid , session = add_files_to_session (signers , (self .uids [0 ],))
440+ sid , session = add_files_to_session (signers , (self .uids [1 ],), session_id = sid )
441+ sid , session = add_files_to_session (signers , (self .uids [2 ],), session_id = sid )
442+ self .assertEqual ([f ["uid" ] for f in session ["files" ]], [self .uids [0 ], self .uids [2 ], self .uids [1 ]])
443+
444+ # Case 2: uid[4] (3rd in folder0) added before uid[0] (1st in folder0)
445+ # uid[0] must land before uid[4]
446+ reset_annotation ()
447+ sid , session = add_files_to_session (signers , (self .uids [4 ],))
448+ sid , session = add_files_to_session (signers , (self .uids [0 ],), session_id = sid )
449+ self .assertEqual ([f ["uid" ] for f in session ["files" ]], [self .uids [0 ], self .uids [4 ]])
450+
451+ # Case 3: uid[0], uid[4] in session, then uid[1] (different context), then uid[2]
452+ # uid[2] must be inserted between uid[0] and uid[4]
453+ reset_annotation ()
454+ sid , session = add_files_to_session (signers , (self .uids [0 ],))
455+ sid , session = add_files_to_session (signers , (self .uids [4 ],), session_id = sid )
456+ sid , session = add_files_to_session (signers , (self .uids [1 ],), session_id = sid )
457+ sid , session = add_files_to_session (signers , (self .uids [2 ],), session_id = sid )
458+ self .assertEqual ([f ["uid" ] for f in session ["files" ]], [self .uids [0 ], self .uids [2 ], self .uids [4 ], self .uids [1 ]])
459+
425460 def test_add_files_with_duplicate_filenames (self ):
426461 """Test that files with duplicate filenames are renamed with suffix."""
427462 annot = get_session_annotation ()
0 commit comments