aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Database/Tests/Unit/FSyncPointTests.m
blob: 442975e3141d97b3e96c2f82cfdf0054473cd029 (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
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <Foundation/Foundation.h>
#import "FirebaseCommunity/FIRLogger.h"
#import "FSyncPointTests.h"
#import "FListenProvider.h"
#import "FQuerySpec.h"
#import "FQueryParams.h"
#import "FPathIndex.h"
#import "FKeyIndex.h"
#import "FPriorityIndex.h"
#import "FIRDatabaseQuery_Private.h"
#import "FSyncTree.h"
#import "FChange.h"
#import "FDataEvent.h"
#import "FIRDataSnapshot_Private.h"
#import "FCancelEvent.h"
#import "FSnapshotUtilities.h"
#import "FEventRegistration.h"
#import "FCompoundWrite.h"
#import "FEmptyNode.h"
#import "FTestClock.h"
#import "FIRDatabaseConfig_Private.h"
#import "FSnapshotUtilities.h"

typedef NSDictionary* (^fbt_nsdictionary_void)(void);

@interface FTestEventRegistration : NSObject<FEventRegistration>
@property (nonatomic, strong) NSDictionary *spec;
@property (nonatomic, strong) FQuerySpec *query;
@end

@implementation FTestEventRegistration
- (id) initWithSpec:(NSDictionary *)eventSpec query:(FQuerySpec *)query {
    self = [super init];
    if (self) {
        self.spec = eventSpec;
        self.query = query;
    }
    return self;
}

- (BOOL) responseTo:(FIRDataEventType)eventType {
    return YES;
}
- (FDataEvent *) createEventFrom:(FChange *)change query:(FQuerySpec *)query {
    FIRDataSnapshot *snap = nil;
    FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithRepo:nil path:query.path];
    if (change.type == FIRDataEventTypeValue) {
        snap = [[FIRDataSnapshot alloc] initWithRef:ref indexedNode:change.indexedNode];
    } else {
        snap = [[FIRDataSnapshot alloc] initWithRef:[ref child:change.childKey]
                                        indexedNode:change.indexedNode];
    }
    return [[FDataEvent alloc] initWithEventType:change.type eventRegistration:self dataSnapshot:snap prevName:change.prevKey];
}

- (BOOL) matches:(id<FEventRegistration>)other {
    if (![other isKindOfClass:[FTestEventRegistration class]]) {
        return NO;
    } else {
        FTestEventRegistration *otherRegistration = other;
        if (self.spec[@"callbackId"] && otherRegistration.spec[@"callbackId"] &&
                [self.spec[@"callbackId"] isEqualToNumber:otherRegistration.spec[@"callbackId"]]) {
            return YES;
        } else {
            return NO;
        }
    }
}

- (void) fireEvent:(id<FEvent>)event queue:(dispatch_queue_t)queue {
    [NSException raise:@"NotImplementedError" format:@"Method not implemneted."];
}
- (FCancelEvent *) createCancelEventFromError:(NSError *)error path:(FPath *)path {
    [NSException raise:@"NotImplementedError" format:@"Method not implemneted."];
    return nil;
}

- (FIRDatabaseHandle) handle {
    [NSException raise:@"NotImplementedError" format:@"Method not implemneted."];
    return 0;
}
@end

@implementation FSyncPointTests

- (NSString *) queryKeyForQuery:(FQuerySpec *)query tagId:(NSNumber *)tagId {
    return [NSString stringWithFormat:@"%@|%@|%@", query.path, query.params, tagId];
}

- (void) actualEvent:(FDataEvent *)actual equalsExpected:(NSDictionary *)expected {
    XCTAssertEqual(actual.eventType, [self stringToEventType:expected[@"type"]], @"Event type should be equal");
    if (actual.eventType != FIRDataEventTypeValue) {
        NSString *childName = actual.snapshot.key;
        XCTAssertEqualObjects(childName, expected[@"name"], @"Snapshot name should be equal");
        if (expected[@"prevName"] == [NSNull null]) {
            XCTAssertNil(actual.prevName, @"prevName should be nil");
        } else {
            XCTAssertEqualObjects(actual.prevName, expected[@"prevName"], @"prevName should be equal");
        }
    }
    NSString *actualHash = [actual.snapshot.node.node dataHash];
    NSString *expectedHash = [[FSnapshotUtilities nodeFrom:expected[@"data"]] dataHash];
    XCTAssertEqualObjects(actualHash, expectedHash, @"Data hash should be equal");
}

