Files
dxdesigner-part-replacement/integration/dxdesigner795/PartReplacementContract.ps1

153 lines
5.9 KiB
PowerShell

function Assert-ExactObject {
param([object]$Value, [string[]]$Fields, [string]$Context)
if ($null -eq $Value -or
$Value -isnot [System.Management.Automation.PSCustomObject]) {
throw "$Context must be an object"
}
$actual = @($Value.PSObject.Properties.Name)
if ($actual.Count -ne $Fields.Count) {
throw "$Context fields are invalid"
}
foreach ($field in $Fields) {
if (-not ($actual -ccontains $field)) {
throw "$Context is missing $field"
}
}
}
function Assert-OptionalString {
param([object]$Value, [string]$Name)
if ($null -ne $Value -and
($Value -isnot [string] -or [string]::IsNullOrWhiteSpace($Value))) {
throw "$Name must be null or a nonempty string"
}
}
function Assert-OperatorStepResult {
param([object]$Step, [string]$ExpectedJobId)
Assert-ExactObject $Step @(
"bridge_state", "command", "complete", "consumed_action",
"instruction", "manual_route", "project", "state"
) "operator-step result"
if ($Step.complete -isnot [bool]) {
throw "operator-step complete must be Boolean"
}
foreach ($name in @(
"bridge_state", "consumed_action", "instruction", "manual_route"
)) {
Assert-OptionalString $Step.$name "operator-step $name"
}
if ($Step.complete) {
if ($null -ne $Step.command -or $null -ne $Step.project -or
$null -ne $Step.state) {
throw "completed operator-step must not expose a current command"
}
return
}
Assert-ExactObject $Step.project @(
"display_name", "project_id", "project_path"
) "operator-step project"
foreach ($name in @("display_name", "project_id", "project_path")) {
if ($Step.project.$name -isnot [string] -or
[string]::IsNullOrWhiteSpace($Step.project.$name)) {
throw "operator-step project $name is invalid"
}
}
if ($Step.state -isnot [string] -or
[string]::IsNullOrWhiteSpace($Step.state)) {
throw "operator-step state is invalid"
}
Assert-ExactObject $Step.command @(
"action", "command_id", "expected_count", "expected_fingerprint",
"expected_logical_keys", "format_version", "job_id",
"new_part_number", "old_part_number", "project_id", "scope"
) "operator-step command"
foreach ($name in @(
"action", "command_id", "expected_fingerprint", "job_id",
"new_part_number", "old_part_number", "project_id", "scope"
)) {
if ($Step.command.$name -isnot [string]) {
throw "operator-step command $name is invalid"
}
}
if ($Step.command.action -notin @(
"close_project", "close_without_save", "erc", "inventory",
"netlist", "open_project", "packager", "replace_part", "save",
"undo"
)) {
throw "operator-step command action is unsupported"
}
if ($Step.command.command_id -notmatch '^[0-9a-f]{32}$') {
throw "operator-step command_id is invalid"
}
if ($Step.command.format_version -ne 1 -or
$Step.command.format_version -isnot [int]) {
throw "operator-step command format_version is invalid"
}
if ($Step.command.expected_count -isnot [int] -or
$Step.command.expected_count -lt 0) {
throw "operator-step expected_count is invalid"
}
$expectedKeys = @($Step.command.expected_logical_keys)
if ($expectedKeys.Count -ne $Step.command.expected_count -or
@($expectedKeys | Where-Object { $_ -isnot [string] }).Count -ne 0) {
throw "operator-step expected logical keys are invalid"
}
if ($Step.command.scope -cne "full_project" -or
$Step.command.job_id -cne $ExpectedJobId -or
$Step.command.project_id -cne $Step.project.project_id) {
throw "operator-step command identity is invalid"
}
}
function Assert-ResumeStatus {
param([object]$Status)
Assert-ExactObject $Status @("job_id", "projects") "status result"
if ($Status.job_id -isnot [string] -or
[string]::IsNullOrWhiteSpace($Status.job_id)) {
throw "status result job_id is invalid"
}
if ($Status.projects -isnot [object[]] -or
$Status.projects.Count -eq 0) {
throw "status result projects must be a nonempty array"
}
$allowedActions = @(
"await_inventory", "complete", "confirm-erc", "confirm-netlist",
"confirm-packager", "confirm-saved", "confirm-undo", "ingest",
"prepare", "prepare-close", "prepare-erc", "prepare-netlist",
"prepare-open", "prepare-packager", "prepare-save", "prepare-undo",
"reconcile"
)
$allowedStates = @(
"apply_pending", "applying", "awaiting_external_checks",
"awaiting_save_confirmation", "blocked", "inventory_complete",
"inventory_pending", "verified_saved"
)
foreach ($project in $Status.projects) {
Assert-ExactObject $project @(
"action", "display_name", "project_id", "project_path", "state"
) "status project"
foreach ($name in @(
"action", "display_name", "project_id", "project_path", "state"
)) {
if ($project.$name -isnot [string] -or
[string]::IsNullOrWhiteSpace($project.$name)) {
throw "status project $name is invalid"
}
}
if ($project.action -cnotin $allowedActions) {
throw "status project action is invalid"
}
if ($project.state -cnotin $allowedStates) {
throw "status project state is invalid"
}
if (-not [IO.Path]::IsPathRooted([string]$project.project_path)) {
throw "status project path must be absolute"
}
if (($project.state -ceq "verified_saved") -ne
($project.action -ceq "complete")) {
throw "status project completion is inconsistent"
}
}
}