Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve util-endpoints debug msg #1268

Merged
merged 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/moody-wasps-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/util-endpoints": patch
---

improves the debug message in util-endpoints
28 changes: 26 additions & 2 deletions packages/util-endpoints/src/utils/evaluateCondition.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { debugId, toDebugString } from "../debug";
import { EndpointError, EvaluateOptions } from "../types";
import { callFunction } from "./callFunction";
import { evaluateCondition } from "./evaluateCondition";
Expand Down Expand Up @@ -31,19 +32,42 @@ 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}`
);
});
});
2 changes: 1 addition & 1 deletion packages/util-endpoints/src/utils/evaluateCondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion packages/util-endpoints/src/utils/evaluateConditions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { debugId, toDebugString } from "../debug";
import { ConditionObject, EvaluateOptions } from "../types";
import { evaluateCondition } from "./evaluateCondition";
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" };
Expand Down Expand Up @@ -49,7 +57,7 @@ describe(evaluateConditions.name, () => {
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,
Expand All @@ -60,5 +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)}`);
});
});
2 changes: 1 addition & 1 deletion packages/util-endpoints/src/utils/evaluateConditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
}
}

Expand Down
32 changes: 25 additions & 7 deletions packages/util-endpoints/src/utils/evaluateEndpointRule.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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"] },
Expand Down Expand Up @@ -65,6 +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)}`
);
});

it("with headers and properties", () => {
Expand All @@ -76,15 +90,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
);
Expand All @@ -96,6 +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)}`
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 && {
Expand Down
Loading