prettier/eslint auto-verifier.test.ts

This commit is contained in:
Michael Peters 2022-10-19 23:51:36 -07:00
parent 0af021bd32
commit a6a512617c
2 changed files with 1334 additions and 1339 deletions

View File

@ -132,6 +132,7 @@ module.exports = {
// typescript/react/jsx // typescript/react/jsx
// 'jsx-quotes': [ 'warn', 'prefer-double' ], // 'jsx-quotes': [ 'warn', 'prefer-double' ],
'@typescript-eslint/no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'unused-imports/no-unused-imports': 'warn', 'unused-imports/no-unused-imports': 'warn',
'react/self-closing-comp': 'warn', 'react/self-closing-comp': 'warn',
'react/jsx-boolean-value': ['warn', 'always'], 'react/jsx-boolean-value': ['warn', 'always'],

View File

@ -1,3 +1,4 @@
/* eslint-disable max-lines */
import { AutoVerifier } from '../../webapp/auto-verifier'; import { AutoVerifier } from '../../webapp/auto-verifier';
import { WithEquals } from '../../webapp/data-types'; import { WithEquals } from '../../webapp/data-types';
import Util from '../../webapp/util'; import Util from '../../webapp/util';
@ -9,15 +10,17 @@ class BasicWE implements WithEquals<BasicWE> {
return ids.map(id => new BasicWE(id)); return ids.map(id => new BasicWE(id));
} }
static fromCombos(combos: { id: string | number, extra: string | null }[]): BasicWE[] { static fromCombos(combos: { id: string | number; extra: string | null }[]): BasicWE[] {
return combos.map(({ id, extra }) => new BasicWE(id, extra)); return combos.map(({ id, extra }) => new BasicWE(id, extra));
} }
id: string; id: string;
constructor(public id_: string | number, public readonly extra: string | null = null) { this.id = id_ + '' } constructor(public id_: string | number, public readonly extra: string | null = null) {
this.id = id_ + '';
}
equals(other: BasicWE) { equals(other: BasicWE) {
return this.id == other.id && this.extra == other.extra; return this.id === other.id && this.extra === other.extra;
} }
} }
@ -78,8 +81,18 @@ describe('getChanges tests', () => {
}); });
test('changed', () => { test('changed', () => {
const primary = BasicWE.fromCombos([ { id: 2, extra: 'a' }, { id: 3, extra: 'b' }, { id: 4, extra: 'c' }, { id: 5, extra: 'd' } ]); const primary = BasicWE.fromCombos([
const trusted = BasicWE.fromCombos([ { id: 2, extra: '*' }, { id: 3, extra: 'b' }, { id: 4, extra: '*' }, { id: 5, extra: 'd' } ]); { id: 2, extra: 'a' },
{ id: 3, extra: 'b' },
{ id: 4, extra: 'c' },
{ id: 5, extra: 'd' },
]);
const trusted = BasicWE.fromCombos([
{ id: 2, extra: '*' },
{ id: 3, extra: 'b' },
{ id: 4, extra: '*' },
{ id: 5, extra: 'd' },
]);
const changes = AutoVerifier.getChanges(primary, trusted); const changes = AutoVerifier.getChanges(primary, trusted);
expect(changes).toEqual({ expect(changes).toEqual({
added: [], added: [],
@ -108,21 +121,21 @@ describe('constructor/factory tests', () => {
const av = new AutoVerifier(jest.fn(), jest.fn(), jest.fn()); const av = new AutoVerifier(jest.fn(), jest.fn(), jest.fn());
expect(av.primaryPromise).toBe(null); expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(null); expect(av.trustedPromise).toBe(null);
expect(av.trustedStatus).toBe('none') expect(av.trustedStatus).toBe('none');
}); });
test('createStandardListAutoVerifier', () => { test('createStandardListAutoVerifier', () => {
const av = AutoVerifier.createStandardListAutoVerifier(jest.fn(), jest.fn(), jest.fn()); const av = AutoVerifier.createStandardListAutoVerifier(jest.fn(), jest.fn(), jest.fn());
expect(av.primaryPromise).toBe(null); expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(null); expect(av.trustedPromise).toBe(null);
expect(av.trustedStatus).toBe('none') expect(av.trustedStatus).toBe('none');
}); });
test('createStandardSingleAutoVerifier', () => { test('createStandardSingleAutoVerifier', () => {
const av = AutoVerifier.createStandardListAutoVerifier(jest.fn(), jest.fn(), jest.fn()); const av = AutoVerifier.createStandardListAutoVerifier(jest.fn(), jest.fn(), jest.fn());
expect(av.primaryPromise).toBe(null); expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(null); expect(av.trustedPromise).toBe(null);
expect(av.trustedStatus).toBe('none') expect(av.trustedStatus).toBe('none');
}); });
}); });
@ -130,11 +143,11 @@ describe('fetchAndVerifyIfNeeded tests', () => {
class ManualPromise<T> { class ManualPromise<T> {
promise: Promise<T>; promise: Promise<T>;
resolve: (value: T) => void; resolve: (value: T) => void;
reject: (reason?: any) => void; reject: (reason: unknown) => void;
constructor() { constructor() {
this.resolve = undefined as unknown as (value: T) => void; this.resolve = undefined as unknown as (value: T) => void;
this.reject = undefined as unknown as (reason?: any) => void; this.reject = undefined as unknown as (reason: unknown) => void;
this.promise = new Promise<T>((resolve, reject) => { this.promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve; this.resolve = resolve;
this.reject = reject; this.reject = reject;
@ -143,17 +156,17 @@ describe('fetchAndVerifyIfNeeded tests', () => {
} }
function getManualsAndMocks(): { function getManualsAndMocks(): {
nextPrimary: () => ManualPromise<BasicWE | null>, nextPrimary: () => ManualPromise<BasicWE | null>;
nextTrusted: () => ManualPromise<BasicWE | null>, nextTrusted: () => ManualPromise<BasicWE | null>;
nextVerify: () => ManualPromise<boolean>, nextVerify: () => ManualPromise<boolean>;
primaryFunc: jest.Mock<Promise<BasicWE | null>, []>, primaryFunc: jest.Mock<Promise<BasicWE | null>, []>;
trustedFunc: jest.Mock<Promise<BasicWE | null>, []>, trustedFunc: jest.Mock<Promise<BasicWE | null>, []>;
verifyFunc: jest.Mock<Promise<boolean>, [primaryResult: BasicWE | null, trustedResult: BasicWE | null]>, verifyFunc: jest.Mock<Promise<boolean>, [primaryResult: BasicWE | null, trustedResult: BasicWE | null]>;
} { } {
const manuals: { const manuals: {
primary: ManualPromise<BasicWE | null>[], primary: ManualPromise<BasicWE | null>[];
trusted: ManualPromise<BasicWE | null>[], trusted: ManualPromise<BasicWE | null>[];
verify: ManualPromise<boolean>[], verify: ManualPromise<boolean>[];
} = { } = {
primary: [], primary: [],
trusted: [], trusted: [],
@ -161,13 +174,13 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}; };
const calls: { const calls: {
primary: number, primary: number;
trusted: number, trusted: number;
verify: number, verify: number;
} = { } = {
primary: 0, primary: 0,
trusted: 0, trusted: 0,
verify: 0 verify: 0,
}; };
function getNext<T>(manualList: ManualPromise<T>[]) { function getNext<T>(manualList: ManualPromise<T>[]) {
@ -182,8 +195,11 @@ describe('fetchAndVerifyIfNeeded tests', () => {
nextVerify: () => getNext(manuals.verify), nextVerify: () => getNext(manuals.verify),
primaryFunc: jest.fn(() => manuals.primary[calls.primary++]!.promise), primaryFunc: jest.fn(() => manuals.primary[calls.primary++]!.promise),
trustedFunc: jest.fn(() => manuals.trusted[calls.trusted++]!.promise), trustedFunc: jest.fn(() => manuals.trusted[calls.trusted++]!.promise),
verifyFunc: jest.fn((_primaryResult: BasicWE | null, _trustedResult: BasicWE | null) => manuals.verify[calls.verify++]!.promise), verifyFunc: jest.fn(
} (_primaryResult: BasicWE | null, _trustedResult: BasicWE | null) =>
manuals.verify[calls.verify++]!.promise,
),
};
} }
// this function makes it easier to do concurrent request // this function makes it easier to do concurrent request
@ -195,15 +211,15 @@ describe('fetchAndVerifyIfNeeded tests', () => {
nextPrimary: () => ManualPromise<T | null>, nextPrimary: () => ManualPromise<T | null>,
nextTrusted: () => ManualPromise<T | null>, nextTrusted: () => ManualPromise<T | null>,
nextVerify: () => ManualPromise<boolean>, nextVerify: () => ManualPromise<boolean>,
lazyVerify: boolean = false, lazyVerify = false,
) { ) {
const state: { const state: {
primary: ManualPromise<T | null>, primary: ManualPromise<T | null>;
trusted: ManualPromise<T | null>, trusted: ManualPromise<T | null>;
verify: ManualPromise<boolean>, verify: ManualPromise<boolean>;
result: T | null | undefined, result: T | null | undefined;
rejection: Error | undefined, rejection: Error | undefined;
resultPromise: Promise<T | null>, resultPromise: Promise<T | null>;
} = { } = {
primary: nextPrimary(), primary: nextPrimary(),
trusted: nextTrusted(), trusted: nextTrusted(),
@ -211,11 +227,15 @@ describe('fetchAndVerifyIfNeeded tests', () => {
result: undefined, result: undefined,
rejection: undefined, rejection: undefined,
resultPromise: av.fetchAndVerifyIfNeeded(lazyVerify), resultPromise: av.fetchAndVerifyIfNeeded(lazyVerify),
} };
state.resultPromise state.resultPromise
.then((value: T | null) => { state.result = value; }) .then((value: T | null) => {
.catch((value: Error) => { state.rejection = value; }); state.result = value;
})
.catch((value: Error) => {
state.rejection = value;
});
return state; return state;
} }
@ -225,10 +245,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
} }
test('primary null, then trusted with value - cache miss, return from server', async () => { test('primary null, then trusted with value - cache miss, return from server', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -237,7 +254,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(); const resultPromise = av.fetchAndVerifyIfNeeded();
let result: BasicWE | null | undefined = undefined; let result: BasicWE | null | undefined = undefined;
resultPromise.then(value => { result = value; }); resultPromise.then(value => {
result = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -279,10 +298,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
test('primary null, then trusted with value, then refetch, primary with value - primary use case', async () => { test('primary null, then trusted with value, then refetch, primary with value - primary use case', async () => {
// the main use case! // the main use case!
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary1 = nextPrimary(); const primary1 = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -292,7 +308,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const result1Promise = av.fetchAndVerifyIfNeeded(); const result1Promise = av.fetchAndVerifyIfNeeded();
let result1: BasicWE | null | undefined = undefined; let result1: BasicWE | null | undefined = undefined;
result1Promise.then(value => { result1 = value; }); result1Promise.then(value => {
result1 = value;
});
expect(av.primaryPromise).toBe(primary1.promise); expect(av.primaryPromise).toBe(primary1.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -329,7 +347,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const result2Promise = av.fetchAndVerifyIfNeeded(); const result2Promise = av.fetchAndVerifyIfNeeded();
let result2: BasicWE | null | undefined = undefined; let result2: BasicWE | null | undefined = undefined;
result2Promise.then(value => { result2 = value; }); result2Promise.then(value => {
result2 = value;
});
expect(av.primaryPromise).toBe(primary2.promise); expect(av.primaryPromise).toBe(primary2.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -350,14 +370,10 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(primaryFunc).toHaveBeenCalledTimes(2); expect(primaryFunc).toHaveBeenCalledTimes(2);
expect(trustedFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(verifyFunc).toHaveBeenCalledTimes(1); expect(verifyFunc).toHaveBeenCalledTimes(1);
}); });
test('primary with value, then trusted with value - cache hit, verify with server', async () => { test('primary with value, then trusted with value - cache hit, verify with server', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -366,7 +382,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(); const resultPromise = av.fetchAndVerifyIfNeeded();
let result: BasicWE | null | undefined = undefined; let result: BasicWE | null | undefined = undefined;
resultPromise.then(value => { result = value; }); resultPromise.then(value => {
result = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -407,10 +425,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
test('primary null, then trusted with null - cache miss, server confirms miss', async () => { test('primary null, then trusted with null - cache miss, server confirms miss', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -419,7 +434,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(); const resultPromise = av.fetchAndVerifyIfNeeded();
let result: BasicWE | null | undefined = undefined; let result: BasicWE | null | undefined = undefined;
resultPromise.then(value => { result = value; }); resultPromise.then(value => {
result = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -460,10 +477,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
test('primary with value, then trusted with null - cache hit, server deleted', async () => { test('primary with value, then trusted with null - cache hit, server deleted', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -472,7 +486,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(); const resultPromise = av.fetchAndVerifyIfNeeded();
let result: BasicWE | null | undefined = undefined; let result: BasicWE | null | undefined = undefined;
resultPromise.then(value => { result = value; }); resultPromise.then(value => {
result = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -513,10 +529,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
test('primary with null, lazy - lazy cache miss, need to ping server', async () => { test('primary with null, lazy - lazy cache miss, need to ping server', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -525,7 +538,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(true); const resultPromise = av.fetchAndVerifyIfNeeded(true);
let result: BasicWE | null | undefined = undefined; let result: BasicWE | null | undefined = undefined;
resultPromise.then(value => { result = value; }); resultPromise.then(value => {
result = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(null); // trustedFunc will never be called expect(av.trustedPromise).toBe(null); // trustedFunc will never be called
@ -571,17 +586,16 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
test('primary with value, lazy - lazy cache hit, no need to ping server', async () => { test('primary with value, lazy - lazy cache hit, no need to ping server', async () => {
const { const { nextPrimary, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(true); const resultPromise = av.fetchAndVerifyIfNeeded(true);
let result: BasicWE | null | undefined = undefined; let result: BasicWE | null | undefined = undefined;
resultPromise.then(value => { result = value; }); resultPromise.then(value => {
result = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(null); // trustedFunc will never be called expect(av.trustedPromise).toBe(null); // trustedFunc will never be called
@ -604,10 +618,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
test('primary rejects - cache fail', async () => { test('primary rejects - cache fail', async () => {
// expecting the promise to reject, the server to succeed, but the verify function to never be called // expecting the promise to reject, the server to succeed, but the verify function to never be called
const { const { nextPrimary, nextTrusted, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -615,7 +626,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(); const resultPromise = av.fetchAndVerifyIfNeeded();
let rejection: Error | undefined = undefined; let rejection: Error | undefined = undefined;
resultPromise.catch(value => { rejection = value; }); resultPromise.catch(value => {
rejection = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -643,10 +656,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
test('primary with null, trusted rejects - cache miss, failed to ping server', async () => { test('primary with null, trusted rejects - cache miss, failed to ping server', async () => {
// expect the promise to reject, but the verify function to never be called // expect the promise to reject, but the verify function to never be called
const { const { nextPrimary, nextTrusted, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -654,7 +664,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(); const resultPromise = av.fetchAndVerifyIfNeeded();
let rejection: Error | undefined = undefined; let rejection: Error | undefined = undefined;
resultPromise.catch(value => { rejection = value; }); resultPromise.catch(value => {
rejection = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -687,10 +699,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
test('primary with value, trusted rejects - cache hit, failed to ping server', async () => { test('primary with value, trusted rejects - cache hit, failed to ping server', async () => {
// expect the promise to reject, but the verify function to never be called // expect the promise to reject, but the verify function to never be called
const { const { nextPrimary, nextTrusted, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -698,7 +707,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(); const resultPromise = av.fetchAndVerifyIfNeeded();
let result: BasicWE | null | undefined = undefined; let result: BasicWE | null | undefined = undefined;
resultPromise.then(value => { result = value; }); resultPromise.then(value => {
result = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -716,7 +727,10 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(av.trustedStatus).toBe('verifying'); expect(av.trustedStatus).toBe('verifying');
// suppress the warning about trusted rejecting after primary hit // suppress the warning about trusted rejecting after primary hit
const cwSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}).mockReset(); // suppress the warning const cwSpy = jest
.spyOn(console, 'warn')
.mockImplementationOnce(() => undefined)
.mockReset(); // suppress the warning
trusted.reject(new Error('rejection')); trusted.reject(new Error('rejection'));
await disjoint(); await disjoint();
@ -733,10 +747,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
test('primary null, then trusted with value, verifyFunc rejects - cache miss, return from server, verify failed', async () => { test('primary null, then trusted with value, verifyFunc rejects - cache miss, return from server, verify failed', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -747,8 +758,12 @@ describe('fetchAndVerifyIfNeeded tests', () => {
let result: BasicWE | null | undefined = undefined; let result: BasicWE | null | undefined = undefined;
let rejection: unknown | undefined = undefined; let rejection: unknown | undefined = undefined;
resultPromise resultPromise
.then(value => { result = value; }) .then(value => {
.catch(value => { rejection = value; }); result = value;
})
.catch(value => {
rejection = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -774,7 +789,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(result).toBeUndefined(); expect(result).toBeUndefined();
expect(rejection).toBeUndefined(); expect(rejection).toBeUndefined();
verify.reject(new Error('rejection')) verify.reject(new Error('rejection'));
await disjoint(); await disjoint();
expect(result).toBeUndefined(); expect(result).toBeUndefined();
@ -791,10 +806,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
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 { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
@ -841,17 +853,14 @@ describe('fetchAndVerifyIfNeeded tests', () => {
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(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(verifyFunc).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 { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
@ -898,17 +907,14 @@ describe('fetchAndVerifyIfNeeded tests', () => {
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(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(verifyFunc).toHaveBeenCalledTimes(1); expect(verifyFunc).toHaveBeenCalledTimes(1);
}); });
test('ab, a: primary with null, b: primary dedup, ab: trusted rejects', async () => { test('ab, a: primary with null, b: primary dedup, ab: trusted rejects', async () => {
const { const { nextPrimary, nextTrusted, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
@ -917,7 +923,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const resultPromise1 = av.fetchAndVerifyIfNeeded(); const resultPromise1 = av.fetchAndVerifyIfNeeded();
let rejection1: Error | undefined = undefined; let rejection1: Error | undefined = undefined;
resultPromise1.catch(value => { rejection1 = value }); resultPromise1.catch(value => {
rejection1 = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -927,7 +935,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const resultPromise2 = av.fetchAndVerifyIfNeeded(); const resultPromise2 = av.fetchAndVerifyIfNeeded();
let rejection2: Error | undefined = undefined; let rejection2: Error | undefined = undefined;
resultPromise2.catch(value => { rejection2 = value }); resultPromise2.catch(value => {
rejection2 = value;
});
expect(av.primaryPromise).toBe(primary.promise); // doesn't change expect(av.primaryPromise).toBe(primary.promise); // doesn't change
expect(av.trustedPromise).toBe(trusted.promise); // doesn't change expect(av.trustedPromise).toBe(trusted.promise); // doesn't change
@ -952,17 +962,14 @@ describe('fetchAndVerifyIfNeeded tests', () => {
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(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(verifyFunc).toHaveBeenCalledTimes(0); expect(verifyFunc).toHaveBeenCalledTimes(0);
}); });
test('ab, a: primary with null, b: primary dedup, ab: trusted dedup with value, verifyFunc false', async () => { test('ab, a: primary with null, b: primary dedup, ab: trusted dedup with value, verifyFunc false', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
@ -1015,10 +1022,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
test('a: primary with null, b: primary re-fetch null, a: trusted with value, b: dedup', async () => { test('a: primary with null, b: primary re-fetch null, a: trusted with value, b: dedup', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
@ -1078,10 +1082,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
test('primary null, unverify during first trusted fetch, second trusted with value - cache miss, unverify during fetch, 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 { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted1 = nextTrusted(); const trusted1 = nextTrusted();
@ -1091,7 +1092,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(); const resultPromise = av.fetchAndVerifyIfNeeded();
let result: BasicWE | null | undefined = undefined; let result: BasicWE | null | undefined = undefined;
resultPromise.then(value => { result = value; }); resultPromise.then(value => {
result = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted1.promise); expect(av.trustedPromise).toBe(trusted1.promise);
@ -1112,7 +1115,12 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(av.trustedPromise).toBe(null); expect(av.trustedPromise).toBe(null);
expect(av.trustedStatus).toBe('none'); expect(av.trustedStatus).toBe('none');
const cwSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {}).mockReset(); // suppress the warning // eslint-disable-next-line no-empty-function
// eslint-disable-next-line no-empty
const cwSpy = jest
.spyOn(console, 'warn')
.mockImplementationOnce(() => undefined)
.mockReset(); // suppress the warning
trusted1.resolve(new BasicWE(2)); trusted1.resolve(new BasicWE(2));
await disjoint(); await disjoint();
@ -1148,10 +1156,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
test('primary null, unverify during verify, result is original trusted - cache miss, unverify during verify, return from server', async () => { test('primary null, unverify during verify, result is original trusted - cache miss, unverify during verify, return from server', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const primary = nextPrimary(); const primary = nextPrimary();
const trusted = nextTrusted(); const trusted = nextTrusted();
@ -1160,7 +1165,9 @@ describe('fetchAndVerifyIfNeeded tests', () => {
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
const resultPromise = av.fetchAndVerifyIfNeeded(); const resultPromise = av.fetchAndVerifyIfNeeded();
let result: BasicWE | null | undefined = undefined; let result: BasicWE | null | undefined = undefined;
resultPromise.then(value => { result = value; }); resultPromise.then(value => {
result = value;
});
expect(av.primaryPromise).toBe(primary.promise); expect(av.primaryPromise).toBe(primary.promise);
expect(av.trustedPromise).toBe(trusted.promise); expect(av.trustedPromise).toBe(trusted.promise);
@ -1208,10 +1215,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
test('ab: a: primary with null, b: primary dedup, ab: reject trusted fetch and unverify, second trusted with value', async () => { test('ab: a: primary with null, b: primary dedup, ab: reject trusted fetch and unverify, second trusted with value', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
@ -1232,13 +1236,16 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(av.trustedStatus).toBe('verifying'); expect(av.trustedStatus).toBe('verifying');
a.trusted.reject(new Error('rejection')); a.trusted.reject(new Error('rejection'));
av.unverify() av.unverify();
expect(av.primaryPromise).toBe(null); expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(null); expect(av.trustedPromise).toBe(null);
expect(av.trustedStatus).toBe('none'); expect(av.trustedStatus).toBe('none');
const cwSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}).mockReset(); // suppress the warning const cwSpy = jest
.spyOn(console, 'warn')
.mockImplementation(() => undefined)
.mockReset(); // suppress the warning
await disjoint(); await disjoint();
expect(cwSpy).toHaveBeenCalledTimes(2); expect(cwSpy).toHaveBeenCalledTimes(2);
@ -1258,7 +1265,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(a.result).toBeUndefined(); expect(a.result).toBeUndefined();
expect(b.result).toBeUndefined(); expect(b.result).toBeUndefined();
a.verify.resolve(true); a.verify.resolve(true);
await disjoint() await disjoint();
expect(a.result).toEqual(new BasicWE(2)); expect(a.result).toEqual(new BasicWE(2));
expect(b.result).toEqual(new BasicWE(2)); expect(b.result).toEqual(new BasicWE(2));
@ -1273,12 +1280,8 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(verifyFunc).toHaveBeenCalledTimes(1); expect(verifyFunc).toHaveBeenCalledTimes(1);
}); });
test('ab: a: primary with null, b: primary dedup, ab: resolve trusted fetch and unverify, second trusted with value', async () => { test('ab: a: primary with null, b: primary dedup, ab: resolve trusted fetch and unverify, second trusted with value', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
@ -1299,7 +1302,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(av.trustedStatus).toBe('verifying'); expect(av.trustedStatus).toBe('verifying');
a.trusted.resolve(new BasicWE(2)); a.trusted.resolve(new BasicWE(2));
av.unverify() av.unverify();
expect(av.primaryPromise).toBe(null); expect(av.primaryPromise).toBe(null);
expect(av.trustedPromise).toBe(null); expect(av.trustedPromise).toBe(null);
@ -1323,7 +1326,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(a.result).toBeUndefined(); expect(a.result).toBeUndefined();
expect(b.result).toBeUndefined(); expect(b.result).toBeUndefined();
a.verify.resolve(true); a.verify.resolve(true);
await disjoint() await disjoint();
expect(a.result).toEqual(new BasicWE(3)); expect(a.result).toEqual(new BasicWE(3));
expect(b.result).toEqual(new BasicWE(3)); expect(b.result).toEqual(new BasicWE(3));
@ -1339,10 +1342,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
}); });
test('ab: a: primary with null, b: primary dedup, ab: unverify during verify, ab: resolve with trusted anyway', async () => { test('ab: a: primary with null, b: primary dedup, ab: unverify during verify, ab: resolve with trusted anyway', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
@ -1391,17 +1391,14 @@ describe('fetchAndVerifyIfNeeded tests', () => {
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(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(verifyFunc).toHaveBeenCalledTimes(1); expect(verifyFunc).toHaveBeenCalledTimes(1);
}); });
test('ab: a: primary with null, b: primary dedup, ab: verifyFunc false, ab: resolve with trusted', async () => { test('ab: a: primary with null, b: primary dedup, ab: verifyFunc false, ab: resolve with trusted', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
@ -1449,17 +1446,14 @@ describe('fetchAndVerifyIfNeeded tests', () => {
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(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(verifyFunc).toHaveBeenCalledTimes(1); expect(verifyFunc).toHaveBeenCalledTimes(1);
}); });
test('ab: a: primary with null, b: primary dedup, ab: verifyFunc false, ab: resolve with trusted', async () => { test('ab: a: primary with null, b: primary dedup, ab: verifyFunc false, ab: resolve with trusted', async () => {
const { const { nextPrimary, nextTrusted, nextVerify, primaryFunc, trustedFunc, verifyFunc } = getManualsAndMocks();
nextPrimary, nextTrusted, nextVerify,
primaryFunc, trustedFunc, verifyFunc
} = getManualsAndMocks();
const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc); const av = new AutoVerifier(primaryFunc, trustedFunc, verifyFunc);
@ -1496,7 +1490,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
expect(a.result).toBeUndefined(); expect(a.result).toBeUndefined();
expect(b.result).toBeUndefined(); expect(b.result).toBeUndefined();
a.verify.reject(new Error('rejection')) a.verify.reject(new Error('rejection'));
await disjoint(); await disjoint();
expect(a.result).toBeUndefined(); expect(a.result).toBeUndefined();
@ -1509,7 +1503,7 @@ describe('fetchAndVerifyIfNeeded tests', () => {
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(primaryFunc).toHaveBeenCalledTimes(1);
expect(trustedFunc).toHaveBeenCalledTimes(1); expect(trustedFunc).toHaveBeenCalledTimes(1);
expect(verifyFunc).toHaveBeenCalledTimes(1); expect(verifyFunc).toHaveBeenCalledTimes(1);