1+ import 'dart:convert' ;
2+ import 'package:flutter_test/flutter_test.dart' ;
3+ import 'package:flutter_local_db/src/model/local_db_request_model.dart' ;
4+
5+ void main () {
6+ group ('Hash Verification - toJson() Usage' , () {
7+ test ('Verify toJson() includes auto-generated hash' , () {
8+ print ('\n 🔍 TESTING ACTUAL toJson() BEHAVIOR' );
9+
10+ // Test data without hash
11+ final data = {'name' : 'Juan' , 'age' : 25 };
12+ final model = LocalDbModel (id: 'test-user' , data: data);
13+
14+ // Convert to JSON like the database does
15+ final json = model.toJson ();
16+ final jsonString = jsonEncode (json);
17+
18+ print ('📝 Original data: $data ' );
19+ print ('🏷️ Model hash field: ${model .hash }' );
20+ print ('📤 JSON output: $json ' );
21+ print ('🚀 JSON string to Rust: $jsonString ' );
22+
23+ // Verify hash was auto-generated
24+ expect (json['hash' ], isA <String >());
25+ expect (json['hash' ], isNot (equals ('null' )));
26+ expect (json['hash' ], equals (data.hashCode.toString ()));
27+
28+ // Verify JSON structure is correct for Rust
29+ expect (json.containsKey ('id' ), true );
30+ expect (json.containsKey ('hash' ), true );
31+ expect (json.containsKey ('data' ), true );
32+
33+ print ('✅ Hash auto-generated: ${json ['hash' ]}' );
34+ print ('✅ JSON structure valid for Rust' );
35+ });
36+
37+ test ('Verify custom hash is preserved' , () {
38+ print ('\n 🔍 TESTING CUSTOM HASH PRESERVATION' );
39+
40+ final data = {'name' : 'Maria' , 'age' : 30 };
41+ final model = LocalDbModel (id: 'test-user-2' , hash: 'custom-hash-123' , data: data);
42+
43+ final json = model.toJson ();
44+ final jsonString = jsonEncode (json);
45+
46+ print ('📝 Original data: $data ' );
47+ print ('🏷️ Custom hash: custom-hash-123' );
48+ print ('📤 JSON output: $json ' );
49+ print ('🚀 JSON string to Rust: $jsonString ' );
50+
51+ // Verify custom hash is used
52+ expect (json['hash' ], equals ('custom-hash-123' ));
53+ expect (json['hash' ], isNot (equals (data.hashCode.toString ())));
54+
55+ print ('✅ Custom hash preserved: ${json ['hash' ]}' );
56+ });
57+
58+ test ('Verify complex data hash generation' , () {
59+ print ('\n 🔍 TESTING COMPLEX DATA HASH' );
60+
61+ final complexData = {
62+ 'user' : {
63+ 'name' : 'Carlos' ,
64+ 'profile' : {
65+ 'age' : 35 ,
66+ 'skills' : ['Flutter' , 'Dart' , 'Rust' ],
67+ },
68+ },
69+ 'metadata' : {
70+ 'created' : '2024-01-01' ,
71+ 'active' : true ,
72+ },
73+ };
74+
75+ final model = LocalDbModel (id: 'complex-user' , data: complexData);
76+ final json = model.toJson ();
77+ final jsonString = jsonEncode (json);
78+
79+ print ('📝 Complex data keys: ${complexData .keys }' );
80+ print ('🏷️ Generated hash: ${json ['hash' ]}' );
81+ print ('📤 JSON length: ${jsonString .length } chars' );
82+ print ('🚀 JSON preview: ${jsonString .substring (0 , 100 )}...' );
83+
84+ // Verify hash generation for complex data
85+ expect (json['hash' ], isA <String >());
86+ expect (json['hash' ], equals (complexData.hashCode.toString ()));
87+
88+ print ('✅ Complex data hash generated successfully' );
89+ });
90+ });
91+ }
0 commit comments