/**
* @param actual is an array of id<FEvent>
* @param expected is an array of dictionaries?
*/
- (void) actualEvents:(NSArray *)actual exactMatchesExpected:(NSArray *)expected {
    if ([expected count] < [actual count]) {
        XCTFail(@"Got extra events: %@", actual);
    } else if ([expected count] > [actual count]) {
        XCTFail(@"Missing events: %@", actual);
    } else {
        NSUInteger i = 0;
        for (i = 0; i < [expected count]; i++) {
            FDataEvent *actualEvent = actual[i];
            NSDictionary *expectedEvent = expected[i];
            [self actualEvent:actualEvent equalsExpected:expectedEvent];
        }
    }
}

- (void)assertOrderedFirstEvent:(FIRDataEventType)e1 secondEvent:(FIRDataEventType)e2 {
    static NSArray *eventOrdering = nil;
    if (!eventOrdering) {
        eventOrdering = @[
                [NSNumber numberWithInteger:FIRDataEventTypeChildRemoved],
                [NSNumber numberWithInteger:FIRDataEventTypeChildAdded],
                [NSNumber numberWithInteger:FIRDataEventTypeChildMoved],
                [NSNumber numberWithInteger:FIRDataEventTypeChildChanged],
                [NSNumber numberWithInteger:FIRDataEventTypeValue]
        ];
    }
    NSUInteger idx1 = [eventOrdering indexOfObject:[NSNumber numberWithInteger:e1]];
    NSUInteger idx2 = [eventOrdering indexOfObject:[NSNumber numberWithInteger:e2]];
    if (idx1 > idx2) {
        XCTFail(@"Received %d after %d", (int)e2, (int)e1);
    }
}

- (FIRDataEventType)stringToEventType:(NSString *)stringType {
    if ([stringType isEqualToString:@"child_added"]) {
        return FIRDataEventTypeChildAdded;
    } else if ([stringType isEqualToString:@"child_removed"]) {
        return FIRDataEventTypeChildRemoved;
    } else if ([stringType isEqualToString:@"child_changed"]) {
        return FIRDataEventTypeChildChanged;
    } else if ([stringType isEqualToString:@"child_moved"]) {
        return FIRDataEventTypeChildMoved;
    } else if ([stringType isEqualToString:@"value"]) {
        return FIRDataEventTypeValue;
    } else {
        XCTFail(@"Unknown event type %@", stringType);
        return FIRDataEventTypeValue;
    }
}

