No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

434 líneas
30KB

  1. <#
  2. .SYNOPSIS
  3. Automated checks for the C64/C128 Dev Foundry.
  4. .DESCRIPTION
  5. Groups (all run by default):
  6. validation - example and test briefs, conflicts, drafts and resume, override rules
  7. generation - every valid brief through tools/New-FoundryProject.ps1 into a folder whose
  8. path contains spaces; generated files, template completeness, project.json,
  9. Git (branch main, commit, clean tree), no-identity behavior
  10. safety - existing destinations (non-empty, Git repo, file, empty), destinations inside
  11. the Foundry, invalid names, interrupted generation and retry, stale staging
  12. toolchain - for each generated project: tools/check-prereqs.ps1, tools/build.ps1,
  13. tools/smoke.ps1 (VICE headless); on-Commodore round trip; simulated missing tool.
  14. Skipped with a reason when cc65/VICE are not installed.
  15. Results are printed and written to tests/results/latest.md (commands and outcomes).
  16. Exit code 0 = no failures.
  17. .EXAMPLE
  18. pwsh -File tests/Run-FoundryTests.ps1
  19. pwsh -File tests/Run-FoundryTests.ps1 -Group validation,safety
  20. pwsh -File tests/Run-FoundryTests.ps1 -KeepOutput
  21. #>
  22. [CmdletBinding()]
  23. param(
  24. [string[]]$Group = @('validation', 'generation', 'safety', 'toolchain'),
  25. [string]$OutputRoot,
  26. [switch]$KeepOutput
  27. )
  28. $ErrorActionPreference = 'Stop'
  29. $Group = @($Group | ForEach-Object { $_ -split ',' } | ForEach-Object { $_.Trim() } | Where-Object { $_ })
  30. foreach ($g in $Group) { if ($g -notin 'validation', 'generation', 'safety', 'toolchain') { throw "Unknown group '$g'. Use validation, generation, safety, toolchain." } }
  31. $root = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
  32. Import-Module (Join-Path $root 'tools/Foundry.psm1') -Force
  33. $gen = Join-Path $root 'tools/New-FoundryProject.ps1'
  34. $val = Join-Path $root 'tools/Test-FoundryBrief.ps1'
  35. if (-not $OutputRoot) { $OutputRoot = Join-Path ([IO.Path]::GetTempPath()) ("Foundry Tests\run " + (Get-Date -Format 'yyyyMMdd-HHmmss')) }
  36. $projects = Join-Path $OutputRoot 'My C64 Projects' # deliberately contains spaces
  37. New-Item -ItemType Directory -Force -Path $projects | Out-Null
  38. $script:Results = [System.Collections.Generic.List[object]]::new()
  39. $script:Log = [System.Collections.Generic.List[string]]::new()
  40. function Invoke-Check([string]$Name, [scriptblock]$Body) {
  41. $sw = [Diagnostics.Stopwatch]::StartNew()
  42. try {
  43. $detail = & $Body
  44. $status = if ($detail -is [string] -and $detail.StartsWith('SKIP:')) { 'SKIP' } else { 'PASS' }
  45. $script:Results.Add([pscustomobject]@{ Name = $Name; Status = $status; Detail = "$detail"; Seconds = [math]::Round($sw.Elapsed.TotalSeconds, 1) })
  46. } catch {
  47. $script:Results.Add([pscustomobject]@{ Name = $Name; Status = 'FAIL'; Detail = $_.Exception.Message; Seconds = [math]::Round($sw.Elapsed.TotalSeconds, 1) })
  48. }
  49. $r = $script:Results[-1]
  50. $color = @{ PASS = 'Green'; FAIL = 'Red'; SKIP = 'Yellow' }[$r.Status]
  51. Write-Host ("[{0}] {1}{2}" -f $r.Status, $Name, $(if ($r.Detail) { " - $($r.Detail)" } else { '' })) -ForegroundColor $color
  52. }
  53. function Assert([bool]$Condition, [string]$Message) { if (-not $Condition) { throw $Message } }
  54. function Invoke-Pwsh([string]$Script, [string[]]$Arguments, [hashtable]$Env = @{}) {
  55. $saved = @{}
  56. foreach ($k in $Env.Keys) { $saved[$k] = [Environment]::GetEnvironmentVariable($k); [Environment]::SetEnvironmentVariable($k, $Env[$k]) }
  57. try {
  58. $out = & pwsh -NoProfile -File $Script @Arguments 2>&1 | ForEach-Object { "$_" }
  59. $code = $LASTEXITCODE
  60. } finally { foreach ($k in $saved.Keys) { [Environment]::SetEnvironmentVariable($k, $saved[$k]) } }
  61. $quoted = ($Arguments | ForEach-Object { if ($_ -match '\s') { "`"$_`"" } else { $_ } }) -join ' '
  62. $rel = $Script.Replace($root + [IO.Path]::DirectorySeparatorChar, '')
  63. $script:Log.Add("pwsh -File $rel $quoted -> exit $code")
  64. [pscustomobject]@{ Code = $code; Output = @($out); Text = ($out -join "`n") }
  65. }
  66. function Get-Hash([string]$Dir) {
  67. (Get-ChildItem -LiteralPath $Dir -Recurse -File -Force | Sort-Object FullName | ForEach-Object { "$($_.FullName.Substring($Dir.Length))|$((Get-FileHash -LiteralPath $_.FullName).Hash)" }) -join "`n"
  68. }
  69. $examples = @(
  70. @{ Name = 'traffic-control-64'; Brief = Join-Path $root 'examples/traffic-control-64/brief.json'; Expect = 0 },
  71. @{ Name = 'c128-text-viewer'; Brief = Join-Path $root 'examples/c128-text-viewer/brief.json'; Expect = 0 }
  72. )
  73. $valid = @(
  74. @{ Name = 'c128-40-basic7'; Expect = 0 }, @{ Name = 'dual-native'; Expect = 0 }, @{ Name = 'dual-c64mode'; Expect = 0 },
  75. @{ Name = 'on-commodore-basic'; Expect = 0 }, @{ Name = 'override-asm'; Expect = 0 }, @{ Name = 'unverified-onboard-asm'; Expect = 3 },
  76. @{ 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 }
  77. ) | ForEach-Object { $_.Brief = Join-Path $root "tests/briefs/$($_.Name).json"; $_ }
  78. $conflicts = 'conflict-basic7-on-c64', 'conflict-80col-on-c64', 'conflict-override-capability', 'conflict-build-location'
  79. $all = @($examples) + @($valid)
  80. # ---------------------------------------------------------------------------
  81. if ('validation' -in $Group) {
  82. foreach ($b in $all) {
  83. Invoke-Check "validation: $($b.Name) is a valid approved brief" {
  84. $r = Invoke-Pwsh $val @($b.Brief, '-Mode', 'Approved')
  85. Assert ($r.Code -eq 0) "exit $($r.Code): $($r.Text)"
  86. }
  87. }
  88. foreach ($c in $conflicts) {
  89. Invoke-Check "validation: $c is reported as a conflict and refused" {
  90. $path = Join-Path $root "tests/briefs/$c.json"
  91. $r = Invoke-Pwsh $val @($path, '-Mode', 'Approved')
  92. Assert ($r.Code -eq 4) "expected exit 4 (conflict), got $($r.Code)"
  93. Assert ($r.Text -match 'CONFLICT') 'no CONFLICT line'
  94. Assert ($r.Text -match 'Ask the user which constraint to change') 'conflict does not ask the user which constraint to change'
  95. $g = Invoke-Pwsh $gen @($path, '-Destination', (Join-Path $projects "should-not-exist $c"), '-SkipVerify')
  96. Assert ($g.Code -eq 1) 'generator did not refuse'
  97. Assert (-not (Test-Path -LiteralPath (Join-Path $projects "should-not-exist $c"))) 'destination was created'
  98. ($r.Output | Where-Object { $_ -match 'CONFLICT' } | Select-Object -First 1).Trim().Substring(0, 90) + '...'
  99. }
  100. }
  101. Invoke-Check 'validation: user override conflict names the override and the recommended language' {
  102. $r = Invoke-Pwsh $val @((Join-Path $root 'tests/briefs/conflict-override-capability.json'))
  103. Assert ($r.Text -match "user override") 'override not mentioned'
  104. Assert ($r.Text -match "recommended 'c'") 'recommended language not offered'
  105. }
  106. Invoke-Check 'validation: language different from recommendation without an override is an error' {
  107. $b = Read-FoundryJson (Join-Path $root 'tests/briefs/override-asm.json')
  108. $b.toolchain.override = $null
  109. $res = Test-FoundryBrief -Brief $b -Mode Approved
  110. Assert (-not $res.Valid) 'accepted'
  111. Assert (@($res.Errors | Where-Object { $_.Message -match 'no override is recorded' }).Count -eq 1) 'missing error'
  112. }
  113. Invoke-Check 'validation: override requested but not applied is an error' {
  114. $b = Read-FoundryJson (Join-Path $root 'tests/briefs/override-asm.json')
  115. $b.toolchain.language = 'c'
  116. $res = Test-FoundryBrief -Brief $b -Mode Approved
  117. Assert (@($res.Errors | Where-Object { $_.Message -match 'takes precedence' }).Count -eq 1) 'missing precedence error'
  118. }
  119. Invoke-Check 'validation: dual-target strategy must match the targets' {
  120. $b = Read-FoundryJson (Join-Path $root 'tests/briefs/dual-native.json')
  121. $b.dualTarget.strategy = 'c64-mode-on-both'
  122. $res = Test-FoundryBrief -Brief $b -Mode Approved
  123. Assert ($res.Conflicts.Count -ge 1) 'no conflict for native target under c64-mode-on-both'
  124. $b.dualTarget = $null
  125. $res = Test-FoundryBrief -Brief $b -Mode Approved
  126. Assert (@($res.Missing | Where-Object { $_.Path -eq 'dualTarget.strategy' }).Count -eq 1) 'missing strategy not reported'
  127. }
  128. Invoke-Check 'validation: unlinked requirement and unknown requirement reference are errors' {
  129. $b = Read-FoundryJson (Join-Path $root 'tests/briefs/c128-40-basic7.json')
  130. $b.milestones[0].tasks[0].requirements = @('R9')
  131. $res = Test-FoundryBrief -Brief $b -Mode Approved
  132. Assert (@($res.Errors | Where-Object { $_.Message -match "unknown requirement 'R9'" }).Count -eq 1) 'unknown ref not reported'
  133. Assert (@($res.Errors | Where-Object { $_.Message -match 'R2 is not linked' }).Count -eq 1) 'unlinked requirement not reported'
  134. }
  135. Invoke-Check 'validation: unapproved brief is refused by the generator' {
  136. $tmp = Join-Path $OutputRoot 'unapproved.json'
  137. $b = Read-FoundryJson (Join-Path $root 'tests/briefs/c128-40-basic7.json')
  138. $b.status = 'draft'
  139. [IO.File]::WriteAllText($tmp, ($b | ConvertTo-Json -Depth 32))
  140. $g = Invoke-Pwsh $gen @($tmp, '-Destination', (Join-Path $projects 'unapproved'), '-SkipVerify')
  141. Assert ($g.Code -eq 1 -and $g.Text -match "status must be 'approved'") "exit $($g.Code)"
  142. }
  143. Invoke-Check 'draft: paused draft reports the stage and questions to resume with' {
  144. $r = Invoke-Pwsh $val @((Join-Path $root 'tests/briefs/draft-paused.json'))
  145. Assert ($r.Code -eq 0) "draft mode should accept an incomplete draft (exit $($r.Code))"
  146. Assert ($r.Text -match 'Resume interview at stage: first-release') 'wrong resume stage'
  147. Assert ($r.Text -match 'Should the first release include sound\?') 'saved open question not repeated'
  148. $a = Invoke-Pwsh $val @((Join-Path $root 'tests/briefs/draft-paused.json'), '-Mode', 'Approved')
  149. Assert ($a.Code -eq 1) 'incomplete draft passed approved-mode validation'
  150. 'resume stage: first-release'
  151. }
  152. Invoke-Check 'draft: New-FoundryDraft creates a draft that resumes at the first stage with gaps' {
  153. $slug = 'zz-test-draft-' + [Guid]::NewGuid().ToString('N').Substring(0, 6)
  154. $r = Invoke-Pwsh (Join-Path $root 'tools/New-FoundryDraft.ps1') @('-Name', 'Test Draft', '-Idea', 'A tiny test idea.', '-Slug', $slug)
  155. $path = Join-Path $root "drafts/$slug.json"
  156. try {
  157. Assert (Test-Path -LiteralPath $path) 'draft file not created'
  158. Assert ($r.Text -match 'Resume interview at stage: idea') "unexpected stage: $($r.Text)"
  159. $again = Invoke-Pwsh (Join-Path $root 'tools/New-FoundryDraft.ps1') @('-Name', 'Test Draft', '-Idea', 'x', '-Slug', $slug)
  160. Assert ($again.Code -eq 1) 'existing draft was overwritten'
  161. } finally { Remove-Item -LiteralPath $path -ErrorAction SilentlyContinue }
  162. }
  163. Invoke-Check 'schema: every example, test brief and the draft template match schema/project-brief.schema.json' {
  164. $schema = Join-Path $root 'schema/project-brief.schema.json'
  165. $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'))
  166. foreach ($f in $files) { Assert (Test-Json -Path $f.FullName -SchemaFile $schema -ErrorAction SilentlyContinue) "$($f.Name) does not match the schema" }
  167. $bad = Join-Path $OutputRoot 'bad-schema.json'
  168. $b = Read-FoundryJson (Join-Path $root 'tests/briefs/c128-40-basic7.json'); $b.targets[0].platform = 'vic20'
  169. [IO.File]::WriteAllText($bad, ($b | ConvertTo-Json -Depth 32))
  170. $r = Invoke-Pwsh $val @($bad)
  171. Assert ($r.Code -ne 0 -and $r.Text -match 'SCHEMA') 'schema violation not reported'
  172. "$($files.Count) files conform; invalid platform rejected"
  173. }
  174. Invoke-Check 'templates: unknown placeholder is rejected' {
  175. $threw = $false
  176. try { Expand-FoundryTemplate 'Hello {{NOT_A_VAR}}' @{} 'unit' | Out-Null } catch { $threw = $_.Exception.Message -match 'NOT_A_VAR' }
  177. Assert $threw 'no error for unknown placeholder'
  178. }
  179. }
  180. # ---------------------------------------------------------------------------
  181. $generated = @{}
  182. if ('generation' -in $Group -or 'toolchain' -in $Group) {
  183. foreach ($b in $all) {
  184. Invoke-Check "generation: $($b.Name) through tools/New-FoundryProject.ps1" {
  185. $dest = Join-Path $projects $b.Name.Replace('-', ' ')
  186. $r = Invoke-Pwsh $gen @($b.Brief, '-Destination', $dest, '-SkipVerify', '-Json')
  187. Assert ($r.Code -eq 0) "generator exit $($r.Code): $($r.Text)"
  188. $json = ($r.Output | Where-Object { $_ -notmatch '^\s*$' }) -join "`n"
  189. $res = $json.Substring($json.IndexOf('{')) | ConvertFrom-Json
  190. $generated[$b.Name] = $dest
  191. $res.Destination
  192. }
  193. }
  194. }
  195. if ('generation' -in $Group) {
  196. foreach ($b in $all) {
  197. $dest = $generated[$b.Name]
  198. if (-not $dest) { continue }
  199. Invoke-Check "generated files: $($b.Name)" {
  200. $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'
  201. foreach ($f in $required) { Assert (Test-Path -LiteralPath (Join-Path $dest $f)) "missing $f" }
  202. Assert (@(Get-ChildItem -LiteralPath (Join-Path $dest 'src') -File).Count -ge 1) 'no source files'
  203. $p = Get-Content -LiteralPath (Join-Path $dest 'project.json') -Raw | ConvertFrom-Json -AsHashtable
  204. Assert ($p.status -eq 'approved' -and $p.resolved -and @($p.resolved.targets).Count -ge 1) 'project.json incomplete'
  205. Assert ($p.toolchain.language -and $p.buildLocation -and @($p.targets).Count -eq @($p.resolved.targets).Count) 'project.json missing target/language/build location'
  206. $spec = Get-Content -LiteralPath (Join-Path $dest 'docs/spec.md') -Raw
  207. foreach ($req in $p.requirements) { Assert ($spec -match [regex]::Escape("### $($req.id) ")) "spec lacks $($req.id)" }
  208. foreach ($t in $p.targets) { Assert ($spec -match [regex]::Escape("``$($t.id)``")) "spec lacks target $($t.id)" }
  209. $ms = Get-Content -LiteralPath (Join-Path $dest 'docs/milestones.md') -Raw
  210. Assert ($ms -match '## M0 - Starter builds and runs') 'milestones lack M0'
  211. foreach ($m in $p.milestones) { Assert ($ms -match "## $($m.id) ") "milestones lack $($m.id)" }
  212. $mt = Get-Content -LiteralPath (Join-Path $dest 'docs/manual-tests.md') -Raw
  213. foreach ($t in $p.targets) { Assert ($mt -match "MT0-$($t.id)") "manual tests lack M0 for $($t.id)" }
  214. $agents = Get-Content -LiteralPath (Join-Path $dest 'AGENTS.md') -Raw
  215. Assert ($agents -match 'PENDING until you confirm' -and $agents -match 'docs/spec.md') 'AGENTS.md incomplete'
  216. $oc = Test-Path -LiteralPath (Join-Path $dest 'tools/to-commodore.ps1')
  217. Assert ($oc -eq ($p.buildLocation -eq 'on-commodore')) 'on-Commodore scripts present/absent incorrectly'
  218. if ($p.toolchain.override) { Assert ((Get-Content -LiteralPath (Join-Path $dest 'docs/toolchain.md') -Raw) -match 'User override \(approved\)') 'override not documented' }
  219. "$(@(Get-ChildItem -LiteralPath $dest -Recurse -File -Force | Where-Object FullName -notmatch '\\.git\\').Count) files"
  220. }
  221. Invoke-Check "template completeness: $($b.Name) has no unresolved placeholders" {
  222. $u = @(Find-UnresolvedPlaceholders $dest)
  223. Assert ($u.Count -eq 0) (($u | ForEach-Object { "$($_.File):$($_.Line)" }) -join ', ')
  224. }
  225. Invoke-Check "git: $($b.Name) is on main with a clean initial commit" {
  226. $branch = (& git -C $dest symbolic-ref --short HEAD) -join ''
  227. Assert ($branch -eq 'main') "branch is '$branch'"
  228. $count = (& git -C $dest rev-list --count HEAD 2>$null) -join ''
  229. Assert ($count -eq '1') "expected 1 commit, found '$count'"
  230. $status = (& git -C $dest status --porcelain) -join "`n"
  231. Assert (-not $status) "working tree not clean: $status"
  232. $remotes = (& git -C $dest remote) -join ''
  233. Assert (-not $remotes) 'a remote was configured'
  234. }
  235. }
  236. Invoke-Check 'git: without a Git identity the files are staged, not committed' {
  237. $empty = Join-Path $OutputRoot 'empty-gitconfig'
  238. Set-Content -LiteralPath $empty -Value ''
  239. $dest = Join-Path $projects 'no identity'
  240. $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 }
  241. Assert ($r.Code -eq 0) "exit $($r.Code): $($r.Text)"
  242. Assert ($r.Text -match 'no Git identity configured') 'no-identity message missing'
  243. & git -C $dest rev-parse --verify HEAD 2>$null | Out-Null
  244. Assert ($LASTEXITCODE -ne 0) 'a commit exists'
  245. $staged = @(& git -C $dest diff --cached --name-only)
  246. Assert ($staged.Count -gt 10) "only $($staged.Count) files staged"
  247. "$($staged.Count) files staged"
  248. }
  249. }
  250. # ---------------------------------------------------------------------------
  251. if ('safety' -in $Group) {
  252. $brief = Join-Path $root 'tests/briefs/c128-40-basic7.json'
  253. Invoke-Check 'safety: existing non-empty folder is refused and left untouched' {
  254. $dest = Join-Path $projects 'existing work'
  255. New-Item -ItemType Directory -Force -Path (Join-Path $dest 'notes') | Out-Null
  256. Set-Content -LiteralPath (Join-Path $dest 'notes/todo.txt') -Value 'my work'
  257. $before = Get-Hash $dest
  258. $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify')
  259. Assert ($r.Code -eq 1 -and $r.Text -match 'not empty') "exit $($r.Code)"
  260. Assert ((Get-Hash $dest) -eq $before) 'existing folder changed'
  261. Assert (@(Get-ChildItem -LiteralPath $projects -Force -Filter '.existing work.foundry-staging-*').Count -eq 0) 'staging folder left behind'
  262. }
  263. Invoke-Check 'safety: existing project repository is refused and left untouched' {
  264. $dest = Join-Path $projects 'existing repo'
  265. New-Item -ItemType Directory -Force -Path $dest | Out-Null
  266. & git -C $dest init -q -b main
  267. Set-Content -LiteralPath (Join-Path $dest 'README.md') -Value 'existing project'
  268. $before = Get-Hash $dest
  269. $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify')
  270. Assert ($r.Code -eq 1 -and $r.Text -match 'already contains a Git repository') "exit $($r.Code): $($r.Text)"
  271. Assert ((Get-Hash $dest) -eq $before) 'existing repo changed'
  272. }
  273. Invoke-Check 'safety: destination that is a file is refused' {
  274. $dest = Join-Path $projects 'a file'
  275. Set-Content -LiteralPath $dest -Value 'x'
  276. $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify')
  277. Assert ($r.Code -eq 1 -and (Get-Content -LiteralPath $dest) -eq 'x') "exit $($r.Code)"
  278. }
  279. Invoke-Check 'safety: existing empty folder is accepted' {
  280. $dest = Join-Path $projects 'empty folder'
  281. New-Item -ItemType Directory -Force -Path $dest | Out-Null
  282. $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify')
  283. Assert ($r.Code -eq 0 -and (Test-Path -LiteralPath (Join-Path $dest 'AGENTS.md'))) "exit $($r.Code): $($r.Text)"
  284. }
  285. Invoke-Check 'safety: destination inside the Foundry repository is refused' {
  286. $r = Invoke-Pwsh $gen @($brief, '-Destination', (Join-Path $root 'output/inside'), '-SkipVerify')
  287. Assert ($r.Code -eq 1 -and $r.Text -match 'inside the Foundry') "exit $($r.Code)"
  288. Assert (-not (Test-Path -LiteralPath (Join-Path $root 'output/inside'))) 'folder created inside Foundry'
  289. }
  290. Invoke-Check 'safety: invalid destination names are refused' {
  291. foreach ($bad in @((Join-Path $projects 'bad|name'), (Join-Path $projects 'trailing dot.'), (Join-Path $projects 'CON'))) {
  292. $r = Invoke-Pwsh $gen @($brief, '-Destination', $bad, '-SkipVerify')
  293. Assert ($r.Code -eq 1) "accepted '$bad'"
  294. }
  295. }
  296. foreach ($stepName in 'templates', 'files', 'git') {
  297. Invoke-Check "safety: interruption after '$stepName' leaves nothing behind, and a retry succeeds" {
  298. $dest = Join-Path $projects "interrupted $stepName"
  299. $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify', '-FailAfterStep', $stepName)
  300. Assert ($r.Code -eq 1 -and $r.Text -match 'Simulated interruption') "exit $($r.Code)"
  301. Assert (-not (Test-Path -LiteralPath $dest)) 'destination created by failed run'
  302. Assert (@(Get-ChildItem -LiteralPath $projects -Force -Filter ".interrupted $stepName.foundry-staging-*").Count -eq 0) 'staging left behind'
  303. $retry = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify')
  304. Assert ($retry.Code -eq 0 -and (Test-Path -LiteralPath (Join-Path $dest 'project.json'))) "retry failed: $($retry.Text)"
  305. }
  306. }
  307. Invoke-Check 'safety: interruption into an existing empty folder keeps the folder' {
  308. $dest = Join-Path $projects 'empty kept'
  309. New-Item -ItemType Directory -Force -Path $dest | Out-Null
  310. $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify', '-FailAfterStep', 'git')
  311. Assert ($r.Code -eq 1) 'did not fail'
  312. Assert ((Test-Path -LiteralPath $dest -PathType Container) -and @(Get-ChildItem -LiteralPath $dest -Force).Count -eq 0) 'empty folder not restored'
  313. }
  314. Invoke-Check 'safety: leftover staging folder from a killed run is cleaned on retry' {
  315. $dest = Join-Path $projects 'after crash'
  316. $stale = Join-Path $projects '.after crash.foundry-staging-20000101000000-dead00'
  317. New-Item -ItemType Directory -Force -Path (Join-Path $stale 'docs') | Out-Null
  318. Set-Content -LiteralPath (Join-Path $stale 'docs/partial.md') -Value 'partial'
  319. $r = Invoke-Pwsh $gen @($brief, '-Destination', $dest, '-SkipVerify')
  320. Assert ($r.Code -eq 0 -and $r.Text -match 'leftover staging') "exit $($r.Code): $($r.Text)"
  321. Assert (-not (Test-Path -LiteralPath $stale)) 'stale staging still there'
  322. }
  323. }
  324. # ---------------------------------------------------------------------------
  325. if ('toolchain' -in $Group) {
  326. $probe = $generated['traffic-control-64']
  327. $pre = if ($probe) { Invoke-Pwsh (Join-Path $probe 'tools/check-prereqs.ps1') @() } else { $null }
  328. $toolsOk = $pre -and $pre.Code -eq 0
  329. foreach ($b in $all) {
  330. $dest = $generated[$b.Name]
  331. if (-not $dest) { continue }
  332. Invoke-Check "toolchain: $($b.Name) check-prereqs, build and smoke test (expect exit $($b.Expect))" {
  333. if (-not $toolsOk) { return "SKIP: prerequisites missing ($(($pre.Output | Where-Object { $_ -match 'MISSING' }) -join '; '))" }
  334. $p = Invoke-Pwsh (Join-Path $dest 'tools/check-prereqs.ps1') @()
  335. Assert ($p.Code -eq 0) "check-prereqs exit $($p.Code)"
  336. $bld = Invoke-Pwsh (Join-Path $dest 'tools/build.ps1') @()
  337. Assert ($bld.Code -eq 0) "build exit $($bld.Code): $($bld.Text)"
  338. $s = Invoke-Pwsh (Join-Path $dest 'tools/smoke.ps1') @()
  339. Assert ($s.Code -eq $b.Expect) "smoke exit $($s.Code), expected $($b.Expect):`n$($s.Text)"
  340. $lines = @($s.Output | Where-Object { $_ -match '^\s+\[(PASS|FAIL|MANUAL)\]' } | ForEach-Object { $_.Trim() })
  341. foreach ($l in $s.Output | Where-Object { $_ -match '^\s+(ok|FAIL)\s' }) { $script:Log.Add(" $($l.Trim())") }
  342. $lines -join ', '
  343. }
  344. }
  345. Invoke-Check 'toolchain: tools/run.ps1 starts VICE with the program (interactive launch)' {
  346. if (-not $toolsOk) { return 'SKIP: prerequisites missing' }
  347. $results = @()
  348. foreach ($name in 'traffic-control-64', 'c128-text-viewer') {
  349. $dest = $generated[$name]
  350. $p = Get-Content -LiteralPath (Join-Path $dest 'project.json') -Raw | ConvertFrom-Json -AsHashtable
  351. $exe = $p.resolved.targets[0].emulator.binary
  352. $before = @(Get-Process $exe -ErrorAction SilentlyContinue | ForEach-Object Id)
  353. $r = Invoke-Pwsh (Join-Path $dest 'tools/run.ps1') @('-NoBuild')
  354. Assert ($r.Code -eq 0) "run.ps1 exit $($r.Code): $($r.Text)"
  355. Start-Sleep -Seconds 6
  356. $new = @(Get-Process $exe -ErrorAction SilentlyContinue | Where-Object { $_.Id -notin $before })
  357. Assert ($new.Count -ge 1) "$exe exited or did not start (check the emulator arguments): $($r.Text)"
  358. $new | Stop-Process -Force
  359. $results += "$name -> $exe running"
  360. }
  361. $results -join '; '
  362. }
  363. Invoke-Check 'toolchain: on-Commodore round trip (host -> transfer disk -> host) is lossless' {
  364. if (-not $toolsOk) { return 'SKIP: prerequisites missing' }
  365. $dest = $generated['on-commodore-basic']
  366. $to = Invoke-Pwsh (Join-Path $dest 'tools/to-commodore.ps1') @()
  367. Assert ($to.Code -eq 0 -and (Test-Path -LiteralPath (Join-Path $dest 'build/transfer/spelling-bee.d64'))) "to-commodore exit $($to.Code): $($to.Text)"
  368. $from = Invoke-Pwsh (Join-Path $dest 'tools/from-commodore.ps1') @()
  369. Assert ($from.Code -eq 0) "from-commodore exit $($from.Code): $($from.Text)"
  370. $orig = (& git -C $dest show HEAD:src/main.bas) -join "`n"
  371. $back = (Get-Content -LiteralPath (Join-Path $dest 'src/main.bas')) -join "`n"
  372. $norm = { param($t) ($t -split "`n" | ForEach-Object { $_.Trim() } | Where-Object { $_ }) -join "`n" }
  373. Assert ((& $norm $orig) -eq (& $norm $back)) "listing changed:`n$back"
  374. & git -C $dest checkout -q -- src/main.bas
  375. 'listing identical after detokenizing'
  376. }
  377. Invoke-Check 'toolchain: on-Commodore change made on the target comes back to the host' {
  378. if (-not $toolsOk) { return 'SKIP: prerequisites missing' }
  379. $dest = $generated['on-commodore-basic']
  380. # Simulate editing on the Commodore: a new line saved to the transfer disk.
  381. $p = Get-Content -LiteralPath (Join-Path $dest 'project.json') -Raw | ConvertFrom-Json -AsHashtable
  382. $tool = { param($id) $t = $p.resolved.tools | Where-Object { $_.id -eq $id }; . (Join-Path $dest 'tools/lib.ps1'); Find-ToolPath $t }
  383. $petcat = & $tool 'petcat'; $c1541 = & $tool 'c1541'
  384. $edited = Join-Path $OutputRoot 'edited.bas'
  385. (Get-Content -LiteralPath (Join-Path $dest 'src/main.bas')) + '80 rem edited on the c64' | Set-Content -LiteralPath $edited
  386. & $petcat -w2 -f -o (Join-Path $OutputRoot 'edited.prg') -- $edited | Out-Null
  387. & $c1541 -attach (Join-Path $dest 'build/transfer/spelling-bee.d64') -delete spellingbee -write (Join-Path $OutputRoot 'edited.prg') spellingbee 2>&1 | Out-Null
  388. $from = Invoke-Pwsh (Join-Path $dest 'tools/from-commodore.ps1') @()
  389. Assert ($from.Code -eq 0) "from-commodore exit $($from.Code)"
  390. Assert ((Get-Content -LiteralPath (Join-Path $dest 'src/main.bas') -Raw) -match '80 rem edited on the c64') 'edit not returned'
  391. & git -C $dest checkout -q -- src/main.bas
  392. 'line 80 arrived in src/main.bas'
  393. }
  394. Invoke-Check 'toolchain: a missing tool is reported with the first command and what stays unverified' {
  395. $src = $generated['c128-40-basic7']
  396. $dest = Join-Path $projects 'missing tool copy'
  397. Copy-Item -LiteralPath $src -Destination $dest -Recurse
  398. $pj = Join-Path $dest 'project.json'
  399. $p = Get-Content -LiteralPath $pj -Raw | ConvertFrom-Json -AsHashtable
  400. ($p.resolved.tools | Where-Object { $_.id -eq 'x128' }).executable = 'x128-not-installed'
  401. [IO.File]::WriteAllText($pj, ($p | ConvertTo-Json -Depth 64))
  402. $c = Invoke-Pwsh (Join-Path $dest 'tools/check-prereqs.ps1') @()
  403. Assert ($c.Code -eq 2) "check-prereqs exit $($c.Code)"
  404. 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)"
  405. $s = Invoke-Pwsh (Join-Path $dest 'tools/smoke.ps1') @()
  406. Assert ($s.Code -eq 2 -and $s.Text -match 'MISSING TOOL') "smoke exit $($s.Code)"
  407. 'exit 2 with install command'
  408. }
  409. }
  410. # ---------------------------------------------------------------------------
  411. $pass = @($Results | Where-Object Status -eq 'PASS').Count
  412. $fail = @($Results | Where-Object Status -eq 'FAIL').Count
  413. $skip = @($Results | Where-Object Status -eq 'SKIP').Count
  414. Write-Host ""
  415. Write-Host "Summary: $pass passed, $fail failed, $skip skipped. Output: $OutputRoot"
  416. $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 |', '|---|---|---|---|')
  417. $md += $Results | ForEach-Object { "| $($_.Status) | $($_.Name) | $(($_.Detail -replace '\|', '/' -replace '\r?\n', ' ').Substring(0, [Math]::Min(300, $_.Detail.Length))) | $($_.Seconds) |" }
  418. $md += ''; $md += '## Commands run'; $md += ''; $md += '```'; $md += $Log; $md += '```'
  419. $resultsDir = Join-Path $root 'tests/results'
  420. New-Item -ItemType Directory -Force -Path $resultsDir | Out-Null
  421. [IO.File]::WriteAllText((Join-Path $resultsDir 'latest.md'), ($md -join "`n"), [Text.UTF8Encoding]::new($false))
  422. if (-not $KeepOutput -and $fail -eq 0) { Remove-Item -LiteralPath $OutputRoot -Recurse -Force -ErrorAction SilentlyContinue }
  423. exit $(if ($fail) { 1 } else { 0 })

Powered by TurnKey Linux.