Address review: naming, arg validation, test coverage, help count

This commit is contained in:
Neoreo 2026-07-30 16:41:54 +02:00
parent 47b9ffb393
commit a65044004b
2 changed files with 30 additions and 8 deletions

View File

@ -5,12 +5,11 @@
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import cptable from "codepage";
import {toBase64, fromBase64} from "../lib/Base64.mjs";
// PowerShell -EncodedCommand uses UTF-16LE (code page 1200)
const UTF16LE = 1200;
const UTF_16LE = 1200;
/**
* PowerShell -e Encode/Decode operation
@ -56,14 +55,11 @@ class PowerShellEncodeDecode extends Operation {
const [mode] = args;
if (mode === "Encode") {
const encoded = cptable.utils.encode(UTF16LE, input);
const encoded = cptable.utils.encode(UTF_16LE, input);
return toBase64(new Uint8Array(encoded).buffer);
} else if (mode === "Decode") {
const bytes = fromBase64(input, "A-Za-z0-9+/=", "byteArray");
return cptable.utils.decode(UTF16LE, new Uint8Array(bytes));
} else {
throw new OperationError("Invalid mode");
}
const bytes = fromBase64(input, "A-Za-z0-9+/=", "byteArray");
return cptable.utils.decode(UTF_16LE, new Uint8Array(bytes));
}
}

View File

@ -57,4 +57,30 @@ TestRegister.addTests([
},
],
},
{
name: "PowerShell -e Encode/Decode: round trip with non-ASCII characters",
input: "Write-Host \"café ☕\"",
expectedOutput: "Write-Host \"café ☕\"",
recipeConfig: [
{
op: "PowerShell -e Encode/Decode",
args: ["Encode"],
},
{
op: "PowerShell -e Encode/Decode",
args: ["Decode"],
},
],
},
{
name: "PowerShell -e Encode/Decode: decode ignores characters outside the Base64 alphabet",
input: "!!!@@@###",
expectedOutput: "",
recipeConfig: [
{
op: "PowerShell -e Encode/Decode",
args: ["Decode"],
},
],
},
]);