diff --git a/src/client/tests/webapp/auto-verifier.test.ts b/src/client/tests/webapp/auto-verifier.test.ts index 3e3a872..e8a5c2e 100644 --- a/src/client/tests/webapp/auto-verifier.test.ts +++ b/src/client/tests/webapp/auto-verifier.test.ts @@ -186,33 +186,33 @@ describe('fetchAndVerifyIfNeeded tests', () => { } } - function fetchAndVerifyIfNeededManually( - av: AutoVerifier, - nextPrimary: () => ManualPromise, - nextTrusted: () => ManualPromise, - nextVerify: () => ManualPromise, - lazyVerify: boolean = false, - ) { - const state: { - primary: ManualPromise, - trusted: ManualPromise, - verify: ManualPromise, - result: T | null | undefined, - rejection: Error | undefined, - } = { - primary: nextPrimary(), - trusted: nextTrusted(), - verify: nextVerify(), - result: undefined, - rejection: undefined, - } + function fetchAndVerifyIfNeededManually( + av: AutoVerifier, + nextPrimary: () => ManualPromise, + nextTrusted: () => ManualPromise, + nextVerify: () => ManualPromise, + lazyVerify: boolean = false, + ) { + const state: { + primary: ManualPromise, + trusted: ManualPromise, + verify: ManualPromise, + result: T | null | undefined, + rejection: Error | undefined, + } = { + primary: nextPrimary(), + trusted: nextTrusted(), + verify: nextVerify(), + result: undefined, + rejection: undefined, + } - const resultPromise = av.fetchAndVerifyIfNeeded(lazyVerify); - resultPromise.then((value) => { state.result = value; }); - resultPromise.catch((value) => { state.rejection = value; }); + const resultPromise = av.fetchAndVerifyIfNeeded(lazyVerify); + resultPromise.then((value) => { state.result = value; }); + resultPromise.catch((value) => { state.rejection = value; }); - return state; - } + return state; + } async function disjoint() { await Util.sleep(0); @@ -649,121 +649,121 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(verifyFunc).toHaveBeenCalledTimes(0); }); - test('ab, a: primary with value, b: primary dedup, ab: trusted dedup with value', async () => { + test('ab, a: primary with value, b: primary dedup, ab: trusted dedup with value', async () => { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks(); - const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); + const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); - const a = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); + 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(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'); - - expect(a.result).toBeUndefined(); - expect(b.result).toBeUndefined(); - a.primary.resolve(new BasicWE(2)); - await disjoint(); + 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'); + + expect(a.result).toBeUndefined(); + expect(b.result).toBeUndefined(); + a.primary.resolve(new BasicWE(2)); + await disjoint(); - expect(a.result).toEqual(new BasicWE(2)); - expect(b.result).toEqual(new BasicWE(2)); + expect(a.result).toEqual(new BasicWE(2)); + expect(b.result).toEqual(new BasicWE(2)); 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).toHaveBeenCalledTimes(0); + a.trusted.resolve(new BasicWE(2)); + await disjoint(); - expect(verifyFunc).toHaveBeenCalledWith(new BasicWE(2), new BasicWE(2)); + expect(verifyFunc).toHaveBeenCalledWith(new BasicWE(2), new BasicWE(2)); expect(av.primaryPromise).toBe(null); expect(av.trustedPromise).toBe(a.trusted.promise); expect(av.trustedStatus).toBe('verifying'); - a.verify.resolve(true); - await disjoint(); + a.verify.resolve(true); + await disjoint(); - expect(av.primaryPromise).toBe(null); + expect(av.primaryPromise).toBe(null); expect(av.trustedPromise).toBe(a.trusted.promise); expect(av.trustedStatus).toBe('verified'); - await disjoint(); // sanity check + await disjoint(); // sanity check - // Even though there were two fetchAnd... calls, they were deduped such that each func was only called once + // 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('ab, a: primary with null, b: primary dedup, ab: trusted dedup with value', async () => { + test('ab, a: primary with null, b: primary dedup, ab: trusted dedup with value', async () => { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks(); - const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); + const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); - const a = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); + 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(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(true); - await disjoint(); + const b = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); - expect(a.result).toEqual(new BasicWE(2)); - expect(b.result).toEqual(new BasicWE(2)); - expect(av.primaryPromise).toBe(null); + 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(true); + 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(a.trusted.promise); expect(av.trustedStatus).toBe('verified'); - await disjoint(); // sanity check + await disjoint(); // sanity check - // Even though there were two fetchAnd... calls, they were deduped such that each func was only called once + // 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); - }); + }); // 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 + // Trusted resolves *BEFORE* ensureTrustedFuncReady + // Fetching after verified doesn't re-fetch trusted });