[cloud functions] Test code that copies approvals from try results

Change-Id: I11b01bf3df42ec5bd3e2829493ca41030ad0e315
Reviewed-on: https://dart-review.googlesource.com/c/dart_ci/+/127280
Reviewed-by: Alexander Thomas <athom@google.com>
diff --git a/functions/node/test/test.dart b/functions/node/test/test.dart
index 0ae6fe1..3873536 100644
--- a/functions/node/test/test.dart
+++ b/functions/node/test/test.dart
@@ -51,6 +51,11 @@
         .thenAnswer((_) => Future(() => landedResponses.removeAt(0)));
     when(firestore.getLastCommit()).thenAnswer(
         (_) => Future(() => {...existingCommit, 'id': existingCommitHash}));
+    when(firestore.tryApprovals(44445)).thenAnswer((_) => Future(() =>
+        tryjobResults
+            .where((result) => result['review'] == 44445)
+            .where((result) => result['approved'] == true)
+            .toList()));
     when(firestore.reviewIsLanded(any)).thenAnswer((_) => Future.value(true));
 
     when(client.get(any))
@@ -61,6 +66,7 @@
     await builder.storeBuildCommitsInfo();
     expect(builder.endIndex, landedCommit['index']);
     expect(builder.startIndex, existingCommit['index'] + 1);
+    expect(builder.tryApprovals, {testResult(tryjobResults[0])});
     verifyInOrder([
       verify(firestore.getCommit(landedCommitHash)).called(2),
       verify(firestore.getLastCommit()),
@@ -85,4 +91,29 @@
       verify(firestore.getCommit(existingCommitHash))
     ]);
   });
+
+  test("copy approvals from try results", () async {
+    final firestore = FirestoreServiceMock();
+    final client = HttpClientMock();
+    when(firestore.getCommit(existingCommitHash))
+        .thenAnswer((_) => Future.value(existingCommit));
+    when(firestore.getCommit(landedCommitHash))
+        .thenAnswer((_) => Future.value(landedCommit));
+    when(firestore.tryApprovals(44445)).thenAnswer((_) => Future(() =>
+        tryjobResults
+            .where((result) => result['review'] == 44445)
+            .where((result) => result['approved'] == true)
+            .toList()));
+
+    final builder =
+        Build(landedCommitHash, landedCommitChange, firestore, client);
+    await builder.process([landedCommitChange]);
+
+    verifyZeroInteractions(client);
+    verifyInOrder([
+      verify(firestore.updateConfiguration(
+          "dart2js-new-rti-linux-x64-d8", "dart2js-rti-linux-x64-d8")),
+      verify(firestore.storeChange(any, 53, 54, approved: true))
+    ]);
+  });
 }
diff --git a/functions/node/test/test_data.dart b/functions/node/test/test_data.dart
index 55a02bf..1348569 100644
--- a/functions/node/test/test_data.dart
+++ b/functions/node/test/test_data.dart
@@ -49,7 +49,8 @@
   'author': 'gerrit_user@example.com',
   'created': DateTime.parse('2019-11-29 15:15:00Z'),
   'index': 54,
-  'title': 'A commit used for testing tryjob approvals, with index 54'
+  'title': 'A commit used for testing tryjob approvals, with index 54',
+  'review': 44445
 };
 
 const Map<String, dynamic> landedCommitChange = {
@@ -60,7 +61,7 @@
   "time_ms": 2384,
   "result": "RuntimeError",
   "expected": "Pass",
-  "matches": true,
+  "matches": false,
   "bot_name": "luci-dart-try-xenial-70-8fkh",
   "commit_hash": landedCommitHash,
   "commit_time": 1563576771,
@@ -75,7 +76,22 @@
   "changed": true
 };
 
-const Map<String, dynamic> tryjobResult = {};
+const List<Map<String, dynamic>> tryjobResults = [
+  {
+    "review": 44445,
+    "configurations": [
+      "dart2js-new-rti-linux-x64-d8",
+      "dartk-reload-rollback-linux-debug-x64",
+      "dartk-reload-linux-debug-x64"
+    ],
+    "name": "dart2js_extra/local_function_signatures_strong_test/none",
+    "patchset": 1,
+    "result": "RuntimeError",
+    "expected": "Pass",
+    "previous_result": "Pass",
+    "approved": true
+  },
+];
 
 String gitLogCommitHash = "a commit fetched from the git log";