From e1dca579a7bb5fc7711de34c94e9b9898cf55f45 Mon Sep 17 00:00:00 2001 From: necisam Date: Thu, 9 May 2024 15:07:49 +0800 Subject: [PATCH 1/5] fix(util-endpoints) improve evaluateCondition's debug msg #1267 --- .../src/utils/evaluateCondition.spec.ts | 19 +++++++++++++++++-- .../src/utils/evaluateCondition.ts | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/util-endpoints/src/utils/evaluateCondition.spec.ts b/packages/util-endpoints/src/utils/evaluateCondition.spec.ts index 65348c2bf75..9d6dc119df8 100644 --- a/packages/util-endpoints/src/utils/evaluateCondition.spec.ts +++ b/packages/util-endpoints/src/utils/evaluateCondition.spec.ts @@ -1,3 +1,4 @@ +import { debugId, toDebugString } from "../debug"; import { EndpointError, EvaluateOptions } from "../types"; import { callFunction } from "./callFunction"; import { evaluateCondition } from "./evaluateCondition"; @@ -31,19 +32,33 @@ describe(evaluateCondition.name, () => { [false, [false, 0, -0, null, undefined, NaN]], ])("returns %s for", (result, testCases) => { it.each(testCases)(`value: '%s'`, (mockReturn) => { + const mockLogger = { + debug: jest.fn(), + info: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + }; (callFunction as jest.Mock).mockReturnValue(mockReturn); - const { result, toAssign } = evaluateCondition(mockFnArgs, mockOptions); + const { result, toAssign } = evaluateCondition(mockFnArgs, {...mockOptions, logger:mockLogger}); expect(result).toBe(result); expect(toAssign).toBeUndefined(); + expect(mockLogger.debug).nthCalledWith(1, `${debugId} evaluateCondition: ${toDebugString(mockFnArgs)} = ${mockReturn}`) }); }); }); it("returns assigned value if defined", () => { const mockAssignedValue = "mockAssignedValue"; + const mockLogger = { + debug: jest.fn(), + info: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + }; (callFunction as jest.Mock).mockReturnValue(mockAssignedValue); - const { result, toAssign } = evaluateCondition({ assign: mockAssign, ...mockFnArgs }, mockOptions); + const { result, toAssign } = evaluateCondition({ assign: mockAssign, ...mockFnArgs }, {...mockOptions, logger:mockLogger}); expect(result).toBe(true); expect(toAssign).toEqual({ name: mockAssign, value: mockAssignedValue }); + expect(mockLogger.debug).nthCalledWith(1, `${debugId} evaluateCondition: ${toDebugString(mockFnArgs)} = ${mockAssignedValue}`) }); }); diff --git a/packages/util-endpoints/src/utils/evaluateCondition.ts b/packages/util-endpoints/src/utils/evaluateCondition.ts index 7f26abf0cdf..45d5f1b12a7 100644 --- a/packages/util-endpoints/src/utils/evaluateCondition.ts +++ b/packages/util-endpoints/src/utils/evaluateCondition.ts @@ -8,7 +8,7 @@ export const evaluateCondition = ({ assign, ...fnArgs }: ConditionObject, option } const value = callFunction(fnArgs, options); - options.logger?.debug?.(debugId, `evaluateCondition: ${toDebugString(fnArgs)} = ${toDebugString(value)}`); + options.logger?.debug?.(`${debugId} evaluateCondition: ${toDebugString(fnArgs)} = ${toDebugString(value)}`); return { result: value === "" ? true : !!value, From ce6b1beb867f26c9655f8259e906ba1d659aa785 Mon Sep 17 00:00:00 2001 From: necisam Date: Thu, 9 May 2024 15:08:01 +0800 Subject: [PATCH 2/5] fix(util-endpoints) improve evaluateConditions' debug msg #1267 --- .../src/utils/evaluateConditions.spec.ts | 14 +++++++++++++- .../util-endpoints/src/utils/evaluateConditions.ts | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/util-endpoints/src/utils/evaluateConditions.spec.ts b/packages/util-endpoints/src/utils/evaluateConditions.spec.ts index ef4041ab741..1f662ca5e68 100644 --- a/packages/util-endpoints/src/utils/evaluateConditions.spec.ts +++ b/packages/util-endpoints/src/utils/evaluateConditions.spec.ts @@ -1,3 +1,4 @@ +import { debugId, toDebugString } from "../debug"; import { ConditionObject, EvaluateOptions, FunctionReturn } from "../types"; import { evaluateCondition } from "./evaluateCondition"; import { evaluateConditions } from "./evaluateConditions"; @@ -5,9 +6,16 @@ import { evaluateConditions } from "./evaluateConditions"; jest.mock("./evaluateCondition"); describe(evaluateConditions.name, () => { + const mockLogger = { + debug: jest.fn(), + info: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + }; const mockOptions: EvaluateOptions = { endpointParams: {}, referenceRecord: {}, + logger: mockLogger, }; const mockCn1: ConditionObject = { fn: "fn1", argv: ["arg1"], assign: "assign1" }; const mockCn2: ConditionObject = { fn: "fn2", argv: ["arg2"], assign: "assign2" }; @@ -48,8 +56,9 @@ describe(evaluateConditions.name, () => { result: true, toAssign: { name: mockCn2.assign, value: value2 }, }); + - const { result, referenceRecord } = evaluateConditions([mockCn1, mockCn2], mockOptions); + const { result, referenceRecord } = evaluateConditions([mockCn1, mockCn2], {...mockOptions, }); expect(result).toBe(true); expect(referenceRecord).toEqual({ [mockCn1.assign!]: value1, @@ -60,5 +69,8 @@ describe(evaluateConditions.name, () => { ...mockOptions, referenceRecord: { [mockCn1.assign!]: value1 }, }); + expect(mockLogger.debug).nthCalledWith(1, `${debugId} assign: ${mockCn1.assign} := ${toDebugString(value1)}`) + expect(mockLogger.debug).nthCalledWith(2, `${debugId} assign: ${mockCn2.assign} := ${toDebugString(value2)}`) + }); }); diff --git a/packages/util-endpoints/src/utils/evaluateConditions.ts b/packages/util-endpoints/src/utils/evaluateConditions.ts index 24e34cb5e92..70b7d30e0fc 100644 --- a/packages/util-endpoints/src/utils/evaluateConditions.ts +++ b/packages/util-endpoints/src/utils/evaluateConditions.ts @@ -20,7 +20,7 @@ export const evaluateConditions = (conditions: ConditionObject[] = [], options: if (toAssign) { conditionsReferenceRecord[toAssign.name] = toAssign.value; - options.logger?.debug?.(debugId, `assign: ${toAssign.name} := ${toDebugString(toAssign.value)}`); + options.logger?.debug?.(`${debugId} assign: ${toAssign.name} := ${toDebugString(toAssign.value)}`); } } From 787abc564e34cf39e87a4159b1aae20c5b6b6ea8 Mon Sep 17 00:00:00 2001 From: necisam Date: Thu, 9 May 2024 15:08:16 +0800 Subject: [PATCH 3/5] fix(util-endpoints) improve evaluateEndpointRule's debug msg #1267 --- .../src/utils/evaluateEndpointRule.spec.ts | 26 ++++++++++++++----- .../src/utils/evaluateEndpointRule.ts | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/util-endpoints/src/utils/evaluateEndpointRule.spec.ts b/packages/util-endpoints/src/utils/evaluateEndpointRule.spec.ts index 735d3e00aae..1fafa42594d 100644 --- a/packages/util-endpoints/src/utils/evaluateEndpointRule.spec.ts +++ b/packages/util-endpoints/src/utils/evaluateEndpointRule.spec.ts @@ -1,3 +1,6 @@ +import { EvaluateOptions } from "@smithy/types"; + +import { debugId, toDebugString } from "../debug"; import { ConditionObject, EndpointRuleObject } from "../types"; import { evaluateConditions } from "./evaluateConditions"; import { evaluateEndpointRule } from "./evaluateEndpointRule"; @@ -11,9 +14,16 @@ jest.mock("./getEndpointHeaders"); jest.mock("./getEndpointProperties"); describe(evaluateEndpointRule.name, () => { - const mockOptions = { + const mockLogger = { + debug: jest.fn(), + info: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + }; + const mockOptions: EvaluateOptions = { endpointParams: {}, referenceRecord: {}, + logger: mockLogger, }; const mockConditions: ConditionObject[] = [ { fn: "fn1", argv: ["arg1"] }, @@ -65,6 +75,7 @@ describe(evaluateEndpointRule.name, () => { }); expect(getEndpointHeaders).not.toHaveBeenCalled(); expect(getEndpointProperties).not.toHaveBeenCalled(); + expect(mockLogger.debug).nthCalledWith(1, `${debugId} Resolving endpoint from template: ${toDebugString(mockEndpointRule.endpoint)}`) }); it("with headers and properties", () => { @@ -76,15 +87,15 @@ describe(evaluateEndpointRule.name, () => { (getEndpointHeaders as jest.Mock).mockReturnValue(mockOutputHeaders); (getEndpointProperties as jest.Mock).mockReturnValue(mockOutputProperties); - + const headerEndpoint = { + ...mockEndpoint, + headers: mockInputHeaders, + properties: mockInputProperties, + } const result = evaluateEndpointRule( { ...mockEndpointRule, - endpoint: { - ...mockEndpoint, - headers: mockInputHeaders, - properties: mockInputProperties, - }, + endpoint: headerEndpoint, }, mockOptions ); @@ -96,6 +107,7 @@ describe(evaluateEndpointRule.name, () => { }); expect(getEndpointHeaders).toHaveBeenCalledWith(mockInputHeaders, mockUpdatedOptions); expect(getEndpointProperties).toHaveBeenCalledWith(mockInputProperties, mockUpdatedOptions); + expect(mockLogger.debug).nthCalledWith(1, `${debugId} Resolving endpoint from template: ${toDebugString(headerEndpoint)}`) }); }); }); diff --git a/packages/util-endpoints/src/utils/evaluateEndpointRule.ts b/packages/util-endpoints/src/utils/evaluateEndpointRule.ts index e5e3eaa48de..0e59cb4c452 100644 --- a/packages/util-endpoints/src/utils/evaluateEndpointRule.ts +++ b/packages/util-endpoints/src/utils/evaluateEndpointRule.ts @@ -25,7 +25,7 @@ export const evaluateEndpointRule = ( const { url, properties, headers } = endpoint; - options.logger?.debug?.(debugId, `Resolving endpoint from template: ${toDebugString(endpoint)}`); + options.logger?.debug?.(`${debugId} Resolving endpoint from template: ${toDebugString(endpoint)}`); return { ...(headers != undefined && { From 2c9083b1641a4f4f1cab0fb3d4003bda9ebffd11 Mon Sep 17 00:00:00 2001 From: necisam Date: Thu, 9 May 2024 15:25:36 +0800 Subject: [PATCH 4/5] chroe: add changeset generated md --- .changeset/moody-wasps-try.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/moody-wasps-try.md diff --git a/.changeset/moody-wasps-try.md b/.changeset/moody-wasps-try.md new file mode 100644 index 00000000000..1e23b161825 --- /dev/null +++ b/.changeset/moody-wasps-try.md @@ -0,0 +1,5 @@ +--- +"@smithy/util-endpoints": patch +--- + +improves the debug message in util-endpoints From e204c738bc9b82bc89a3b1f4d4ca57834a3e9adc Mon Sep 17 00:00:00 2001 From: necisam Date: Fri, 10 May 2024 09:45:55 +0800 Subject: [PATCH 5/5] lint: run yarn format --- .../src/utils/evaluateCondition.spec.ts | 17 +++++++++++++---- .../src/utils/evaluateConditions.spec.ts | 8 +++----- .../src/utils/evaluateEndpointRule.spec.ts | 12 +++++++++--- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/util-endpoints/src/utils/evaluateCondition.spec.ts b/packages/util-endpoints/src/utils/evaluateCondition.spec.ts index 9d6dc119df8..6d6c0ebe9af 100644 --- a/packages/util-endpoints/src/utils/evaluateCondition.spec.ts +++ b/packages/util-endpoints/src/utils/evaluateCondition.spec.ts @@ -39,10 +39,13 @@ describe(evaluateCondition.name, () => { error: jest.fn(), }; (callFunction as jest.Mock).mockReturnValue(mockReturn); - const { result, toAssign } = evaluateCondition(mockFnArgs, {...mockOptions, logger:mockLogger}); + const { result, toAssign } = evaluateCondition(mockFnArgs, { ...mockOptions, logger: mockLogger }); expect(result).toBe(result); expect(toAssign).toBeUndefined(); - expect(mockLogger.debug).nthCalledWith(1, `${debugId} evaluateCondition: ${toDebugString(mockFnArgs)} = ${mockReturn}`) + expect(mockLogger.debug).nthCalledWith( + 1, + `${debugId} evaluateCondition: ${toDebugString(mockFnArgs)} = ${mockReturn}` + ); }); }); }); @@ -56,9 +59,15 @@ describe(evaluateCondition.name, () => { error: jest.fn(), }; (callFunction as jest.Mock).mockReturnValue(mockAssignedValue); - const { result, toAssign } = evaluateCondition({ assign: mockAssign, ...mockFnArgs }, {...mockOptions, logger:mockLogger}); + const { result, toAssign } = evaluateCondition( + { assign: mockAssign, ...mockFnArgs }, + { ...mockOptions, logger: mockLogger } + ); expect(result).toBe(true); expect(toAssign).toEqual({ name: mockAssign, value: mockAssignedValue }); - expect(mockLogger.debug).nthCalledWith(1, `${debugId} evaluateCondition: ${toDebugString(mockFnArgs)} = ${mockAssignedValue}`) + expect(mockLogger.debug).nthCalledWith( + 1, + `${debugId} evaluateCondition: ${toDebugString(mockFnArgs)} = ${mockAssignedValue}` + ); }); }); diff --git a/packages/util-endpoints/src/utils/evaluateConditions.spec.ts b/packages/util-endpoints/src/utils/evaluateConditions.spec.ts index 1f662ca5e68..7f4732ea068 100644 --- a/packages/util-endpoints/src/utils/evaluateConditions.spec.ts +++ b/packages/util-endpoints/src/utils/evaluateConditions.spec.ts @@ -56,9 +56,8 @@ describe(evaluateConditions.name, () => { result: true, toAssign: { name: mockCn2.assign, value: value2 }, }); - - const { result, referenceRecord } = evaluateConditions([mockCn1, mockCn2], {...mockOptions, }); + const { result, referenceRecord } = evaluateConditions([mockCn1, mockCn2], { ...mockOptions }); expect(result).toBe(true); expect(referenceRecord).toEqual({ [mockCn1.assign!]: value1, @@ -69,8 +68,7 @@ describe(evaluateConditions.name, () => { ...mockOptions, referenceRecord: { [mockCn1.assign!]: value1 }, }); - expect(mockLogger.debug).nthCalledWith(1, `${debugId} assign: ${mockCn1.assign} := ${toDebugString(value1)}`) - expect(mockLogger.debug).nthCalledWith(2, `${debugId} assign: ${mockCn2.assign} := ${toDebugString(value2)}`) - + expect(mockLogger.debug).nthCalledWith(1, `${debugId} assign: ${mockCn1.assign} := ${toDebugString(value1)}`); + expect(mockLogger.debug).nthCalledWith(2, `${debugId} assign: ${mockCn2.assign} := ${toDebugString(value2)}`); }); }); diff --git a/packages/util-endpoints/src/utils/evaluateEndpointRule.spec.ts b/packages/util-endpoints/src/utils/evaluateEndpointRule.spec.ts index 1fafa42594d..3fa011831a9 100644 --- a/packages/util-endpoints/src/utils/evaluateEndpointRule.spec.ts +++ b/packages/util-endpoints/src/utils/evaluateEndpointRule.spec.ts @@ -75,7 +75,10 @@ describe(evaluateEndpointRule.name, () => { }); expect(getEndpointHeaders).not.toHaveBeenCalled(); expect(getEndpointProperties).not.toHaveBeenCalled(); - expect(mockLogger.debug).nthCalledWith(1, `${debugId} Resolving endpoint from template: ${toDebugString(mockEndpointRule.endpoint)}`) + expect(mockLogger.debug).nthCalledWith( + 1, + `${debugId} Resolving endpoint from template: ${toDebugString(mockEndpointRule.endpoint)}` + ); }); it("with headers and properties", () => { @@ -91,7 +94,7 @@ describe(evaluateEndpointRule.name, () => { ...mockEndpoint, headers: mockInputHeaders, properties: mockInputProperties, - } + }; const result = evaluateEndpointRule( { ...mockEndpointRule, @@ -107,7 +110,10 @@ describe(evaluateEndpointRule.name, () => { }); expect(getEndpointHeaders).toHaveBeenCalledWith(mockInputHeaders, mockUpdatedOptions); expect(getEndpointProperties).toHaveBeenCalledWith(mockInputProperties, mockUpdatedOptions); - expect(mockLogger.debug).nthCalledWith(1, `${debugId} Resolving endpoint from template: ${toDebugString(headerEndpoint)}`) + expect(mockLogger.debug).nthCalledWith( + 1, + `${debugId} Resolving endpoint from template: ${toDebugString(headerEndpoint)}` + ); }); }); });