summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/unit/test_reconcile.js
blob: 1700f89fe39eb880e0a7128f5486e9237565502f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
"use strict";

const TEST_STORE_FILE_NAME = "test-profile.json";
const { CREDIT_CARD_SCHEMA_VERSION } = ChromeUtils.importESModule(
  "resource://autofill/FormAutofillStorageBase.sys.mjs"
);

// NOTE: a guide to reading these test-cases:
// parent: What the local record looked like the last time we wrote the
//         record to the Sync server.
// local:  What the local record looks like now. IOW, the differences between
//         'parent' and 'local' are changes recently made which we wish to sync.
// remote: An incoming record we need to apply (ie, a record that was possibly
//         changed on a remote device)
//
// To further help understanding this, a few of the testcases are annotated.
const ADDRESS_RECONCILE_TESTCASES = [
  {
    description: "Local change",
    parent: {
      // So when we last wrote the record to the server, it had these values.
      guid: "2bbd2d8fbc6b",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
    },
    local: [
      {
        // The current local record - by comparing against parent we can see that
        // only the given-name has changed locally.
        "given-name": "Skip",
        "family-name": "Hammond",
      },
    ],
    remote: {
      // This is the incoming record. It has the same values as "parent", so
      // we can deduce the record hasn't actually been changed remotely so we
      // can safely ignore the incoming record and write our local changes.
      guid: "2bbd2d8fbc6b",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
    },
    reconciled: {
      guid: "2bbd2d8fbc6b",
      "given-name": "Skip",
      "family-name": "Hammond",
    },
  },
  {
    description: "Remote change",
    parent: {
      guid: "e3680e9f890d",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
    },
    local: [
      {
        "given-name": "Mark",
        "family-name": "Hammond",
      },
    ],
    remote: {
      guid: "e3680e9f890d",
      version: 1,
      "given-name": "Skip",
      "family-name": "Hammond",
    },
    reconciled: {
      guid: "e3680e9f890d",
      "given-name": "Skip",
      "family-name": "Hammond",
    },
  },
  {
    description: "New local field",
    parent: {
      guid: "0cba738b1be0",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
    },
    local: [
      {
        "given-name": "Mark",
        "family-name": "Hammond",
        tel: "123456",
      },
    ],
    remote: {
      guid: "0cba738b1be0",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
    },
    reconciled: {
      guid: "0cba738b1be0",
      "given-name": "Mark",
      "family-name": "Hammond",
      tel: "123456",
    },
  },
  {
    description: "New remote field",
    parent: {
      guid: "be3ef97f8285",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
    },
    local: [
      {
        "given-name": "Mark",
        "family-name": "Hammond",
      },
    ],
    remote: {
      guid: "be3ef97f8285",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      tel: "123456",
    },
    reconciled: {
      guid: "be3ef97f8285",
      "given-name": "Mark",
      "family-name": "Hammond",
      tel: "123456",
    },
  },
  {
    description: "Deleted field locally",
    parent: {
      guid: "9627322248ec",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      tel: "123456",
    },
    local: [
      {
        "given-name": "Mark",
        "family-name": "Hammond",
      },
    ],
    remote: {
      guid: "9627322248ec",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      tel: "123456",
    },
    reconciled: {
      guid: "9627322248ec",
      "given-name": "Mark",
      "family-name": "Hammond",
    },
  },
  {
    description: "Deleted field remotely",
    parent: {
      guid: "7d7509f3eeb2",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      tel: "123456",
    },
    local: [
      {
        "given-name": "Mark",
        "family-name": "Hammond",
        tel: "123456",
      },
    ],
    remote: {
      guid: "7d7509f3eeb2",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
    },
    reconciled: {
      guid: "7d7509f3eeb2",
      "given-name": "Mark",
      "family-name": "Hammond",
    },
  },
  {
    description: "Local and remote changes to unrelated fields",
    parent: {
      // The last time we wrote this to the server, country was NZ.
      guid: "e087a06dfc57",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      country: "NZ",
      // We also had an unknown field we round-tripped
      foo: "bar",
    },
    local: [
      {
        // The current local record - so locally we've changed given-name to Skip.
        "given-name": "Skip",
        "family-name": "Hammond",
        country: "NZ",
      },
    ],
    remote: {
      // Remotely, we've changed the country to AU.
      guid: "e087a06dfc57",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      country: "AU",
      // This is a new unknown field that should send instead!
      "unknown-1": "an unknown field from another client",
    },
    reconciled: {
      guid: "e087a06dfc57",
      "given-name": "Skip",
      "family-name": "Hammond",
      country: "AU",
      // This is a new unknown field that should send instead!
      "unknown-1": "an unknown field from another client",
    },
  },
  {
    description: "Multiple local changes",
    parent: {
      guid: "340a078c596f",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      tel: "123456",
    },
    local: [
      {
        "given-name": "Skip",
        "family-name": "Hammond",
      },
      {
        "given-name": "Skip",
        "family-name": "Hammond",
        organization: "Mozilla",
      },
    ],
    remote: {
      guid: "340a078c596f",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      tel: "123456",
      country: "AU",
    },
    reconciled: {
      guid: "340a078c596f",
      "given-name": "Skip",
      "family-name": "Hammond",
      organization: "Mozilla",
      country: "AU",
    },
  },
  {
    // Local and remote diverged from the shared parent, but the values are the
    // same, so we shouldn't fork.
    description: "Same change to local and remote",
    parent: {
      guid: "0b3a72a1bea2",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      // unknown fields we previously roundtripped
      foo: "bar",
    },
    local: [
      {
        "given-name": "Skip",
        "family-name": "Hammond",
      },
    ],
    remote: {
      guid: "0b3a72a1bea2",
      version: 1,
      "given-name": "Skip",
      "family-name": "Hammond",
      // New unknown field that should be the new round trip
      "unknown-1": "an unknown field from another client",
    },
    reconciled: {
      guid: "0b3a72a1bea2",
      "given-name": "Skip",
      "family-name": "Hammond",
    },
  },
  {
    description: "Conflicting changes to single field",
    parent: {
      // This is what we last wrote to the sync server.
      guid: "62068784d089",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      "unknown-1": "an unknown field from another client",
    },
    local: [
      {
        // The current version of the local record - the given-name has changed locally.
        "given-name": "Skip",
        "family-name": "Hammond",
      },
    ],
    remote: {
      // An incoming record has a different given-name than any of the above!
      guid: "62068784d089",
      version: 1,
      "given-name": "Kip",
      "family-name": "Hammond",
      "unknown-1": "an unknown field from another client",
    },
    forked: {
      // So we've forked the local record to a new GUID (and the next sync is
      // going to write this as a new record)
      "given-name": "Skip",
      "family-name": "Hammond",
      "unknown-1": "an unknown field from another client",
    },
    reconciled: {
      // And we've updated the local version of the record to be the remote version.
      guid: "62068784d089",
      "given-name": "Kip",
      "family-name": "Hammond",
      "unknown-1": "an unknown field from another client",
    },
  },
  {
    description: "Conflicting changes to multiple fields",
    parent: {
      guid: "244dbb692e94",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      country: "NZ",
    },
    local: [
      {
        "given-name": "Skip",
        "family-name": "Hammond",
        country: "AU",
      },
    ],
    remote: {
      guid: "244dbb692e94",
      version: 1,
      "given-name": "Kip",
      "family-name": "Hammond",
      country: "CA",
    },
    forked: {
      "given-name": "Skip",
      "family-name": "Hammond",
      country: "AU",
    },
    reconciled: {
      guid: "244dbb692e94",
      "given-name": "Kip",
      "family-name": "Hammond",
      country: "CA",
    },
  },
  {
    description: "Field deleted locally, changed remotely",
    parent: {
      guid: "6fc45e03d19a",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      country: "AU",
    },
    local: [
      {
        "given-name": "Mark",
        "family-name": "Hammond",
      },
    ],
    remote: {
      guid: "6fc45e03d19a",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      country: "NZ",
    },
    forked: {
      "given-name": "Mark",
      "family-name": "Hammond",
    },
    reconciled: {
      guid: "6fc45e03d19a",
      "given-name": "Mark",
      "family-name": "Hammond",
      country: "NZ",
    },
  },
  {
    description: "Field changed locally, deleted remotely",
    parent: {
      guid: "fff9fa27fa18",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      country: "AU",
    },
    local: [
      {
        "given-name": "Mark",
        "family-name": "Hammond",
        country: "NZ",
      },
    ],
    remote: {
      guid: "fff9fa27fa18",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
    },
    forked: {
      "given-name": "Mark",
      "family-name": "Hammond",
      country: "NZ",
    },
    reconciled: {
      guid: "fff9fa27fa18",
      "given-name": "Mark",
      "family-name": "Hammond",
    },
  },
  {
    // Created, last modified should be synced; last used and times used should
    // be local. Remote created time older than local, remote modified time
    // newer than local.
    description:
      "Created, last modified time reconciliation without local changes",
    parent: {
      guid: "5113f329c42f",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      timeCreated: 1234,
      timeLastModified: 5678,
      timeLastUsed: 5678,
      timesUsed: 6,
    },
    local: [],
    remote: {
      guid: "5113f329c42f",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      timeCreated: 1200,
      timeLastModified: 5700,
      timeLastUsed: 5700,
      timesUsed: 3,
    },
    reconciled: {
      guid: "5113f329c42f",
      "given-name": "Mark",
      "family-name": "Hammond",
      timeCreated: 1200,
      timeLastModified: 5700,
      timeLastUsed: 5678,
      timesUsed: 6,
    },
  },
  {
    // Local changes, remote created time newer than local, remote modified time
    // older than local.
    description:
      "Created, last modified time reconciliation with local changes",
    parent: {
      guid: "791e5608b80a",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      timeCreated: 1234,
      timeLastModified: 5678,
      timeLastUsed: 5678,
      timesUsed: 6,
    },
    local: [
      {
        "given-name": "Skip",
        "family-name": "Hammond",
      },
    ],
    remote: {
      guid: "791e5608b80a",
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      timeCreated: 1300,
      timeLastModified: 5000,
      timeLastUsed: 5000,
      timesUsed: 3,
    },
    reconciled: {
      guid: "791e5608b80a",
      "given-name": "Skip",
      "family-name": "Hammond",
      timeCreated: 1234,
      timeLastUsed: 5678,
      timesUsed: 6,
    },
  },
];

