From fd364bca1832b860424dda6bd527b3444c643137 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Tue, 9 Jan 2024 11:47:14 +0000 Subject: [PATCH] Added tests for `action-input-descriptor` --- .../actions/action-input-descriptor.spec.ts | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tests/unit/utils/actions/action-input-descriptor.spec.ts diff --git a/tests/unit/utils/actions/action-input-descriptor.spec.ts b/tests/unit/utils/actions/action-input-descriptor.spec.ts new file mode 100644 index 0000000..46ff67c --- /dev/null +++ b/tests/unit/utils/actions/action-input-descriptor.spec.ts @@ -0,0 +1,126 @@ +import { ActionMetadata } from "@/utils/actions/action-metadata"; +import { parseActionParameterTypeDescriptor } from "@/utils/actions/action-parameter-type-descriptor"; +import { getActionInputDescriptor, getActionInputDescriptors } from "@/utils/actions/action-input-descriptor"; +import { ActionParameterPathParser } from "@/utils/actions/action-parameter-path-parser"; + +const METADATA = Object.freeze({ + name: "test-metadata", + description: "Test description", + runs: { main: "index.js", using: "node16" }, + + inputs: { + "foo": { + description: "Foo description", + default: 3, + type: "number", + deprecationMessage: "Foo is deprecated", + }, + + "bar": { + description: "Bar description", + default: true, + type: "boolean", + required: false, + redirect: "bar-bar", + }, + + "baz-path": { + description: "Baz description", + type: "utils.io.FileInfo[]:{findFiles}?processSeparately=false", + required: true, + }, + }, +} as ActionMetadata); + +describe("getActionInputDescriptor", () => { + test("returns an input descriptor by its name", () => { + for (const name of Object.keys(METADATA.inputs)) { + const descriptor = getActionInputDescriptor(METADATA, name); + + expect(descriptor).toEqual({ + name, + path: [name], + type: parseActionParameterTypeDescriptor(METADATA.inputs[name].type), + description: METADATA.inputs[name].description ?? "", + redirect: METADATA.inputs[name].redirect, + required: !!METADATA.inputs[name].required, + default: METADATA.inputs[name].default, + deprecationMessage: METADATA.inputs[name].deprecationMessage, + }); + } + }); + + test("uses custom path parser", () => { + const pathParser: ActionParameterPathParser = name => name.split("-"); + + for (const name of Object.keys(METADATA.inputs)) { + const descriptor = getActionInputDescriptor(METADATA, name, { pathParser }); + + expect(descriptor).toEqual({ + name, + path: pathParser(name), + type: parseActionParameterTypeDescriptor(METADATA.inputs[name].type), + description: METADATA.inputs[name].description ?? "", + redirect: METADATA.inputs[name].redirect, + required: !!METADATA.inputs[name].required, + default: METADATA.inputs[name].default, + deprecationMessage: METADATA.inputs[name].deprecationMessage, + }); + } + }); + + test("returns undefined if an input with the specified name doesn't exist", () => { + const descriptor = getActionInputDescriptor(METADATA, "there's no such input"); + + expect(descriptor).toBeUndefined(); + }); +}); + +describe("getActionInputDescriptors", () => { + test("returns all input descriptors", () => { + const names = Object.keys(METADATA.inputs); + + const descriptors = getActionInputDescriptors(METADATA); + + expect(descriptors).toHaveLength(names.length); + for (const name of names) { + expect(descriptors).toContainEqual({ + name, + path: [name], + type: parseActionParameterTypeDescriptor(METADATA.inputs[name].type), + description: METADATA.inputs[name].description ?? "", + redirect: METADATA.inputs[name].redirect, + required: !!METADATA.inputs[name].required, + default: METADATA.inputs[name].default, + deprecationMessage: METADATA.inputs[name].deprecationMessage, + }); + } + }); + + test("uses custom path parser", () => { + const names = Object.keys(METADATA.inputs); + const pathParser: ActionParameterPathParser = name => name.split("-"); + + const descriptors = getActionInputDescriptors(METADATA, { pathParser }); + + expect(descriptors).toHaveLength(names.length); + for (const name of names) { + expect(descriptors).toContainEqual({ + name, + path: pathParser(name), + type: parseActionParameterTypeDescriptor(METADATA.inputs[name].type), + description: METADATA.inputs[name].description ?? "", + redirect: METADATA.inputs[name].redirect, + required: !!METADATA.inputs[name].required, + default: METADATA.inputs[name].default, + deprecationMessage: METADATA.inputs[name].deprecationMessage, + }); + } + }); + + test("returns an empty array if the metadata doesn't specify any inputs", () => { + const descriptors = getActionInputDescriptors({} as ActionMetadata); + + expect(descriptors).toEqual([]); + }); +});