feat: Implement robust option-type ingredient validation and resolve test suite failures
This commit is contained in:
parent
3c32e3f64d
commit
1cc1d7808c
@ -79,6 +79,9 @@ class Ingredient {
|
||||
if (this.type === "toggleString" && val && typeof val === "object" && "string" in val) {
|
||||
checkVal = val.string;
|
||||
}
|
||||
if (this.type === "option" && Array.isArray(checkVal)) {
|
||||
checkVal = checkVal[this.defaultIndex ?? 0];
|
||||
}
|
||||
|
||||
// 1. check if empty
|
||||
let isEmpty = false;
|
||||
@ -124,6 +127,21 @@ class Ingredient {
|
||||
}
|
||||
}
|
||||
|
||||
// 4. option checks
|
||||
if (this.type === "option") {
|
||||
if (Array.isArray(this.defaultValue)) {
|
||||
const permittedOptions = this.defaultValue.filter(opt => {
|
||||
if (typeof opt !== "string") return false;
|
||||
return !opt.match(/^\[\/?[a-z0-9 -()^]+\]$/i);
|
||||
});
|
||||
const valStr = (checkVal !== null && checkVal !== undefined) ? String(checkVal).toLowerCase() : "";
|
||||
const matchedOption = permittedOptions.find(opt => opt.toLowerCase() === valStr);
|
||||
if (!matchedOption) {
|
||||
throw new OperationError(`${this.name} must be one of the following: ${permittedOptions.join(", ")}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -60,6 +60,12 @@ class AutomatedValidationTestOp extends Operation {
|
||||
},
|
||||
"toggleValues": ["Option A", "Option B"],
|
||||
"allowEmpty": false
|
||||
},
|
||||
{
|
||||
"name": "Option Ingredient",
|
||||
"type": "option",
|
||||
"value": ["[Group 1]", "Option 1", "Option 2", "[/Group 1]", "[Group 2]", "Option 3", "[/Group 2]"],
|
||||
"allowEmpty": false
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
@ -43,7 +43,7 @@ class SM4Encrypt extends Operation {
|
||||
{
|
||||
"name": "Mode",
|
||||
"type": "option",
|
||||
"value": ["CBC", "CFB", "OFB", "CTR", "ECB"]
|
||||
"value": ["CBC", "CFB", "OFB", "CTR", "ECB", "CBC/NoPadding", "ECB/NoPadding"]
|
||||
},
|
||||
{
|
||||
"name": "Input",
|
||||
|
||||
@ -74,7 +74,7 @@ function transformArgs(opArgsList, newArgs) {
|
||||
return opArgs.map((arg) => {
|
||||
if (arg.type === "option") {
|
||||
// pick default option if not already chosen
|
||||
return typeof arg.value === "string" ? arg.value : arg.value[arg.defaultIndex ?? 0];
|
||||
return !Array.isArray(arg.value) ? arg.value : arg.value[arg.defaultIndex ?? 0];
|
||||
}
|
||||
|
||||
if (arg.type === "editableOption") {
|
||||
|
||||
@ -15,7 +15,7 @@ TestRegister.addTests([
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }]
|
||||
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -26,7 +26,7 @@ TestRegister.addTests([
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [4, 1.5, "hello", "", { "option": "Option A", "string": "test" }]
|
||||
args: [4, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -37,7 +37,7 @@ TestRegister.addTests([
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [11, 1.5, "hello", "", { "option": "Option A", "string": "test" }]
|
||||
args: [11, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -48,7 +48,7 @@ TestRegister.addTests([
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5.5, 1.5, "hello", "", { "option": "Option A", "string": "test" }]
|
||||
args: [5.5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -59,7 +59,7 @@ TestRegister.addTests([
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5, 1.4, "hello", "", { "option": "Option A", "string": "test" }]
|
||||
args: [5, 1.4, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -70,7 +70,7 @@ TestRegister.addTests([
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5, 5.6, "hello", "", { "option": "Option A", "string": "test" }]
|
||||
args: [5, 5.6, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -81,7 +81,7 @@ TestRegister.addTests([
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5, 1.5, "helloooo", "", { "option": "Option A", "string": "test" }]
|
||||
args: [5, 1.5, "helloooo", "", { "option": "Option A", "string": "test" }, "Option 1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -92,7 +92,7 @@ TestRegister.addTests([
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5, 1.5, "", "", { "option": "Option A", "string": "test" }]
|
||||
args: [5, 1.5, "", "", { "option": "Option A", "string": "test" }, "Option 1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -103,7 +103,7 @@ TestRegister.addTests([
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }]
|
||||
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -114,7 +114,40 @@ TestRegister.addTests([
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "" }]
|
||||
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "" }, "Option 1"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Automated Validation: Invalid Option value",
|
||||
input: "test",
|
||||
expectedOutput: "Option Ingredient must be one of the following: Option 1, Option 2, Option 3",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "Option 4"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Automated Validation: Option value as optgroup heading (invalid)",
|
||||
input: "test",
|
||||
expectedOutput: "Option Ingredient must be one of the following: Option 1, Option 2, Option 3",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, "[Group 1]"]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "Automated Validation: Option value empty (invalid)",
|
||||
input: "test",
|
||||
expectedOutput: "Option Ingredient cannot be empty.",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "Automated Validation Test Op",
|
||||
args: [5, 1.5, "hello", "", { "option": "Option A", "string": "test" }, ""]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@ -67,12 +67,12 @@ TestRegister.addTests([
|
||||
{
|
||||
name: "Generate Lorem Ipsum: Incorrect lengthType",
|
||||
input: "",
|
||||
expectedOutput: "Invalid length type",
|
||||
expectedOutput: "Length in must be one of the following: Paragraphs, Sentences, Words, Bytes",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "Generate Lorem Ipsum",
|
||||
"args": [999_999, "Novels"]
|
||||
},
|
||||
}
|
||||
],
|
||||
},
|
||||
|
||||
|
||||
@ -113,7 +113,7 @@ TestRegister.addTests([
|
||||
},
|
||||
{
|
||||
"op": "SM2 Decrypt",
|
||||
"args": [PRIVATE_K, "C1C2C2", CURVE]
|
||||
"args": [PRIVATE_K, "C1C2C3", CURVE]
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user