From 46e0d4c1a7925e610f194301b83c49e262867b59 Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Wed, 19 Oct 2022 22:13:25 -0700 Subject: [PATCH] verifyFunc false tests --- src/client/tests/webapp/auto-verifier.test.ts | 119 +++++++++++++++++- src/client/webapp/auto-verifier.ts | 1 + 2 files changed, 117 insertions(+), 3 deletions(-) diff --git a/src/client/tests/webapp/auto-verifier.test.ts b/src/client/tests/webapp/auto-verifier.test.ts index 9323256..35a3d61 100644 --- a/src/client/tests/webapp/auto-verifier.test.ts +++ b/src/client/tests/webapp/auto-verifier.test.ts @@ -444,7 +444,7 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(result).toBeNull(); expect(av.primaryPromise).toBe(null); - expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedPromise).toBe(null); expect(av.trustedStatus).toBe('none'); await disjoint(); // sanity check @@ -497,7 +497,7 @@ describe('fetchAndVerifyIfNeeded tests', () => { await disjoint(); expect(av.primaryPromise).toBe(null); - expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedPromise).toBe(null); expect(av.trustedStatus).toBe('none'); await disjoint(); // sanity check @@ -894,6 +894,63 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(trustedFunc).toHaveBeenCalledTimes(1); expect(verifyFunc).toHaveBeenCalledTimes(0); }); + + test('ab, a: primary with null, b: primary dedup, ab: trusted dedup with value, verifyFunc false', async () => { + const { + nextPrimary, nextTrusted, nextVerify, + primaryFunc, trustedFunc, verifyFunc + } = getManualsAndMocks(); + + const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); + + const a = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); + + expect(av.primaryPromise).toBe(a.primary.promise); + expect(av.trustedPromise).toBe(a.trusted.promise); + expect(av.trustedStatus).toBe('fetching'); + expect(primaryFunc).toHaveBeenCalled(); + expect(trustedFunc).toHaveBeenCalled(); + + const b = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); + + expect(av.primaryPromise).toBe(a.primary.promise); // doesn't change from a + expect(av.trustedPromise).toBe(a.trusted.promise); // doesn't change from a + expect(av.trustedStatus).toBe('fetching'); + + a.primary.resolve(null); + await disjoint(); + + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(a.trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + expect(verifyFunc).toHaveBeenCalledTimes(0); + a.trusted.resolve(new BasicWE(2)); + await disjoint(); + + expect(verifyFunc).toHaveBeenCalledWith(null, new BasicWE(2)); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(a.trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + expect(a.result).toBeUndefined(); + expect(b.result).toBeUndefined(); + a.verify.resolve(false); + await disjoint(); + + expect(a.result).toEqual(new BasicWE(2)); + expect(b.result).toEqual(new BasicWE(2)); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(null); + expect(av.trustedStatus).toBe('none'); + + await disjoint(); // sanity check + + // Even though there were two fetchAnd... calls, they were deduped such that each func was only called once + expect(primaryFunc).toHaveBeenCalledTimes(1); + expect(trustedFunc).toHaveBeenCalledTimes(1); + expect(verifyFunc).toHaveBeenCalledTimes(1); + }); test('a: primary with null, b: primary re-fetch null, a: trusted with value, b: dedup', async () => { const { @@ -1278,6 +1335,63 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(verifyFunc).toHaveBeenCalledTimes(1); }); + test('ab: a: primary with null, b: primary dedup, ab: verifyFunc false, ab: resolve with trusted', async () => { + const { + nextPrimary, nextTrusted, nextVerify, + primaryFunc, trustedFunc, verifyFunc + } = getManualsAndMocks(); + + const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); + + const a = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); + + expect(av.primaryPromise).toBe(a.primary.promise); + expect(av.trustedPromise).toBe(a.trusted.promise); + expect(av.trustedStatus).toBe('fetching'); + expect(primaryFunc).toHaveBeenCalled(); + expect(trustedFunc).toHaveBeenCalled(); + + const b = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); + + expect(av.primaryPromise).toBe(a.primary.promise); // doesn't change from a + expect(av.trustedPromise).toBe(a.trusted.promise); // doesn't change from a + expect(av.trustedStatus).toBe('fetching'); + + a.primary.resolve(null); + await disjoint(); + + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(a.trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + expect(verifyFunc).toHaveBeenCalledTimes(0); + a.trusted.resolve(new BasicWE(2)); + await disjoint(); + + expect(verifyFunc).toHaveBeenCalledWith(null, new BasicWE(2)); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(a.trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + expect(a.result).toBeUndefined(); + expect(b.result).toBeUndefined(); + + a.verify.resolve(false); + await disjoint(); + + expect(a.result).toEqual(new BasicWE(2)); + expect(b.result).toEqual(new BasicWE(2)); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(null); + expect(av.trustedStatus).toBe('none'); + + await disjoint(); // sanity check + + // Even though there were two fetchAnd... calls, they were deduped such that each func was only called once + expect(primaryFunc).toHaveBeenCalledTimes(1); + expect(trustedFunc).toHaveBeenCalledTimes(1); + expect(verifyFunc).toHaveBeenCalledTimes(1); + }); // TODO: Why not have fetcher and a simple dedup wrapper to remove some complexity from this function? // This would likely make it possible to get rid of the "else" block in tryResolveTrustedPromise // Make sure it would work properly for retry functionality @@ -1287,7 +1401,6 @@ describe('fetchAndVerifyIfNeeded tests', () => { // Fetching after verified doesn't re-fetch trusted // Unverify during verify w/ dedups // verifyFunc returns false - // verifyFunc returns false while different trusted succeeds? - this is not possible // verifyFunc rejects // not possible (and therefore not tested) list diff --git a/src/client/webapp/auto-verifier.ts b/src/client/webapp/auto-verifier.ts index b7b9e54..abea834 100644 --- a/src/client/webapp/auto-verifier.ts +++ b/src/client/webapp/auto-verifier.ts @@ -272,6 +272,7 @@ export class AutoVerifier { } else { // we have to re-fetch the trusted promise again next fetch this.trustedStatus = 'none'; + this.trustedPromise = null; } } else { // this actually could be quite common