import { asRuntimeInputs, asResourceInputs } from "../InputsConverters"; import { PackageReference as StepPackageReference, SensitiveValue as StepSensitiveValue, Discriminator, ContainerImageReference, } from "@octopusdeploy/step-inputs"; import { ObjectResourceInputs } from "../../ResourceInputs"; import { ObjectRuntimeInputs } from "../../RuntimeInputs"; import { convertFromJsonSchemaToInputSchema, Definition } from "../../Schema"; describe("converting between RuntimeInputs and ResourceInputs", () => { describe("Sensitive values", () => { type StepInputs = { sensitiveProperty: StepSensitiveValue; }; const schema: RootObjectProperties = { sensitiveProperty: { type: "object", isSensitiveValue: true } }; test("existing value", () => { const resourceInputs: ObjectResourceInputs = { sensitiveProperty: { HasValue: true } }; const runtimeInputs: ObjectRuntimeInputs = { sensitiveProperty: { type: "existing" } }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); test("new value", () => { const resourceInputs: ObjectResourceInputs = { sensitiveProperty: { HasValue: true, NewValue: "TheNewValue" } }; const runtimeInputs: ObjectRuntimeInputs = { sensitiveProperty: { type: "new", newValue: "TheNewValue" } }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); test("empty value", () => { const resourceInputs: ObjectResourceInputs = { sensitiveProperty: { HasValue: false } }; const runtimeInputs: ObjectRuntimeInputs = { sensitiveProperty: { type: "empty" } }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); test("bound value", () => { const boundExpression = "#{BoundValue}"; const resourceInputs: ObjectResourceInputs = { sensitiveProperty: boundExpression }; const runtimeInputs: ObjectRuntimeInputs = { sensitiveProperty: { type: "bound", expression: boundExpression } }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); }); describe("Package References", () => { test("remain untouched", () => { type StepInputs = { package: StepPackageReference; }; const schema: RootObjectProperties = { package: { type: "object", isPackageReference: true } }; //The step ui has a black box for PackageReferences, so in this case so should remain mostly untouched as part of the conversion const resourceInputs: ObjectResourceInputs = { package: { packageId: "ThePackage", feedId: "TheFeed", referenceId: "uniqueIdentifier" }, }; const runtimeInputs: ObjectRuntimeInputs = { package: { packageId: "ThePackage", feedId: "TheFeed", referenceId: "uniqueIdentifier" }, }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); }); describe("Container Image References", () => { test("remain untouched", () => { type StepInputs = { containerImage: ContainerImageReference; }; const schema: RootObjectProperties = { containerImage: { type: "object", isContainerImageReference: true } }; //The step ui has a black box for ContainerImageReference, so in this case so should remain mostly untouched as part of the conversion const resourceInputs: ObjectResourceInputs = { containerImage: { imageName: "ThePackage", feedId: "TheFeed", referenceId: "uniqueIdentifier" }, }; const runtimeInputs: ObjectRuntimeInputs = { containerImage: { imageName: "ThePackage", feedId: "TheFeed", referenceId: "uniqueIdentifier" }, }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); }); describe("String values", () => { describe("remain untouched", () => { type StepInputs = { message: string; }; const schema: RootObjectProperties = { message: { type: "string" } }; test("when not bound", () => { const resourceInputs: ObjectResourceInputs = { message: "The Value" }; const runtimeInputs: ObjectRuntimeInputs = { message: "The Value" }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); test("when bound", () => { const resourceInputs: ObjectResourceInputs = { message: "#{Bound Value}" }; const runtimeInputs: ObjectRuntimeInputs = { message: "#{Bound Value}" }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); }); describe("Number values", () => { type StepInputs = { numberValue: number; }; const schema: RootObjectProperties = { numberValue: { type: "number" } }; test("remain untouched when not bound", () => { const resourceInputs: ObjectResourceInputs = { numberValue: 42 }; const runtimeInputs: ObjectRuntimeInputs = { numberValue: 42 }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); test("will convert between bound value and string as needed", () => { const resourceInputs: ObjectResourceInputs = { numberValue: "#{BoundValue}" }; const runtimeInputs: ObjectRuntimeInputs = { numberValue: { type: "bound", expression: "#{BoundValue}" } }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); }); describe("BigInt values", () => { type StepInputs = { numberValue: bigint; }; const schema: RootObjectProperties = { numberValue: { type: "number" } }; test("remain untouched when not bound", () => { const resourceInputs: ObjectResourceInputs = { numberValue: BigInt(42) }; const runtimeInputs: ObjectRuntimeInputs = { numberValue: BigInt(42) }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); test("will convert between bound value and string as needed", () => { const resourceInputs: ObjectResourceInputs = { numberValue: "#{BoundValue}" }; const runtimeInputs: ObjectRuntimeInputs = { numberValue: { type: "bound", expression: "#{BoundValue}" } }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); }); describe("Boolean values", () => { type StepInputs = { booleanValue: boolean; }; const schema: RootObjectProperties = { booleanValue: { type: "boolean" } }; test("remain untouched when not bound", () => { const resourceInputs: ObjectResourceInputs = { booleanValue: true }; const runtimeInputs: ObjectRuntimeInputs = { booleanValue: true }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); test("will convert between bound value and string as needed", () => { const resourceInputs: ObjectResourceInputs = { booleanValue: "#{BoundValue}" }; const runtimeInputs: ObjectRuntimeInputs = { booleanValue: { type: "bound", expression: "#{BoundValue}" } }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); }); }); describe("Nested objects", () => { type StepInputs = { nested: { theValue: string }; }; const schema: RootObjectProperties = { nested: { type: "object", properties: { theValue: { type: "string" } }, required: ["theValue"] } }; test("maps primitives", () => { const resourceInputs: ObjectResourceInputs = { nested: { theValue: "Hello World" } }; const runtimeInputs: ObjectRuntimeInputs = { nested: { theValue: "Hello World" } }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); }); describe("object union", () => { type Square = { type: Discriminator<"square">; size: number }; type Rectangle = { type: Discriminator<"rectangle">; width: number; height: number }; type PossibleShapes = Square | Rectangle; type StepInputs = { shape: PossibleShapes; }; const schema: RootObjectProperties = { shape: { anyOf: [ { type: "object", properties: { type: { type: "string", const: "square", isDiscriminator: true }, size: { type: "number" } }, required: ["type", "size"], }, { type: "object", properties: { type: { type: "string", const: "rectangle", isDiscriminator: true }, width: { type: "number" }, height: { type: "number" }, }, required: ["type", "width", "height"], }, ], }, }; test("maps to first object in union", () => { const runtimeInputs: ObjectRuntimeInputs = { shape: { type: "square", size: 5 } }; const resourceInputs: ObjectResourceInputs = { shape: { type: "square", size: 5 } }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); test("maps to second object in union", () => { const runtimeInputs: ObjectRuntimeInputs = { shape: { type: "rectangle", width: 5, height: 10 } }; const resourceInputs: ObjectResourceInputs = { shape: { type: "rectangle", width: 5, height: 10 } }; assertValuesRoundTrip(schema, runtimeInputs, resourceInputs); }); }); }); function assertValuesRoundTrip( inputProperties: RootObjectProperties, runtimeInputs: ObjectRuntimeInputs, resourceInputs: ObjectResourceInputs ) { const schemaFromResourceInputs = convertFromJsonSchemaToInputSchema( { definitions: { RootInputs: { type: "object", isRoot: true, properties: inputProperties, required: [] } } }, resourceInputs ); expect(asRuntimeInputs(schemaFromResourceInputs.properties, resourceInputs)).toStrictEqual(runtimeInputs); const schemaFromRuntimeInputs = convertFromJsonSchemaToInputSchema( { definitions: { RootInputs: { type: "object", isRoot: true, properties: inputProperties, required: [] } } }, runtimeInputs ); expect(asResourceInputs(schemaFromRuntimeInputs.properties, runtimeInputs)).toStrictEqual(resourceInputs); } type RootObjectProperties = { [key: string]: Definition };