@@ -26,3 +26,138 @@ async fn test_event() -> Result<()> {
2626
2727 Ok ( ( ) )
2828}
29+
30+ #[ tokio:: test]
31+ async fn test_event_screen_size_accepted ( ) -> Result < ( ) > {
32+ let app = common:: app ( ) ;
33+ let ( tx, _rx) = common:: events ( ) ;
34+ let client = common:: TestClient :: new ( app. clone ( ) , tx) ;
35+ app. entities . create ( & Entity { display_name : "Entity 1" . to_string ( ) , id : "entity-1" . to_string ( ) } , & [ ] ) ?;
36+
37+ let user_agent = vec ! [ ( "user-agent" . to_string( ) , "Mozilla/5.0 (Linux x86_64)" . to_string( ) ) ] ;
38+ let event_desktop_screen_size = json ! ( {
39+ "entity_id" : "entity-1" ,
40+ "name" : "pageview" ,
41+ "url" : "https://example.com/" ,
42+ "screen_width" : 1920 ,
43+ "screen_height" : 1080
44+ } ) ;
45+
46+ let res = client. post_with_headers ( "/api/event" , event_desktop_screen_size. clone ( ) , user_agent. clone ( ) ) . await ;
47+ res. assert_status_success ( ) ;
48+
49+ let event_mobile_screen_size = json ! ( {
50+ "entity_id" : "entity-1" ,
51+ "name" : "pageview" ,
52+ "url" : "https://example.com/" ,
53+ "screen_width" : 390 ,
54+ "screen_height" : 844
55+ } ) ;
56+
57+ let res = client. post_with_headers ( "/api/event" , event_mobile_screen_size. clone ( ) , user_agent. clone ( ) ) . await ;
58+ res. assert_status_success ( ) ;
59+
60+ Ok ( ( ) )
61+ }
62+
63+ #[ tokio:: test]
64+ async fn test_event_screen_size_read_write ( ) -> Result < ( ) > {
65+ use chrono:: Utc ;
66+ use liwan:: app:: models:: Event ;
67+
68+ let app = common:: app ( ) ;
69+ let ( tx, _rx) = common:: events ( ) ;
70+ let client = common:: TestClient :: new ( app. clone ( ) , tx) ;
71+
72+ app. seed_database ( 0 ) ?;
73+
74+ let events_to_insert = vec ! [
75+ Event {
76+ entity_id: "entity-1" . to_string( ) ,
77+ visitor_id: "visitor-desktop" . to_string( ) ,
78+ event: "pageview" . to_string( ) ,
79+ created_at: Utc :: now( ) ,
80+ fqdn: Some ( "example.com" . to_string( ) ) ,
81+ path: Some ( "/" . to_string( ) ) ,
82+ referrer: None ,
83+ platform: None ,
84+ browser: None ,
85+ mobile: Some ( false ) ,
86+ country: None ,
87+ city: None ,
88+ utm_source: None ,
89+ utm_medium: None ,
90+ utm_campaign: None ,
91+ utm_content: None ,
92+ utm_term: None ,
93+ screen_width: Some ( 1920 ) ,
94+ screen_height: Some ( 1080 ) ,
95+ } ,
96+ Event {
97+ entity_id: "entity-1" . to_string( ) ,
98+ visitor_id: "visitor-mobile" . to_string( ) ,
99+ event: "pageview" . to_string( ) ,
100+ created_at: Utc :: now( ) ,
101+ fqdn: Some ( "example.com" . to_string( ) ) ,
102+ path: Some ( "/" . to_string( ) ) ,
103+ referrer: None ,
104+ platform: None ,
105+ browser: None ,
106+ mobile: Some ( true ) ,
107+ country: None ,
108+ city: None ,
109+ utm_source: None ,
110+ utm_medium: None ,
111+ utm_campaign: None ,
112+ utm_content: None ,
113+ utm_term: None ,
114+ screen_width: Some ( 390 ) ,
115+ screen_height: Some ( 844 ) ,
116+ } ,
117+ Event {
118+ entity_id: "entity-1" . to_string( ) ,
119+ visitor_id: "visitor-old" . to_string( ) ,
120+ event: "pageview" . to_string( ) ,
121+ created_at: Utc :: now( ) ,
122+ fqdn: Some ( "example.com" . to_string( ) ) ,
123+ path: Some ( "/" . to_string( ) ) ,
124+ referrer: None ,
125+ platform: None ,
126+ browser: None ,
127+ mobile: None ,
128+ country: None ,
129+ city: None ,
130+ utm_source: None ,
131+ utm_medium: None ,
132+ utm_campaign: None ,
133+ utm_content: None ,
134+ utm_term: None ,
135+ screen_width: None ,
136+ screen_height: None ,
137+ } ,
138+ ] ;
139+ app. events . append ( events_to_insert. into_iter ( ) ) ?;
140+
141+ let start = ( Utc :: now ( ) - chrono:: Duration :: hours ( 1 ) ) . to_rfc3339 ( ) ;
142+ let end = Utc :: now ( ) . to_rfc3339 ( ) ;
143+
144+ let query = json ! ( {
145+ "dimension" : "screen_resolution" ,
146+ "filters" : [ ] ,
147+ "metric" : "views" ,
148+ "range" : { "start" : start, "end" : end }
149+ } ) ;
150+
151+ let res = client. post ( "/api/dashboard/project/public-project/dimension" , query. clone ( ) ) . await ;
152+ res. assert_status_success ( ) ;
153+
154+ let body: serde_json:: Value = res. json ( ) ;
155+ let rows = body[ "data" ] . as_array ( ) . expect ( "expected data array" ) ;
156+ let values: Vec < & str > = rows. iter ( ) . filter_map ( |r| r[ "dimensionValue" ] . as_str ( ) ) . collect ( ) ;
157+
158+ assert ! ( values. contains( & "1920x1080" ) , "expected 1920x1080 in results, got: {values:?}" ) ;
159+ assert ! ( values. contains( & "390x844" ) , "expected 390x844 in results, got: {values:?}" ) ;
160+ assert ! ( values. contains( & "Unknown" ) , "expected Unknown for NULL screen data, got: {values:?}" ) ;
161+
162+ Ok ( ( ) )
163+ }
0 commit comments