diff --git a/src/client/tests/webapp/auto-verifier.test.ts b/src/client/tests/webapp/auto-verifier.test.ts index 9fc361a..33c982c 100644 --- a/src/client/tests/webapp/auto-verifier.test.ts +++ b/src/client/tests/webapp/auto-verifier.test.ts @@ -254,6 +254,8 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedStatus).toBe('verified'); + await disjoint(); // sanity check + expect(primaryFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1); expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1); @@ -311,11 +313,13 @@ describe('fetchAndVerifyIfNeeded tests', () => { verify.resolve(true); await disjoint(); - + expect(av.primaryPromise).toBe(null); expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedStatus).toBe('verified'); + await disjoint(); // sanity check + expect(primaryFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1); expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1); @@ -378,6 +382,8 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedStatus).toBe('none'); + await disjoint(); // sanity check + expect(primaryFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1); expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1); @@ -440,6 +446,8 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedStatus).toBe('none'); + await disjoint(); // sanity check + expect(primaryFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1); expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1); @@ -508,6 +516,8 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedStatus).toBe('verified'); + await disjoint(); // sanity check + expect(primaryFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1); expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(1); @@ -539,12 +549,52 @@ describe('fetchAndVerifyIfNeeded tests', () => { expect(result).toEqual(new BasicWE(2)); expect(av.trustedStatus).toBe('none'); + await disjoint(); // sanity check + expect(primaryFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(0); expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(0); expect(verifyFunc).toHaveBeenCalledTimes(0); }); + test('primary rejects - cache fail', async () => { + // expecting the promise to reject, the server to succeed, but the verify function not to be called + const { + nextPrimary, nextTrusted, + primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc + } = getManualsAndMocks(); + + const primary = nextPrimary(); + const trusted = nextTrusted(); + + const av = new AutoVerifier(primaryFunc, trustedFunc, ensureTrustedFuncReadyFunc, verifyFunc); + const resultPromise = av.fetchAndVerifyIfNeeded(); + let rejection: Error | undefined = undefined; + resultPromise.catch(value => { rejection = value; }); + + expect(av.primaryPromise).toBe(primary.promise); + 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(rejection).toBeUndefined(); + + primary.reject(new BasicWE(2)); // this will also 'unverify' the AutoVerifier + await disjoint(); + + expect(rejection).toBeDefined(); + 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); // notably, this server response will be thrown out + expect(ensureTrustedFuncReadyFunc).toHaveBeenCalledTimes(0); + expect(verifyFunc).toHaveBeenCalledTimes(0); + }); + // Make sure to do try/catch errors/rejections, verification failures, unverifies in the middle // Multiple tryResolveTrustedPromises }); diff --git a/src/jest.config.ts b/src/jest.config.ts index 5e5b23e..2b4dc9a 100644 --- a/src/jest.config.ts +++ b/src/jest.config.ts @@ -6,6 +6,7 @@ const config: Config = { coverageDirectory: './coverage', collectCoverage: true, testPathIgnorePatterns: [ '/^node_modules$/', '/^archive$/' ], + setupFilesAfterEnv: [ './jest.setup.ts' ], }; export default config; diff --git a/src/jest.setup.ts b/src/jest.setup.ts new file mode 100644 index 0000000..139d3e1 --- /dev/null +++ b/src/jest.setup.ts @@ -0,0 +1,8 @@ +// silence debug/log/warn console logs (since they are HUGE in the jest output) +global.console = { + ...console, + debug: jest.fn(), + log: jest.fn(), + warn: jest.fn(), +}; +