<# .SYNOPSIS Automated checks for the C64/C128 Dev Foundry. .DESCRIPTION Groups (all run by default): validation - example and test briefs, conflicts, drafts and resume, override rules generation - every valid brief through tools/New-FoundryProject.ps1 into a folder whose path contains spaces; generated files, template completeness, project.json, Git (branch main, commit, clean tree), no-identity behavior safety - existing destinations (non-empty, Git repo, file, empty), destinations inside the Foundry, invalid names, interrupted generation and retry, stale staging toolchain - for each generated project: tools/check-prereqs.ps1, tools/build.ps1, tools/smoke.ps1 (VICE headless); on-Commodore round trip; simulated missing tool. Skipped with a reason when cc65/VICE are not installed. Results are printed and written to tests/results/latest.md (commands and outcomes). Exit code 0 = no failures. .EXAMPLE pwsh -File tests/Run-FoundryTests.ps1 pwsh -File tests/Run-FoundryTests.ps1 -Group validation,safety pwsh -File tests/Run-FoundryTests.ps1 -KeepOutput #> [CmdletBinding()] param( [string[]]$Group = @('validation', 'generation', 'safety', 'toolchain'), [string]$OutputRoot, [switch]$KeepOutput ) $ErrorActionPreference = 'Stop' $Group = @($Group | ForEach-Object { $_ -split ',' } | ForEach-Object { $_.Trim() } | Where-Object { $_ }) foreach ($g in $Group) { if ($g -notin 'validation', 'generation', 'safety', 'toolchain') { throw "Unknown group '$g'. Use validation, generation, safety, toolchain." } } $root = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path Import-Module (Join-Path $root 'tools/Foundry.psm1') -Force $gen = Join-Path $root 'tools/New-FoundryProject.ps1' $val = Join-Path $root 'tools/Test-FoundryBrief.ps1' if (-not $OutputRoot) { $OutputRoot = Join-Path ([IO.Path]::GetTempPath()) ("Foundry Tests\run " + (Get-Date -Format 'yyyyMMdd-HHmmss')) } $projects = Join-Path $OutputRoot 'My C64 Projects' # deliberately contains spaces New-Item -ItemType Directory -Force -Path $projects | Out-Null $script:Results = [System.Collections.Generic.List[object]]::new() $script:Log = [System.Collections.Generic.List[string]]::new() function Invoke-Check([string]$Name, [scriptblock]$Body) { $sw = [Diagnostics.Stopwatch]::StartNew() try { $detail = & $Body $status = if ($detail -is [string] -and $detail.StartsWith('SKIP:')) { 'SKIP' } else { 'PASS' } $script:Results.Add([pscustomobject]@{ Name = $Name; Status = $status; Detail = "$detail"; Seconds = [math]::Round($sw.Elapsed.TotalSeconds, 1) }) } catch { $script:Results.Add([pscustomobject]@{ Name = $Name; Status = 'FAIL'; Detail = $_.Exception.Message; Seconds = [math]::Round($sw.Elapsed.TotalSeconds, 1) }) } $r = $script:Results[-1] $color = @{ PASS = 'Green'; FAIL = 'Red'; SKIP = 'Yellow' }[$r.Status] Write-Host ("[{0}] {1}{2}" -f $r.Status, $Name, $(if ($r.Detail) { " - $($r.Detail)" } else { '' })) -ForegroundColor $color } function Assert([bool]$Condition, [string]$Message) { if (-not $Condition) { throw $Message } } function Invoke-Pwsh([string]$Script, [string[]]$Arguments, [hashtable]$Env = @{}) { $saved = @{} foreach ($k in $Env.Keys) { $saved[$k] = [Environment]::GetEnvironmentVariable($k); [Environment]::SetEnvironmentVariable($k, $Env[$k]) } try { $out = & pwsh -NoProfile -File $Script @Arguments 2>&1 | ForEach-Object { "$_" } $code = $LASTEXITCODE } finally { foreach ($k in $saved.Keys) { [Environment]::SetEnvironmentVariable($k, $saved[$k]) } } $quoted = ($Arguments | ForEach-Object { if ($_ -match '\s') { "`"$_`"" } else { $_ } }) -join ' ' $rel = $Script.Replace($root + [IO.Path]::DirectorySeparatorChar, '') $script:Log.Add("pwsh -File $rel $quoted -> exit $code") [pscustomobject]@{ Code = $code; Output = @($out); Text = ($out -join "`n") } } function Get-Hash([string]$Dir) { (Get-ChildItem -LiteralPath $Dir -Recurse -File -Force | Sort-Object FullName | ForEach-Object { "$($_.FullName.Substring($Dir.Length))|$((Get-FileHash -LiteralPath $_.FullName).Hash)" }) -join "`n" } $examples = @( @{ Name = 'traffic-control-64'; Brief = Join-Path $root 'examples/traffic-control-64/brief.json'; Expect = 0 }, @{ Name = 'c128-text-viewer'; Brief = Join-Path $root 'examples/c128-text-viewer/brief.json'; Expect = 0 } ) $valid = @( @{ Name = 'c128-40-basic7'; Expect = 0 }, @{ Name = 'dual-native'; Expect = 0 }, @{ Name = 'dual-c64mode'; Expect = 0 }, @{ Name = 'on-commodore-basic'; Expect = 0 }, @{ Name = 'override-asm'; Expect = 0 }, @{ Name = 'unverified-onboard-asm'; Expect = 3 }, @{ Name = 'multi-display-c'; Expect = 0 }, @{ Name = 'c128-40-asm'; Expect = 0 }, @{ Name = 'dual-c64mode-c'; Expect = 0 }, @{ Name = 'c128-80-basic7'; Expect = 0 }, @{ Name = 'on-commodore-basic7'; Expect = 0 }, @{ Name = 'mouse-1351-c'; Expect = 0 } ) | ForEach-Object { $_.Brief = Join-Path $root "tests/briefs/$($_.Name).json"; $_ } $conflicts = 'conflict-basic7-on-c64', 'conflict-80col-on-c64', 'conflict-override-capability', 'conflict-build-location', 'conflict-basic-mouse' $all = @($examples) + @($valid) # --------------------------------------------------------------------------- if ('validation' -in $Group) { foreach ($b in $all) { Invoke-Check "validation: $($b.Name) is a valid approved brief" { $r = Invoke-Pwsh $val @($b.Brief, '-Mode', 'Approved') Assert ($r.Code -eq 0) "exit $($r.Code): $($r.Text)" } } foreach ($c in $conflicts) { Invoke-Check "validation: $c is reported as a conflict and refused" { $path = Join-Path $root "tests/briefs/$c.json" $r = Invoke-Pwsh $val @($path, '-Mode', 'Approved') Assert ($r.Code -eq 4) "expected exit 4 (conflict), got $($r.Code)" Assert ($r.Text -match 'CONFLICT') 'no CONFLICT line' Assert ($r.Text -match 'Ask the user which constraint to change') 'conflict does not ask the user which constraint to change' $g = Invoke-Pwsh $gen @($path, '-Destination', (Join-Path $projects "should-not-exist $c"), '-SkipVerify') Assert ($g.Code -eq 1) 'generator did not refuse' Assert (-not (Test-Path -LiteralPath (Join-Path $projects "should-not-exist $c"))) 'destination was created' ($r.Output | Where-Object { $_ -match 'CONFLICT' } | Select-Object -First 1).Trim().Substring(0, 90) + '...' } } Invoke-Check 'validation: user override conflict names the override and the recommended language' { $r = Invoke-Pwsh $val @((Join-Path $root 'tests/briefs/conflict-override-capability.json')) Assert ($r.Text -match "user override") 'override not mentioned' Assert ($r.Text -match "recommended 'c'") 'recommended language not offered' } Invoke-Check 'validation: language different from recommendation without an override is an error' { $b = Read-FoundryJson (Join-Path $root 'tests/briefs/override-asm.json') $b.toolchain.override = $null $res = Test-FoundryBrief -Brief $b -Mode Approved Assert (-not $res.Valid) 'accepted' Assert (@($res.Errors | Where-Object { $_.Message -match 'no override is recorded' }).Count -eq 1) 'missing error' } Invoke-Check 'validation: override requested but not applied is an error' { $b = Read-FoundryJson (Join-Path $root 'tests/briefs/override-asm.json') $b.toolchain.language = 'c' $res = Test-FoundryBrief -Brief $b -Mode Approved Assert (@($res.Errors | Where-Object { $_.Message -match 'takes precedence' }).Count -eq 1) 'missing precedence error' } Invoke-Check 'validation: mouse-1351 adds VICE 1351 mouse emulation on control port 1' { $r = Resolve-FoundryProject -Brief (Read-FoundryJson (Join-Path $root 'tests/briefs/mouse-1351-c.json')) $emuArgs = @($r.targets[0].emulator.args) -join ' ' Assert ($emuArgs -match '-controlport1device 3') "emulator args: $emuArgs" Assert (@($r.targets[0].emulator.notes).Count -eq 1) 'mouse emulator note missing' $r2 = Resolve-FoundryProject -Brief (Read-FoundryJson (Join-Path $root 'tests/briefs/dual-c64mode-c.json')) Assert (-not ((@($r2.targets[0].emulator.args) -join ' ') -match 'controlport')) 'mouse args added without the capability' $emuArgs } Invoke-Check 'validation: mouse-1351 brings the Foundry 1351 reader (not cc65''s driver) for C on the C64 only' { $r = Resolve-FoundryProject -Brief (Read-FoundryJson (Join-Path $root 'tests/briefs/mouse-1351-c.json')) $cf = @($r.capabilityFiles) Assert ($cf.Count -eq 1 -and $cf[0].dir -eq 'capabilities/mouse-1351') "capability files: $($cf | ConvertTo-Json -Compress)" Assert (Test-Path -LiteralPath (Join-Path $root 'templates/capabilities/mouse-1351/src/mouse1351.s')) 'reader template missing' $r2 = Resolve-FoundryProject -Brief (Read-FoundryJson (Join-Path $root 'tests/briefs/dual-c64mode-c.json')) Assert (@($r2.capabilityFiles).Count -eq 0) 'reader added without the capability' $b = Read-FoundryJson (Join-Path $root 'tests/briefs/c128-40-asm.json') $b.capabilities = @(@($b.capabilities) + 'mouse-1351') $r3 = Resolve-FoundryProject -Brief $b Assert (@($r3.capabilityFiles).Count -eq 1 -and -not $r3.capabilityFiles[0].dir) 'reader copied for an assembly C128 project' $cf[0].dir } Invoke-Check 'validation: dual-target strategy must match the targets' { $b = Read-FoundryJson (Join-Path $root 'tests/briefs/dual-native.json') $b.dualTarget.strategy = 'c64-mode-on-both' $res = Test-FoundryBrief -Brief $b -Mode Approved Assert ($res.Conflicts.Count -ge 1) 'no conflict for native target under c64-mode-on-both' $b.dualTarget = $null $res = Test-FoundryBrief -Brief $b -Mode Approved Assert (@($res.Missing | Where-Object { $_.Path -eq 'dualTarget.strategy' }).Count -eq 1) 'missing strategy not reported' } Invoke-Check 'validation: unlinked requirement and unknown requirement reference are errors' { $b = Read-FoundryJson (Join-Path $root 'tests/briefs/c128-40-basic7.json') $b.milestones[0].tasks[0].requirements = @('R9') $res = Test-FoundryBrief -Brief $b -Mode Approved Assert (@($res.Errors | Where-Object { $_.Message -match "unknown requirement 'R9'" }).Count -eq 1) 'unknown ref not reported' Assert (@($res.Errors | Where-Object { $_.Message -match 'R2 is not linked' }).Count -eq 1) 'unlinked requirement not reported' } Invoke-Check 'validation: unapproved brief is refused by the generator' { $tmp = Join-Path $OutputRoot 'unapproved.json' $b = Read-FoundryJson (Join-Path $root 'tests/briefs/c128-40-basic7.json') $b.status = 'draft' [IO.File]::WriteAllText($tmp, ($b | ConvertTo-Json -Depth 32)) $g = Invoke-Pwsh $gen @($tmp, '-Destination', (Join-Path $projects 'unapproved'), '-SkipVerify') Assert ($g.Code -eq 1 -and $g.Text -match "status must be 'approved'") "exit $($g.Code)" } Invoke-Check 'draft: paused draft reports the stage and questions to resume with' { $r = Invoke-Pwsh $val @((Join-Path $root 'tests/briefs/draft-paused.json')) Assert ($r.Code -eq 0) "draft mode should accept an incomplete draft (exit $($r.Code))" Assert ($r.Text -match 'Resume interview at stage: first-release') 'wrong resume stage' Assert ($r.Text -match 'Should the first release include sound\?') 'saved open question not repeated' $a = Invoke-Pwsh $val @((Join-Path $root 'tests/briefs/draft-paused.json'), '-Mode', 'Approved') Assert ($a.Code -eq 1) 'incomplete draft passed approved-mode validation' 'resume stage: first-release' } Invoke-Check 'draft: New-FoundryDraft creates a draft that resumes at the first stage with gaps' { $slug = 'zz-test-draft-' + [Guid]::NewGuid().ToString('N').Substring(0, 6) $r = Invoke-Pwsh (Join-Path $root 'tools/New-FoundryDraft.ps1') @('-Name', 'Test Draft', '-Idea', 'A tiny test idea.', '-Slug', $slug) $path = Join-Path $root "drafts/$slug.json" try { Assert (Test-Path -LiteralPath $path) 'draft file not created' Assert ($r.Text -match 'Resume interview at stage: idea') "unexpected stage: $($r.Text)" $again = Invoke-Pwsh (Join-Path $root 'tools/New-FoundryDraft.ps1') @('-Name', 'Test Draft', '-Idea', 'x', '-Slug', $slug) Assert ($again.Code -eq 1) 'existing draft was overwritten' } finally { Remove-Item -LiteralPath $path -ErrorAction SilentlyContinue } } Invoke-Check 'schema: every example, test brief and the draft template match schema/project-brief.schema.json' { $schema = Join-Path $root 'schema/project-brief.schema.json' $files = @(Get-ChildItem (Join-Path $root 'examples') -Recurse -Filter brief.json) + @(Get-ChildItem (Join-Path $root 'tests/briefs') -Filter *.json) + @(Get-Item (Join-Path $root 'templates/brief/draft.json')) foreach ($f in $files) { Assert (Test-Json -Path $f.FullName -SchemaFile $schema -ErrorAction SilentlyContinue) "$($f.Name) does not match the schema" } $bad = Join-Path $OutputRoot 'bad-schema.json' $b = Read-FoundryJson (Join-Path $root 'tests/briefs/c128-40-basic7.json'); $b.targets[0].platform = 'vic20' [IO.File]::WriteAllText($bad, ($b | ConvertTo-Json -Depth 32)) $r = Invoke-Pwsh $val @($bad) Assert ($r.Code -ne 0 -and $r.Text -match 'SCHEMA') 'schema violation not reported' "$($files.Count) files conform; invalid platform rejected" } Invoke-Check 'templates: unknown placeholder is rejected' { $threw = $false try { Expand-FoundryTemplate 'Hello {{NOT_A_VAR}}' @{} 'unit' | Out-Null } catch { $threw = $_.Exception.Message -match 'NOT_A_VAR' } Assert $threw 'no error for unknown placeholder' } } # --------------------------------------------------------------------------- $generated = @{} if ('generation' -in $Group -or 'toolchain' -in $Group) { foreach ($b in $all) { Invoke-Check "generation: $($b.Name) through tools/New-FoundryProject.ps1" { $dest = Join-Path $projects $b.Name.Replace('-', ' ') $r = Invoke-Pwsh $gen @($b.Brief, '-Destination', $dest, '-SkipVerify', '-Json') Assert ($r.Code -eq 0) "generator exit $($r.Code): $($r.Text)" $json = ($r.Output | Where-Object { $_ -notmatch '^\s*$' }) -join "`n" $res = $json.Substring($json.IndexOf('{')) | ConvertFrom-Json $generated[$b.Name] = $dest $res.Destination } } } if ('generation' -in $Group) { foreach ($b in $all) { $dest = $generated[$b.Name] if (-not $dest) { continue } Invoke-Check "generated files: $($b.Name)" { $required = 'README.md', 'AGENTS.md', 'CLAUDE.md', '.gitignore', '.gitattributes', 'project.json', 'docs/spec.md', 'docs/toolchain.md', 'docs/milestones.md', 'docs/manual-tests.md', 'docs/verification-status.md', 'tools/lib.ps1', 'tools/build.ps1', 'tools/run.ps1', 'tools/smoke.ps1', 'tools/check-prereqs.ps1' foreach ($f in $required) { Assert (Test-Path -LiteralPath (Join-Path $dest $f)) "missing $f" } Assert (@(Get-ChildItem -LiteralPath (Join-Path $dest 'src') -File).Count -ge 1) 'no source files' $p = Get-Content -LiteralPath (Join-Path $dest 'project.json') -Raw | ConvertFrom-Json -AsHashtable Assert ($p.status -eq 'approved' -and $p.resolved -and @($p.resolved.targets).Count -ge 1) 'project.json incomplete' Assert ($p.toolchain.language -and $p.buildLocation -and @($p.targets).Count -eq @($p.resolved.targets).Count) 'project.json missing target/language/build location' $spec = Get-Content -LiteralPath (Join-Path $dest 'docs/spec.md') -Raw foreach ($req in $p.requirements) { Assert ($spec -match [regex]::Escape("### $($req.id) ")) "spec lacks $($req.id)" } foreach ($t in $p.targets) { Assert ($spec -match [regex]::Escape("``$($t.id)``")) "spec lacks target $($t.id)" } $ms = Get-Content -LiteralPath (Join-Path $dest 'docs/milestones.md') -Raw Assert ($ms -match '## M0 - Starter builds and runs') 'milestones lack M0' foreach ($m in $p.milestones) { Assert ($ms -match "## $($m.id) ") "milestones lack $($m.id)" } $mt = Get-Content -LiteralPath (Join-Path $dest 'docs/manual-tests.md') -Raw foreach ($t in $p.targets) { Assert ($mt -match "MT0-$($t.id)") "manual tests lack M0 for $($t.id)" } $agents = Get-Content -LiteralPath (Join-Path $dest 'AGENTS.md') -Raw Assert ($agents -match 'PENDING until you confirm' -and $agents -match 'docs/spec.md') 'AGENTS.md incomplete' foreach ($f in 'tools/knowledge.ps1', 'skills/knowledge-management/SKILL.md') { Assert (Test-Path -LiteralPath (Join-Path $dest $f)) "missing $f" } Assert ($agents -match 'Knowledge system \(mandatory policy\)' -and $agents -match [regex]::Escape("Project key: ``$($p.project.slug)``")) 'AGENTS.md lacks the knowledge policy or project key' Assert ($p.generated.foundryRoot -and (Test-Path -LiteralPath (Join-Path $p.generated.foundryRoot 'powershell/Foundry.Knowledge/Foundry.Knowledge.psd1'))) 'project.json does not point at the Foundry knowledge module' $oc = Test-Path -LiteralPath (Join-Path $dest 'tools/to-commodore.ps1') Assert ($oc -eq ($p.buildLocation -eq 'on-commodore')) 'on-Commodore scripts present/absent incorrectly' if ($p.toolchain.override) { Assert ((Get-Content -LiteralPath (Join-Path $dest 'docs/toolchain.md') -Raw) -match 'User override \(approved\)') 'override not documented' } "$(@(Get-ChildItem -LiteralPath $dest -Recurse -File -Force | Where-Object FullName -notmatch '\\.git\\').Count) files" } Invoke-Check "template completeness: $($b.Name) has no unresolved placeholders" { $u = @(Find-UnresolvedPlaceholders $dest) Assert ($u.Count -eq 0) (($u | ForEach-Object { "$($_.File):$($_.Line)" }) -join ', ') } Invoke-Check "git: $($b.Name) is on main with a clean initial commit" { $branch = (& git -C $dest symbolic-ref --short HEAD) -join '' Assert ($branch -eq 'main') "branch is '$branch'" $count = (& git -C $dest rev-list --count HEAD 2>$null) -join '' Assert ($count -eq '1') "expected 1 commit, found '$count'" $status = (& git -C $dest status --porcelain) -join "`n" Assert (-not $status) "working tree not clean: $status" $remotes = (& git -C $dest remote) -join '' Assert (-not $remotes) 'a remote was configured' } } Invoke-Check 'git: without a Git identity the files are staged, not committed' { $empty = Join-Path $OutputRoot 'empty-gitconfig' Set-Content -LiteralPath $empty -Value '' $dest = Join-Path $projects 'no identity' $r = Invoke-Pwsh $gen @((Join-Path $root 'tests/briefs/c128-40-basic7.json'), '-Destination', $dest, '-SkipVerify') @{ GIT_CONFIG_GLOBAL = $empty; GIT_CONFIG_NOSYSTEM = '1'; GIT_AUTHOR_NAME = $null; GIT_AUTHOR_EMAIL = $null; GIT_COMMITTER_NAME = $null; GIT_COMMITTER_EMAIL = $null } Assert ($r.Code -eq 0) "exit $($r.Code): $($r.Text)" Assert ($r.Text -match 'no Git identity configured') 'no-identity message missing' & git -C $dest rev-parse --verify HEAD 2>$null | Out-Null Assert ($LASTEXITCODE -ne 0) 'a commit exists' $staged = @(& git -C $dest diff --cached --name-only) Assert ($staged.Count -gt 10) "only $($staged.Count) files staged" "$($staged.Count) files staged" } } # --------------------------------------------------------------------------- if ('safety' -in $Group) { $brief = Join-Path $root 'tests/briefs/c128-40-basic7.json' Invoke-Check 'safety: existing non-empty folder is refused and left untouched' { $dest = Join-Path $projects 'existing work' New-Item -ItemType Directory -Force -Path (Join-Path $dest 'notes') | Out-Null Set-Content -LiteralPath (Join-Path $dest 'notes/todo.txt') -Value 'my work' $before = Get-Hash $dest $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify') Assert ($r.Code -eq 1 -and $r.Text -match 'not empty') "exit $($r.Code)" Assert ((Get-Hash $dest) -eq $before) 'existing folder changed' Assert (@(Get-ChildItem -LiteralPath $projects -Force -Filter '.existing work.foundry-staging-*').Count -eq 0) 'staging folder left behind' } Invoke-Check 'safety: existing project repository is refused and left untouched' { $dest = Join-Path $projects 'existing repo' New-Item -ItemType Directory -Force -Path $dest | Out-Null & git -C $dest init -q -b main Set-Content -LiteralPath (Join-Path $dest 'README.md') -Value 'existing project' $before = Get-Hash $dest $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify') Assert ($r.Code -eq 1 -and $r.Text -match 'already contains a Git repository') "exit $($r.Code): $($r.Text)" Assert ((Get-Hash $dest) -eq $before) 'existing repo changed' } Invoke-Check 'safety: destination that is a file is refused' { $dest = Join-Path $projects 'a file' Set-Content -LiteralPath $dest -Value 'x' $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify') Assert ($r.Code -eq 1 -and (Get-Content -LiteralPath $dest) -eq 'x') "exit $($r.Code)" } Invoke-Check 'safety: existing empty folder is accepted' { $dest = Join-Path $projects 'empty folder' New-Item -ItemType Directory -Force -Path $dest | Out-Null $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify') Assert ($r.Code -eq 0 -and (Test-Path -LiteralPath (Join-Path $dest 'AGENTS.md'))) "exit $($r.Code): $($r.Text)" } Invoke-Check 'safety: destination inside the Foundry repository is refused' { $r = Invoke-Pwsh $gen @($brief, '-Destination', (Join-Path $root 'output/inside'), '-SkipVerify') Assert ($r.Code -eq 1 -and $r.Text -match 'inside the Foundry') "exit $($r.Code)" Assert (-not (Test-Path -LiteralPath (Join-Path $root 'output/inside'))) 'folder created inside Foundry' } Invoke-Check 'safety: invalid destination names are refused' { foreach ($bad in @((Join-Path $projects 'bad|name'), (Join-Path $projects 'trailing dot.'), (Join-Path $projects 'CON'))) { $r = Invoke-Pwsh $gen @($brief, '-Destination', $bad, '-SkipVerify') Assert ($r.Code -eq 1) "accepted '$bad'" } } foreach ($stepName in 'templates', 'files', 'git') { Invoke-Check "safety: interruption after '$stepName' leaves nothing behind, and a retry succeeds" { $dest = Join-Path $projects "interrupted $stepName" $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify', '-FailAfterStep', $stepName) Assert ($r.Code -eq 1 -and $r.Text -match 'Simulated interruption') "exit $($r.Code)" Assert (-not (Test-Path -LiteralPath $dest)) 'destination created by failed run' Assert (@(Get-ChildItem -LiteralPath $projects -Force -Filter ".interrupted $stepName.foundry-staging-*").Count -eq 0) 'staging left behind' $retry = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify') Assert ($retry.Code -eq 0 -and (Test-Path -LiteralPath (Join-Path $dest 'project.json'))) "retry failed: $($retry.Text)" } } Invoke-Check 'safety: interruption into an existing empty folder keeps the folder' { $dest = Join-Path $projects 'empty kept' New-Item -ItemType Directory -Force -Path $dest | Out-Null $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify', '-FailAfterStep', 'git') Assert ($r.Code -eq 1) 'did not fail' Assert ((Test-Path -LiteralPath $dest -PathType Container) -and @(Get-ChildItem -LiteralPath $dest -Force).Count -eq 0) 'empty folder not restored' } Invoke-Check 'safety: leftover staging folder from a killed run is cleaned on retry' { $dest = Join-Path $projects 'after crash' $stale = Join-Path $projects '.after crash.foundry-staging-20000101000000-dead00' New-Item -ItemType Directory -Force -Path (Join-Path $stale 'docs') | Out-Null Set-Content -LiteralPath (Join-Path $stale 'docs/partial.md') -Value 'partial' $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify') Assert ($r.Code -eq 0 -and $r.Text -match 'leftover staging') "exit $($r.Code): $($r.Text)" Assert (-not (Test-Path -LiteralPath $stale)) 'stale staging still there' } } # --------------------------------------------------------------------------- if ('toolchain' -in $Group) { $probe = $generated['traffic-control-64'] $pre = if ($probe) { Invoke-Pwsh (Join-Path $probe 'tools/check-prereqs.ps1') @() } else { $null } $toolsOk = $pre -and $pre.Code -eq 0 foreach ($b in $all) { $dest = $generated[$b.Name] if (-not $dest) { continue } Invoke-Check "toolchain: $($b.Name) check-prereqs, build and smoke test (expect exit $($b.Expect))" { if (-not $toolsOk) { return "SKIP: prerequisites missing ($(($pre.Output | Where-Object { $_ -match 'MISSING' }) -join '; '))" } $p = Invoke-Pwsh (Join-Path $dest 'tools/check-prereqs.ps1') @() Assert ($p.Code -eq 0) "check-prereqs exit $($p.Code)" $bld = Invoke-Pwsh (Join-Path $dest 'tools/build.ps1') @() Assert ($bld.Code -eq 0) "build exit $($bld.Code): $($bld.Text)" $s = Invoke-Pwsh (Join-Path $dest 'tools/smoke.ps1') @() Assert ($s.Code -eq $b.Expect) "smoke exit $($s.Code), expected $($b.Expect):`n$($s.Text)" $lines = @($s.Output | Where-Object { $_ -match '^\s+\[(PASS|FAIL|MANUAL)\]' } | ForEach-Object { $_.Trim() }) foreach ($l in $s.Output | Where-Object { $_ -match '^\s+(ok|FAIL)\s' }) { $script:Log.Add(" $($l.Trim())") } $lines -join ', ' } } Invoke-Check 'toolchain: tools/run.ps1 starts VICE with the program (interactive launch)' { if (-not $toolsOk) { return 'SKIP: prerequisites missing' } $results = @() foreach ($name in 'traffic-control-64', 'c128-text-viewer') { $dest = $generated[$name] $p = Get-Content -LiteralPath (Join-Path $dest 'project.json') -Raw | ConvertFrom-Json -AsHashtable $exe = $p.resolved.targets[0].emulator.binary $before = @(Get-Process $exe -ErrorAction SilentlyContinue | ForEach-Object Id) $r = Invoke-Pwsh (Join-Path $dest 'tools/run.ps1') @('-NoBuild') Assert ($r.Code -eq 0) "run.ps1 exit $($r.Code): $($r.Text)" Start-Sleep -Seconds 6 $new = @(Get-Process $exe -ErrorAction SilentlyContinue | Where-Object { $_.Id -notin $before }) Assert ($new.Count -ge 1) "$exe exited or did not start (check the emulator arguments): $($r.Text)" $new | Stop-Process -Force $results += "$name -> $exe running" } $results -join '; ' } Invoke-Check 'toolchain: on-Commodore round trip (host -> transfer disk -> host) is lossless' { if (-not $toolsOk) { return 'SKIP: prerequisites missing' } $dest = $generated['on-commodore-basic'] $to = Invoke-Pwsh (Join-Path $dest 'tools/to-commodore.ps1') @() Assert ($to.Code -eq 0 -and (Test-Path -LiteralPath (Join-Path $dest 'build/transfer/spelling-bee.d64'))) "to-commodore exit $($to.Code): $($to.Text)" $from = Invoke-Pwsh (Join-Path $dest 'tools/from-commodore.ps1') @() Assert ($from.Code -eq 0) "from-commodore exit $($from.Code): $($from.Text)" $orig = (& git -C $dest show HEAD:src/main.bas) -join "`n" $back = (Get-Content -LiteralPath (Join-Path $dest 'src/main.bas')) -join "`n" $norm = { param($t) ($t -split "`n" | ForEach-Object { $_.Trim() } | Where-Object { $_ }) -join "`n" } Assert ((& $norm $orig) -eq (& $norm $back)) "listing changed:`n$back" & git -C $dest checkout -q -- src/main.bas 'listing identical after detokenizing' } Invoke-Check 'toolchain: on-Commodore change made on the target comes back to the host' { if (-not $toolsOk) { return 'SKIP: prerequisites missing' } $dest = $generated['on-commodore-basic'] # Simulate editing on the Commodore: a new line saved to the transfer disk. $p = Get-Content -LiteralPath (Join-Path $dest 'project.json') -Raw | ConvertFrom-Json -AsHashtable $tool = { param($id) $t = $p.resolved.tools | Where-Object { $_.id -eq $id }; . (Join-Path $dest 'tools/lib.ps1'); Find-ToolPath $t } $petcat = & $tool 'petcat'; $c1541 = & $tool 'c1541' $edited = Join-Path $OutputRoot 'edited.bas' (Get-Content -LiteralPath (Join-Path $dest 'src/main.bas')) + '80 rem edited on the c64' | Set-Content -LiteralPath $edited & $petcat -w2 -f -o (Join-Path $OutputRoot 'edited.prg') -- $edited | Out-Null & $c1541 -attach (Join-Path $dest 'build/transfer/spelling-bee.d64') -delete spellingbee -write (Join-Path $OutputRoot 'edited.prg') spellingbee 2>&1 | Out-Null $from = Invoke-Pwsh (Join-Path $dest 'tools/from-commodore.ps1') @() Assert ($from.Code -eq 0) "from-commodore exit $($from.Code)" Assert ((Get-Content -LiteralPath (Join-Path $dest 'src/main.bas') -Raw) -match '80 rem edited on the c64') 'edit not returned' & git -C $dest checkout -q -- src/main.bas 'line 80 arrived in src/main.bas' } Invoke-Check 'toolchain: a missing tool is reported with the first command and what stays unverified' { $src = $generated['c128-40-basic7'] $dest = Join-Path $projects 'missing tool copy' Copy-Item -LiteralPath $src -Destination $dest -Recurse $pj = Join-Path $dest 'project.json' $p = Get-Content -LiteralPath $pj -Raw | ConvertFrom-Json -AsHashtable ($p.resolved.tools | Where-Object { $_.id -eq 'x128' }).executable = 'x128-not-installed' [IO.File]::WriteAllText($pj, ($p | ConvertTo-Json -Depth 64)) $c = Invoke-Pwsh (Join-Path $dest 'tools/check-prereqs.ps1') @() Assert ($c.Code -eq 2) "check-prereqs exit $($c.Code)" Assert ($c.Text -match 'MISSING\] x128-not-installed' -and $c.Text -match 'First step : winget install' -and $c.Text -match 'Unverified until installed: run and smoke test of target c128-40') "message incomplete:`n$($c.Text)" $s = Invoke-Pwsh (Join-Path $dest 'tools/smoke.ps1') @() Assert ($s.Code -eq 2 -and $s.Text -match 'MISSING TOOL') "smoke exit $($s.Code)" 'exit 2 with install command' } } # --------------------------------------------------------------------------- $pass = @($Results | Where-Object Status -eq 'PASS').Count $fail = @($Results | Where-Object Status -eq 'FAIL').Count $skip = @($Results | Where-Object Status -eq 'SKIP').Count Write-Host "" Write-Host "Summary: $pass passed, $fail failed, $skip skipped. Output: $OutputRoot" $md = @("# Foundry test run $(Get-Date -Format 'yyyy-MM-dd HH:mm')", '', "Groups: $($Group -join ', '). Output folder: ``$OutputRoot``", '', "Result: **$pass passed, $fail failed, $skip skipped**", '', '| Status | Check | Detail | s |', '|---|---|---|---|') $md += $Results | ForEach-Object { "| $($_.Status) | $($_.Name) | $(($_.Detail -replace '\|', '/' -replace '\r?\n', ' ').Substring(0, [Math]::Min(300, $_.Detail.Length))) | $($_.Seconds) |" } $md += ''; $md += '## Commands run'; $md += ''; $md += '```'; $md += $Log; $md += '```' $resultsDir = Join-Path $root 'tests/results' New-Item -ItemType Directory -Force -Path $resultsDir | Out-Null [IO.File]::WriteAllText((Join-Path $resultsDir 'latest.md'), ($md -join "`n"), [Text.UTF8Encoding]::new($false)) if (-not $KeepOutput -and $fail -eq 0) { Remove-Item -LiteralPath $OutputRoot -Recurse -Force -ErrorAction SilentlyContinue } exit $(if ($fail) { 1 } else { 0 })