const CREDIT_CARD_RECONCILE_TESTCASES = [
  {
    description: "Local change",
    parent: {
      // So when we last wrote the record to the server, it had these values.
      guid: "2bbd2d8fbc6b",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "unknown-1": "an unknown field from another client",
    },
    local: [
      {
        // The current local record - by comparing against parent we can see that
        // only the cc-number has changed locally.
        "cc-name": "John Doe",
        "cc-number": "4929001587121045",
      },
    ],
    remote: {
      // This is the incoming record. It has the same values as "parent", so
      // we can deduce the record hasn't actually been changed remotely so we
      // can safely ignore the incoming record and write our local changes.
      guid: "2bbd2d8fbc6b",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "unknown-2": "a newer unknown field",
    },
    reconciled: {
      guid: "2bbd2d8fbc6b",
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
      "unknown-2": "a newer unknown field",
    },
  },
  {
    description: "Remote change",
    parent: {
      guid: "e3680e9f890d",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "unknown-1": "unknown field",
    },
    local: [
      {
        "cc-name": "John Doe",
        "cc-number": "4111111111111111",
      },
    ],
    remote: {
      guid: "e3680e9f890d",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
      "unknown-1": "unknown field",
    },
    reconciled: {
      guid: "e3680e9f890d",
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
      "unknown-1": "unknown field",
    },
  },

  {
    description: "New local field",
    parent: {
      guid: "0cba738b1be0",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
    },
    local: [
      {
        "cc-name": "John Doe",
        "cc-number": "4111111111111111",
        "cc-exp-month": 12,
      },
    ],
    remote: {
      guid: "0cba738b1be0",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
    },
    reconciled: {
      guid: "0cba738b1be0",
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
    },
  },
  {
    description: "New remote field",
    parent: {
      guid: "be3ef97f8285",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
    },
    local: [
      {
        "cc-name": "John Doe",
        "cc-number": "4111111111111111",
      },
    ],
    remote: {
      guid: "be3ef97f8285",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
    },
    reconciled: {
      guid: "be3ef97f8285",
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
    },
  },
  {
    description: "Deleted field locally",
    parent: {
      guid: "9627322248ec",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
    },
    local: [
      {
        "cc-name": "John Doe",
        "cc-number": "4111111111111111",
      },
    ],
    remote: {
      guid: "9627322248ec",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
    },
    reconciled: {
      guid: "9627322248ec",
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
    },
  },
  {
    description: "Deleted field remotely",
    parent: {
      guid: "7d7509f3eeb2",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
    },
    local: [
      {
        "cc-name": "John Doe",
        "cc-number": "4111111111111111",
        "cc-exp-month": 12,
      },
    ],
    remote: {
      guid: "7d7509f3eeb2",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
    },
    reconciled: {
      guid: "7d7509f3eeb2",
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
    },
  },
  {
    description: "Local and remote changes to unrelated fields",
    parent: {
      // The last time we wrote this to the server, "cc-exp-month" was 12.
      guid: "e087a06dfc57",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
      "unknown-1": "unknown field",
    },
    local: [
      {
        // The current local record - so locally we've changed "cc-number".
        "cc-name": "John Doe",
        "cc-number": "4929001587121045",
        "cc-exp-month": 12,
      },
    ],
    remote: {
      // Remotely, we've changed "cc-exp-month" to 1.
      guid: "e087a06dfc57",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 1,
      "unknown-2": "a newer unknown field",
    },
    reconciled: {
      guid: "e087a06dfc57",
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
      "cc-exp-month": 1,
      "unknown-2": "a newer unknown field",
    },
  },
  {
    description: "Multiple local changes",
    parent: {
      guid: "340a078c596f",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "unknown-1": "unknown field",
    },
    local: [
      {
        "cc-name": "Skip",
        "cc-number": "4111111111111111",
      },
      {
        "cc-name": "Skip",
        "cc-number": "4111111111111111",
        "cc-exp-month": 12,
      },
    ],
    remote: {
      guid: "340a078c596f",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-year": 2000,
      "unknown-1": "unknown field",
    },
    reconciled: {
      guid: "340a078c596f",
      "cc-name": "Skip",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
      "cc-exp-year": 2000,
      "unknown-1": "unknown field",
    },
  },
  {
    // Local and remote diverged from the shared parent, but the values are the
    // same, so we shouldn't fork.
    description: "Same change to local and remote",
    parent: {
      guid: "0b3a72a1bea2",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
    },
    local: [
      {
        "cc-name": "John Doe",
        "cc-number": "4929001587121045",
      },
    ],
    remote: {
      guid: "0b3a72a1bea2",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
    },
    reconciled: {
      guid: "0b3a72a1bea2",
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
    },
  },
  {
    description: "Conflicting changes to single field",
    parent: {
      // This is what we last wrote to the sync server.
      guid: "62068784d089",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "unknown-1": "unknown field",
    },
    local: [
      {
        // The current version of the local record - the cc-number has changed locally.
        "cc-name": "John Doe",
        "cc-number": "5103059495477870",
      },
    ],
    remote: {
      // An incoming record has a different cc-number than any of the above!
      guid: "62068784d089",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
      "unknown-1": "unknown field",
    },
    forked: {
      // So we've forked the local record to a new GUID (and the next sync is
      // going to write this as a new record)
      "cc-name": "John Doe",
      "cc-number": "5103059495477870",
      "unknown-1": "unknown field",
    },
    reconciled: {
      // And we've updated the local version of the record to be the remote version.
      guid: "62068784d089",
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
      "unknown-1": "unknown field",
    },
  },
  {
    description: "Conflicting changes to multiple fields",
    parent: {
      guid: "244dbb692e94",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
    },
    local: [
      {
        "cc-name": "John Doe",
        "cc-number": "5103059495477870",
        "cc-exp-month": 1,
      },
    ],
    remote: {
      guid: "244dbb692e94",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
      "cc-exp-month": 3,
    },
    forked: {
      "cc-name": "John Doe",
      "cc-number": "5103059495477870",
      "cc-exp-month": 1,
    },
    reconciled: {
      guid: "244dbb692e94",
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
      "cc-exp-month": 3,
    },
  },
  {
    description: "Field deleted locally, changed remotely",
    parent: {
      guid: "6fc45e03d19a",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
    },
    local: [
      {
        "cc-name": "John Doe",
        "cc-number": "4111111111111111",
      },
    ],
    remote: {
      guid: "6fc45e03d19a",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 3,
    },
    forked: {
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
    },
    reconciled: {
      guid: "6fc45e03d19a",
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 3,
    },
  },
  {
    description: "Field changed locally, deleted remotely",
    parent: {
      guid: "fff9fa27fa18",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 12,
    },
    local: [
      {
        "cc-name": "John Doe",
        "cc-number": "4111111111111111",
        "cc-exp-month": 3,
      },
    ],
    remote: {
      guid: "fff9fa27fa18",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
    },
    forked: {
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      "cc-exp-month": 3,
    },
    reconciled: {
      guid: "fff9fa27fa18",
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
    },
  },
  {
    // Created, last modified should be synced; last used and times used should
    // be local. Remote created time older than local, remote modified time
    // newer than local.
    description:
      "Created, last modified time reconciliation without local changes",
    parent: {
      guid: "5113f329c42f",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      timeCreated: 1234,
      timeLastModified: 5678,
      timeLastUsed: 5678,
      timesUsed: 6,
    },
    local: [],
    remote: {
      guid: "5113f329c42f",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      timeCreated: 1200,
      timeLastModified: 5700,
      timeLastUsed: 5700,
      timesUsed: 3,
    },
    reconciled: {
      guid: "5113f329c42f",
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      timeCreated: 1200,
      timeLastModified: 5700,
      timeLastUsed: 5678,
      timesUsed: 6,
    },
  },
  {
    // Local changes, remote created time newer than local, remote modified time
    // older than local.
    description:
      "Created, last modified time reconciliation with local changes",
    parent: {
      guid: "791e5608b80a",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      timeCreated: 1234,
      timeLastModified: 5678,
      timeLastUsed: 5678,
      timesUsed: 6,
    },
    local: [
      {
        "cc-name": "John Doe",
        "cc-number": "4929001587121045",
      },
    ],
    remote: {
      guid: "791e5608b80a",
      version: CREDIT_CARD_SCHEMA_VERSION,
      "cc-name": "John Doe",
      "cc-number": "4111111111111111",
      timeCreated: 1300,
      timeLastModified: 5000,
      timeLastUsed: 5000,
      timesUsed: 3,
    },
    reconciled: {
      guid: "791e5608b80a",
      "cc-name": "John Doe",
      "cc-number": "4929001587121045",
      timeCreated: 1234,
      timeLastUsed: 5678,
      timesUsed: 6,
    },
  },
];