- (void) actualEventSet:(id)actual matchesExpected:(id)expected atBasePath:(NSString *)basePathStr {
    // don't worry about order for now
    XCTAssertEqual([expected count], [actual count], @"Mismatched lengths.\nExpected: %@\nActual: %@", expected, actual);

    NSArray *currentExpected = expected;
    NSArray *currentActual = actual;
    FPath *basePath = basePathStr != nil ? [[FPath alloc] initWith:basePathStr] : [FPath empty];
    while ([currentExpected count] > 0) {
        // Step 1: find location range in expected
        // we expect all events for a particular path to be in a group
        FPath *currentPath = [basePath childFromString:currentExpected[0][@"path"]];
        NSUInteger i = 1;
        while (i < [currentExpected count]) {
            FPath *otherPath = [basePath childFromString:currentExpected[i][@"path"]];
            if ([currentPath isEqual:otherPath]) {
                i++;
            } else {
                break;
            }
        }

        // Step 2: foreach in actual, asserting location
        NSUInteger j = 0;
        for (j = 0; j < i; j++) {
            FDataEvent *actualEventData = currentActual[j];
            FTestEventRegistration *eventRegistration = actualEventData.eventRegistration;
            NSDictionary *specStep = eventRegistration.spec;
            FPath *actualPath = [basePath childFromString:specStep[@"path"]];
            if (![currentPath isEqual:actualPath]) {
                XCTFail(@"Expected path %@ to equal %@", actualPath, currentPath);
            }
        }

        // Step 3: slice each array
        NSMutableArray *expectedSlice = [[currentExpected subarrayWithRange:NSMakeRange(0, i)] mutableCopy];
        NSArray *actualSlice = [currentActual subarrayWithRange:NSMakeRange(0, i)];

        // foreach in actual, stack up to enforce ordering, find in expected
        NSMutableDictionary *actualMap = [[NSMutableDictionary alloc] init];
        for (FDataEvent *actualEvent in actualSlice) {
            FTestEventRegistration *eventRegistration = actualEvent.eventRegistration;
            FQuerySpec *query = eventRegistration.query;
            NSDictionary *spec = eventRegistration.spec;
            NSString *listenId = [NSString stringWithFormat:@"%@|%@", [basePath childFromString:spec[@"path"]], query];
            if (actualMap[listenId]) {
                // stack this event up, and make sure it obeys ordering constraints
                NSMutableArray *eventStack = actualMap[listenId];
                FDataEvent *prevEvent = eventStack[[eventStack count] - 1];
                [self assertOrderedFirstEvent:prevEvent.eventType secondEvent:actualEvent.eventType];
                [eventStack addObject:actualEvent];
            } else {
                // this is the first event for this listen, just initialize it
                actualMap[listenId] = [[NSMutableArray alloc] initWithObjects:actualEvent, nil];
            }
            // Ordering has been enforced, make sure we can find this in the expected events
            __block NSUInteger indexToRemove = NSNotFound;
            [expectedSlice enumerateObjectsUsingBlock:^(NSDictionary *expectedEvent, NSUInteger idx, BOOL *stop) {
                if ([self stringToEventType:expectedEvent[@"type"]] == actualEvent.eventType) {
                    if ([self stringToEventType:expectedEvent[@"type"]] != FIRDataEventTypeValue) {
                        if (![expectedEvent[@"name"] isEqualToString:actualEvent.snapshot.key]) {
                            return; // short circuit, not a match
                        }
                        if ([self stringToEventType:expectedEvent[@"type"]] != FIRDataEventTypeChildRemoved &&
                            !(expectedEvent[@"prevName"] == [NSNull null] && actualEvent.prevName == nil) &&
                            !(expectedEvent[@"prevName"] != [NSNull null] && [expectedEvent[@"prevName"] isEqualToString:actualEvent.prevName])) {
                            return; // short circuit, not a match
                        }
                    }
                    // make sure the snapshots match
                    NSString *snapHash = [actualEvent.snapshot.node.node dataHash];
                    NSString *expectedHash = [[FSnapshotUtilities nodeFrom:expectedEvent[@"data"]] dataHash];
                    if ([snapHash isEqualToString:expectedHash]) {
                        indexToRemove = idx;
                        *stop = YES;
                    }
                }
            }];
            XCTAssertFalse(indexToRemove == NSNotFound, @"Could not find matching expected event for %@", actualEvent);
            [expectedSlice removeObjectAtIndex:indexToRemove];
        }
        currentExpected = [currentExpected subarrayWithRange:NSMakeRange(i, [currentExpected count] - i)];
        currentActual = [currentActual subarrayWithRange:NSMakeRange(i, [currentActual count] - i)];
    }
}

- (FQuerySpec *)parseParams:(NSDictionary *)specParams forPath:(FPath *)path {
    FQueryParams *query = [[FQueryParams alloc] init];
    NSMutableDictionary *params;

    if (specParams) {
        params = [specParams mutableCopy];
        if (!params[@"tag"]) {
            XCTFail(@"Error: Non-default queries must have tag");
        }
    } else {
        params = [NSMutableDictionary dictionary];
    }

    if (params[@"orderBy"]) {
        FPath *indexPath = [FPath pathWithString:params[@"orderBy"]];
        id<FIndex> index = [[FPathIndex alloc] initWithPath:indexPath];
        query = [query orderBy:index];
        [params removeObjectForKey:@"orderBy"];
    }
    if (params[@"orderByKey"]) {
        query = [query orderBy:[FKeyIndex keyIndex]];
        [params removeObjectForKey:@"orderByKey"];
    }
    if (params[@"orderByPriority"]) {
        query = [query orderBy:[FPriorityIndex priorityIndex]];
        [params removeObjectForKey:@"orderByPriority"];
    }

    if (params[@"startAt"]) {
        id<FNode> node = [FSnapshotUtilities nodeFrom:params[@"startAt"][@"index"]];
        if (params[@"startAt"][@"name"]) {
            query = [query startAt:node childKey:params[@"startAt"][@"name"]];
        } else {
            query = [query startAt:node];
        }
        [params removeObjectForKey:@"startAt"];
    }
    if (params[@"endAt"]) {
        id<FNode> node = [FSnapshotUtilities nodeFrom:params[@"endAt"][@"index"]];
        if (params[@"endAt"][@"name"]) {
            query = [query endAt:node childKey:params[@"endAt"][@"name"]];
        } else {
            query = [query endAt:node];
        }
        [params removeObjectForKey:@"endAt"];
    }
    if (params[@"equalTo"]) {
        id<FNode> node = [FSnapshotUtilities nodeFrom:params[@"equalTo"][@"index"]];
        if (params[@"equalTo"][@"name"]) {
            NSString *name = params[@"equalTo"][@"name"];
            query = [[query startAt:node childKey:name] endAt:node childKey:name];
        } else {
            query = [[query startAt:node] endAt:node];
        }
        [params removeObjectForKey:@"equalTo"];
    }

    if (params[@"limitToFirst"]) {
        query = [query limitToFirst:[params[@"limitToFirst"] integerValue]];
        [params removeObjectForKey:@"limitToFirst"];
    }
    if (params[@"limitToLast"]) {
        query = [query limitToLast:[params[@"limitToLast"] integerValue]];
        [params removeObjectForKey:@"limitToLast"];
    }

    [params removeObjectForKey:@"tag"];
    if ([params count] > 0) {
        XCTFail(@"Unsupported query parameter: %@", params);
    }
    return [[FQuerySpec alloc] initWithPath:path params:query];
}

