Fix duplicate check results showing up in file diff view

We are using both the check results from the selected and the latest
patchset in the diff view, in the hope that one of them is likely
matching the diff that the user is looking at. But we have to make
sure that we don't show results twice, if the two patchsets are the
same.

Release-Notes: skip
Google-Bug-Id: b/299294378
Change-Id: Ia6ab19a67306fee895778527aa6c3f68097c3caf
diff --git a/polygerrit-ui/app/models/checks/checks-model.ts b/polygerrit-ui/app/models/checks/checks-model.ts
index 612862b..6718df9 100644
--- a/polygerrit-ui/app/models/checks/checks-model.ts
+++ b/polygerrit-ui/app/models/checks/checks-model.ts
@@ -410,11 +410,12 @@
   public allResults$ = select(
     combineLatest([
       this.checksSelectedPatchsetNumber$,
+      this.changeModel.latestPatchNum$,
       this.allResultsSelected$,
       this.allResultsLatest$,
     ]),
-    ([selectedPs, selected, latest]) =>
-      selectedPs ? [...selected, ...latest] : latest
+    ([selectedPs, latestPs, selected, latest]) =>
+      selectedPs && selectedPs !== latestPs ? [...selected, ...latest] : latest
   );
 
   constructor(
diff --git a/polygerrit-ui/app/models/checks/checks-model_test.ts b/polygerrit-ui/app/models/checks/checks-model_test.ts
index c8fd37a..767e604 100644
--- a/polygerrit-ui/app/models/checks/checks-model_test.ts
+++ b/polygerrit-ui/app/models/checks/checks-model_test.ts
@@ -291,6 +291,40 @@
     assert.equal(current.runs[0].results![0].summary, 'new');
   });
 
+  test('allResults$', async () => {
+    let results: CheckResult[] | undefined = undefined;
+    model.allResults$.subscribe(allResults => (results = allResults));
+    testResolver(changeViewModelToken).updateState({
+      checksPatchset: 1 as PatchSetNumber,
+    });
+    testResolver(changeModelToken).updateStateChange(createParsedChange());
+
+    model.updateStateSetProvider(PLUGIN_NAME, ChecksPatchset.SELECTED);
+    model.updateStateSetProvider(PLUGIN_NAME, ChecksPatchset.LATEST);
+    assert.equal(results!.length, 0);
+
+    model.updateStateSetResults(
+      PLUGIN_NAME,
+      RUNS,
+      [],
+      [],
+      undefined,
+      ChecksPatchset.LATEST
+    );
+    assert.equal(results!.length, 1);
+
+    model.updateStateSetResults(
+      PLUGIN_NAME,
+      RUNS,
+      [],
+      [],
+      undefined,
+      ChecksPatchset.SELECTED
+    );
+
+    assert.equal(results!.length, 1);
+  });
+
   test('polls for changes', async () => {
     const clock = sinon.useFakeTimers();
     let change: ParsedChangeInfo | undefined = undefined;