From 0df23134a1fe4ba6dfb2b8f553e2bbd56947c5f7 Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Tue, 11 Oct 2022 06:51:39 -0700 Subject: [PATCH] add test for unverify during verify --- src/client/tests/webapp/auto-verifier.test.ts | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/client/tests/webapp/auto-verifier.test.ts b/src/client/tests/webapp/auto-verifier.test.ts index 4929673..e543d20 100644 --- a/src/client/tests/webapp/auto-verifier.test.ts +++ b/src/client/tests/webapp/auto-verifier.test.ts @@ -826,7 +826,7 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(verifyFunc).toHaveBeenCalledTimes(1); }); - test('primary null, unverify during first trusted, then trusted with value - cache miss, unverify, return from server', async () => { + test('primary null, unverify during first trusted fetch, second trusted with value - cache miss, unverify during fetch, return from server', async () => { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc @@ -895,6 +895,66 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(trustedFunc).toHaveBeenCalledTimes(2); expect(verifyFunc).toHaveBeenCalledTimes(1); }); + + test('primary null, unverify during verify, result is original trusted - cache miss, unverify during verify, return from server', async () => { + const { + nextPrimary, nextTrusted, nextVerify, + primaryFunc, trustedFunc, verifyFunc + } = getManualsAndMocks(); + + const primary = nextPrimary(); + const trusted = nextTrusted(); + const verify = nextVerify(); + + const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); + const resultPromise = av.fetchAndVerifyIfNeeded(); + let result: BasicWE | null | undefined = undefined; + resultPromise.then(value => { result = value; }); + + expect(av.primaryPromise).toBe(primary.promise); + expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedStatus).toBe('fetching'); + expect(primaryFunc).toHaveBeenCalled(); + expect(trustedFunc).toHaveBeenCalledTimes(1); + + primary.resolve(null); + await disjoint(); + + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + expect(verifyFunc).toHaveBeenCalledTimes(0); + trusted.resolve(new BasicWE(2)); + await disjoint(); + + expect(verifyFunc).toHaveBeenCalledWith(null, new BasicWE(2)); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted.promise); + expect(av.trustedStatus).toBe('verifying'); + + av.unverify(); // effectively during the verify function call + + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(null); + expect(av.trustedStatus).toBe('none'); + + expect(result).toBeUndefined(); + verify.resolve(true); + await disjoint(); + + // result is the value of the trusted promise, but the trusted will be re-called on next fetch + expect(result).toEqual(new BasicWE(2)); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(null); + expect(av.trustedStatus).toBe('none'); + + await disjoint(); // sanity check + + expect(primaryFunc).toHaveBeenCalledTimes(1); + expect(trustedFunc).toHaveBeenCalledTimes(1); + expect(verifyFunc).toHaveBeenCalledTimes(1); + }); // Make sure to do try/catch errors/rejections, verification failures, unverifies in the middle // Multiple tryResolveTrustedPromises - multiple fetchAndVerifyIfNeeded at the same time