- (void) runTest:(NSDictionary *)testSpec atBasePath:(NSString *)basePath {
    NSMutableDictionary *listens = [[NSMutableDictionary alloc] init];
    __weak FSyncPointTests *weakSelf = self;

    FListenProvider *listenProvider = [[FListenProvider alloc] init];
    listenProvider.startListening = ^(FQuerySpec *query, NSNumber *tagId, id<FSyncTreeHash> hash, fbt_nsarray_nsstring onComplete) {
        FQueryParams *queryParams = query.params;
        FPath *path = query.path;
        NSString *logTag = [NSString stringWithFormat:@"%@ (%@)", queryParams, tagId];
        NSString *key = [weakSelf queryKeyForQuery:query tagId:tagId];
        FFLog(@"I-RDB143001", @"Listening at %@ for %@", path, logTag);
        id existing = listens[key];
        NSAssert(existing == nil, @"Duplicate listen");
        listens[key] = @YES;
        return @[];
    };

    listenProvider.stopListening = ^(FQuerySpec *query, NSNumber *tagId) {
        FQueryParams *queryParams = query.params;
        FPath *path = query.path;
        NSString *logTag = [NSString stringWithFormat:@"%@ (%@)", queryParams, tagId];
        NSString *key = [weakSelf queryKeyForQuery:query tagId:tagId];
        FFLog(@"I-RDB143002", @"Stop listening at %@ for %@", path, logTag);
        id existing = listens[key];
        XCTAssertTrue(existing != nil, @"Missing record of query that we're removing");
        [listens removeObjectForKey:key];
    };

    FSyncTree *syncTree = [[FSyncTree alloc] initWithListenProvider:listenProvider];

    NSLog(@"Running %@", testSpec[@"name"]);
    NSInteger currentWriteId = 0;
    for (NSDictionary *step in testSpec[@"steps"]) {
        NSMutableDictionary *spec = [step mutableCopy];
        if (spec[@".comment"]) {
            NSLog(@" > %@", spec[@".comment"]);
        }
        if (spec[@"debug"] != nil) {
            // TODO: Ideally we'd pause the debugger somehow (like "debugger;" in JS).
            NSLog(@"Start debugging");
        }
        // Almost everything has a path...
        FPath *path = [FPath empty];
        if (basePath != nil) {
            path = [path childFromString:basePath];
        }
        if (spec[@"path"] != nil) {
            path = [path childFromString:spec[@"path"]];
        }
        NSArray *events;
        if ([spec[@"type"] isEqualToString:@"listen"]) {
            FQuerySpec *query = [self parseParams:spec[@"params"] forPath:path];
            FTestEventRegistration *eventRegistration = [[FTestEventRegistration alloc] initWithSpec:spec query:query];
            events = [syncTree addEventRegistration:eventRegistration forQuery:query];
            [self actualEvents:events exactMatchesExpected:spec[@"events"]];

        } else if ([spec[@"type"] isEqualToString:@"unlisten"]) {
            FQuerySpec *query = [self parseParams:spec[@"params"] forPath:path];
            FTestEventRegistration *eventRegistration = [[FTestEventRegistration alloc] initWithSpec:spec query:query];
            events = [syncTree removeEventRegistration:eventRegistration forQuery:query cancelError:nil];
            [self actualEvents:events exactMatchesExpected:spec[@"events"]];

        } else if ([spec[@"type"] isEqualToString:@"serverUpdate"]) {
            id<FNode> update = [FSnapshotUtilities nodeFrom:spec[@"data"]];
            if (spec[@"tag"]) {
                events = [syncTree applyTaggedQueryOverwriteAtPath:path newData:update tagId:spec[@"tag"]];
            } else {
                events = [syncTree applyServerOverwriteAtPath:path newData:update];
            }
            [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];

        } else if ([spec[@"type"] isEqualToString:@"serverMerge"]) {
            FCompoundWrite *compoundWrite = [FCompoundWrite compoundWriteWithValueDictionary:spec[@"data"]];
            if (spec[@"tag"]) {
                events = [syncTree applyTaggedQueryMergeAtPath:path changedChildren:compoundWrite tagId:spec[@"tag"]];
            } else {
                events = [syncTree applyServerMergeAtPath:path changedChildren:compoundWrite];
            }
            [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];

        } else if ([spec[@"type"] isEqualToString:@"set"]) {
            id<FNode> toSet = [FSnapshotUtilities nodeFrom:spec[@"data"]];
            BOOL visible = (spec[@"visible"] != nil) ? [spec[@"visible"] boolValue] : YES;
            events = [syncTree applyUserOverwriteAtPath:path newData:toSet writeId:currentWriteId++ isVisible:visible];
            [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];

        } else if ([spec[@"type"] isEqualToString:@"update"]) {
            FCompoundWrite *compoundWrite = [FCompoundWrite compoundWriteWithValueDictionary:spec[@"data"]];
            events = [syncTree applyUserMergeAtPath:path changedChildren:compoundWrite writeId:currentWriteId++];
            [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
        } else if ([spec[@"type"] isEqualToString:@"ackUserWrite"]) {
            NSInteger writeId = [spec[@"writeId"] integerValue];
            BOOL revert = [spec[@"revert"] boolValue];
            events = [syncTree ackUserWriteWithWriteId:writeId revert:revert persist:YES clock:[[FTestClock alloc] init]];
            [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
        } else if ([spec[@"type"] isEqualToString:@"suppressWarning"]) {
            // Do nothing. This is a hack so JS's Jasmine tests don't throw warnings for "expect no errors" tests.
        } else {
            XCTFail(@"Unknown step: %@", spec[@"type"]);
        }
    }
}

- (NSArray *) loadSpecs {
    static NSArray *json;
    if (json == nil) {
        NSString *syncPointSpec = [[NSBundle bundleForClass:[FSyncPointTests class]] pathForResource:@"syncPointSpec" ofType:@"json"];
        NSLog(@"%@", syncPointSpec);
        NSData *specData = [NSData dataWithContentsOfFile:syncPointSpec];
        NSError *error = nil;
        json = [NSJSONSerialization JSONObjectWithData:specData options:kNilOptions error:&error];

        if (error) {
            XCTFail(@"Error occurred parsing JSON: %@", error);
        }
    }

    return json;
}

- (NSDictionary *) specsForName:(NSString *)name {
    for (NSDictionary *spec in [self loadSpecs]) {
        if ([name isEqualToString:spec[@"name"]]) {
            return spec;
        }
    }

    XCTFail(@"No such test: %@", name);
    return nil;
}

- (void) runTestForName:(NSString *)name {
    NSDictionary *spec = [self specsForName:name];
    [self runTest:spec atBasePath:nil];
    // run again at a deeper location
    [self runTest:spec atBasePath:@"/foo/bar/baz"];
}

- (void) testAll {
    NSArray *specs = [self loadSpecs];
    for (NSDictionary *spec in specs) {
        [self runTest:spec atBasePath:nil];
        // run again at a deeper location
        [self runTest:spec atBasePath:@"/foo/bar/baz"];
    }
}

- (void) testDefaultListenHandlesParentSet {
    [self runTestForName:@"Default listen handles a parent set"];
}

- (void) testDefaultListenHandlesASetAtTheSameLevel {
    [self runTestForName:@"Default listen handles a set at the same level"];
}

- (void) testAQueryCanGetACompleteCacheThenAMerge {
    [self runTestForName:@"A query can get a complete cache then a merge"];
}

- (void) testServerMergeOnListenerWithCompleteChildren {
    [self runTestForName:@"Server merge on listener with complete children"];
}

- (void) testDeepMergeOnListenerWithCompleteChildren {
    [self runTestForName:@"Deep merge on listener with complete children"];
}

- (void) testUpdateChildListenerTwice {
    [self runTestForName:@"Update child listener twice"];
}

- (void) testChildOfDefaultListenThatAlreadyHasACompleteCache {
    [self runTestForName:@"Update child of default listen that already has a complete cache"];
}

- (void) testUpdateChildOfDefaultListenThatHasNoCache {
    [self runTestForName:@"Update child of default listen that has no cache"];
}

// failing
- (void) testUpdateTheChildOfACoLocatedDefaultListenerAndQuery {
    [self runTestForName:@"Update (via set) the child of a co-located default listener and query"];
}

- (void) testUpdateTheChildOfAQueryWithAFullCache {
    [self runTestForName:@"Update (via set) the child of a query with a full cache"];
}

- (void) testUpdateAChildBelowAnEmptyQuery {
    [self runTestForName:@"Update (via set) a child below an empty query"];
}

- (void) testUpdateDescendantOfDefaultListenerWithFullCache {
    [self runTestForName:@"Update descendant of default listener with full cache"];
}

- (void) testDescendantSetBelowAnEmptyDefaultLIstenerIsIgnored {
    [self runTestForName:@"Descendant set below an empty default listener is ignored"];
}

- (void) testUpdateOfAChild {
    [self runTestForName:@"Update of a child. This can happen if a child listener is added and removed"];
}

- (void) testRevertSetWithOnlyChildCaches {
    [self runTestForName:@"Revert set with only child caches"];
}

- (void) testCanRevertADuplicateChildSet {
    [self runTestForName:@"Can revert a duplicate child set"];
}

- (void) testCanRevertAChildSetAndSeeTheUnderlyingData {
    [self runTestForName:@"Can revert a child set and see the underlying data"];
}

- (void) testRevertChildSetWithNoServerData {
    [self runTestForName:@"Revert child set with no server data"];
}

- (void) testRevertDeepSetWithNoServerData {
    [self runTestForName:@"Revert deep set with no server data"];
}

- (void) testRevertSetCoveredByNonvisibleTransaction {
    [self runTestForName:@"Revert set covered by non-visible transaction"];
}

- (void) testClearParentShadowingServerValuesSetWithServerChildren {
    [self runTestForName:@"Clear parent shadowing server values set with server children"];
}

- (void) testClearChildShadowingServerValuesSetWithServerChildren {
    [self runTestForName:@"Clear child shadowing server values set with server children"];
}

- (void) testUnrelatedMergeDoesntShadowServerUpdates {
    [self runTestForName:@"Unrelated merge doesn't shadow server updates"];
}

- (void) testCanSetAlongsideARemoteMerge {
    [self runTestForName:@"Can set alongside a remote merge"];
}

- (void) testSetPriorityOnALocationWithNoCache {
    [self runTestForName:@"setPriority on a location with no cache"];
}

- (void) testDeepUpdateDeletesChildFromLimitWindowAndPullsInNewChild {
    [self runTestForName:@"deep update deletes child from limit window and pulls in new child"];
}

- (void) testDeepSetDeletesChildFromLimitWindowAndPullsInNewChild {
    [self runTestForName:@"deep set deletes child from limit window and pulls in new child"];
}

- (void) testEdgeCaseInNewChildForChange {
    [self runTestForName:@"Edge case in newChildForChange_"];
}

- (void) testRevertSetInQueryWindow {
    [self runTestForName:@"Revert set in query window"];
}

- (void) testHandlesAServerValueMovingAChildOutOfAQueryWindow {
    [self runTestForName:@"Handles a server value moving a child out of a query window"];
}

- (void) testUpdateOfIndexedChildWorks {
    [self runTestForName:@"Update of indexed child works"];
}

- (void) testMergeAppliedToEmptyLimit {
    [self runTestForName:@"Merge applied to empty limit"];
}

- (void) testLimitIsRefilledFromServerDataAfterMerge {
    [self runTestForName:@"Limit is refilled from server data after merge"];
}

- (void) testHandleRepeatedListenWithMergeAsFirstUpdate {
    [self runTestForName:@"Handle repeated listen with merge as first update"];
}

- (void) testLimitIsRefilledFromServerDataAfterSet {
    [self runTestForName:@"Limit is refilled from server data after set"];
}

- (void) testQueryOnWeirdPath {
    [self runTestForName:@"query on weird path."];
}

- (void) testRunsRound2 {
    [self runTestForName:@"runs, round2"];
}

- (void) testHandlesNestedListens {
    [self runTestForName:@"handles nested listens"];
}

- (void) testHandlesASetBelowAListen {
    [self runTestForName:@"Handles a set below a listen"];
}

- (void) testDoesNonDefaultQueries {
    [self runTestForName:@"does non-default queries"];
}

- (void) testHandlesCoLocatedDefaultListenerAndQuery {
    [self runTestForName:@"handles a co-located default listener and query"];
}

- (void) testDefaultAndNonDefaultListenerAtSameLocationWithServerUpdate {
    [self runTestForName:@"Default and non-default listener at same location with server update"];
}

- (void) testAddAParentListenerToACompleteChildListenerExpectChildEvent {
    [self runTestForName:@"Add a parent listener to a complete child listener, expect child event"];
}

- (void) testAddListensToASetExpectCorrectEventsIncludingAChildEvent {
    [self runTestForName:@"Add listens to a set, expect correct events, including a child event"];
}

- (void) testServerUpdateToAChildListenerRaisesChildEventsAtParent {
    [self runTestForName:@"ServerUpdate to a child listener raises child events at parent"];
}

- (void) testServerUpdateToAChildListenerRaisesChildEventsAtParentQuery {
    [self runTestForName:@"ServerUpdate to a child listener raises child events at parent query"];
}

- (void) testMultipleCompleteChildrenAreHandleProperly {
    [self runTestForName:@"Multiple complete children are handled properly"];
}

- (void) testWriteLeafNodeOverwriteAtParentNode {
    [self runTestForName:@"Write leaf node, overwrite at parent node"];
}

- (void) testConfirmCompleteChildrenFromTheServer {
    [self runTestForName:@"Confirm complete children from the server"];
}

- (void) testWriteLeafOverwriteFromParent {
    [self runTestForName:@"Write leaf, overwrite from parent"];
}

- (void) testBasicUpdateTest {
    [self runTestForName:@"Basic update test"];
}

- (void) testNoDoubleValueEventsForUserAck {
    [self runTestForName:@"No double value events for user ack"];
}

- (void) testBasicKeyIndexSanityCheck {
    [self runTestForName:@"Basic key index sanity check"];
}

- (void) testCollectCorrectSubviewsToListenOn {
    [self runTestForName:@"Collect correct subviews to listen on"];
}

- (void) testLimitToFirstOneOnOrderedQuery {
    [self runTestForName:@"Limit to first one on ordered query"];
}

- (void) testLimitToLastOneOnOrderedQuery {
    [self runTestForName:@"Limit to last one on ordered query"];
}

- (void) testUpdateIndexedValueOnExistingChildFromLimitedQuery {
    [self runTestForName:@"Update indexed value on existing child from limited query"];
}

- (void) testCanCreateStartAtEndAtEqualToQueriesWithBool {
    [self runTestForName:@"Can create startAt, endAt, equalTo queries with bool"];
}

- (void) testQueryWithExistingServerSnap {
    [self runTestForName:@"Query with existing server snap"];
}

- (void) testServerDataIsNotPurgedForNonServerIndexedQueries {
    [self runTestForName:@"Server data is not purged for non-server-indexed queries"];
}

- (void) testStartAtEndAtDominatesLimit {
    [self runTestForName:@"startAt/endAt dominates limit"];
}

- (void) testUpdateToSingleChildThatMovesOutOfWindow {
    [self runTestForName:@"Update to single child that moves out of window"];
}

- (void) testLimitedQueryDoesntPullInOutOfRangeChild {
    [self runTestForName:@"Limited query doesn't pull in out of range child"];
}

- (void) testWithCustomOrderByIsRefilledWithCorrectItem {
    [self runTestForName:@"Limit with custom orderBy is refilled with correct item"];
}

- (void) testMergeForLocationWithDefaultAndLimitedListener {
    [self runTestForName:@"Merge for location with default and limited listener"];
}

- (void) testUserMergePullsInCorrectValues {
    [self runTestForName:@"User merge pulls in correct values"];
}

- (void) testUserDeepSetPullsInCorrectValues {
    [self runTestForName:@"User deep set pulls in correct values"];
}

- (void) testQueriesWithEqualToNullWork {
    [self runTestForName:@"Queries with equalTo(null) work"];
}

- (void) testRevertedWritesUpdateQuery {
    [self runTestForName:@"Reverted writes update query"];
}

- (void) testDeepSetForNonLocalDataDoesntRaiseEvents {
    [self runTestForName:@"Deep set for non-local data doesn't raise events"];
}

- (void) testUserUpdateWithNewChildrenTriggersEvents {
    [self runTestForName:@"User update with new children triggers events"];
}

- (void) testUserWriteWithDeepOverwrite {
    [self runTestForName:@"User write with deep user overwrite"];
}

- (void) testServerUpdatesPriority {
    [self runTestForName:@"Server updates priority"];
}

- (void) testRevertFullUnderlyingWrite {
    [self runTestForName:@"Revert underlying full overwrite"];
}

- (void) testUserChildOverwriteForNonexistentServerNode {
    [self runTestForName:@"User child overwrite for non-existent server node"];
}

- (void) testRevertUserOverwriteOfChildOnLeafNode {
    [self runTestForName:@"Revert user overwrite of child on leaf node"];
}

- (void) testServerOverwriteWithDeepUserDelete {
    [self runTestForName:@"Server overwrite with deep user delete"];
}

- (void) testUserOverwritesLeafNodeWithPriority {
    [self runTestForName:@"User overwrites leaf node with priority"];
}

- (void) testUserOverwritesInheritPriorityValuesFromLeafNodes {
    [self runTestForName:@"User overwrites inherit priority values from leaf nodes"];
}

- (void) testUserUpdateOnUserSetLeafNodeWithPriorityAfterServerUpdate {
    [self runTestForName:@"User update on user set leaf node with priority after server update"];
}

- (void) testServerDeepDeleteOnLeafNode {
    [self runTestForName:@"Server deep delete on leaf node"];
}

- (void) testUserSetsRootPriority {
    [self runTestForName:@"User sets root priority"];
}

- (void) testUserUpdatesPriorityOnEmptyRoot {
    [self runTestForName:@"User updates priority on empty root"];
}

- (void) testRevertSetAtRootWithPriority {
    [self runTestForName:@"Revert set at root with priority"];
}

- (void) testServerUpdatesPriorityAfterUserSetsPriority {
    [self runTestForName:@"Server updates priority after user sets priority"];
}

- (void) testEmptySetDoesntPreventServerUpdates {
    [self runTestForName:@"Empty set doesn't prevent server updates"];
}

- (void) testUserUpdatesPriorityTwiceFirstIsReverted {
    [self runTestForName:@"User updates priority twice, first is reverted"];
}

- (void) testServerAcksRootPrioritySetAfterUserDeletesRootNode {
    [self runTestForName:@"Server acks root priority set after user deletes root node"];
}

- (void) testADeleteInAMergeDoesntPushOutNodes {
    [self runTestForName:@"A delete in a merge doesn't push out nodes"];
}

- (void) testATaggedQueryFiresEventsEventually {
    [self runTestForName:@"A tagged query fires events eventually"];
}

- (void) testUserWriteOutsideOfLimitIsIgnoredForTaggedQueries {
    [self runTestForName:@"User write outside of limit is ignored for tagged queries"];
}

- (void) testAckForMergeDoesntRaiseValueEventForLaterListen {
    [self runTestForName:@"Ack for merge doesn't raise value event for later listen"];
}

- (void) testClearParentShadowingServerValuesMergeWithServerChildren {
    [self runTestForName:@"Clear parent shadowing server values merge with server children"];
}

- (void) testPrioritiesDontMakeMeSick {
    [self runTestForName:@"Priorities don't make me sick"];
}

- (void) testMergeThatMovesChildFromWindowToBoundaryDoesNotCauseChildToBeReadded {
    [self runTestForName:@"Merge that moves child from window to boundary does not cause child to be readded"];
}

- (void) testDeepMergeAckIsHandledCorrectly {
    [self runTestForName:@"Deep merge ack is handled correctly."];
}

- (void) testDeepMergeAckOnIncompleteDataAndWithServerValues {
    [self runTestForName:@"Deep merge ack (on incomplete data, and with server values)"];
}

- (void) testLimitQueryHandlesDeepServerMergeForOutOfViewItem {
    [self runTestForName:@"Limit query handles deep server merge for out-of-view item."];
}

- (void) testLimitQueryHandlesDeepUserMergeForOutOfViewItem {
    [self runTestForName:@"Limit query handles deep user merge for out-of-view item."];
}

- (void) testLimitQueryHandlesDeepUserMergeForOutOfViewItemFollowedByServerUpdate {
    [self runTestForName:@"Limit query handles deep user merge for out-of-view item followed by server update."];
}

- (void) testUnrelatedUntaggedUpdateIsNotCachedInTaggedListen {
    [self runTestForName:@"Unrelated, untagged update is not cached in tagged listen"];
}

- (void) testUnrelatedAckedSetIsNotCachedInTaggedListen {
    [self runTestForName:@"Unrelated, acked set is not cached in tagged listen"];
}

- (void) testUnrelatedAckedUpdateIsNotCachedInTaggedListen {
    [self runTestForName:@"Unrelated, acked update is not cached in tagged listen"];
}

- (void) testdeepUpdateRaisesImmediateEventsOnlyIfHasCompleteData {
    [self runTestForName:@"Deep update raises immediate events only if has complete data"];
}

- (void) testdeepUpdateReturnsMinimumDataRequired {
    [self runTestForName:@"Deep update returns minimum data required"];
}

- (void) testdeepUpdateRaisesAllEvents {
    [self runTestForName:@"Deep update raises all events"];
}

@end