@@ -58,7 +58,10 @@ fn test_partial_duration_scenario() {
5858 // Total deposit = 10 * 100 = 1000 tokens
5959 let rate_per_second = 10_i128 ;
6060 let max_duration = 100_u64 ;
61- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
61+ let _booking_id = {
62+ client. set_my_rate ( & expert, & rate_per_second) ;
63+ client. book_session ( & user, & expert, & max_duration)
64+ } ;
6265
6366 // Verify user's balance decreased
6467 assert_eq ! ( token. balance( & user) , 9_000 ) ;
@@ -94,7 +97,10 @@ fn test_full_duration_no_refund() {
9497 // Book session
9598 let rate_per_second = 10_i128 ;
9699 let max_duration = 100_u64 ;
97- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
100+ let _booking_id = {
101+ client. set_my_rate ( & expert, & rate_per_second) ;
102+ client. book_session ( & user, & expert, & max_duration)
103+ } ;
98104
99105 // Oracle finalizes with full duration (100 seconds)
100106 let actual_duration = 100_u64 ;
@@ -125,7 +131,10 @@ fn test_double_finalization_protection() {
125131
126132 let rate_per_second = 10_i128 ;
127133 let max_duration = 100_u64 ;
128- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
134+ let _booking_id = {
135+ client. set_my_rate ( & expert, & rate_per_second) ;
136+ client. book_session ( & user, & expert, & max_duration)
137+ } ;
129138
130139 // First finalization succeeds
131140 let actual_duration = 50_u64 ;
@@ -156,7 +165,10 @@ fn test_oracle_authorization_enforcement() {
156165
157166 let rate_per_second = 10_i128 ;
158167 let max_duration = 100_u64 ;
159- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
168+ let _booking_id = {
169+ client. set_my_rate ( & expert, & rate_per_second) ;
170+ client. book_session ( & user, & expert, & max_duration)
171+ } ;
160172
161173 // Clear all mocked auths to test Oracle authorization
162174 env. set_auths ( & [ ] ) ;
@@ -192,7 +204,10 @@ fn test_zero_duration_finalization() {
192204
193205 let rate_per_second = 10_i128 ;
194206 let max_duration = 100_u64 ;
195- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
207+ let _booking_id = {
208+ client. set_my_rate ( & expert, & rate_per_second) ;
209+ client. book_session ( & user, & expert, & max_duration)
210+ } ;
196211
197212 // Oracle finalizes with 0 duration (session cancelled)
198213 let actual_duration = 0_u64 ;
@@ -255,7 +270,10 @@ fn test_book_session_balance_transfer() {
255270 assert_eq ! ( token. balance( & client. address) , 0 ) ;
256271
257272 // Book session
258- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
273+ let _booking_id = {
274+ client. set_my_rate ( & expert, & rate_per_second) ;
275+ client. book_session ( & user, & expert, & max_duration)
276+ } ;
259277
260278 // Acceptance Criteria #1: User's balance decreases
261279 assert_eq ! ( token. balance( & user) , initial_balance - expected_deposit) ;
@@ -268,7 +286,10 @@ fn test_book_session_balance_transfer() {
268286
269287 // Create another booking to verify uniqueness
270288 token. mint ( & user, & expected_deposit) ; // Mint more tokens for second booking
271- let booking_id_2 = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
289+ let booking_id_2 = {
290+ client. set_my_rate ( & expert, & rate_per_second) ;
291+ client. book_session ( & user, & expert, & max_duration)
292+ } ;
272293
273294 // Second booking should have different ID
274295 assert_eq ! ( booking_id_2, 2 ) ;
@@ -296,8 +317,14 @@ fn test_get_user_and_expert_bookings() {
296317 // Create 2 bookings for the same user with different experts
297318 let rate_per_second = 10_i128 ;
298319 let max_duration = 100_u64 ;
299- let booking_id_1 = client. book_session ( & user, & expert1, & rate_per_second, & max_duration) ;
300- let booking_id_2 = client. book_session ( & user, & expert2, & rate_per_second, & max_duration) ;
320+ let booking_id_1 = {
321+ client. set_my_rate ( & expert1, & rate_per_second) ;
322+ client. book_session ( & user, & expert1, & max_duration)
323+ } ;
324+ let booking_id_2 = {
325+ client. set_my_rate ( & expert2, & rate_per_second) ;
326+ client. book_session ( & user, & expert2, & max_duration)
327+ } ;
301328
302329 // Test get_user_bookings - should return 2 bookings
303330 let user_bookings = client. get_user_bookings ( & user) ;
@@ -349,7 +376,10 @@ fn test_reclaim_stale_session_too_early() {
349376 // Create booking
350377 let rate_per_second = 10_i128 ;
351378 let max_duration = 100_u64 ;
352- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
379+ let _booking_id = {
380+ client. set_my_rate ( & expert, & rate_per_second) ;
381+ client. book_session ( & user, & expert, & max_duration)
382+ } ;
353383
354384 // User tries to reclaim immediately (should fail - too early)
355385 let result = client. try_reclaim_stale_session ( & user, & booking_id) ;
@@ -380,10 +410,14 @@ fn test_reclaim_stale_session_success() {
380410 // Create booking
381411 let rate_per_second = 10_i128 ;
382412 let max_duration = 100_u64 ;
383- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
413+ let _booking_id = {
414+ client. set_my_rate ( & expert, & rate_per_second) ;
415+ client. book_session ( & user, & expert, & max_duration)
416+ } ;
384417
385418 // Advance ledger timestamp by 25 hours (90000 seconds)
386- env. ledger ( ) . set_timestamp ( env. ledger ( ) . timestamp ( ) + 90_000 ) ;
419+ env. ledger ( )
420+ . set_timestamp ( env. ledger ( ) . timestamp ( ) + 90_000 ) ;
387421
388422 // User tries to reclaim after 25 hours (should succeed)
389423 let result = client. try_reclaim_stale_session ( & user, & booking_id) ;
@@ -416,10 +450,14 @@ fn test_reclaim_stale_session_wrong_user() {
416450 // Create booking
417451 let rate_per_second = 10_i128 ;
418452 let max_duration = 100_u64 ;
419- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
453+ let _booking_id = {
454+ client. set_my_rate ( & expert, & rate_per_second) ;
455+ client. book_session ( & user, & expert, & max_duration)
456+ } ;
420457
421458 // Advance ledger timestamp by 25 hours
422- env. ledger ( ) . set_timestamp ( env. ledger ( ) . timestamp ( ) + 90_000 ) ;
459+ env. ledger ( )
460+ . set_timestamp ( env. ledger ( ) . timestamp ( ) + 90_000 ) ;
423461
424462 // Other user tries to reclaim (should fail - not authorized)
425463 let result = client. try_reclaim_stale_session ( & other_user, & booking_id) ;
@@ -449,13 +487,17 @@ fn test_reclaim_already_finalized() {
449487 // Create booking
450488 let rate_per_second = 10_i128 ;
451489 let max_duration = 100_u64 ;
452- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
490+ let _booking_id = {
491+ client. set_my_rate ( & expert, & rate_per_second) ;
492+ client. book_session ( & user, & expert, & max_duration)
493+ } ;
453494
454495 // Oracle finalizes the session
455496 client. finalize_session ( & booking_id, & 50 ) ;
456497
457498 // Advance ledger timestamp by 25 hours
458- env. ledger ( ) . set_timestamp ( env. ledger ( ) . timestamp ( ) + 90_000 ) ;
499+ env. ledger ( )
500+ . set_timestamp ( env. ledger ( ) . timestamp ( ) + 90_000 ) ;
459501
460502 // User tries to reclaim after finalization (should fail - not pending)
461503 let result = client. try_reclaim_stale_session ( & user, & booking_id) ;
@@ -482,7 +524,10 @@ fn test_expert_rejects_pending_session() {
482524 // Create booking
483525 let rate_per_second = 10_i128 ;
484526 let max_duration = 100_u64 ;
485- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
527+ let _booking_id = {
528+ client. set_my_rate ( & expert, & rate_per_second) ;
529+ client. book_session ( & user, & expert, & max_duration)
530+ } ;
486531
487532 // Verify initial state
488533 assert_eq ! ( token. balance( & user) , 9_000 ) ;
@@ -522,7 +567,10 @@ fn test_user_cannot_reject_session() {
522567
523568 let rate_per_second = 10_i128 ;
524569 let max_duration = 100_u64 ;
525- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
570+ let _booking_id = {
571+ client. set_my_rate ( & expert, & rate_per_second) ;
572+ client. book_session ( & user, & expert, & max_duration)
573+ } ;
526574
527575 // User tries to reject their own session (should fail - not authorized)
528576 let result = client. try_reject_session ( & user, & booking_id) ;
@@ -551,7 +599,10 @@ fn test_reject_already_complete_session() {
551599
552600 let rate_per_second = 10_i128 ;
553601 let max_duration = 100_u64 ;
554- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
602+ let _booking_id = {
603+ client. set_my_rate ( & expert, & rate_per_second) ;
604+ client. book_session ( & user, & expert, & max_duration)
605+ } ;
555606
556607 // Oracle finalizes the session
557608 client. finalize_session ( & booking_id, & 50 ) ;
@@ -580,10 +631,14 @@ fn test_reject_already_reclaimed_session() {
580631
581632 let rate_per_second = 10_i128 ;
582633 let max_duration = 100_u64 ;
583- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
634+ let _booking_id = {
635+ client. set_my_rate ( & expert, & rate_per_second) ;
636+ client. book_session ( & user, & expert, & max_duration)
637+ } ;
584638
585639 // Advance time and user reclaims
586- env. ledger ( ) . set_timestamp ( env. ledger ( ) . timestamp ( ) + 90_000 ) ;
640+ env. ledger ( )
641+ . set_timestamp ( env. ledger ( ) . timestamp ( ) + 90_000 ) ;
587642 client. reclaim_stale_session ( & user, & booking_id) ;
588643
589644 // Expert tries to reject after reclamation (should fail - not pending)
@@ -611,7 +666,10 @@ fn test_wrong_expert_cannot_reject() {
611666
612667 let rate_per_second = 10_i128 ;
613668 let max_duration = 100_u64 ;
614- let booking_id = client. book_session ( & user, & expert, & rate_per_second, & max_duration) ;
669+ let _booking_id = {
670+ client. set_my_rate ( & expert, & rate_per_second) ;
671+ client. book_session ( & user, & expert, & max_duration)
672+ } ;
615673
616674 // Different expert tries to reject (should fail - not authorized)
617675 let result = client. try_reject_session ( & wrong_expert, & booking_id) ;
@@ -639,3 +697,87 @@ fn test_reject_nonexistent_booking() {
639697 assert ! ( result. is_err( ) ) ;
640698}
641699
700+ #[ test]
701+ fn test_expert_can_set_and_update_rate ( ) {
702+ let env = Env :: default ( ) ;
703+ env. mock_all_auths ( ) ;
704+
705+ let admin = Address :: generate ( & env) ;
706+ let expert = Address :: generate ( & env) ;
707+ let oracle = Address :: generate ( & env) ;
708+ let token = Address :: generate ( & env) ;
709+
710+ let client = create_client ( & env) ;
711+ client. init ( & admin, & token, & oracle) ;
712+
713+ // Initial set
714+ let res1 = client. try_set_my_rate ( & expert, & 10_i128 ) ;
715+ assert ! ( res1. is_ok( ) ) ;
716+
717+ // Update rate
718+ let res2 = client. try_set_my_rate ( & expert, & 25_i128 ) ;
719+ assert ! ( res2. is_ok( ) ) ;
720+
721+ // Fails with invalid rate
722+ let res3 = client. try_set_my_rate ( & expert, & 0_i128 ) ;
723+ assert ! ( res3. is_err( ) ) ;
724+ }
725+
726+ #[ test]
727+ fn test_book_session_calculates_correct_deposit ( ) {
728+ let env = Env :: default ( ) ;
729+ env. mock_all_auths ( ) ;
730+
731+ let admin = Address :: generate ( & env) ;
732+ let user = Address :: generate ( & env) ;
733+ let expert = Address :: generate ( & env) ;
734+ let oracle = Address :: generate ( & env) ;
735+
736+ let token_admin = Address :: generate ( & env) ;
737+ let token = create_token_contract ( & env, & token_admin) ;
738+ let initial_balance = 5_000_i128 ;
739+ token. mint ( & user, & initial_balance) ;
740+
741+ let client = create_client ( & env) ;
742+ client. init ( & admin, & token. address , & oracle) ;
743+
744+ // Set expert rate
745+ let stored_rate = 15_i128 ;
746+ client. set_my_rate ( & expert, & stored_rate) ;
747+
748+ // Book session
749+ let max_duration = 100_u64 ;
750+ let expected_deposit = stored_rate * ( max_duration as i128 ) ; // 1500 tokens
751+
752+ let _booking_id = client. book_session ( & user, & expert, & max_duration) ;
753+
754+ // Verify correct deposit was extracted
755+ assert_eq ! ( token. balance( & user) , initial_balance - expected_deposit) ;
756+ assert_eq ! ( token. balance( & client. address) , expected_deposit) ;
757+ }
758+
759+ #[ test]
760+ fn test_book_session_fails_if_expert_rate_not_set ( ) {
761+ let env = Env :: default ( ) ;
762+ env. mock_all_auths ( ) ;
763+
764+ let admin = Address :: generate ( & env) ;
765+ let user = Address :: generate ( & env) ;
766+ let expert = Address :: generate ( & env) ;
767+ let oracle = Address :: generate ( & env) ;
768+
769+ let token_admin = Address :: generate ( & env) ;
770+ let token = create_token_contract ( & env, & token_admin) ;
771+ token. mint ( & user, & 5_000 ) ;
772+
773+ let client = create_client ( & env) ;
774+ client. init ( & admin, & token. address , & oracle) ;
775+
776+ // Expert has NOT set rate
777+
778+ // Book session should fail
779+ let max_duration = 100_u64 ;
780+ let res = client. try_book_session ( & user, & expert, & max_duration) ;
781+
782+ assert ! ( res. is_err( ) ) ;
783+ }
0 commit comments