From c5af40cca4926301bb2de747e669db155fba4ef6 Mon Sep 17 00:00:00 2001 From: "denys.oblohin" Date: Fri, 30 Jun 2023 11:09:47 +0300 Subject: [PATCH] 1st param of getRemediator - idxResponse --- lib/idx/remediate.ts | 4 +- lib/idx/util.ts | 6 +-- test/spec/idx/remediate.ts | 26 +++++------ test/spec/idx/util.ts | 89 ++++++++++++++++++++++---------------- 4 files changed, 69 insertions(+), 56 deletions(-) diff --git a/lib/idx/remediate.ts b/lib/idx/remediate.ts index e2da0efc5..30c29ae2d 100644 --- a/lib/idx/remediate.ts +++ b/lib/idx/remediate.ts @@ -75,7 +75,7 @@ export async function remediate( return { idxResponse }; } - const remediator = getRemediator(neededToProceed, values, options, context); + const remediator = getRemediator(idxResponse, values, options); // Try actions in idxResponse first const actionFromValues = getActionFromValues(values, idxResponse); @@ -175,7 +175,7 @@ export async function remediate( // return nextStep directly if (options.useGenericRemediator && !idxResponse.interactionCode && !isTerminalResponse(idxResponse)) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const gr = getRemediator(idxResponse.neededToProceed, values, options, idxResponse.context)!; + const gr = getRemediator(idxResponse, values, options)!; const nextStep = getNextStep(authClient, gr, idxResponse); return { idxResponse, diff --git a/lib/idx/util.ts b/lib/idx/util.ts index 2a2e4f5f9..c725c97f1 100644 --- a/lib/idx/util.ts +++ b/lib/idx/util.ts @@ -210,14 +210,14 @@ function getRemediatorClass(remediation: IdxRemediation, options: RemediateOptio // Return first match idxRemediation in allowed remediators // eslint-disable-next-line complexity export function getRemediator( - idxRemediations: IdxRemediation[], + idxResponse: IdxResponse, values: RemediationValues, options: RemediateOptions, - context?: IdxContext, ): Remediator | undefined { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const remediators = options.remediators!; const useGenericRemediator = options.useGenericRemediator; + const {neededToProceed: idxRemediations, context} = idxResponse; let remediator: Remediator; // remediation name specified by caller - fast-track remediator lookup @@ -285,7 +285,7 @@ export function handleFailedResponse( if (terminal) { return { idxResponse, terminal, messages }; } else { - const remediator = getRemediator(idxResponse.neededToProceed, {}, options, idxResponse.context); + const remediator = getRemediator(idxResponse, {}, options); const nextStep = remediator && getNextStep(authClient, remediator, idxResponse); return { idxResponse, diff --git a/test/spec/idx/remediate.ts b/test/spec/idx/remediate.ts index 8cc035480..e18eee367 100644 --- a/test/spec/idx/remediate.ts +++ b/test/spec/idx/remediate.ts @@ -88,8 +88,8 @@ describe('idx/remediate', () => { nextStep: {} }); expect(util.getRemediator).toHaveBeenCalledTimes(2); - expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, { resend: true }, {}); - expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction.neededToProceed, {}, { actions: [] }); + expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, { resend: true }, {}); + expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction, {}, { actions: [] }); }); it('will handle exceptions', async () => { @@ -145,8 +145,8 @@ describe('idx/remediate', () => { nextStep: {} }); expect(util.getRemediator).toHaveBeenCalledTimes(2); - expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, { actions: ['some-action'] }); - expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction.neededToProceed, {}, { actions: [] }); + expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, { actions: ['some-action'] }); + expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction, {}, { actions: [] }); }); it('will handle exceptions', async () => { const { authClient } = testContext; @@ -195,8 +195,8 @@ describe('idx/remediate', () => { nextStep: {} }); expect(util.getRemediator).toHaveBeenCalledTimes(2); - expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, { actions: ['some-remediation'] }); - expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromRemediation.neededToProceed, {}, { actions: [] }); + expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, { actions: ['some-remediation'] }); + expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromRemediation, {}, { actions: [] }); }); it('will handle exceptions', async () => { let { authClient, idxResponse } = testContext; @@ -280,8 +280,8 @@ describe('idx/remediate', () => { nextStep: {} }); expect(util.getRemediator).toHaveBeenCalledTimes(2); - expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, { actions: [action] }); - expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction.neededToProceed, {}, { actions: [] }); + expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, { actions: [action] }); + expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction, {}, { actions: [] }); }); it('will handle exceptions', async () => { let { authClient, idxResponse } = testContext; @@ -339,8 +339,8 @@ describe('idx/remediate', () => { nextStep: {} }); expect(util.getRemediator).toHaveBeenCalledTimes(2); - expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, { actions: [action] }); - expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromRemediation.neededToProceed, {}, { actions: [] }); + expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, { actions: [action] }); + expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromRemediation, {}, { actions: [] }); }); it('will handle exceptions', async () => { let { authClient, idxResponse } = testContext; @@ -432,7 +432,7 @@ describe('idx/remediate', () => { }, }); expect(util.getRemediator).toHaveBeenCalledTimes(1); - expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, { step: 'some-remediation' }); + expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, { step: 'some-remediation' }); expect(idxResponse.proceed).toHaveBeenCalledWith('some-remediation', {}); }); it('will handle exceptions', async () => { @@ -530,8 +530,8 @@ describe('idx/remediate', () => { }); expect(idxResponse.proceed).toHaveBeenCalledWith(name, data); expect(util.getRemediator).toHaveBeenCalledTimes(2); - expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, {}); - expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromProceed.neededToProceed, valuesAfterProceed, {}); + expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, {}); + expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromProceed, valuesAfterProceed, {}); }); diff --git a/test/spec/idx/util.ts b/test/spec/idx/util.ts index 2af716ec2..2b4fd0305 100644 --- a/test/spec/idx/util.ts +++ b/test/spec/idx/util.ts @@ -218,75 +218,83 @@ describe('idx/util', () => { describe('A Remediator exists that matches one of the idx remediations', () => { beforeEach(() => { - const idxRemediations = [{ + const neededToProceed = [{ name: 'foo' }, { name: 'bar' }]; + const idxResponse = { + neededToProceed + } as IdxResponse; testContext = { ...testContext, - idxRemediations + neededToProceed, + idxResponse }; }); it('if first Remediator can remediate, returns the first Remediator instance', () => { - const { idxRemediations, values, options, FooRemediator } = testContext; + const { idxResponse, neededToProceed, values, options, FooRemediator } = testContext; FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(true); - expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(FooRemediator); - expect(FooRemediator).toHaveBeenCalledWith(idxRemediations[0], values, options); + expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(FooRemediator); + expect(FooRemediator).toHaveBeenCalledWith(neededToProceed[0], values, options); }); it('if first matched Remediator cannot remediate, but 2nd Remediator can, returns the 2nd Remediator', () => { - const { idxRemediations, values, options, FooRemediator, BarRemediator } = testContext; + const { idxResponse, neededToProceed, values, options, FooRemediator, BarRemediator } = testContext; FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false); BarRemediator.prototype.canRemediate = jest.fn().mockReturnValue(true); - expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(BarRemediator); - expect(BarRemediator).toHaveBeenCalledWith(idxRemediations[1], values, options); + expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(BarRemediator); + expect(BarRemediator).toHaveBeenCalledWith(neededToProceed[1], values, options); }); it('if no Remediator can remediate, returns the first matching Remediator instance', () => { - const { idxRemediations, values, options, FooRemediator, BarRemediator } = testContext; + const { idxResponse, neededToProceed, values, options, FooRemediator, BarRemediator } = testContext; FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false); BarRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false); - expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(FooRemediator); - expect(FooRemediator).toHaveBeenCalledWith(idxRemediations[0], values, options); + expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(FooRemediator); + expect(FooRemediator).toHaveBeenCalledWith(neededToProceed[0], values, options); }); describe('with options.step', () => { it('returns a remediator instance if a Remediator exists that matches the idx remediation with the given step name', () => { - const { idxRemediations, values, options, BarRemediator } = testContext; + const { idxResponse, neededToProceed, values, options, BarRemediator } = testContext; options.step = 'bar'; - expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(BarRemediator); - expect(BarRemediator).toHaveBeenCalledWith(idxRemediations[1], values, options); + expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(BarRemediator); + expect(BarRemediator).toHaveBeenCalledWith(neededToProceed[1], values, options); }); it('returns undefined if no Remediator could be found matching the idx remediation with the given step name', () => { - const { idxRemediations, values, options } = testContext; + const { idxResponse, values, options } = testContext; options.step = 'bar'; options.remediators = { 'other': jest.fn() }; - expect(getRemediator(idxRemediations, values, options)).toBe(undefined); + expect(getRemediator(idxResponse, values, options)).toBe(undefined); }); it('returns undefined if no idx remediation is found matching the given step name', () => { - const { values, options } = testContext; + const { idxResponse, values, options } = testContext; options.step = 'bar'; - const idxRemediations = [{ + const neededToProceed = [{ name: 'other' }]; - expect(getRemediator(idxRemediations, values, options)).toBe(undefined); + idxResponse.neededToProceed = neededToProceed; + expect(getRemediator(idxResponse, values, options)).toBe(undefined); }); }); }); it('returns undefined if no Remediator exists that matches the idx remediations', () => { const { values, options } = testContext; - const idxRemediations = [{ + const neededToProceed = [{ name: 'unknown' }]; + const idxResponse = { + neededToProceed + } as IdxResponse; options.remediators = { other: jest.fn() }; - expect(getRemediator(idxRemediations, values, options)).toBe(undefined); + expect(getRemediator(idxResponse, values, options)).toBe(undefined); }); describe('with options.useGenericRemediator', () => { @@ -295,56 +303,61 @@ describe('idx/util', () => { ...testContext.options, useGenericRemediator: true }; - const idxRemediations = [{ + const neededToProceed = [{ name: 'foo' }, { name: 'bar' }]; + const idxResponse = { + neededToProceed + } as IdxResponse; testContext = { ...testContext, - idxRemediations, + neededToProceed, + idxResponse, options }; }); describe('with options.step', () => { it('returns GenericRemediator instance if one idx remediation can match the given step name', () => { - const { idxRemediations, values, options } = testContext; + const { idxResponse, neededToProceed, values, options } = testContext; options.step = 'bar'; - expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(GenericRemediator); - expect(GenericRemediator).toHaveBeenCalledWith(idxRemediations[1], values, options); + expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(GenericRemediator); + expect(GenericRemediator).toHaveBeenCalledWith(neededToProceed[1], values, options); }); it('returns undefined if no idx remediation is found matching the given step name', () => { - const { values, options } = testContext; + const { idxResponse, values, options } = testContext; options.step = 'bar'; - const idxRemediations = [{ + const neededToProceed = [{ name: 'other' }]; - expect(getRemediator(idxRemediations, values, options)).toBe(undefined); + idxResponse.neededToProceed = neededToProceed; + expect(getRemediator(idxResponse, values, options)).toBe(undefined); }); }); describe('without options.step', () => { it('if first Remediator can remediate, returns the first Remediator instance', () => { - const { idxRemediations, values, options, FooRemediator } = testContext; + const { idxResponse, neededToProceed, values, options, FooRemediator } = testContext; FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(true); - expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(GenericRemediator); - expect(GenericRemediator).toHaveBeenCalledWith(idxRemediations[0], values, options); + expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(GenericRemediator); + expect(GenericRemediator).toHaveBeenCalledWith(neededToProceed[0], values, options); }); it('if first matched Remediator cannot remediate, but 2nd Remediator can, returns the first Remediator instance', () => { - const { idxRemediations, values, options, FooRemediator, BarRemediator } = testContext; + const { idxResponse, neededToProceed, values, options, FooRemediator, BarRemediator } = testContext; FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false); BarRemediator.prototype.canRemediate = jest.fn().mockReturnValue(true); - expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(GenericRemediator); - expect(GenericRemediator).toHaveBeenCalledWith(idxRemediations[0], values, options); + expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(GenericRemediator); + expect(GenericRemediator).toHaveBeenCalledWith(neededToProceed[0], values, options); }); it('if no Remediator can remediate, returns the first Remediator instance', () => { - const { idxRemediations, values, options, FooRemediator, BarRemediator } = testContext; + const { idxResponse, neededToProceed, values, options, FooRemediator, BarRemediator } = testContext; FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false); BarRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false); - expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(GenericRemediator); - expect(GenericRemediator).toHaveBeenCalledWith(idxRemediations[0], values, options); + expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(GenericRemediator); + expect(GenericRemediator).toHaveBeenCalledWith(neededToProceed[0], values, options); }); });