add_task(async function test_reconcile_unknown_version() {
  let profileStorage = await initProfileStorage(TEST_STORE_FILE_NAME);

  // Cross-version reconciliation isn't supported yet. See bug 1377204.
  await Assert.rejects(
    profileStorage.addresses.reconcile({
      guid: "31d83d2725ec",
      version: 3,
      "given-name": "Mark",
      "family-name": "Hammond",
    }),
    /Got unknown record version/
  );
});

add_task(async function test_reconcile_idempotent() {
  let profileStorage = await initProfileStorage(TEST_STORE_FILE_NAME);

  let guid = "de1ba7b094fe";
  await profileStorage.addresses.add(
    {
      guid,
      version: 1,
      "given-name": "Mark",
      "family-name": "Hammond",
      // an unknown field from a previous sync
      foo: "bar",
    },
    { sourceSync: true }
  );
  await profileStorage.addresses.update(guid, {
    "given-name": "Skip",
    "family-name": "Hammond",
    organization: "Mozilla",
  });

  let remote = {
    guid,
    version: 1,
    "given-name": "Mark",
    "family-name": "Hammond",
    tel: "123456",
    "unknown-1": "an unknown field from another client",
  };

  {
    let { forkedGUID } = await profileStorage.addresses.reconcile(remote);
    let updatedRecord = await profileStorage.addresses.get(guid, {
      rawData: true,
    });

    ok(!forkedGUID, "First merge should not fork record");
    ok(
      objectMatches(updatedRecord, {
        guid: "de1ba7b094fe",
        "given-name": "Skip",
        "family-name": "Hammond",
        organization: "Mozilla",
        tel: "123456",
        "unknown-1": "an unknown field from another client",
      }),
      "First merge should merge local and remote changes"
    );
  }

  {
    let { forkedGUID } = await profileStorage.addresses.reconcile(remote);
    let updatedRecord = await profileStorage.addresses.get(guid, {
      rawData: true,
    });

    ok(!forkedGUID, "Second merge should not fork record");
    ok(
      objectMatches(updatedRecord, {
        guid: "de1ba7b094fe",
        "given-name": "Skip",
        "family-name": "Hammond",
        organization: "Mozilla",
        tel: "123456",
        "unknown-1": "an unknown field from another client",
      }),
      "Second merge should not change record"
    );
  }
});

