Skip to content
Open
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
37 changes: 37 additions & 0 deletions packages/aio-commerce-lib-app/test/fixtures/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2026 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import type { RuntimeActionParams } from "@adobe/aio-commerce-lib-core/params";

type CreateRuntimeActionParamsArgs = RuntimeActionParams & {
method?: RuntimeActionParams["__ow_method"];
path?: string;
body?: unknown;
query?: string;
headers?: Record<string, string | undefined>;
};

/** Builds OpenWhisk-style runtime action params for handler tests. */
export function createRuntimeActionParams(
args: CreateRuntimeActionParamsArgs = {},
): RuntimeActionParams {
const { method = "get", path = "/", body, query, headers, ...params } = args;

return {
...params,
__ow_method: method,
__ow_path: path,
...(body === undefined ? {} : { __ow_body: JSON.stringify(body) }),
...(query ? { __ow_query: query } : {}),
...(headers ? { __ow_headers: headers } : {}),
};
}
54 changes: 54 additions & 0 deletions packages/aio-commerce-lib-app/test/unit/actions/app-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2026 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import { describe, expect, test } from "vitest";

import { appConfigRuntimeAction } from "#actions/app-config";
import { createRuntimeActionParams } from "#test/fixtures/actions";
import { minimalValidConfig } from "#test/fixtures/config";

import type { CommerceAppConfigOutputModel } from "#config/schema/app";

describe("appConfigRuntimeAction", () => {
test("returns the validated app config for GET /", async () => {
const handler = appConfigRuntimeAction({
appConfig: minimalValidConfig,
});

const result = await handler(createRuntimeActionParams());

expect(result).toEqual({
type: "success",
statusCode: 200,
body: minimalValidConfig,
});
});

test("returns a 500 error when the app config is invalid", async () => {
const handler = appConfigRuntimeAction({
appConfig: {} as unknown as CommerceAppConfigOutputModel,
});

const result = await handler(createRuntimeActionParams());

expect(result).toMatchObject({
type: "error",
error: {
statusCode: 500,
body: {
message: "Internal server error",
error: "Invalid commerce app config",
},
},
});
});
});
224 changes: 224 additions & 0 deletions packages/aio-commerce-lib-app/test/unit/actions/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
* Copyright 2026 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import { beforeEach, describe, expect, test, vi } from "vitest";

const {
byScopeIdMock,
getConfigurationMock,
initializeMock,
setConfigurationMock,
} = vi.hoisted(() => ({
byScopeIdMock: vi.fn((scopeId: string) => ({ scopeId })),
getConfigurationMock: vi.fn(),
initializeMock: vi.fn(),
setConfigurationMock: vi.fn(),
}));

vi.mock("@adobe/aio-commerce-lib-config", async () => {
const actual = await vi.importActual<
typeof import("@adobe/aio-commerce-lib-config")
>("@adobe/aio-commerce-lib-config");

return {
...actual,
byScopeId: byScopeIdMock,
getConfiguration: getConfigurationMock,
initialize: initializeMock,
setConfiguration: setConfigurationMock,
};
});

import { configRuntimeAction } from "#actions/config";
import { createRuntimeActionParams } from "#test/fixtures/actions";

import type { BusinessConfigSchema } from "@adobe/aio-commerce-lib-config";

const configSchema = [
{
name: "apiKey",
label: "API Key",
description: "Commerce API key",
type: "password",
default: "",
},
{
name: "mode",
label: "Mode",
description: "App mode",
type: "text",
default: "sandbox",
},
] satisfies BusinessConfigSchema;

