diff --git a/src/client/tests/webapp/auto-verifier.test.ts b/src/client/tests/webapp/auto-verifier.test.ts index 36a02e3..b77a1cd 100644 --- a/src/client/tests/webapp/auto-verifier.test.ts +++ b/src/client/tests/webapp/auto-verifier.test.ts @@ -539,11 +539,10 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedStatus).toBe('fetching'); expect(primaryFunc).toHaveBeenCalled(); - expect(trustedFunc).toHaveBeenCalled(); // TODO: Is this the source of the problem? - trustedFunc needs to wait to be called until ensureTrustedFuncReadyFunc is resolved + expect(trustedFunc).toHaveBeenCalled(); expect(rejection).toBeUndefined(); - jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); // suppress the warning - primary.reject(new BasicWE(2)); // this will also 'unverify' the AutoVerifier + primary.reject(new Error('rejection')); // this will also 'unverify' the AutoVerifier await disjoint(); expect(rejection).toBeDefined(); @@ -556,6 +555,8 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(primaryFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1); // notably, this server response will be thrown out expect(verifyFunc).toHaveBeenCalledTimes(0); + + // TODO: Make sure the trusted rejecting later doesn't throw an uncaught promise rejection error }); test('primary with null, trusted rejects - cache miss, failed to ping server', async () => { @@ -633,11 +634,11 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(av.trustedStatus).toBe('verifying'); // suppress the warning about trusted rejecting after primary hit - const cwSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); + const cwSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}).mockReset(); // suppress the warning trusted.reject(new Error()); await disjoint(); - expect(cwSpy).toHaveBeenCalled(); + expect(cwSpy).toHaveBeenCalledTimes(1); expect(av.primaryPromise).toBe(null); expect(av.trustedPromise).toBe(null); expect(av.trustedStatus).toBe('none'); @@ -861,11 +862,11 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(av.trustedPromise).toBe(null); expect(av.trustedStatus).toBe('none'); - const cwSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}); // suppress the warning + const cwSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}).mockReset(); // suppress the warning trusted1.resolve(new BasicWE(2)); await disjoint(); - expect(cwSpy).toHaveBeenCalled(); + expect(cwSpy).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(2); expect(av.primaryPromise).toBe(null); expect(av.trustedPromise).toBe(trusted2.promise); @@ -987,8 +988,10 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(av.trustedPromise).toBe(null); expect(av.trustedStatus).toBe('none'); + const cwSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}).mockReset(); // suppress the warning await disjoint(); + expect(cwSpy).toHaveBeenCalledTimes(2); expect(av.primaryPromise).toBe(null); expect(av.trustedPromise).toBe(b.trusted.promise); expect(av.trustedStatus).toBe('verifying'); @@ -1020,6 +1023,71 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(verifyFunc).toHaveBeenCalledTimes(1); }); + + /* test('ab: a: primary with null, b: primary dedup, ab: resolve trusted fetch and unverify, second trusted with value', async () => { */ + /* const { */ + /* nextPrimary, nextTrusted, nextVerify, */ + /* primaryFunc, trustedFunc, verifyFunc */ + /* } = getManualsAndMocks(); */ + + /* const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); */ + + /* const a = fetchAndVerifyIfNeededManually(av, nextPrimary, nextTrusted, nextVerify); */ + /* const b = 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(); */ + + /* a.primary.resolve(null); */ + /* await disjoint(); */ + + /* expect(av.primaryPromise).toBe(null); */ + /* expect(av.trustedPromise).toBe(a.trusted.promise); */ + /* expect(av.trustedStatus).toBe('verifying'); */ + /* */ + /* a.trusted.reject(new Error('rejected')); */ + /* av.unverify() */ + /* */ + /* expect(av.primaryPromise).toBe(null); */ + /* expect(av.trustedPromise).toBe(null); */ + /* expect(av.trustedStatus).toBe('none'); */ + + /* await disjoint(); */ + + /* expect(av.primaryPromise).toBe(null); */ + /* expect(av.trustedPromise).toBe(b.trusted.promise); */ + /* expect(av.trustedStatus).toBe('verifying'); */ + + /* expect(verifyFunc).toHaveBeenCalledTimes(0); */ + /* b.trusted.resolve(new BasicWE(2)); */ + /* await disjoint(); */ + + /* expect(verifyFunc).toHaveBeenCalledWith(null, new BasicWE(2)); */ + /* expect(av.primaryPromise).toBe(null); */ + /* expect(av.trustedPromise).toBe(b.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(b.trusted.promise); */ + /* expect(av.trustedStatus).toBe('verified'); */ + + /* await disjoint(); */ + + /* expect(primaryFunc).toHaveBeenCalledTimes(1); */ + /* expect(trustedFunc).toHaveBeenCalledTimes(2); */ + /* 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 diff --git a/src/client/webapp/auto-verifier.ts b/src/client/webapp/auto-verifier.ts index ff6292e..cc116e2 100644 --- a/src/client/webapp/auto-verifier.ts +++ b/src/client/webapp/auto-verifier.ts @@ -163,11 +163,7 @@ export class AutoVerifier { console.warn('caught unverified primary promise', e); }); } - if (this.trustedPromise) { - this.trustedPromise.catch(e => { - console.warn('caught unverified trusted promise', e); - }); - } + // trusted promise rejections are logged through fetchAndVerifyIfNeeded if (this.verifyPromise) { this.verifyPromise.catch(e => { console.warn('caught unverified verify promise', e); @@ -235,7 +231,7 @@ export class AutoVerifier { try { trustedResult = await origTrustedPromise; } catch (e: unknown) { - if (this.trustedPromise == origTrustedPromise) { + if (this.trustedPromise === origTrustedPromise) { throw e; } else { console.warn('trusted promise rejected after unverify', e);