add_task(async function test_reconcile_three_way_merge() {
  let TESTCASES = {
    addresses: ADDRESS_RECONCILE_TESTCASES,
    creditCards: CREDIT_CARD_RECONCILE_TESTCASES,
  };

  for (let collectionName in TESTCASES) {
    info(`Start to test reconcile on ${collectionName}`);

    let profileStorage = await initProfileStorage(
      TEST_STORE_FILE_NAME,
      null,
      collectionName
    );

    for (let test of TESTCASES[collectionName]) {
      info(test.description);

      await profileStorage[collectionName].add(test.parent, {
        sourceSync: true,
      });

      for (let updatedRecord of test.local) {
        await profileStorage[collectionName].update(
          test.parent.guid,
          updatedRecord
        );
      }

      let localRecord = await profileStorage[collectionName].get(
        test.parent.guid,
        {
          rawData: true,
        }
      );

      let onReconciled = TestUtils.topicObserved(
        "formautofill-storage-changed",
        (subject, data) =>
          data == "reconcile" &&
          subject.wrappedJSObject.collectionName == collectionName
      );
      let { forkedGUID } = await profileStorage[collectionName].reconcile(
        test.remote
      );
      await onReconciled;
      let reconciledRecord = await profileStorage[collectionName].get(
        test.parent.guid,
        {
          rawData: true,
        }
      );
      if (forkedGUID) {
        let forkedRecord = await profileStorage[collectionName].get(
          forkedGUID,
          {
            rawData: true,
          }
        );

        notEqual(forkedRecord.guid, reconciledRecord.guid);
        equal(forkedRecord.timeLastModified, localRecord.timeLastModified);
        ok(
          objectMatches(forkedRecord, test.forked),
          `${test.description} should fork record`
        );
      } else {
        ok(!test.forked, `${test.description} should not fork record`);
      }

      ok(objectMatches(reconciledRecord, test.reconciled));
    }
  }
});