describe("configRuntimeAction", () => {
beforeEach(() => {
vi.clearAllMocks();
byScopeIdMock.mockImplementation((scopeId: string) => ({ scopeId }));
});

test("masks password values when retrieving configuration", async () => {
getConfigurationMock.mockResolvedValue({
scopeId: "store-1",
config: [
{ name: "apiKey", value: "super-secret", origin: "scope" },
{ name: "mode", value: "live", origin: "scope" },
],
});

const handler = configRuntimeAction({ configSchema });

const result = await handler(
createRuntimeActionParams({
query: "scopeId=store-1",
AIO_COMMERCE_CONFIG_ENCRYPTION_KEY: "encryption-key",
}),
);

expect(initializeMock).toHaveBeenCalledWith({ schema: configSchema });
expect(byScopeIdMock).toHaveBeenCalledWith("store-1");
expect(getConfigurationMock).toHaveBeenCalledWith(
{ scopeId: "store-1" },
{ encryptionKey: "encryption-key" },
);
Comment on lines +87 to +92
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines seem to assert implementation details that this test should not care about. I'd remove them

expect(result).toEqual({
type: "success",
statusCode: 200,
body: {
schema: configSchema,
values: {
scopeId: "store-1",
config: [
{ name: "apiKey", value: "*****", origin: "scope" },
{ name: "mode", value: "live", origin: "scope" },
],
},
},
});
Comment on lines +93 to +106
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably only assert the result in a specific test for the 200 result.

});

test("returns a 400 error when the scope id query parameter is missing", async () => {
const handler = configRuntimeAction({ configSchema });

const result = await handler(createRuntimeActionParams());

expect(result).toMatchObject({
type: "error",
error: {
statusCode: 400,
body: {
message: "Invalid query parameters",
},
},
});
});

test("filters masked password values before saving with PUT /", async () => {
setConfigurationMock.mockResolvedValue({
scopeId: "store-1",
config: [
{ name: "apiKey", value: "updated-secret", origin: "scope" },
{ name: "mode", value: "live", origin: "scope" },
],
});

const handler = configRuntimeAction({ configSchema });

const result = await handler(
createRuntimeActionParams({
method: "put",
body: {
scopeId: "store-1",
config: [
{ name: "apiKey", value: "*****" },
{ name: "mode", value: "live" },
],
},
}),
);

expect(initializeMock).toHaveBeenCalledWith({ schema: configSchema });
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idem on this initialize check. Don't think this test needs to care whether we call initialize or not.

expect(setConfigurationMock).toHaveBeenCalledWith(
{
config: [{ name: "mode", value: "live" }],
},
{ scopeId: "store-1" },
{ encryptionKey: undefined },
Comment on lines +154 to +155
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's assert only the parameters we want to actually check (in this case the first one). For others we can just expect.any(Object) or something like that. This way if those parameters are different in the future, these tests won't break.

);
expect(result).toEqual({
type: "success",
statusCode: 200,
headers: {
"Cache-Control": "no-store",
Deprecation: "Wed, 15 Apr 2026 00:00:00 GMT",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a specific test only for this header, to ensure future updates don't delete it.

},
body: {
scopeId: "store-1",
config: [
{ name: "apiKey", value: "*****", origin: "scope" },
{ name: "mode", value: "live", origin: "scope" },
],
},
});
Comment on lines +157 to +171
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I would not assert the result in this test. I would only do it in a test that is intended to do that.

});

test("forwards partial updates and null unsets with PATCH /", async () => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test it's a bit confusing to me. I would refactor it to two separate tests that assert the following:

  • PUT doesn't accept null values
  • PATCH does accept null values.

setConfigurationMock.mockResolvedValue({
scopeId: "store-1",
config: [
{ name: "apiKey", value: "persisted-secret", origin: "scope" },
{ name: "mode", value: "live", origin: "scope" },
],
});

const handler = configRuntimeAction({ configSchema });

const result = await handler(
createRuntimeActionParams({
method: "patch",
body: {
scopeId: "store-1",
config: [
{ name: "apiKey", value: null },
{ name: "mode", value: "live" },
],
},
AIO_COMMERCE_CONFIG_ENCRYPTION_KEY: "encryption-key",
}),
);

expect(setConfigurationMock).toHaveBeenCalledWith(
{
config: [
{ name: "apiKey", value: null },
{ name: "mode", value: "live" },
],
},
{ scopeId: "store-1" },
{ encryptionKey: "encryption-key" },
);
expect(result).toEqual({
type: "success",
statusCode: 200,
headers: {
"Cache-Control": "no-store",
},
body: {
scopeId: "store-1",
config: [
{ name: "apiKey", value: "*****", origin: "scope" },
{ name: "mode", value: "live", origin: "scope" },
],
},
});
});
});
Loading