11use super :: * ;
22use crate :: input:: clipboard:: test_nonce:: test_nonce;
3+ use agent_desktop_core:: Deadline ;
34
45fn record ( nonce : & str ) -> OwnerRecord {
56 OwnerRecord {
@@ -62,20 +63,26 @@ fn a_failure_outcome_carries_its_message_and_is_not_a_record() {
6263 read_outcome( & dir, ":7" , & nonce) ,
6364 Some ( Outcome :: Failed ( "payload missing" . into( ) ) )
6465 ) ;
65- assert ! ( records_in( & dir, ":7" ) . expect( "list" ) . is_empty( ) ) ;
66+ assert ! (
67+ records_in( & dir, ":7" , Deadline :: standard( ) . unwrap( ) )
68+ . expect( "list" )
69+ . is_empty( )
70+ ) ;
6671}
6772
6873#[ test]
6974fn records_are_listed_per_display_only ( ) {
7075 let dir = scratch ( "listing" ) ;
7176 let seven_nonce = test_nonce ( ) ;
7277 let mut seven = record ( & seven_nonce) ;
78+ seven. pid = std:: process:: id ( ) ;
79+ seven. start_ticks = start_ticks_of ( seven. pid ) . expect ( "live process" ) ;
7380 seven. display = ":7" . into ( ) ;
7481 let mut eight = record ( & test_nonce ( ) ) ;
7582 eight. display = ":8" . into ( ) ;
7683 publish ( & dir, & seven) . expect ( "seven" ) ;
7784 publish ( & dir, & eight) . expect ( "eight" ) ;
78- let found = records_in ( & dir, ":7.0" ) . expect ( "list" ) ;
85+ let found = records_in ( & dir, ":7.0" , Deadline :: standard ( ) . unwrap ( ) ) . expect ( "list" ) ;
7986 assert_eq ! (
8087 found,
8188 [ OwnerRecord {
@@ -88,13 +95,56 @@ fn records_are_listed_per_display_only() {
8895 ) ;
8996}
9097
98+ #[ test]
99+ fn stale_and_other_display_records_do_not_block_the_live_owner ( ) {
100+ let dir = scratch ( "stale-limit" ) ;
101+ let mut live = record ( & test_nonce ( ) ) ;
102+ live. pid = std:: process:: id ( ) ;
103+ live. start_ticks = start_ticks_of ( live. pid ) . expect ( "live process" ) ;
104+ for index in 0 ..300 {
105+ let mut stale = record ( & format ! ( "stale{index:08}" ) ) ;
106+ stale. pid = if index % 2 == 0 { u32:: MAX } else { live. pid } ;
107+ stale. start_ticks = live. start_ticks + 1 ;
108+ publish ( & dir, & stale) . expect ( "stale record" ) ;
109+ let mut foreign = live. clone ( ) ;
110+ foreign. display = ":8" . into ( ) ;
111+ foreign. nonce = format ! ( "foreign{index:08}" ) ;
112+ publish ( & dir, & foreign) . expect ( "other display" ) ;
113+ }
114+ publish ( & dir, & live) . expect ( "live owner" ) ;
115+ let found = records_in ( & dir, ":7" , Deadline :: standard ( ) . unwrap ( ) )
116+ . expect ( "stale records must not block discovery" ) ;
117+ assert_eq ! ( found. len( ) , 1 ) ;
118+ assert_eq ! ( found[ 0 ] . nonce, live. nonce) ;
119+ assert_eq ! ( std:: fs:: read_dir( & dir) . unwrap( ) . count( ) , 301 ) ;
120+ assert_eq ! (
121+ records_in( & dir, ":8" , Deadline :: standard( ) . unwrap( ) )
122+ . unwrap_err( )
123+ . kind( ) ,
124+ std:: io:: ErrorKind :: InvalidData
125+ ) ;
126+ }
127+
128+ #[ test]
129+ fn an_expired_scan_does_not_read_or_reclaim_records ( ) {
130+ let dir = scratch ( "expired-scan" ) ;
131+ publish ( & dir, & record ( & test_nonce ( ) ) ) . unwrap ( ) ;
132+ let error = records_in ( & dir, ":7" , Deadline :: after ( 0 ) . unwrap ( ) ) . unwrap_err ( ) ;
133+ assert_eq ! ( error. kind( ) , std:: io:: ErrorKind :: TimedOut ) ;
134+ assert_eq ! ( std:: fs:: read_dir( & dir) . unwrap( ) . count( ) , 1 ) ;
135+ }
136+
91137#[ test]
92138fn an_exiting_owner_removes_its_own_record ( ) {
93139 let dir = scratch ( "own" ) ;
94140 let own = record ( & test_nonce ( ) ) ;
95141 publish ( & dir, & own) . expect ( "publish" ) ;
96142 remove_own ( & dir, & own) ;
97- assert ! ( records_in( & dir, ":7" ) . expect( "list" ) . is_empty( ) ) ;
143+ assert ! (
144+ records_in( & dir, ":7" , Deadline :: standard( ) . unwrap( ) )
145+ . expect( "list" )
146+ . is_empty( )
147+ ) ;
98148}
99149
100150#[ test]
@@ -121,7 +171,10 @@ fn records_in_rejects_a_symlink_record() {
121171 let target = dir. join ( "target.json" ) ;
122172 std:: fs:: write ( & target, record ( & test_nonce ( ) ) . to_bytes ( ) ) . expect ( "target" ) ;
123173 symlink ( & target, dir. join ( name) ) . expect ( "symlink" ) ;
124- assert ! ( records_in( & dir, ":7" ) . is_err( ) , "symlinks are refused" ) ;
174+ assert ! (
175+ records_in( & dir, ":7" , Deadline :: standard( ) . unwrap( ) ) . is_err( ) ,
176+ "symlinks are refused"
177+ ) ;
125178}
126179
127180#[ cfg( unix) ]
@@ -141,7 +194,7 @@ fn records_in_rejects_a_fifo_without_waiting_for_a_writer() {
141194 )
142195 . expect ( "fifo" ) ;
143196 assert_eq ! (
144- records_in( & dir, ":7" )
197+ records_in( & dir, ":7" , Deadline :: standard ( ) . unwrap ( ) )
145198 . expect_err( "FIFOs are refused" )
146199 . kind( ) ,
147200 std:: io:: ErrorKind :: InvalidData
@@ -160,7 +213,7 @@ fn records_in_rejects_an_oversized_record() {
160213 )
161214 . expect ( "large record" ) ;
162215 assert_eq ! (
163- records_in( & dir, ":7" )
216+ records_in( & dir, ":7" , Deadline :: standard ( ) . unwrap ( ) )
164217 . expect_err( "oversized records are refused" )
165218 . kind( ) ,
166219 std:: io:: ErrorKind :: InvalidData
@@ -177,7 +230,7 @@ fn records_in_rejects_a_permissive_directory() {
177230 std:: fs:: set_permissions ( & dir, std:: fs:: Permissions :: from_mode ( 0o777 ) )
178231 . expect ( "permissive mode" ) ;
179232 assert_eq ! (
180- records_in( & dir, ":7" )
233+ records_in( & dir, ":7" , Deadline :: standard ( ) . unwrap ( ) )
181234 . expect_err( "permissive directory is refused" )
182235 . kind( ) ,
183236 std:: io:: ErrorKind :: PermissionDenied
0 commit comments