From bae579da8653bdebd3213fb0b5fb7be0340834ee Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Tue, 11 Oct 2022 06:35:23 -0700 Subject: [PATCH] cache miss, unverify during fetch, return from server --- src/client/tests/webapp/auto-verifier.test.ts | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/client/tests/webapp/auto-verifier.test.ts b/src/client/tests/webapp/auto-verifier.test.ts index b596e83..4929673 100644 --- a/src/client/tests/webapp/auto-verifier.test.ts +++ b/src/client/tests/webapp/auto-verifier.test.ts @@ -826,8 +826,79 @@ 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 () => { + const { + nextPrimary, nextTrusted, nextVerify, + primaryFunc, trustedFunc, verifyFunc + } = getManualsAndMocks(); + + const primary = nextPrimary(); + const trusted1 = nextTrusted(); + const trusted2 = 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(trusted1.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(trusted1.promise); + expect(av.trustedStatus).toBe('verifying'); + + av.unverify(); + + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(null); + expect(av.trustedStatus).toBe('none'); + + const cwSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); // suppress the warning + trusted1.resolve(new BasicWE(2)); + await disjoint(); + + expect(cwSpy).toHaveBeenCalled(); + expect(trustedFunc).toHaveBeenCalledTimes(2); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted2.promise); + expect(av.trustedStatus).toBe('verifying'); + + expect(verifyFunc).toHaveBeenCalledTimes(0); + trusted2.resolve(new BasicWE(3)); + await disjoint(); + + expect(verifyFunc).toHaveBeenCalledWith(null, new BasicWE(3)); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted2.promise); + expect(av.trustedStatus).toBe('verifying'); + + expect(result).toBeUndefined(); + verify.resolve(true); + await disjoint(); + + expect(result).toEqual(new BasicWE(3)); + expect(av.primaryPromise).toBe(null); + expect(av.trustedPromise).toBe(trusted2.promise); + expect(av.trustedStatus).toBe('verified'); + + await disjoint(); // sanity check + + expect(primaryFunc).toHaveBeenCalledTimes(1); + expect(trustedFunc).toHaveBeenCalledTimes(2); + 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 // Trusted resolves *BEFORE* ensureTrustedFuncReady // Fetching after verified doesn't re-fetch trusted + // Unverify during verify w/ dedups });