FSyncPointTests.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  18. #import "FirebaseDatabase/Sources/Api/Private/FIRDataSnapshot_Private.h"
  19. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseQuery_Private.h"
  20. #import "FirebaseDatabase/Sources/Core/FListenProvider.h"
  21. #import "FirebaseDatabase/Sources/Core/FQueryParams.h"
  22. #import "FirebaseDatabase/Sources/Core/FQuerySpec.h"
  23. #import "FirebaseDatabase/Sources/Core/FSyncTree.h"
  24. #import "FirebaseDatabase/Sources/Core/View/FCancelEvent.h"
  25. #import "FirebaseDatabase/Sources/Core/View/FChange.h"
  26. #import "FirebaseDatabase/Sources/Core/View/FDataEvent.h"
  27. #import "FirebaseDatabase/Sources/Core/View/FEventRegistration.h"
  28. #import "FirebaseDatabase/Sources/FIRDatabaseConfig_Private.h"
  29. #import "FirebaseDatabase/Sources/FKeyIndex.h"
  30. #import "FirebaseDatabase/Sources/FPathIndex.h"
  31. #import "FirebaseDatabase/Sources/FPriorityIndex.h"
  32. #import "FirebaseDatabase/Sources/Snapshot/FCompoundWrite.h"
  33. #import "FirebaseDatabase/Sources/Snapshot/FEmptyNode.h"
  34. #import "FirebaseDatabase/Sources/Snapshot/FSnapshotUtilities.h"
  35. #import "FirebaseDatabase/Tests/Helpers/FTestClock.h"
  36. #import "FirebaseDatabase/Tests/Unit/FSyncPointTests.h"
  37. typedef NSDictionary * (^fbt_nsdictionary_void)(void);
  38. @interface FTestEventRegistration : NSObject <FEventRegistration>
  39. @property(nonatomic, strong) NSDictionary *spec;
  40. @property(nonatomic, strong) FQuerySpec *query;
  41. @end
  42. @implementation FTestEventRegistration
  43. - (id)initWithSpec:(NSDictionary *)eventSpec query:(FQuerySpec *)query {
  44. self = [super init];
  45. if (self) {
  46. self.spec = eventSpec;
  47. self.query = query;
  48. }
  49. return self;
  50. }
  51. - (BOOL)responseTo:(FIRDataEventType)eventType {
  52. return YES;
  53. }
  54. - (FDataEvent *)createEventFrom:(FChange *)change query:(FQuerySpec *)query {
  55. FIRDataSnapshot *snap = nil;
  56. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithRepo:nil path:query.path];
  57. if (change.type == FIRDataEventTypeValue) {
  58. snap = [[FIRDataSnapshot alloc] initWithRef:ref indexedNode:change.indexedNode];
  59. } else {
  60. snap = [[FIRDataSnapshot alloc] initWithRef:[ref child:change.childKey]
  61. indexedNode:change.indexedNode];
  62. }
  63. return [[FDataEvent alloc] initWithEventType:change.type
  64. eventRegistration:self
  65. dataSnapshot:snap
  66. prevName:change.prevKey];
  67. }
  68. - (BOOL)matches:(id<FEventRegistration>)other {
  69. if (![other isKindOfClass:[FTestEventRegistration class]]) {
  70. return NO;
  71. } else {
  72. FTestEventRegistration *otherRegistration = other;
  73. if (self.spec[@"callbackId"] && otherRegistration.spec[@"callbackId"] &&
  74. [self.spec[@"callbackId"] isEqualToNumber:otherRegistration.spec[@"callbackId"]]) {
  75. return YES;
  76. } else {
  77. return NO;
  78. }
  79. }
  80. }
  81. - (void)fireEvent:(id<FEvent>)event queue:(dispatch_queue_t)queue {
  82. [NSException raise:@"NotImplementedError" format:@"Method not implemented."];
  83. }
  84. - (FCancelEvent *)createCancelEventFromError:(NSError *)error path:(FPath *)path {
  85. [NSException raise:@"NotImplementedError" format:@"Method not implemented."];
  86. return nil;
  87. }
  88. - (FIRDatabaseHandle)handle {
  89. [NSException raise:@"NotImplementedError" format:@"Method not implemented."];
  90. return 0;
  91. }
  92. @end
  93. @implementation FSyncPointTests
  94. - (NSString *)queryKeyForQuery:(FQuerySpec *)query tagId:(NSNumber *)tagId {
  95. return [NSString stringWithFormat:@"%@|%@|%@", query.path, query.params, tagId];
  96. }
  97. - (void)actualEvent:(FDataEvent *)actual equalsExpected:(NSDictionary *)expected {
  98. XCTAssertEqual(actual.eventType, [self stringToEventType:expected[@"type"]],
  99. @"Event type should be equal");
  100. if (actual.eventType != FIRDataEventTypeValue) {
  101. NSString *childName = actual.snapshot.key;
  102. XCTAssertEqualObjects(childName, expected[@"name"], @"Snapshot name should be equal");
  103. if (expected[@"prevName"] == [NSNull null]) {
  104. XCTAssertNil(actual.prevName, @"prevName should be nil");
  105. } else {
  106. XCTAssertEqualObjects(actual.prevName, expected[@"prevName"], @"prevName should be equal");
  107. }
  108. }
  109. NSString *actualHash = [actual.snapshot.node.node dataHash];
  110. NSString *expectedHash = [[FSnapshotUtilities nodeFrom:expected[@"data"]] dataHash];
  111. XCTAssertEqualObjects(actualHash, expectedHash, @"Data hash should be equal");
  112. }
  113. /**
  114. * @param actual is an array of id<FEvent>
  115. * @param expected is an array of dictionaries?
  116. */
  117. - (void)actualEvents:(NSArray *)actual exactMatchesExpected:(NSArray *)expected {
  118. if ([expected count] < [actual count]) {
  119. XCTFail(@"Got extra events: %@", actual);
  120. } else if ([expected count] > [actual count]) {
  121. XCTFail(@"Missing events: %@", actual);
  122. } else {
  123. NSUInteger i = 0;
  124. for (i = 0; i < [expected count]; i++) {
  125. FDataEvent *actualEvent = actual[i];
  126. NSDictionary *expectedEvent = expected[i];
  127. [self actualEvent:actualEvent equalsExpected:expectedEvent];
  128. }
  129. }
  130. }
  131. - (void)assertOrderedFirstEvent:(FIRDataEventType)e1 secondEvent:(FIRDataEventType)e2 {
  132. static NSArray *eventOrdering = nil;
  133. if (!eventOrdering) {
  134. eventOrdering = @[
  135. [NSNumber numberWithInteger:FIRDataEventTypeChildRemoved],
  136. [NSNumber numberWithInteger:FIRDataEventTypeChildAdded],
  137. [NSNumber numberWithInteger:FIRDataEventTypeChildMoved],
  138. [NSNumber numberWithInteger:FIRDataEventTypeChildChanged],
  139. [NSNumber numberWithInteger:FIRDataEventTypeValue]
  140. ];
  141. }
  142. NSUInteger idx1 = [eventOrdering indexOfObject:[NSNumber numberWithInteger:e1]];
  143. NSUInteger idx2 = [eventOrdering indexOfObject:[NSNumber numberWithInteger:e2]];
  144. if (idx1 > idx2) {
  145. XCTFail(@"Received %d after %d", (int)e2, (int)e1);
  146. }
  147. }
  148. - (FIRDataEventType)stringToEventType:(NSString *)stringType {
  149. if ([stringType isEqualToString:@"child_added"]) {
  150. return FIRDataEventTypeChildAdded;
  151. } else if ([stringType isEqualToString:@"child_removed"]) {
  152. return FIRDataEventTypeChildRemoved;
  153. } else if ([stringType isEqualToString:@"child_changed"]) {
  154. return FIRDataEventTypeChildChanged;
  155. } else if ([stringType isEqualToString:@"child_moved"]) {
  156. return FIRDataEventTypeChildMoved;
  157. } else if ([stringType isEqualToString:@"value"]) {
  158. return FIRDataEventTypeValue;
  159. } else {
  160. XCTFail(@"Unknown event type %@", stringType);
  161. return FIRDataEventTypeValue;
  162. }
  163. }
  164. - (void)actualEventSet:(id)actual matchesExpected:(id)expected atBasePath:(NSString *)basePathStr {
  165. // don't worry about order for now
  166. XCTAssertEqual([expected count], [actual count], @"Mismatched lengths.\nExpected: %@\nActual: %@",
  167. expected, actual);
  168. NSArray *currentExpected = expected;
  169. NSArray *currentActual = actual;
  170. FPath *basePath = basePathStr != nil ? [[FPath alloc] initWith:basePathStr] : [FPath empty];
  171. while ([currentExpected count] > 0) {
  172. // Step 1: find location range in expected
  173. // we expect all events for a particular path to be in a group
  174. FPath *currentPath = [basePath childFromString:currentExpected[0][@"path"]];
  175. NSUInteger i = 1;
  176. while (i < [currentExpected count]) {
  177. FPath *otherPath = [basePath childFromString:currentExpected[i][@"path"]];
  178. if ([currentPath isEqual:otherPath]) {
  179. i++;
  180. } else {
  181. break;
  182. }
  183. }
  184. // Step 2: foreach in actual, asserting location
  185. NSUInteger j = 0;
  186. for (j = 0; j < i; j++) {
  187. FDataEvent *actualEventData = currentActual[j];
  188. FTestEventRegistration *eventRegistration = actualEventData.eventRegistration;
  189. NSDictionary *specStep = eventRegistration.spec;
  190. FPath *actualPath = [basePath childFromString:specStep[@"path"]];
  191. if (![currentPath isEqual:actualPath]) {
  192. XCTFail(@"Expected path %@ to equal %@", actualPath, currentPath);
  193. }
  194. }
  195. // Step 3: slice each array
  196. NSMutableArray *expectedSlice =
  197. [[currentExpected subarrayWithRange:NSMakeRange(0, i)] mutableCopy];
  198. NSArray *actualSlice = [currentActual subarrayWithRange:NSMakeRange(0, i)];
  199. // foreach in actual, stack up to enforce ordering, find in expected
  200. NSMutableDictionary *actualMap = [[NSMutableDictionary alloc] init];
  201. for (FDataEvent *actualEvent in actualSlice) {
  202. FTestEventRegistration *eventRegistration = actualEvent.eventRegistration;
  203. FQuerySpec *query = eventRegistration.query;
  204. NSDictionary *spec = eventRegistration.spec;
  205. NSString *listenId =
  206. [NSString stringWithFormat:@"%@|%@", [basePath childFromString:spec[@"path"]], query];
  207. if (actualMap[listenId]) {
  208. // stack this event up, and make sure it obeys ordering constraints
  209. NSMutableArray *eventStack = actualMap[listenId];
  210. FDataEvent *prevEvent = eventStack[[eventStack count] - 1];
  211. [self assertOrderedFirstEvent:prevEvent.eventType secondEvent:actualEvent.eventType];
  212. [eventStack addObject:actualEvent];
  213. } else {
  214. // this is the first event for this listen, just initialize it
  215. actualMap[listenId] = [[NSMutableArray alloc] initWithObjects:actualEvent, nil];
  216. }
  217. // Ordering has been enforced, make sure we can find this in the expected events
  218. __block NSUInteger indexToRemove = NSNotFound;
  219. [expectedSlice enumerateObjectsUsingBlock:^(NSDictionary *expectedEvent, NSUInteger idx,
  220. BOOL *stop) {
  221. if ([self stringToEventType:expectedEvent[@"type"]] == actualEvent.eventType) {
  222. if ([self stringToEventType:expectedEvent[@"type"]] != FIRDataEventTypeValue) {
  223. if (![expectedEvent[@"name"] isEqualToString:actualEvent.snapshot.key]) {
  224. return; // short circuit, not a match
  225. }
  226. if ([self stringToEventType:expectedEvent[@"type"]] != FIRDataEventTypeChildRemoved &&
  227. !(expectedEvent[@"prevName"] == [NSNull null] && actualEvent.prevName == nil) &&
  228. !(expectedEvent[@"prevName"] != [NSNull null] &&
  229. [expectedEvent[@"prevName"] isEqualToString:actualEvent.prevName])) {
  230. return; // short circuit, not a match
  231. }
  232. }
  233. // make sure the snapshots match
  234. NSString *snapHash = [actualEvent.snapshot.node.node dataHash];
  235. NSString *expectedHash = [[FSnapshotUtilities nodeFrom:expectedEvent[@"data"]] dataHash];
  236. if ([snapHash isEqualToString:expectedHash]) {
  237. indexToRemove = idx;
  238. *stop = YES;
  239. }
  240. }
  241. }];
  242. XCTAssertFalse(indexToRemove == NSNotFound, @"Could not find matching expected event for %@",
  243. actualEvent);
  244. [expectedSlice removeObjectAtIndex:indexToRemove];
  245. }
  246. currentExpected =
  247. [currentExpected subarrayWithRange:NSMakeRange(i, [currentExpected count] - i)];
  248. currentActual = [currentActual subarrayWithRange:NSMakeRange(i, [currentActual count] - i)];
  249. }
  250. }
  251. - (FQuerySpec *)parseParams:(NSDictionary *)specParams forPath:(FPath *)path {
  252. FQueryParams *query = [[FQueryParams alloc] init];
  253. NSMutableDictionary *params;
  254. if (specParams) {
  255. params = [specParams mutableCopy];
  256. if (!params[@"tag"]) {
  257. XCTFail(@"Error: Non-default queries must have tag");
  258. }
  259. } else {
  260. params = [NSMutableDictionary dictionary];
  261. }
  262. if (params[@"orderBy"]) {
  263. FPath *indexPath = [FPath pathWithString:params[@"orderBy"]];
  264. id<FIndex> index = [[FPathIndex alloc] initWithPath:indexPath];
  265. query = [query orderBy:index];
  266. [params removeObjectForKey:@"orderBy"];
  267. }
  268. if (params[@"orderByKey"]) {
  269. query = [query orderBy:[FKeyIndex keyIndex]];
  270. [params removeObjectForKey:@"orderByKey"];
  271. }
  272. if (params[@"orderByPriority"]) {
  273. query = [query orderBy:[FPriorityIndex priorityIndex]];
  274. [params removeObjectForKey:@"orderByPriority"];
  275. }
  276. if (params[@"startAt"]) {
  277. id<FNode> node = [FSnapshotUtilities nodeFrom:params[@"startAt"][@"index"]];
  278. if (params[@"startAt"][@"name"]) {
  279. query = [query startAt:node childKey:params[@"startAt"][@"name"]];
  280. } else {
  281. query = [query startAt:node];
  282. }
  283. [params removeObjectForKey:@"startAt"];
  284. }
  285. if (params[@"endAt"]) {
  286. id<FNode> node = [FSnapshotUtilities nodeFrom:params[@"endAt"][@"index"]];
  287. if (params[@"endAt"][@"name"]) {
  288. query = [query endAt:node childKey:params[@"endAt"][@"name"]];
  289. } else {
  290. query = [query endAt:node];
  291. }
  292. [params removeObjectForKey:@"endAt"];
  293. }
  294. if (params[@"equalTo"]) {
  295. id<FNode> node = [FSnapshotUtilities nodeFrom:params[@"equalTo"][@"index"]];
  296. if (params[@"equalTo"][@"name"]) {
  297. NSString *name = params[@"equalTo"][@"name"];
  298. query = [[query startAt:node childKey:name] endAt:node childKey:name];
  299. } else {
  300. query = [[query startAt:node] endAt:node];
  301. }
  302. [params removeObjectForKey:@"equalTo"];
  303. }
  304. if (params[@"limitToFirst"]) {
  305. query = [query limitToFirst:[params[@"limitToFirst"] integerValue]];
  306. [params removeObjectForKey:@"limitToFirst"];
  307. }
  308. if (params[@"limitToLast"]) {
  309. query = [query limitToLast:[params[@"limitToLast"] integerValue]];
  310. [params removeObjectForKey:@"limitToLast"];
  311. }
  312. [params removeObjectForKey:@"tag"];
  313. if ([params count] > 0) {
  314. XCTFail(@"Unsupported query parameter: %@", params);
  315. }
  316. return [[FQuerySpec alloc] initWithPath:path params:query];
  317. }
  318. - (void)runTest:(NSDictionary *)testSpec atBasePath:(NSString *)basePath {
  319. NSMutableDictionary *listens = [[NSMutableDictionary alloc] init];
  320. __weak FSyncPointTests *weakSelf = self;
  321. FListenProvider *listenProvider = [[FListenProvider alloc] init];
  322. listenProvider.startListening = ^(FQuerySpec *query, NSNumber *tagId, id<FSyncTreeHash> hash,
  323. fbt_nsarray_nsstring onComplete) {
  324. FQueryParams *queryParams = query.params;
  325. FPath *path = query.path;
  326. NSString *logTag = [NSString stringWithFormat:@"%@ (%@)", queryParams, tagId];
  327. NSString *key = [weakSelf queryKeyForQuery:query tagId:tagId];
  328. FFLog(@"I-RDB143001", @"Listening at %@ for %@", path, logTag);
  329. id existing = listens[key];
  330. NSAssert(existing == nil, @"Duplicate listen");
  331. listens[key] = @YES;
  332. return @[];
  333. };
  334. listenProvider.stopListening = ^(FQuerySpec *query, NSNumber *tagId) {
  335. FQueryParams *queryParams = query.params;
  336. FPath *path = query.path;
  337. NSString *logTag = [NSString stringWithFormat:@"%@ (%@)", queryParams, tagId];
  338. NSString *key = [weakSelf queryKeyForQuery:query tagId:tagId];
  339. FFLog(@"I-RDB143002", @"Stop listening at %@ for %@", path, logTag);
  340. id existing = listens[key];
  341. XCTAssertTrue(existing != nil, @"Missing record of query that we're removing");
  342. [listens removeObjectForKey:key];
  343. };
  344. FSyncTree *syncTree = [[FSyncTree alloc] initWithListenProvider:listenProvider];
  345. NSLog(@"Running %@", testSpec[@"name"]);
  346. NSInteger currentWriteId = 0;
  347. for (NSDictionary *step in testSpec[@"steps"]) {
  348. NSMutableDictionary *spec = [step mutableCopy];
  349. if (spec[@".comment"]) {
  350. NSLog(@" > %@", spec[@".comment"]);
  351. }
  352. if (spec[@"debug"] != nil) {
  353. // TODO: Ideally we'd pause the debugger somehow (like "debugger;" in JS).
  354. NSLog(@"Start debugging");
  355. }
  356. // Almost everything has a path...
  357. FPath *path = [FPath empty];
  358. if (basePath != nil) {
  359. path = [path childFromString:basePath];
  360. }
  361. if (spec[@"path"] != nil) {
  362. path = [path childFromString:spec[@"path"]];
  363. }
  364. NSArray *events;
  365. if ([spec[@"type"] isEqualToString:@"listen"]) {
  366. FQuerySpec *query = [self parseParams:spec[@"params"] forPath:path];
  367. FTestEventRegistration *eventRegistration =
  368. [[FTestEventRegistration alloc] initWithSpec:spec query:query];
  369. events = [syncTree addEventRegistration:eventRegistration forQuery:query];
  370. [self actualEvents:events exactMatchesExpected:spec[@"events"]];
  371. } else if ([spec[@"type"] isEqualToString:@"unlisten"]) {
  372. FQuerySpec *query = [self parseParams:spec[@"params"] forPath:path];
  373. FTestEventRegistration *eventRegistration =
  374. [[FTestEventRegistration alloc] initWithSpec:spec query:query];
  375. events = [syncTree removeEventRegistration:eventRegistration forQuery:query cancelError:nil];
  376. [self actualEvents:events exactMatchesExpected:spec[@"events"]];
  377. } else if ([spec[@"type"] isEqualToString:@"serverUpdate"]) {
  378. id<FNode> update = [FSnapshotUtilities nodeFrom:spec[@"data"]];
  379. if (spec[@"tag"]) {
  380. events = [syncTree applyTaggedQueryOverwriteAtPath:path newData:update tagId:spec[@"tag"]];
  381. } else {
  382. events = [syncTree applyServerOverwriteAtPath:path newData:update];
  383. }
  384. [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
  385. } else if ([spec[@"type"] isEqualToString:@"serverMerge"]) {
  386. FCompoundWrite *compoundWrite =
  387. [FCompoundWrite compoundWriteWithValueDictionary:spec[@"data"]];
  388. if (spec[@"tag"]) {
  389. events = [syncTree applyTaggedQueryMergeAtPath:path
  390. changedChildren:compoundWrite
  391. tagId:spec[@"tag"]];
  392. } else {
  393. events = [syncTree applyServerMergeAtPath:path changedChildren:compoundWrite];
  394. }
  395. [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
  396. } else if ([spec[@"type"] isEqualToString:@"set"]) {
  397. id<FNode> toSet = [FSnapshotUtilities nodeFrom:spec[@"data"]];
  398. BOOL visible = (spec[@"visible"] != nil) ? [spec[@"visible"] boolValue] : YES;
  399. events = [syncTree applyUserOverwriteAtPath:path
  400. newData:toSet
  401. writeId:currentWriteId++
  402. isVisible:visible];
  403. [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
  404. } else if ([spec[@"type"] isEqualToString:@"update"]) {
  405. FCompoundWrite *compoundWrite =
  406. [FCompoundWrite compoundWriteWithValueDictionary:spec[@"data"]];
  407. events = [syncTree applyUserMergeAtPath:path
  408. changedChildren:compoundWrite
  409. writeId:currentWriteId++];
  410. [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
  411. } else if ([spec[@"type"] isEqualToString:@"ackUserWrite"]) {
  412. NSInteger writeId = [spec[@"writeId"] integerValue];
  413. BOOL revert = [spec[@"revert"] boolValue];
  414. events = [syncTree ackUserWriteWithWriteId:writeId
  415. revert:revert
  416. persist:YES
  417. clock:[[FTestClock alloc] init]];
  418. [self actualEventSet:events matchesExpected:spec[@"events"] atBasePath:basePath];
  419. } else if ([spec[@"type"] isEqualToString:@"suppressWarning"]) {
  420. // Do nothing. This is a hack so JS's Jasmine tests don't throw warnings for "expect no
  421. // errors" tests.
  422. } else {
  423. XCTFail(@"Unknown step: %@", spec[@"type"]);
  424. }
  425. }
  426. }
  427. - (NSArray *)loadSpecs {
  428. static NSArray *json;
  429. if (json == nil) {
  430. NSString *syncPointSpec =
  431. [[NSBundle bundleForClass:[FSyncPointTests class]] pathForResource:@"syncPointSpec"
  432. ofType:@"json"];
  433. NSLog(@"%@", syncPointSpec);
  434. NSData *specData = [NSData dataWithContentsOfFile:syncPointSpec];
  435. NSError *error = nil;
  436. json = [NSJSONSerialization JSONObjectWithData:specData options:kNilOptions error:&error];
  437. if (error) {
  438. XCTFail(@"Error occurred parsing JSON: %@", error);
  439. }
  440. }
  441. return json;
  442. }
  443. - (NSDictionary *)specsForName:(NSString *)name {
  444. for (NSDictionary *spec in [self loadSpecs]) {
  445. if ([name isEqualToString:spec[@"name"]]) {
  446. return spec;
  447. }
  448. }
  449. XCTFail(@"No such test: %@", name);
  450. return nil;
  451. }
  452. - (void)runTestForName:(NSString *)name {
  453. NSDictionary *spec = [self specsForName:name];
  454. [self runTest:spec atBasePath:nil];
  455. // run again at a deeper location
  456. [self runTest:spec atBasePath:@"/foo/bar/baz"];
  457. }
  458. - (void)testAll {
  459. NSArray *specs = [self loadSpecs];
  460. for (NSDictionary *spec in specs) {
  461. [self runTest:spec atBasePath:nil];
  462. // run again at a deeper location
  463. [self runTest:spec atBasePath:@"/foo/bar/baz"];
  464. }
  465. }
  466. - (void)testDefaultListenHandlesParentSet {
  467. [self runTestForName:@"Default listen handles a parent set"];
  468. }
  469. - (void)testDefaultListenHandlesASetAtTheSameLevel {
  470. [self runTestForName:@"Default listen handles a set at the same level"];
  471. }
  472. - (void)testAQueryCanGetACompleteCacheThenAMerge {
  473. [self runTestForName:@"A query can get a complete cache then a merge"];
  474. }
  475. - (void)testServerMergeOnListenerWithCompleteChildren {
  476. [self runTestForName:@"Server merge on listener with complete children"];
  477. }
  478. - (void)testDeepMergeOnListenerWithCompleteChildren {
  479. [self runTestForName:@"Deep merge on listener with complete children"];
  480. }
  481. - (void)testUpdateChildListenerTwice {
  482. [self runTestForName:@"Update child listener twice"];
  483. }
  484. - (void)testChildOfDefaultListenThatAlreadyHasACompleteCache {
  485. [self runTestForName:@"Update child of default listen that already has a complete cache"];
  486. }
  487. - (void)testUpdateChildOfDefaultListenThatHasNoCache {
  488. [self runTestForName:@"Update child of default listen that has no cache"];
  489. }
  490. // failing
  491. - (void)testUpdateTheChildOfACoLocatedDefaultListenerAndQuery {
  492. [self runTestForName:@"Update (via set) the child of a co-located default listener and query"];
  493. }
  494. - (void)testUpdateTheChildOfAQueryWithAFullCache {
  495. [self runTestForName:@"Update (via set) the child of a query with a full cache"];
  496. }
  497. - (void)testUpdateAChildBelowAnEmptyQuery {
  498. [self runTestForName:@"Update (via set) a child below an empty query"];
  499. }
  500. - (void)testUpdateDescendantOfDefaultListenerWithFullCache {
  501. [self runTestForName:@"Update descendant of default listener with full cache"];
  502. }
  503. - (void)testDescendantSetBelowAnEmptyDefaultLIstenerIsIgnored {
  504. [self runTestForName:@"Descendant set below an empty default listener is ignored"];
  505. }
  506. - (void)testUpdateOfAChild {
  507. [self runTestForName:
  508. @"Update of a child. This can happen if a child listener is added and removed"];
  509. }
  510. - (void)testRevertSetWithOnlyChildCaches {
  511. [self runTestForName:@"Revert set with only child caches"];
  512. }
  513. - (void)testCanRevertADuplicateChildSet {
  514. [self runTestForName:@"Can revert a duplicate child set"];
  515. }
  516. - (void)testCanRevertAChildSetAndSeeTheUnderlyingData {
  517. [self runTestForName:@"Can revert a child set and see the underlying data"];
  518. }
  519. - (void)testRevertChildSetWithNoServerData {
  520. [self runTestForName:@"Revert child set with no server data"];
  521. }
  522. - (void)testRevertDeepSetWithNoServerData {
  523. [self runTestForName:@"Revert deep set with no server data"];
  524. }
  525. - (void)testRevertSetCoveredByNonvisibleTransaction {
  526. [self runTestForName:@"Revert set covered by non-visible transaction"];
  527. }
  528. - (void)testClearParentShadowingServerValuesSetWithServerChildren {
  529. [self runTestForName:@"Clear parent shadowing server values set with server children"];
  530. }
  531. - (void)testClearChildShadowingServerValuesSetWithServerChildren {
  532. [self runTestForName:@"Clear child shadowing server values set with server children"];
  533. }
  534. - (void)testUnrelatedMergeDoesntShadowServerUpdates {
  535. [self runTestForName:@"Unrelated merge doesn't shadow server updates"];
  536. }
  537. - (void)testCanSetAlongsideARemoteMerge {
  538. [self runTestForName:@"Can set alongside a remote merge"];
  539. }
  540. - (void)testSetPriorityOnALocationWithNoCache {
  541. [self runTestForName:@"setPriority on a location with no cache"];
  542. }
  543. - (void)testDeepUpdateDeletesChildFromLimitWindowAndPullsInNewChild {
  544. [self runTestForName:@"deep update deletes child from limit window and pulls in new child"];
  545. }
  546. - (void)testDeepSetDeletesChildFromLimitWindowAndPullsInNewChild {
  547. [self runTestForName:@"deep set deletes child from limit window and pulls in new child"];
  548. }
  549. - (void)testEdgeCaseInNewChildForChange {
  550. [self runTestForName:@"Edge case in newChildForChange_"];
  551. }
  552. - (void)testRevertSetInQueryWindow {
  553. [self runTestForName:@"Revert set in query window"];
  554. }
  555. - (void)testHandlesAServerValueMovingAChildOutOfAQueryWindow {
  556. [self runTestForName:@"Handles a server value moving a child out of a query window"];
  557. }
  558. - (void)testUpdateOfIndexedChildWorks {
  559. [self runTestForName:@"Update of indexed child works"];
  560. }
  561. - (void)testMergeAppliedToEmptyLimit {
  562. [self runTestForName:@"Merge applied to empty limit"];
  563. }
  564. - (void)testLimitIsRefilledFromServerDataAfterMerge {
  565. [self runTestForName:@"Limit is refilled from server data after merge"];
  566. }
  567. - (void)testHandleRepeatedListenWithMergeAsFirstUpdate {
  568. [self runTestForName:@"Handle repeated listen with merge as first update"];
  569. }
  570. - (void)testLimitIsRefilledFromServerDataAfterSet {
  571. [self runTestForName:@"Limit is refilled from server data after set"];
  572. }
  573. - (void)testQueryOnWeirdPath {
  574. [self runTestForName:@"query on weird path."];
  575. }
  576. - (void)testRunsRound2 {
  577. [self runTestForName:@"runs, round2"];
  578. }
  579. - (void)testHandlesNestedListens {
  580. [self runTestForName:@"handles nested listens"];
  581. }
  582. - (void)testHandlesASetBelowAListen {
  583. [self runTestForName:@"Handles a set below a listen"];
  584. }
  585. - (void)testDoesNonDefaultQueries {
  586. [self runTestForName:@"does non-default queries"];
  587. }
  588. - (void)testHandlesCoLocatedDefaultListenerAndQuery {
  589. [self runTestForName:@"handles a co-located default listener and query"];
  590. }
  591. - (void)testDefaultAndNonDefaultListenerAtSameLocationWithServerUpdate {
  592. [self runTestForName:@"Default and non-default listener at same location with server update"];
  593. }
  594. - (void)testAddAParentListenerToACompleteChildListenerExpectChildEvent {
  595. [self runTestForName:@"Add a parent listener to a complete child listener, expect child event"];
  596. }
  597. - (void)testAddListensToASetExpectCorrectEventsIncludingAChildEvent {
  598. [self runTestForName:@"Add listens to a set, expect correct events, including a child event"];
  599. }
  600. - (void)testServerUpdateToAChildListenerRaisesChildEventsAtParent {
  601. [self runTestForName:@"ServerUpdate to a child listener raises child events at parent"];
  602. }
  603. - (void)testServerUpdateToAChildListenerRaisesChildEventsAtParentQuery {
  604. [self runTestForName:@"ServerUpdate to a child listener raises child events at parent query"];
  605. }
  606. - (void)testMultipleCompleteChildrenAreHandleProperly {
  607. [self runTestForName:@"Multiple complete children are handled properly"];
  608. }
  609. - (void)testWriteLeafNodeOverwriteAtParentNode {
  610. [self runTestForName:@"Write leaf node, overwrite at parent node"];
  611. }
  612. - (void)testConfirmCompleteChildrenFromTheServer {
  613. [self runTestForName:@"Confirm complete children from the server"];
  614. }
  615. - (void)testWriteLeafOverwriteFromParent {
  616. [self runTestForName:@"Write leaf, overwrite from parent"];
  617. }
  618. - (void)testBasicUpdateTest {
  619. [self runTestForName:@"Basic update test"];
  620. }
  621. - (void)testNoDoubleValueEventsForUserAck {
  622. [self runTestForName:@"No double value events for user ack"];
  623. }
  624. - (void)testBasicKeyIndexSanityCheck {
  625. [self runTestForName:@"Basic key index sanity check"];
  626. }
  627. - (void)testCollectCorrectSubviewsToListenOn {
  628. [self runTestForName:@"Collect correct subviews to listen on"];
  629. }
  630. - (void)testLimitToFirstOneOnOrderedQuery {
  631. [self runTestForName:@"Limit to first one on ordered query"];
  632. }
  633. - (void)testLimitToLastOneOnOrderedQuery {
  634. [self runTestForName:@"Limit to last one on ordered query"];
  635. }
  636. - (void)testUpdateIndexedValueOnExistingChildFromLimitedQuery {
  637. [self runTestForName:@"Update indexed value on existing child from limited query"];
  638. }
  639. - (void)testCanCreateStartAtEndAtEqualToQueriesWithBool {
  640. [self runTestForName:@"Can create startAt, endAt, equalTo queries with bool"];
  641. }
  642. - (void)testQueryWithExistingServerSnap {
  643. [self runTestForName:@"Query with existing server snap"];
  644. }
  645. - (void)testServerDataIsNotPurgedForNonServerIndexedQueries {
  646. [self runTestForName:@"Server data is not purged for non-server-indexed queries"];
  647. }
  648. - (void)testStartAtEndAtDominatesLimit {
  649. [self runTestForName:@"startAt/endAt dominates limit"];
  650. }
  651. - (void)testUpdateToSingleChildThatMovesOutOfWindow {
  652. [self runTestForName:@"Update to single child that moves out of window"];
  653. }
  654. - (void)testLimitedQueryDoesntPullInOutOfRangeChild {
  655. [self runTestForName:@"Limited query doesn't pull in out of range child"];
  656. }
  657. - (void)testWithCustomOrderByIsRefilledWithCorrectItem {
  658. [self runTestForName:@"Limit with custom orderBy is refilled with correct item"];
  659. }
  660. - (void)testMergeForLocationWithDefaultAndLimitedListener {
  661. [self runTestForName:@"Merge for location with default and limited listener"];
  662. }
  663. - (void)testUserMergePullsInCorrectValues {
  664. [self runTestForName:@"User merge pulls in correct values"];
  665. }
  666. - (void)testUserDeepSetPullsInCorrectValues {
  667. [self runTestForName:@"User deep set pulls in correct values"];
  668. }
  669. - (void)testQueriesWithEqualToNullWork {
  670. [self runTestForName:@"Queries with equalTo(null) work"];
  671. }
  672. - (void)testRevertedWritesUpdateQuery {
  673. [self runTestForName:@"Reverted writes update query"];
  674. }
  675. - (void)testDeepSetForNonLocalDataDoesntRaiseEvents {
  676. [self runTestForName:@"Deep set for non-local data doesn't raise events"];
  677. }
  678. - (void)testUserUpdateWithNewChildrenTriggersEvents {
  679. [self runTestForName:@"User update with new children triggers events"];
  680. }
  681. - (void)testUserWriteWithDeepOverwrite {
  682. [self runTestForName:@"User write with deep user overwrite"];
  683. }
  684. - (void)testServerUpdatesPriority {
  685. [self runTestForName:@"Server updates priority"];
  686. }
  687. - (void)testRevertFullUnderlyingWrite {
  688. [self runTestForName:@"Revert underlying full overwrite"];
  689. }
  690. - (void)testUserChildOverwriteForNonexistentServerNode {
  691. [self runTestForName:@"User child overwrite for non-existent server node"];
  692. }
  693. - (void)testRevertUserOverwriteOfChildOnLeafNode {
  694. [self runTestForName:@"Revert user overwrite of child on leaf node"];
  695. }
  696. - (void)testServerOverwriteWithDeepUserDelete {
  697. [self runTestForName:@"Server overwrite with deep user delete"];
  698. }
  699. - (void)testUserOverwritesLeafNodeWithPriority {
  700. [self runTestForName:@"User overwrites leaf node with priority"];
  701. }
  702. - (void)testUserOverwritesInheritPriorityValuesFromLeafNodes {
  703. [self runTestForName:@"User overwrites inherit priority values from leaf nodes"];
  704. }
  705. - (void)testUserUpdateOnUserSetLeafNodeWithPriorityAfterServerUpdate {
  706. [self runTestForName:@"User update on user set leaf node with priority after server update"];
  707. }
  708. - (void)testServerDeepDeleteOnLeafNode {
  709. [self runTestForName:@"Server deep delete on leaf node"];
  710. }
  711. - (void)testUserSetsRootPriority {
  712. [self runTestForName:@"User sets root priority"];
  713. }
  714. - (void)testUserUpdatesPriorityOnEmptyRoot {
  715. [self runTestForName:@"User updates priority on empty root"];
  716. }
  717. - (void)testRevertSetAtRootWithPriority {
  718. [self runTestForName:@"Revert set at root with priority"];
  719. }
  720. - (void)testServerUpdatesPriorityAfterUserSetsPriority {
  721. [self runTestForName:@"Server updates priority after user sets priority"];
  722. }
  723. - (void)testEmptySetDoesntPreventServerUpdates {
  724. [self runTestForName:@"Empty set doesn't prevent server updates"];
  725. }
  726. - (void)testUserUpdatesPriorityTwiceFirstIsReverted {
  727. [self runTestForName:@"User updates priority twice, first is reverted"];
  728. }
  729. - (void)testServerAcksRootPrioritySetAfterUserDeletesRootNode {
  730. [self runTestForName:@"Server acks root priority set after user deletes root node"];
  731. }
  732. - (void)testADeleteInAMergeDoesntPushOutNodes {
  733. [self runTestForName:@"A delete in a merge doesn't push out nodes"];
  734. }
  735. - (void)testATaggedQueryFiresEventsEventually {
  736. [self runTestForName:@"A tagged query fires events eventually"];
  737. }
  738. - (void)testUserWriteOutsideOfLimitIsIgnoredForTaggedQueries {
  739. [self runTestForName:@"User write outside of limit is ignored for tagged queries"];
  740. }
  741. - (void)testAckForMergeDoesntRaiseValueEventForLaterListen {
  742. [self runTestForName:@"Ack for merge doesn't raise value event for later listen"];
  743. }
  744. - (void)testClearParentShadowingServerValuesMergeWithServerChildren {
  745. [self runTestForName:@"Clear parent shadowing server values merge with server children"];
  746. }
  747. - (void)testPrioritiesDontMakeMeSick {
  748. [self runTestForName:@"Priorities don't make me sick"];
  749. }
  750. - (void)testMergeThatMovesChildFromWindowToBoundaryDoesNotCauseChildToBeReadded {
  751. [self runTestForName:
  752. @"Merge that moves child from window to boundary does not cause child to be readded"];
  753. }
  754. - (void)testDeepMergeAckIsHandledCorrectly {
  755. [self runTestForName:@"Deep merge ack is handled correctly."];
  756. }
  757. - (void)testDeepMergeAckOnIncompleteDataAndWithServerValues {
  758. [self runTestForName:@"Deep merge ack (on incomplete data, and with server values)"];
  759. }
  760. - (void)testLimitQueryHandlesDeepServerMergeForOutOfViewItem {
  761. [self runTestForName:@"Limit query handles deep server merge for out-of-view item."];
  762. }
  763. - (void)testLimitQueryHandlesDeepUserMergeForOutOfViewItem {
  764. [self runTestForName:@"Limit query handles deep user merge for out-of-view item."];
  765. }
  766. - (void)testLimitQueryHandlesDeepUserMergeForOutOfViewItemFollowedByServerUpdate {
  767. [self runTestForName:
  768. @"Limit query handles deep user merge for out-of-view item followed by server update."];
  769. }
  770. - (void)testUnrelatedUntaggedUpdateIsNotCachedInTaggedListen {
  771. [self runTestForName:@"Unrelated, untagged update is not cached in tagged listen"];
  772. }
  773. - (void)testUnrelatedAckedSetIsNotCachedInTaggedListen {
  774. [self runTestForName:@"Unrelated, acked set is not cached in tagged listen"];
  775. }
  776. - (void)testUnrelatedAckedUpdateIsNotCachedInTaggedListen {
  777. [self runTestForName:@"Unrelated, acked update is not cached in tagged listen"];
  778. }
  779. - (void)testdeepUpdateRaisesImmediateEventsOnlyIfHasCompleteData {
  780. [self runTestForName:@"Deep update raises immediate events only if has complete data"];
  781. }
  782. - (void)testdeepUpdateReturnsMinimumDataRequired {
  783. [self runTestForName:@"Deep update returns minimum data required"];
  784. }
  785. - (void)testdeepUpdateRaisesAllEvents {
  786. [self runTestForName:@"Deep update raises all events"];
  787. }
  788. @end