@@ -668,3 +668,95 @@ fn test_packet_type() {
668668 let packet_type = mqtt:: packet:: v3_1_1:: Publish :: packet_type ( ) ;
669669 assert_eq ! ( packet_type, mqtt:: packet:: PacketType :: Publish ) ;
670670}
671+
672+ // Tests for packet_id() with Option interface
673+
674+ #[ test]
675+ fn test_qos1_with_some_packet_id_success ( ) {
676+ common:: init_tracing ( ) ;
677+ let result = mqtt:: packet:: v3_1_1:: Publish :: builder ( )
678+ . topic_name ( "test/topic" )
679+ . unwrap ( )
680+ . qos ( mqtt:: packet:: Qos :: AtLeastOnce )
681+ . packet_id ( Some ( 42u16 ) )
682+ . build ( )
683+ . unwrap ( ) ;
684+ assert_eq ! ( result. packet_id( ) , Some ( 42u16 ) ) ;
685+ }
686+
687+ #[ test]
688+ fn test_qos2_with_some_packet_id_success ( ) {
689+ common:: init_tracing ( ) ;
690+ let result = mqtt:: packet:: v3_1_1:: Publish :: builder ( )
691+ . topic_name ( "test/topic" )
692+ . unwrap ( )
693+ . qos ( mqtt:: packet:: Qos :: ExactlyOnce )
694+ . packet_id ( Some ( 123u16 ) )
695+ . build ( )
696+ . unwrap ( ) ;
697+ assert_eq ! ( result. packet_id( ) , Some ( 123u16 ) ) ;
698+ }
699+
700+ #[ test]
701+ fn test_qos1_with_none_packet_id_error ( ) {
702+ common:: init_tracing ( ) ;
703+ let err = mqtt:: packet:: v3_1_1:: Publish :: builder ( )
704+ . topic_name ( "test/topic" )
705+ . unwrap ( )
706+ . qos ( mqtt:: packet:: Qos :: AtLeastOnce )
707+ . packet_id ( None :: < u16 > )
708+ . build ( )
709+ . unwrap_err ( ) ;
710+ assert_eq ! ( err, mqtt:: result_code:: MqttError :: MalformedPacket ) ;
711+ }
712+
713+ #[ test]
714+ fn test_qos2_with_none_packet_id_error ( ) {
715+ common:: init_tracing ( ) ;
716+ let err = mqtt:: packet:: v3_1_1:: Publish :: builder ( )
717+ . topic_name ( "test/topic" )
718+ . unwrap ( )
719+ . qos ( mqtt:: packet:: Qos :: ExactlyOnce )
720+ . packet_id ( None :: < u16 > )
721+ . build ( )
722+ . unwrap_err ( ) ;
723+ assert_eq ! ( err, mqtt:: result_code:: MqttError :: MalformedPacket ) ;
724+ }
725+
726+ #[ test]
727+ fn test_qos0_with_none_packet_id_success ( ) {
728+ common:: init_tracing ( ) ;
729+ let result = mqtt:: packet:: v3_1_1:: Publish :: builder ( )
730+ . topic_name ( "test/topic" )
731+ . unwrap ( )
732+ . qos ( mqtt:: packet:: Qos :: AtMostOnce )
733+ . packet_id ( None :: < u16 > )
734+ . build ( )
735+ . unwrap ( ) ;
736+ assert_eq ! ( result. packet_id( ) , None ) ;
737+ }
738+
739+ #[ test]
740+ fn test_qos0_with_some_packet_id_error ( ) {
741+ common:: init_tracing ( ) ;
742+ let err = mqtt:: packet:: v3_1_1:: Publish :: builder ( )
743+ . topic_name ( "test/topic" )
744+ . unwrap ( )
745+ . qos ( mqtt:: packet:: Qos :: AtMostOnce )
746+ . packet_id ( Some ( 42u16 ) )
747+ . build ( )
748+ . unwrap_err ( ) ;
749+ assert_eq ! ( err, mqtt:: result_code:: MqttError :: MalformedPacket ) ;
750+ }
751+
752+ #[ test]
753+ fn test_qos0_without_packet_id_success ( ) {
754+ common:: init_tracing ( ) ;
755+ let result = mqtt:: packet:: v3_1_1:: Publish :: builder ( )
756+ . topic_name ( "test/topic" )
757+ . unwrap ( )
758+ . qos ( mqtt:: packet:: Qos :: AtMostOnce )
759+ . build ( )
760+ . unwrap ( ) ;
761+ assert_eq ! ( result. packet_id( ) , None ) ;
762+ }
0 commit comments