mirror of
https://github.com/game-ci/unity-builder.git
synced 2026-06-01 06:16:14 -07:00
Compare commits
30 Commits
temp/push-
...
release/ne
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc9332e6ab | ||
|
|
d53ed6c519 | ||
|
|
4b44327092 | ||
|
|
bf25e1fb84 | ||
|
|
0633ca96ea | ||
|
|
b774d7fef1 | ||
|
|
c0ca4b6b6c | ||
|
|
78a7f6cd2e | ||
|
|
676855fdaa | ||
|
|
db27f9108d | ||
|
|
4c91a33ee2 | ||
|
|
9f0c5b3f1a | ||
|
|
d81990cb7f | ||
|
|
8b0565569e | ||
|
|
708e6d796c | ||
|
|
8d670d7936 | ||
|
|
f42930df14 | ||
|
|
1e2bb889bf | ||
|
|
7c0c4c2072 | ||
|
|
7615bbd9dd | ||
|
|
5e54bcd4dd | ||
|
|
4870fb5a5c | ||
|
|
118671778f | ||
|
|
aa2e05d468 | ||
|
|
1bb31f3e98 | ||
|
|
ccbe1bcfbf | ||
|
|
3033ee0067 | ||
|
|
b3e1639029 | ||
|
|
49b37f7831 | ||
|
|
8d81236939 |
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"root": true,
|
||||
"plugins": ["jest", "@typescript-eslint", "prettier", "unicorn"],
|
||||
"extends": ["plugin:unicorn/recommended", "plugin:github/recommended", "plugin:prettier/recommended"],
|
||||
"parser": "@typescript-eslint/parser",
|
||||
|
||||
@@ -54,7 +54,7 @@ jobs:
|
||||
# AWS_STACK_NAME: game-ci-github-pipelines
|
||||
CHECKS_UPDATE: ${{ github.event.inputs.checksObject }}
|
||||
run: |
|
||||
git clone -b orchestrator-develop https://github.com/game-ci/unity-builder
|
||||
git clone -b main https://github.com/game-ci/unity-builder
|
||||
cd unity-builder
|
||||
yarn
|
||||
ls
|
||||
|
||||
203
.github/workflows/validate-community-plugins.yml
vendored
Normal file
203
.github/workflows/validate-community-plugins.yml
vendored
Normal file
@@ -0,0 +1,203 @@
|
||||
name: Validate Community Plugins
|
||||
|
||||
on:
|
||||
schedule:
|
||||
# Run weekly on Sunday at 02:00 UTC
|
||||
- cron: '0 2 * * 0'
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
plugin_filter:
|
||||
description: 'Filter plugins by name (regex pattern, empty = all)'
|
||||
required: false
|
||||
default: ''
|
||||
unity_version:
|
||||
description: 'Override Unity version (empty = use plugin default)'
|
||||
required: false
|
||||
default: ''
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
issues: write
|
||||
|
||||
jobs:
|
||||
load-plugins:
|
||||
name: Load Plugin Registry
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
matrix: ${{ steps.parse.outputs.matrix }}
|
||||
plugin_count: ${{ steps.parse.outputs.count }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Parse plugin registry
|
||||
id: parse
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
const yaml = require('js-yaml');
|
||||
|
||||
const registry = yaml.load(fs.readFileSync('community-plugins.yml', 'utf8'));
|
||||
let plugins = registry.plugins || [];
|
||||
|
||||
// Apply name filter if provided
|
||||
const filter = '${{ github.event.inputs.plugin_filter }}';
|
||||
if (filter) {
|
||||
const regex = new RegExp(filter, 'i');
|
||||
plugins = plugins.filter(p => regex.test(p.name));
|
||||
}
|
||||
|
||||
// Expand platform matrix
|
||||
const matrix = [];
|
||||
for (const plugin of plugins) {
|
||||
const platforms = plugin.platforms || ['StandaloneLinux64'];
|
||||
for (const platform of platforms) {
|
||||
matrix.push({
|
||||
name: plugin.name,
|
||||
package: plugin.package,
|
||||
source: plugin.source || 'git',
|
||||
unity: '${{ github.event.inputs.unity_version }}' || plugin.unity || '2021.3',
|
||||
platform: platform,
|
||||
timeout: plugin.timeout || 30
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
core.setOutput('matrix', JSON.stringify({ include: matrix }));
|
||||
core.setOutput('count', matrix.length);
|
||||
console.log(`Found ${matrix.length} plugin-platform combinations to validate`);
|
||||
|
||||
validate:
|
||||
name: '${{ matrix.name }} (${{ matrix.platform }})'
|
||||
needs: load-plugins
|
||||
if: needs.load-plugins.outputs.plugin_count > 0
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: ${{ fromJson(matrix.timeout) }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix: ${{ fromJson(needs.load-plugins.outputs.matrix) }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Create test project
|
||||
run: |
|
||||
mkdir -p test-project/Assets
|
||||
mkdir -p test-project/Packages
|
||||
mkdir -p test-project/ProjectSettings
|
||||
|
||||
# Create minimal manifest.json
|
||||
if [ "${{ matrix.source }}" = "git" ]; then
|
||||
cat > test-project/Packages/manifest.json << 'MANIFEST'
|
||||
{
|
||||
"dependencies": {
|
||||
"com.unity.modules.imgui": "1.0.0",
|
||||
"com.unity.modules.jsonserialize": "1.0.0"
|
||||
}
|
||||
}
|
||||
MANIFEST
|
||||
|
||||
# Add git package via manifest
|
||||
cd test-project
|
||||
cat Packages/manifest.json | python3 -c "
|
||||
import sys, json
|
||||
manifest = json.load(sys.stdin)
|
||||
manifest['dependencies']['${{ matrix.name }}'] = '${{ matrix.package }}'
|
||||
json.dump(manifest, sys.stdout, indent=2)
|
||||
" > Packages/manifest.tmp && mv Packages/manifest.tmp Packages/manifest.json
|
||||
cd ..
|
||||
fi
|
||||
|
||||
# Create minimal ProjectSettings
|
||||
cat > test-project/ProjectSettings/ProjectVersion.txt << EOF
|
||||
m_EditorVersion: ${{ matrix.unity }}
|
||||
EOF
|
||||
|
||||
- name: Build with unity-builder
|
||||
uses: ./
|
||||
id: build
|
||||
with:
|
||||
projectPath: test-project
|
||||
targetPlatform: ${{ matrix.platform }}
|
||||
unityVersion: ${{ matrix.unity }}
|
||||
continue-on-error: true
|
||||
|
||||
- name: Record result
|
||||
if: always()
|
||||
run: |
|
||||
STATUS="${{ steps.build.outcome }}"
|
||||
echo "## ${{ matrix.name }} — ${{ matrix.platform }}" >> $GITHUB_STEP_SUMMARY
|
||||
echo "" >> $GITHUB_STEP_SUMMARY
|
||||
if [ "$STATUS" = "success" ]; then
|
||||
echo "✅ **PASSED** — Compiled and built successfully" >> $GITHUB_STEP_SUMMARY
|
||||
else
|
||||
echo "❌ **FAILED** — Build or compilation failed" >> $GITHUB_STEP_SUMMARY
|
||||
fi
|
||||
echo "" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- Unity: ${{ matrix.unity }}" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- Platform: ${{ matrix.platform }}" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- Source: ${{ matrix.source }}" >> $GITHUB_STEP_SUMMARY
|
||||
echo "- Package: \`${{ matrix.package }}\`" >> $GITHUB_STEP_SUMMARY
|
||||
|
||||
report:
|
||||
name: Validation Report
|
||||
needs: [load-plugins, validate]
|
||||
if: always()
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Generate summary
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const { data: run } = await github.rest.actions.listJobsForWorkflowRun({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
run_id: context.runId
|
||||
});
|
||||
|
||||
const validateJobs = run.jobs.filter(j => j.name.startsWith('validate'));
|
||||
const passed = validateJobs.filter(j => j.conclusion === 'success').length;
|
||||
const failed = validateJobs.filter(j => j.conclusion === 'failure').length;
|
||||
const total = validateJobs.length;
|
||||
|
||||
let summary = `# Community Plugin Validation Report\n\n`;
|
||||
summary += `**${passed}/${total} passed** | ${failed} failed\n\n`;
|
||||
summary += `| Plugin | Platform | Status |\n|--------|----------|--------|\n`;
|
||||
|
||||
for (const job of validateJobs) {
|
||||
const icon = job.conclusion === 'success' ? '✅' : '❌';
|
||||
summary += `| ${job.name} | | ${icon} ${job.conclusion} |\n`;
|
||||
}
|
||||
|
||||
await core.summary.addRaw(summary).write();
|
||||
|
||||
// Create or update issue if there are failures
|
||||
if (failed > 0) {
|
||||
const title = `Community Plugin Validation: ${failed} failure(s) — ${new Date().toISOString().split('T')[0]}`;
|
||||
const body = summary + `\n\n[Workflow Run](${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId})`;
|
||||
|
||||
const { data: issues } = await github.rest.issues.listForRepo({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
state: 'open',
|
||||
labels: 'community-plugin-validation'
|
||||
});
|
||||
|
||||
if (issues.length > 0) {
|
||||
await github.rest.issues.createComment({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: issues[0].number,
|
||||
body: body
|
||||
});
|
||||
} else {
|
||||
await github.rest.issues.create({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
title: title,
|
||||
body: body,
|
||||
labels: ['community-plugin-validation']
|
||||
});
|
||||
}
|
||||
}
|
||||
92
action.yml
92
action.yml
@@ -182,8 +182,8 @@ inputs:
|
||||
required: false
|
||||
default: ''
|
||||
description:
|
||||
'[Orchestrator] Run a custom job instead of the standard build automation for orchestrator (in yaml format with the
|
||||
keys image, secrets (name, value object array), command line string)'
|
||||
'[Orchestrator] Run a custom job instead of the standard build automation for orchestrator (in yaml format with
|
||||
the keys image, secrets (name, value object array), command line string)'
|
||||
awsStackName:
|
||||
default: 'game-ci'
|
||||
required: false
|
||||
@@ -269,6 +269,28 @@ inputs:
|
||||
default: 'false'
|
||||
required: false
|
||||
description: 'Skip the activation/deactivation of Unity. This assumes Unity is already activated.'
|
||||
artifactOutputTypes:
|
||||
description: 'Comma-separated list of output types to collect (build, logs, test-results, coverage, images, metrics, data-export, server-build, custom)'
|
||||
required: false
|
||||
default: 'build,logs,test-results'
|
||||
artifactUploadTarget:
|
||||
description: 'Where to upload artifacts: github-artifacts, storage, local, none'
|
||||
required: false
|
||||
default: 'github-artifacts'
|
||||
artifactUploadPath:
|
||||
description: 'Destination path for artifact upload (storage URI or local path)'
|
||||
required: false
|
||||
artifactCompression:
|
||||
description: 'Compression for artifacts: none, gzip, lz4'
|
||||
required: false
|
||||
default: 'gzip'
|
||||
artifactRetentionDays:
|
||||
description: 'Retention period for uploaded artifacts in days'
|
||||
required: false
|
||||
default: '30'
|
||||
artifactCustomTypes:
|
||||
description: 'JSON string defining custom output types [{name, defaultPath, description}]'
|
||||
required: false
|
||||
cloneDepth:
|
||||
default: '50'
|
||||
required: false
|
||||
@@ -279,6 +301,70 @@ inputs:
|
||||
description:
|
||||
'[Orchestrator] Specifies the repo for the unity builder. Useful if you forked the repo for testing, features, or
|
||||
fixes.'
|
||||
testSuitePath:
|
||||
description: 'Path to YAML test suite definition file'
|
||||
required: false
|
||||
testSuiteEvent:
|
||||
description: 'CI event name for suite selection (pr, push, release)'
|
||||
required: false
|
||||
testTaxonomyPath:
|
||||
description: 'Path to custom taxonomy definition YAML'
|
||||
required: false
|
||||
testResultFormat:
|
||||
description: 'Test result output format: junit, json, or both'
|
||||
required: false
|
||||
default: 'junit'
|
||||
testResultPath:
|
||||
description: 'Directory for structured test result output'
|
||||
required: false
|
||||
default: './test-results'
|
||||
|
||||
hotRunnerEnabled:
|
||||
description: '[HotRunner] Use persistent hot runner for builds (requires pre-registered runners)'
|
||||
required: false
|
||||
default: 'false'
|
||||
hotRunnerTransport:
|
||||
description: '[HotRunner] Transport protocol for hot runner communication: websocket, grpc, named-pipe'
|
||||
required: false
|
||||
default: 'websocket'
|
||||
hotRunnerHost:
|
||||
description: '[HotRunner] Hot runner host address'
|
||||
required: false
|
||||
default: 'localhost'
|
||||
hotRunnerPort:
|
||||
description: '[HotRunner] Hot runner port number'
|
||||
required: false
|
||||
default: '9090'
|
||||
hotRunnerHealthInterval:
|
||||
description: '[HotRunner] Health check interval in seconds'
|
||||
required: false
|
||||
default: '30'
|
||||
hotRunnerMaxIdle:
|
||||
description: '[HotRunner] Maximum idle time in seconds before recycling runner'
|
||||
required: false
|
||||
default: '3600'
|
||||
hotRunnerFallbackToCold:
|
||||
description: '[HotRunner] Fall back to cold build if no hot runner available'
|
||||
required: false
|
||||
default: 'true'
|
||||
syncStrategy:
|
||||
description: 'Workspace sync strategy: full, git-delta, direct-input, storage-pull'
|
||||
required: false
|
||||
default: 'full'
|
||||
syncInputRef:
|
||||
description: 'URI for direct-input or storage-pull content (storage://remote/path or file path)'
|
||||
required: false
|
||||
syncStorageRemote:
|
||||
description: 'rclone remote name for storage-backed inputs (defaults to rcloneRemote)'
|
||||
required: false
|
||||
syncRevertAfter:
|
||||
description: 'Revert overlaid changes after job completion'
|
||||
required: false
|
||||
default: 'true'
|
||||
syncStatePath:
|
||||
description: 'Path to sync state file for delta tracking'
|
||||
required: false
|
||||
default: '.game-ci/sync-state.json'
|
||||
|
||||
outputs:
|
||||
volume:
|
||||
@@ -292,6 +378,8 @@ outputs:
|
||||
'Returns the exit code from the build scripts. This code is 0 if the build was successful. If there was an error
|
||||
during activation, the code is from the activation step. If activation is successful, the code is from the project
|
||||
build step.'
|
||||
artifactManifestPath:
|
||||
description: 'Path to the generated artifact manifest JSON file'
|
||||
branding:
|
||||
icon: 'box'
|
||||
color: 'gray-dark'
|
||||
|
||||
27
community-plugins.yml
Normal file
27
community-plugins.yml
Normal file
@@ -0,0 +1,27 @@
|
||||
# Community Plugin Validation Registry
|
||||
# Packages listed here are automatically tested on a schedule
|
||||
# to ensure compatibility with unity-builder.
|
||||
#
|
||||
# Format:
|
||||
# - name: Human-readable name
|
||||
# package: UPM package name or git URL
|
||||
# source: upm | git | asset-store
|
||||
# unity: Minimum Unity version (optional, defaults to 2021.3)
|
||||
# platforms: List of platforms to test (optional, defaults to [StandaloneLinux64])
|
||||
# timeout: Build timeout in minutes (optional, defaults to 30)
|
||||
|
||||
plugins:
|
||||
# Example entries — community members can submit PRs to add their packages
|
||||
- name: UniTask
|
||||
package: https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask
|
||||
source: git
|
||||
platforms: [StandaloneLinux64, StandaloneWindows64]
|
||||
|
||||
- name: NaughtyAttributes
|
||||
package: https://github.com/dbrizov/NaughtyAttributes.git?path=Assets/NaughtyAttributes
|
||||
source: git
|
||||
|
||||
- name: Unity Atoms
|
||||
package: https://github.com/unity-atoms/unity-atoms.git
|
||||
source: git
|
||||
platforms: [StandaloneLinux64]
|
||||
138
delete-me-update-all-integration-branches.ps1
Normal file
138
delete-me-update-all-integration-branches.ps1
Normal file
@@ -0,0 +1,138 @@
|
||||
# delete-me-update-all-integration-branches.ps1
|
||||
# Updates ALL integration branches from their component branches.
|
||||
# Run from any branch -- it will stash changes, update each integration branch, then return.
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
$originalBranch = git rev-parse --abbrev-ref HEAD
|
||||
$stashed = $false
|
||||
|
||||
# Stash any uncommitted changes
|
||||
$status = git status --porcelain
|
||||
if ($status) {
|
||||
Write-Host "Stashing uncommitted changes..." -ForegroundColor Cyan
|
||||
git stash push -m "auto-stash before integration branch update"
|
||||
$stashed = $true
|
||||
}
|
||||
|
||||
Write-Host "Fetching all branches from origin..." -ForegroundColor Cyan
|
||||
git fetch origin
|
||||
|
||||
$integrationBranches = @(
|
||||
@{
|
||||
Name = 'release/next-gen'
|
||||
Branches = @(
|
||||
'feature/test-workflow-engine'
|
||||
'feature/hot-runner-protocol'
|
||||
'feature/generic-artifact-system'
|
||||
'feature/incremental-sync-protocol'
|
||||
'feature/community-plugin-validation'
|
||||
'feature/cli-support'
|
||||
)
|
||||
}
|
||||
@{
|
||||
Name = 'release/lts-infrastructure'
|
||||
Branches = @(
|
||||
'feature/orchestrator-enterprise-support'
|
||||
'feature/cloud-run-azure-providers'
|
||||
'feature/provider-load-balancing'
|
||||
'feature/orchestrator-unit-tests'
|
||||
'fix/secure-git-token-usage'
|
||||
'feature/premade-secret-sources'
|
||||
'feature/ci-platform-providers'
|
||||
'feature/build-reliability'
|
||||
'ci/orchestrator-integrity-speedup'
|
||||
)
|
||||
}
|
||||
@{
|
||||
Name = 'release/lts-2.0.0'
|
||||
Branches = @(
|
||||
# Infrastructure
|
||||
'feature/orchestrator-enterprise-support'
|
||||
'feature/cloud-run-azure-providers'
|
||||
'feature/provider-load-balancing'
|
||||
'feature/orchestrator-unit-tests'
|
||||
'fix/secure-git-token-usage'
|
||||
'feature/premade-secret-sources'
|
||||
'feature/ci-platform-providers'
|
||||
'feature/build-reliability'
|
||||
'ci/orchestrator-integrity-speedup'
|
||||
# Next-gen
|
||||
'feature/test-workflow-engine'
|
||||
'feature/hot-runner-protocol'
|
||||
'feature/generic-artifact-system'
|
||||
'feature/incremental-sync-protocol'
|
||||
'feature/community-plugin-validation'
|
||||
'feature/cli-support'
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
foreach ($integration in $integrationBranches) {
|
||||
$name = $integration.Name
|
||||
Write-Host "`n========================================" -ForegroundColor Cyan
|
||||
Write-Host "Updating $name" -ForegroundColor Cyan
|
||||
Write-Host "========================================" -ForegroundColor Cyan
|
||||
|
||||
# Check if branch exists locally
|
||||
$exists = git branch --list $name
|
||||
if (-not $exists) {
|
||||
Write-Host "Creating local branch from origin/$name..." -ForegroundColor Yellow
|
||||
git checkout -b $name "origin/$name"
|
||||
} else {
|
||||
git checkout $name
|
||||
git pull origin $name --ff-only 2>$null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
git pull origin $name --no-edit
|
||||
}
|
||||
}
|
||||
|
||||
$failed = @()
|
||||
foreach ($branch in $integration.Branches) {
|
||||
$remoteBranch = "origin/$branch"
|
||||
# Check if remote branch exists
|
||||
$refExists = git rev-parse --verify $remoteBranch 2>$null
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host " Skipping $branch (not found on remote)" -ForegroundColor DarkGray
|
||||
continue
|
||||
}
|
||||
|
||||
# Check if already merged
|
||||
$mergeBase = git merge-base HEAD $remoteBranch 2>$null
|
||||
$remoteHead = git rev-parse $remoteBranch 2>$null
|
||||
if ($mergeBase -eq $remoteHead) {
|
||||
Write-Host " $branch - already up to date" -ForegroundColor DarkGray
|
||||
continue
|
||||
}
|
||||
|
||||
Write-Host " Merging $branch..." -ForegroundColor Yellow
|
||||
$result = git merge $remoteBranch --no-edit 2>&1
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host " CONFLICT - skipped (resolve manually)" -ForegroundColor Red
|
||||
$failed += $branch
|
||||
git merge --abort
|
||||
} else {
|
||||
Write-Host " OK" -ForegroundColor Green
|
||||
}
|
||||
}
|
||||
|
||||
if ($failed.Count -gt 0) {
|
||||
Write-Host "`n Conflicts in:" -ForegroundColor Red
|
||||
$failed | ForEach-Object { Write-Host " - $_" -ForegroundColor Red }
|
||||
}
|
||||
|
||||
# Push
|
||||
Write-Host " Pushing $name to origin..." -ForegroundColor Cyan
|
||||
git push origin $name
|
||||
}
|
||||
|
||||
# Return to original branch
|
||||
Write-Host "`nReturning to $originalBranch..." -ForegroundColor Cyan
|
||||
git checkout $originalBranch
|
||||
|
||||
if ($stashed) {
|
||||
Write-Host "Restoring stashed changes..." -ForegroundColor Cyan
|
||||
git stash pop
|
||||
}
|
||||
|
||||
Write-Host "`nDone!" -ForegroundColor Green
|
||||
49
delete-me-update-this-integration-branch.ps1
Normal file
49
delete-me-update-this-integration-branch.ps1
Normal file
@@ -0,0 +1,49 @@
|
||||
# delete-me-update-this-integration-branch.ps1
|
||||
# Run this script from the repo root while on the release/next-gen branch.
|
||||
# It merges the latest from each component branch to keep this integration branch current.
|
||||
# After running, review any conflicts, then commit and push.
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
$branchName = git rev-parse --abbrev-ref HEAD
|
||||
if ($branchName -ne 'release/next-gen') {
|
||||
Write-Error "Must be on release/next-gen branch. Currently on: $branchName"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Component branches for this integration branch
|
||||
$branches = @(
|
||||
'feature/test-workflow-engine'
|
||||
'feature/hot-runner-protocol'
|
||||
'feature/generic-artifact-system'
|
||||
'feature/incremental-sync-protocol'
|
||||
'feature/community-plugin-validation'
|
||||
'feature/cli-support'
|
||||
)
|
||||
|
||||
Write-Host "Fetching latest from origin..." -ForegroundColor Cyan
|
||||
git fetch origin
|
||||
|
||||
$failed = @()
|
||||
foreach ($branch in $branches) {
|
||||
Write-Host "`nMerging origin/$branch..." -ForegroundColor Yellow
|
||||
$result = git merge "origin/$branch" --no-edit 2>&1
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host " CONFLICT merging $branch - resolve manually" -ForegroundColor Red
|
||||
$failed += $branch
|
||||
# Abort this merge so we can continue with others
|
||||
git merge --abort
|
||||
} else {
|
||||
Write-Host " Merged successfully" -ForegroundColor Green
|
||||
}
|
||||
}
|
||||
|
||||
if ($failed.Count -gt 0) {
|
||||
Write-Host "`nThe following branches had conflicts and were skipped:" -ForegroundColor Red
|
||||
$failed | ForEach-Object { Write-Host " - $_" -ForegroundColor Red }
|
||||
Write-Host "`nRe-run after resolving, or merge them manually:" -ForegroundColor Yellow
|
||||
$failed | ForEach-Object { Write-Host " git merge origin/$_" -ForegroundColor Yellow }
|
||||
} else {
|
||||
Write-Host "`nAll branches merged successfully!" -ForegroundColor Green
|
||||
Write-Host "Run 'git push origin release/next-gen' to update the remote." -ForegroundColor Cyan
|
||||
}
|
||||
2985
dist/index.js
generated
vendored
2985
dist/index.js
generated
vendored
File diff suppressed because it is too large
Load Diff
2
dist/index.js.map
generated
vendored
2
dist/index.js.map
generated
vendored
File diff suppressed because one or more lines are too long
@@ -74,7 +74,7 @@
|
||||
"unity-changeset": "^3.1.0",
|
||||
"uuid": "^9.0.0",
|
||||
"yaml": "^2.2.2",
|
||||
"yargs": "^18.0.0"
|
||||
"yargs": "^17.7.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/base-64": "^1.0.0",
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import yargs from 'yargs';
|
||||
// eslint-disable-next-line import/no-unresolved
|
||||
import { hideBin } from 'yargs/helpers';
|
||||
import buildCommand from './cli/commands/build';
|
||||
import activateCommand from './cli/commands/activate';
|
||||
import orchestrateCommand from './cli/commands/orchestrate';
|
||||
import cacheCommand from './cli/commands/cache';
|
||||
import statusCommand from './cli/commands/status';
|
||||
import versionCommand from './cli/commands/version';
|
||||
import updateCommand from './cli/commands/update';
|
||||
@@ -18,7 +16,6 @@ const cli = yargs(hideBin(process.argv))
|
||||
.command(buildCommand)
|
||||
.command(activateCommand)
|
||||
.command(orchestrateCommand)
|
||||
.command(cacheCommand)
|
||||
.command(statusCommand)
|
||||
.command(versionCommand)
|
||||
.command(updateCommand)
|
||||
|
||||
@@ -38,7 +38,6 @@ describe('CLI integration', () => {
|
||||
expect(result.stdout).toContain('build');
|
||||
expect(result.stdout).toContain('activate');
|
||||
expect(result.stdout).toContain('orchestrate');
|
||||
expect(result.stdout).toContain('cache');
|
||||
expect(result.stdout).toContain('status');
|
||||
expect(result.stdout).toContain('version');
|
||||
expect(result.stdout).toContain('update');
|
||||
@@ -83,6 +82,7 @@ describe('CLI integration', () => {
|
||||
expect(result.code).toStrictEqual(0);
|
||||
expect(result.stdout).toContain('--target-platform');
|
||||
expect(result.stdout).toContain('--provider-strategy');
|
||||
expect(result.stdout).toContain('cache');
|
||||
});
|
||||
|
||||
it('exits 0 for activate --help', async () => {
|
||||
@@ -92,8 +92,8 @@ describe('CLI integration', () => {
|
||||
expect(result.stdout).toContain('activate');
|
||||
});
|
||||
|
||||
it('exits 0 for cache --help', async () => {
|
||||
const result = await runCli(['cache', '--help']);
|
||||
it('exits 0 for orchestrate cache --help', async () => {
|
||||
const result = await runCli(['orchestrate', 'cache', '--help']);
|
||||
|
||||
expect(result.code).toStrictEqual(0);
|
||||
expect(result.stdout).toContain('cache');
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import buildCommand from '../commands/build';
|
||||
import activateCommand from '../commands/activate';
|
||||
import orchestrateCommand from '../commands/orchestrate';
|
||||
import cacheCommand from '../commands/cache';
|
||||
import statusCommand from '../commands/status';
|
||||
import versionCommand from '../commands/version';
|
||||
import updateCommand from '../commands/update';
|
||||
@@ -13,6 +12,7 @@ function createFakeYargs(): { yargs: any; options: Record<string, any> } {
|
||||
positional: jest.fn(),
|
||||
example: jest.fn(),
|
||||
env: jest.fn(),
|
||||
command: jest.fn(),
|
||||
};
|
||||
|
||||
yargs.option.mockImplementation((name: string, config: any) => {
|
||||
@@ -27,6 +27,7 @@ function createFakeYargs(): { yargs: any; options: Record<string, any> } {
|
||||
});
|
||||
yargs.example.mockReturnValue(yargs);
|
||||
yargs.env.mockReturnValue(yargs);
|
||||
yargs.command.mockReturnValue(yargs);
|
||||
|
||||
return { yargs, options };
|
||||
}
|
||||
@@ -165,7 +166,6 @@ describe('CLI commands', () => {
|
||||
(orchestrateCommand.builder as Function)(yargs);
|
||||
|
||||
expect(options['target-platform']).toBeDefined();
|
||||
expect(options['target-platform'].demandOption).toStrictEqual(true);
|
||||
expect(options['provider-strategy']).toBeDefined();
|
||||
expect(options['provider-strategy'].default).toStrictEqual('aws');
|
||||
expect(options['aws-stack-name']).toBeDefined();
|
||||
@@ -175,23 +175,13 @@ describe('CLI commands', () => {
|
||||
expect(options['watch-to-end']).toBeDefined();
|
||||
expect(options['clone-depth']).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('cache command', () => {
|
||||
it('exports the correct command name', () => {
|
||||
expect(cacheCommand.command).toStrictEqual('cache <action>');
|
||||
});
|
||||
it('registers cache as a subcommand', () => {
|
||||
const { yargs } = createFakeYargs();
|
||||
|
||||
it('has a description', () => {
|
||||
expect(cacheCommand.describe).toBeTruthy();
|
||||
});
|
||||
(orchestrateCommand.builder as Function)(yargs);
|
||||
|
||||
it('has a builder function', () => {
|
||||
expect(typeof cacheCommand.builder).toStrictEqual('function');
|
||||
});
|
||||
|
||||
it('has a handler function', () => {
|
||||
expect(typeof cacheCommand.handler).toStrictEqual('function');
|
||||
expect(yargs.command).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -24,9 +24,9 @@ const cacheCommand: CommandModule = {
|
||||
description: 'Path to the Unity project',
|
||||
default: '.',
|
||||
})
|
||||
.example('game-ci cache list', 'List all cached workspaces')
|
||||
.example('game-ci cache restore --cache-dir ./my-cache', 'Restore a cached workspace')
|
||||
.example('game-ci cache clear', 'Clear all cached workspaces');
|
||||
.example('game-ci orchestrate cache list', 'List all cached workspaces')
|
||||
.example('game-ci orchestrate cache restore --cache-dir ./my-cache', 'Restore a cached workspace')
|
||||
.example('game-ci orchestrate cache clear', 'Clear all cached workspaces');
|
||||
},
|
||||
handler: async (cliArguments) => {
|
||||
const action = cliArguments.action as string;
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { CommandModule } from 'yargs';
|
||||
import * as core from '@actions/core';
|
||||
import { BuildParameters, ImageTag, Orchestrator } from '../../model';
|
||||
import { mapCliArgumentsToInput, CliArguments } from '../input-mapper';
|
||||
import cacheCommand from './cache';
|
||||
|
||||
interface OrchestrateArguments extends CliArguments {
|
||||
targetPlatform: string;
|
||||
@@ -10,14 +11,14 @@ interface OrchestrateArguments extends CliArguments {
|
||||
|
||||
const orchestrateCommand: CommandModule<object, OrchestrateArguments> = {
|
||||
command: 'orchestrate',
|
||||
describe: 'Run a build via orchestrator providers (AWS, Kubernetes, etc.)',
|
||||
describe: 'Orchestrator — remote builds, cache management, and provider tools',
|
||||
builder: (yargs) => {
|
||||
return yargs
|
||||
.command(cacheCommand)
|
||||
.option('target-platform', {
|
||||
alias: 'targetPlatform',
|
||||
type: 'string',
|
||||
description: 'Platform that the build should target',
|
||||
demandOption: true,
|
||||
})
|
||||
.option('provider-strategy', {
|
||||
alias: 'providerStrategy',
|
||||
@@ -188,6 +189,10 @@ const orchestrateCommand: CommandModule<object, OrchestrateArguments> = {
|
||||
},
|
||||
handler: async (cliArguments) => {
|
||||
try {
|
||||
if (!cliArguments.targetPlatform) {
|
||||
throw new Error('--target-platform is required for orchestrate builds. Run game-ci orchestrate --help.');
|
||||
}
|
||||
|
||||
mapCliArgumentsToInput(cliArguments);
|
||||
|
||||
const buildParameters = await BuildParameters.create();
|
||||
|
||||
229
src/index.ts
229
src/index.ts
@@ -1,8 +1,17 @@
|
||||
import * as core from '@actions/core';
|
||||
import path from 'node:path';
|
||||
import { Action, BuildParameters, Cache, Orchestrator, Docker, ImageTag, Output } from './model';
|
||||
import { Cli } from './model/cli/cli';
|
||||
import MacBuilder from './model/mac-builder';
|
||||
import PlatformSetup from './model/platform-setup';
|
||||
import { TestWorkflowService } from './model/orchestrator/services/test-workflow';
|
||||
import { HotRunnerService } from './model/orchestrator/services/hot-runner';
|
||||
import { HotRunnerConfig } from './model/orchestrator/services/hot-runner/hot-runner-types';
|
||||
import { OutputService } from './model/orchestrator/services/output/output-service';
|
||||
import { OutputTypeRegistry } from './model/orchestrator/services/output/output-type-registry';
|
||||
import { ArtifactUploadHandler } from './model/orchestrator/services/output/artifact-upload-handler';
|
||||
import { IncrementalSyncService } from './model/orchestrator/services/sync';
|
||||
import { SyncStrategy } from './model/orchestrator/services/sync/sync-state';
|
||||
|
||||
async function runMain() {
|
||||
try {
|
||||
@@ -17,21 +26,85 @@ async function runMain() {
|
||||
const { workspace, actionFolder } = Action;
|
||||
|
||||
const buildParameters = await BuildParameters.create();
|
||||
|
||||
// If a test suite path is provided, use the test workflow engine
|
||||
// instead of the standard build execution path
|
||||
if (buildParameters.testSuitePath) {
|
||||
core.info('[TestWorkflow] Test suite path detected, using test workflow engine');
|
||||
const results = await TestWorkflowService.executeTestSuite(buildParameters.testSuitePath, buildParameters);
|
||||
|
||||
const totalFailed = results.reduce((sum, r) => sum + r.failed, 0);
|
||||
if (totalFailed > 0) {
|
||||
core.setFailed(`Test workflow completed with ${totalFailed} failure(s)`);
|
||||
} else {
|
||||
core.info('[TestWorkflow] All test runs passed');
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const baseImage = new ImageTag(buildParameters);
|
||||
|
||||
let exitCode = -1;
|
||||
|
||||
if (buildParameters.providerStrategy === 'local') {
|
||||
// Hot runner path: attempt to use a persistent Unity editor instance
|
||||
if (buildParameters.hotRunnerEnabled) {
|
||||
core.info('[HotRunner] Hot runner mode enabled, attempting hot build...');
|
||||
|
||||
const hotRunnerConfig: HotRunnerConfig = {
|
||||
enabled: true,
|
||||
transport: buildParameters.hotRunnerTransport,
|
||||
host: buildParameters.hotRunnerHost,
|
||||
port: buildParameters.hotRunnerPort,
|
||||
healthCheckInterval: buildParameters.hotRunnerHealthInterval,
|
||||
maxIdleTime: buildParameters.hotRunnerMaxIdle,
|
||||
maxJobsBeforeRecycle: 0, // no automatic recycle by job count
|
||||
};
|
||||
|
||||
const hotRunnerService = new HotRunnerService();
|
||||
|
||||
try {
|
||||
await hotRunnerService.initialize(hotRunnerConfig);
|
||||
const result = await hotRunnerService.submitBuild(buildParameters, (output) => {
|
||||
core.info(output);
|
||||
});
|
||||
|
||||
exitCode = result.exitCode;
|
||||
core.info(`[HotRunner] Build completed with exit code ${exitCode}`);
|
||||
await hotRunnerService.shutdown();
|
||||
} catch (hotRunnerError) {
|
||||
await hotRunnerService.shutdown();
|
||||
|
||||
if (buildParameters.hotRunnerFallbackToCold) {
|
||||
core.warning(
|
||||
`[HotRunner] Hot runner failed: ${(hotRunnerError as Error).message}. Falling back to cold build.`,
|
||||
);
|
||||
exitCode = await runColdBuild(buildParameters, baseImage, workspace, actionFolder);
|
||||
} else {
|
||||
throw hotRunnerError;
|
||||
}
|
||||
}
|
||||
} else if (buildParameters.providerStrategy === 'local') {
|
||||
core.info('Building locally');
|
||||
await PlatformSetup.setup(buildParameters, actionFolder);
|
||||
exitCode =
|
||||
process.platform === 'darwin'
|
||||
? await MacBuilder.run(actionFolder)
|
||||
: await Docker.run(baseImage.toString(), {
|
||||
workspace,
|
||||
actionFolder,
|
||||
...buildParameters,
|
||||
});
|
||||
|
||||
// Apply incremental sync strategy before build
|
||||
const syncStrategy = buildParameters.syncStrategy as SyncStrategy;
|
||||
if (syncStrategy !== 'full') {
|
||||
core.info(`[Sync] Applying sync strategy: ${syncStrategy}`);
|
||||
await applySyncStrategy(buildParameters, workspace);
|
||||
}
|
||||
|
||||
exitCode = await runColdBuild(buildParameters, baseImage, workspace, actionFolder);
|
||||
|
||||
// Revert overlays after job completion if configured
|
||||
if (buildParameters.syncRevertAfter && syncStrategy !== 'full') {
|
||||
core.info('[Sync] Reverting overlay changes after job completion');
|
||||
try {
|
||||
await IncrementalSyncService.revertOverlays(workspace, buildParameters.syncStatePath);
|
||||
} catch (revertError) {
|
||||
core.warning(`[Sync] Overlay revert failed: ${(revertError as Error).message}`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
await Orchestrator.run(buildParameters, baseImage.toString());
|
||||
exitCode = 0;
|
||||
@@ -42,6 +115,64 @@ async function runMain() {
|
||||
await Output.setAndroidVersionCode(buildParameters.androidVersionCode);
|
||||
await Output.setEngineExitCode(exitCode);
|
||||
|
||||
// Artifact collection and upload (runs on both success and failure)
|
||||
try {
|
||||
// Register custom output types if provided
|
||||
if (buildParameters.artifactCustomTypes) {
|
||||
try {
|
||||
const customTypes = JSON.parse(buildParameters.artifactCustomTypes);
|
||||
if (Array.isArray(customTypes)) {
|
||||
for (const ct of customTypes) {
|
||||
OutputTypeRegistry.registerType({
|
||||
name: ct.name,
|
||||
defaultPath: ct.defaultPath || ct.pattern || `./${ct.name}/`,
|
||||
description: ct.description || `Custom output type: ${ct.name}`,
|
||||
builtIn: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (parseError) {
|
||||
core.warning(`Failed to parse artifactCustomTypes: ${(parseError as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Collect outputs and generate manifest
|
||||
const manifestPath = path.join(buildParameters.projectPath, 'output-manifest.json');
|
||||
const manifest = await OutputService.collectOutputs(
|
||||
buildParameters.projectPath,
|
||||
buildParameters.buildGuid,
|
||||
buildParameters.artifactOutputTypes,
|
||||
manifestPath,
|
||||
);
|
||||
|
||||
core.setOutput('artifactManifestPath', manifestPath);
|
||||
|
||||
// Upload artifacts
|
||||
const uploadConfig = ArtifactUploadHandler.parseConfig(
|
||||
buildParameters.artifactUploadTarget,
|
||||
buildParameters.artifactUploadPath || undefined,
|
||||
buildParameters.artifactCompression,
|
||||
buildParameters.artifactRetentionDays,
|
||||
);
|
||||
|
||||
const uploadResult = await ArtifactUploadHandler.uploadArtifacts(
|
||||
manifest,
|
||||
uploadConfig,
|
||||
buildParameters.projectPath,
|
||||
);
|
||||
|
||||
if (!uploadResult.success) {
|
||||
core.warning(
|
||||
`Artifact upload completed with errors: ${uploadResult.entries
|
||||
.filter((e) => !e.success)
|
||||
.map((e) => `${e.type}: ${e.error}`)
|
||||
.join('; ')}`,
|
||||
);
|
||||
}
|
||||
} catch (artifactError) {
|
||||
core.warning(`Artifact collection/upload failed: ${(artifactError as Error).message}`);
|
||||
}
|
||||
|
||||
if (exitCode !== 0) {
|
||||
core.setFailed(`Build failed with exit code ${exitCode}`);
|
||||
}
|
||||
@@ -50,4 +181,82 @@ async function runMain() {
|
||||
}
|
||||
}
|
||||
|
||||
async function runColdBuild(
|
||||
buildParameters: BuildParameters,
|
||||
baseImage: ImageTag,
|
||||
workspace: string,
|
||||
actionFolder: string,
|
||||
): Promise<number> {
|
||||
if (buildParameters.providerStrategy === 'local') {
|
||||
core.info('Building locally');
|
||||
await PlatformSetup.setup(buildParameters, actionFolder);
|
||||
|
||||
return process.platform === 'darwin'
|
||||
? await MacBuilder.run(actionFolder)
|
||||
: await Docker.run(baseImage.toString(), {
|
||||
workspace,
|
||||
actionFolder,
|
||||
...buildParameters,
|
||||
});
|
||||
} else {
|
||||
await Orchestrator.run(buildParameters, baseImage.toString());
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the configured sync strategy to the workspace before build.
|
||||
*/
|
||||
async function applySyncStrategy(buildParameters: BuildParameters, workspace: string): Promise<void> {
|
||||
const strategy = buildParameters.syncStrategy as SyncStrategy;
|
||||
const resolvedStrategy = IncrementalSyncService.resolveStrategy(strategy, workspace, buildParameters.syncStatePath);
|
||||
|
||||
if (resolvedStrategy === 'full') {
|
||||
core.info('[Sync] Resolved to full sync (no incremental state available)');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
switch (resolvedStrategy) {
|
||||
case 'git-delta': {
|
||||
const targetReference = buildParameters.gitSha || buildParameters.branch;
|
||||
const changedFiles = await IncrementalSyncService.syncGitDelta(
|
||||
workspace,
|
||||
targetReference,
|
||||
buildParameters.syncStatePath,
|
||||
);
|
||||
core.info(`[Sync] Git delta sync applied: ${changedFiles} file(s) changed`);
|
||||
break;
|
||||
}
|
||||
case 'direct-input': {
|
||||
if (!buildParameters.syncInputRef) {
|
||||
throw new Error('[Sync] direct-input strategy requires syncInputRef to be set');
|
||||
}
|
||||
const overlays = await IncrementalSyncService.applyDirectInput(
|
||||
workspace,
|
||||
buildParameters.syncInputRef,
|
||||
buildParameters.syncStorageRemote || undefined,
|
||||
buildParameters.syncStatePath,
|
||||
);
|
||||
core.info(`[Sync] Direct input applied: ${overlays.length} overlay(s)`);
|
||||
break;
|
||||
}
|
||||
case 'storage-pull': {
|
||||
if (!buildParameters.syncInputRef) {
|
||||
throw new Error('[Sync] storage-pull strategy requires syncInputRef to be set');
|
||||
}
|
||||
const pulledFiles = await IncrementalSyncService.syncStoragePull(workspace, buildParameters.syncInputRef, {
|
||||
rcloneRemote: buildParameters.syncStorageRemote || undefined,
|
||||
syncRevertAfter: buildParameters.syncRevertAfter,
|
||||
statePath: buildParameters.syncStatePath,
|
||||
});
|
||||
core.info(`[Sync] Storage pull complete: ${pulledFiles.length} file(s)`);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
core.warning(`[Sync] Unknown sync strategy: ${resolvedStrategy}`);
|
||||
}
|
||||
}
|
||||
|
||||
runMain();
|
||||
|
||||
@@ -106,6 +106,32 @@ class BuildParameters {
|
||||
public cacheUnityInstallationOnMac!: boolean;
|
||||
public unityHubVersionOnMac!: string;
|
||||
public dockerWorkspacePath!: string;
|
||||
public hotRunnerEnabled!: boolean;
|
||||
public hotRunnerTransport!: 'websocket' | 'grpc' | 'named-pipe';
|
||||
public hotRunnerHost!: string;
|
||||
public hotRunnerPort!: number;
|
||||
public hotRunnerHealthInterval!: number;
|
||||
public hotRunnerMaxIdle!: number;
|
||||
public hotRunnerFallbackToCold!: boolean;
|
||||
|
||||
public testSuitePath!: string;
|
||||
public testSuiteEvent!: string;
|
||||
public testTaxonomyPath!: string;
|
||||
public testResultFormat!: string;
|
||||
public testResultPath!: string;
|
||||
|
||||
public artifactOutputTypes!: string;
|
||||
public artifactUploadTarget!: string;
|
||||
public artifactUploadPath!: string;
|
||||
public artifactCompression!: string;
|
||||
public artifactRetentionDays!: string;
|
||||
public artifactCustomTypes!: string;
|
||||
|
||||
public syncStrategy!: string;
|
||||
public syncInputRef!: string;
|
||||
public syncStorageRemote!: string;
|
||||
public syncRevertAfter!: boolean;
|
||||
public syncStatePath!: string;
|
||||
|
||||
public static shouldUseRetainedWorkspaceMode(buildParameters: BuildParameters) {
|
||||
return buildParameters.maxRetainedWorkspaces > 0 && Orchestrator.lockedWorkspace !== ``;
|
||||
@@ -242,6 +268,29 @@ class BuildParameters {
|
||||
cacheUnityInstallationOnMac: Input.cacheUnityInstallationOnMac,
|
||||
unityHubVersionOnMac: Input.unityHubVersionOnMac,
|
||||
dockerWorkspacePath: Input.dockerWorkspacePath,
|
||||
testSuitePath: Input.testSuitePath,
|
||||
testSuiteEvent: Input.testSuiteEvent,
|
||||
testTaxonomyPath: Input.testTaxonomyPath,
|
||||
testResultFormat: Input.testResultFormat,
|
||||
testResultPath: Input.testResultPath,
|
||||
hotRunnerEnabled: Input.hotRunnerEnabled,
|
||||
hotRunnerTransport: Input.hotRunnerTransport,
|
||||
hotRunnerHost: Input.hotRunnerHost,
|
||||
hotRunnerPort: Input.hotRunnerPort,
|
||||
hotRunnerHealthInterval: Input.hotRunnerHealthInterval,
|
||||
hotRunnerMaxIdle: Input.hotRunnerMaxIdle,
|
||||
hotRunnerFallbackToCold: Input.hotRunnerFallbackToCold,
|
||||
artifactOutputTypes: Input.artifactOutputTypes,
|
||||
artifactUploadTarget: Input.artifactUploadTarget,
|
||||
artifactUploadPath: Input.artifactUploadPath,
|
||||
artifactCompression: Input.artifactCompression,
|
||||
artifactRetentionDays: Input.artifactRetentionDays,
|
||||
artifactCustomTypes: Input.artifactCustomTypes,
|
||||
syncStrategy: Input.syncStrategy,
|
||||
syncInputRef: Input.syncInputRef,
|
||||
syncStorageRemote: Input.syncStorageRemote,
|
||||
syncRevertAfter: Input.syncRevertAfter,
|
||||
syncStatePath: Input.syncStatePath,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -241,6 +241,28 @@ class Input {
|
||||
return Input.getInput('dockerWorkspacePath') ?? '/github/workspace';
|
||||
}
|
||||
|
||||
static get syncStrategy(): string {
|
||||
return Input.getInput('syncStrategy') ?? 'full';
|
||||
}
|
||||
|
||||
static get syncInputRef(): string {
|
||||
return Input.getInput('syncInputRef') ?? '';
|
||||
}
|
||||
|
||||
static get syncStorageRemote(): string {
|
||||
return Input.getInput('syncStorageRemote') ?? '';
|
||||
}
|
||||
|
||||
static get syncRevertAfter(): boolean {
|
||||
const input = Input.getInput('syncRevertAfter') ?? 'true';
|
||||
|
||||
return input === 'true';
|
||||
}
|
||||
|
||||
static get syncStatePath(): string {
|
||||
return Input.getInput('syncStatePath') ?? '.game-ci/sync-state.json';
|
||||
}
|
||||
|
||||
static get dockerCpuLimit(): string {
|
||||
return Input.getInput('dockerCpuLimit') ?? os.cpus().length.toString();
|
||||
}
|
||||
@@ -278,10 +300,86 @@ class Input {
|
||||
return Input.getInput('containerRegistryImageVersion') ?? '3';
|
||||
}
|
||||
|
||||
static get artifactOutputTypes(): string {
|
||||
return Input.getInput('artifactOutputTypes') ?? 'build,logs,test-results';
|
||||
}
|
||||
|
||||
static get artifactUploadTarget(): string {
|
||||
return Input.getInput('artifactUploadTarget') ?? 'github-artifacts';
|
||||
}
|
||||
|
||||
static get artifactUploadPath(): string {
|
||||
return Input.getInput('artifactUploadPath') ?? '';
|
||||
}
|
||||
|
||||
static get artifactCompression(): string {
|
||||
return Input.getInput('artifactCompression') ?? 'gzip';
|
||||
}
|
||||
|
||||
static get artifactRetentionDays(): string {
|
||||
return Input.getInput('artifactRetentionDays') ?? '30';
|
||||
}
|
||||
|
||||
static get artifactCustomTypes(): string {
|
||||
return Input.getInput('artifactCustomTypes') ?? '';
|
||||
}
|
||||
|
||||
static get skipActivation(): string {
|
||||
return Input.getInput('skipActivation')?.toLowerCase() ?? 'false';
|
||||
}
|
||||
|
||||
static get testSuitePath(): string {
|
||||
return Input.getInput('testSuitePath') ?? '';
|
||||
}
|
||||
|
||||
static get testSuiteEvent(): string {
|
||||
return Input.getInput('testSuiteEvent') ?? '';
|
||||
}
|
||||
|
||||
static get testTaxonomyPath(): string {
|
||||
return Input.getInput('testTaxonomyPath') ?? '';
|
||||
}
|
||||
|
||||
static get testResultFormat(): string {
|
||||
return Input.getInput('testResultFormat') ?? 'junit';
|
||||
}
|
||||
|
||||
static get testResultPath(): string {
|
||||
return Input.getInput('testResultPath') ?? './test-results';
|
||||
}
|
||||
|
||||
static get hotRunnerEnabled(): boolean {
|
||||
const input = Input.getInput('hotRunnerEnabled') ?? false;
|
||||
|
||||
return input === 'true';
|
||||
}
|
||||
|
||||
static get hotRunnerTransport(): 'websocket' | 'grpc' | 'named-pipe' {
|
||||
return (Input.getInput('hotRunnerTransport') ?? 'websocket') as 'websocket' | 'grpc' | 'named-pipe';
|
||||
}
|
||||
|
||||
static get hotRunnerHost(): string {
|
||||
return Input.getInput('hotRunnerHost') ?? 'localhost';
|
||||
}
|
||||
|
||||
static get hotRunnerPort(): number {
|
||||
return Number.parseInt(Input.getInput('hotRunnerPort') ?? '9090', 10);
|
||||
}
|
||||
|
||||
static get hotRunnerHealthInterval(): number {
|
||||
return Number.parseInt(Input.getInput('hotRunnerHealthInterval') ?? '30', 10);
|
||||
}
|
||||
|
||||
static get hotRunnerMaxIdle(): number {
|
||||
return Number.parseInt(Input.getInput('hotRunnerMaxIdle') ?? '3600', 10);
|
||||
}
|
||||
|
||||
static get hotRunnerFallbackToCold(): boolean {
|
||||
const input = Input.getInput('hotRunnerFallbackToCold') ?? 'true';
|
||||
|
||||
return input === 'true';
|
||||
}
|
||||
|
||||
public static ToEnvVarFormat(input: string) {
|
||||
if (input.toUpperCase() === input) {
|
||||
return input;
|
||||
|
||||
162
src/model/orchestrator/options/orchestrator-folders.test.ts
Normal file
162
src/model/orchestrator/options/orchestrator-folders.test.ts
Normal file
@@ -0,0 +1,162 @@
|
||||
import { OrchestratorFolders } from './orchestrator-folders';
|
||||
|
||||
// Mock Orchestrator
|
||||
jest.mock('../orchestrator', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
buildParameters: {
|
||||
buildGuid: 'test-guid-abc',
|
||||
cacheKey: 'my-cache-key',
|
||||
projectPath: 'test-project',
|
||||
buildPath: 'Builds',
|
||||
maxRetainedWorkspaces: 0,
|
||||
gitPrivateToken: 'ghp_test123',
|
||||
gitAuthMode: 'url',
|
||||
orchestratorRepoName: 'game-ci/unity-builder',
|
||||
githubRepo: 'user/my-game',
|
||||
},
|
||||
lockedWorkspace: '',
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('../../build-parameters', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
shouldUseRetainedWorkspaceMode: jest.fn().mockReturnValue(false),
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('./orchestrator-options', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
useSharedBuilder: false,
|
||||
},
|
||||
}));
|
||||
|
||||
// Normalize paths for cross-platform test compatibility
|
||||
const normalize = (p: string) => p.replace(/\\/g, '/');
|
||||
|
||||
describe('OrchestratorFolders', () => {
|
||||
describe('static constants', () => {
|
||||
it('repositoryFolder is "repo"', () => {
|
||||
expect(OrchestratorFolders.repositoryFolder).toBe('repo');
|
||||
});
|
||||
|
||||
it('buildVolumeFolder is "data"', () => {
|
||||
expect(OrchestratorFolders.buildVolumeFolder).toBe('data');
|
||||
});
|
||||
|
||||
it('cacheFolder is "cache"', () => {
|
||||
expect(OrchestratorFolders.cacheFolder).toBe('cache');
|
||||
});
|
||||
});
|
||||
|
||||
describe('ToLinuxFolder', () => {
|
||||
it('converts backslashes to forward slashes', () => {
|
||||
expect(OrchestratorFolders.ToLinuxFolder('C:\\Users\\test\\project')).toBe('C:/Users/test/project');
|
||||
});
|
||||
|
||||
it('preserves forward slashes', () => {
|
||||
expect(OrchestratorFolders.ToLinuxFolder('/home/user/project')).toBe('/home/user/project');
|
||||
});
|
||||
|
||||
it('handles mixed slashes', () => {
|
||||
expect(OrchestratorFolders.ToLinuxFolder('some/path\\mixed/slashes\\here')).toBe('some/path/mixed/slashes/here');
|
||||
});
|
||||
|
||||
it('handles empty string', () => {
|
||||
expect(OrchestratorFolders.ToLinuxFolder('')).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('path computations (non-retained workspace mode)', () => {
|
||||
it('uniqueOrchestratorJobFolderAbsolute uses buildGuid', () => {
|
||||
const result = normalize(OrchestratorFolders.uniqueOrchestratorJobFolderAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc');
|
||||
});
|
||||
|
||||
it('cacheFolderForAllFull returns /data/cache', () => {
|
||||
const result = normalize(OrchestratorFolders.cacheFolderForAllFull);
|
||||
expect(result).toBe('/data/cache');
|
||||
});
|
||||
|
||||
it('cacheFolderForCacheKeyFull includes cache key', () => {
|
||||
const result = normalize(OrchestratorFolders.cacheFolderForCacheKeyFull);
|
||||
expect(result).toBe('/data/cache/my-cache-key');
|
||||
});
|
||||
|
||||
it('repoPathAbsolute is under job folder', () => {
|
||||
const result = normalize(OrchestratorFolders.repoPathAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/repo');
|
||||
});
|
||||
|
||||
it('projectPathAbsolute includes project path', () => {
|
||||
const result = normalize(OrchestratorFolders.projectPathAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/repo/test-project');
|
||||
});
|
||||
|
||||
it('libraryFolderAbsolute is under project path', () => {
|
||||
const result = normalize(OrchestratorFolders.libraryFolderAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/repo/test-project/Library');
|
||||
});
|
||||
|
||||
it('projectBuildFolderAbsolute uses buildPath', () => {
|
||||
const result = normalize(OrchestratorFolders.projectBuildFolderAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/repo/Builds');
|
||||
});
|
||||
|
||||
it('lfsFolderAbsolute is under .git/lfs', () => {
|
||||
const result = normalize(OrchestratorFolders.lfsFolderAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/repo/.git/lfs');
|
||||
});
|
||||
|
||||
it('lfsCacheFolderFull is under cache key', () => {
|
||||
const result = normalize(OrchestratorFolders.lfsCacheFolderFull);
|
||||
expect(result).toBe('/data/cache/my-cache-key/lfs');
|
||||
});
|
||||
|
||||
it('libraryCacheFolderFull is under cache key', () => {
|
||||
const result = normalize(OrchestratorFolders.libraryCacheFolderFull);
|
||||
expect(result).toBe('/data/cache/my-cache-key/Library');
|
||||
});
|
||||
});
|
||||
|
||||
describe('builderPathAbsolute', () => {
|
||||
it('uses job folder when shared builder is disabled', () => {
|
||||
const result = normalize(OrchestratorFolders.builderPathAbsolute);
|
||||
expect(result).toBe('/data/test-guid-abc/builder');
|
||||
});
|
||||
});
|
||||
|
||||
describe('repo URLs', () => {
|
||||
it('unityBuilderRepoUrl includes token and repo name', () => {
|
||||
const url = OrchestratorFolders.unityBuilderRepoUrl;
|
||||
expect(url).toBe('https://ghp_test123@github.com/game-ci/unity-builder.git');
|
||||
});
|
||||
|
||||
it('targetBuildRepoUrl includes token and github repo', () => {
|
||||
const url = OrchestratorFolders.targetBuildRepoUrl;
|
||||
expect(url).toBe('https://ghp_test123@github.com/user/my-game.git');
|
||||
});
|
||||
});
|
||||
|
||||
describe('purgeRemoteCaching', () => {
|
||||
it('returns false when env var is not set', () => {
|
||||
const original = process.env.PURGE_REMOTE_BUILDER_CACHE;
|
||||
delete process.env.PURGE_REMOTE_BUILDER_CACHE;
|
||||
expect(OrchestratorFolders.purgeRemoteCaching).toBe(false);
|
||||
if (original !== undefined) process.env.PURGE_REMOTE_BUILDER_CACHE = original;
|
||||
});
|
||||
|
||||
it('returns true when env var is set', () => {
|
||||
const original = process.env.PURGE_REMOTE_BUILDER_CACHE;
|
||||
process.env.PURGE_REMOTE_BUILDER_CACHE = 'true';
|
||||
expect(OrchestratorFolders.purgeRemoteCaching).toBe(true);
|
||||
if (original !== undefined) {
|
||||
process.env.PURGE_REMOTE_BUILDER_CACHE = original;
|
||||
} else {
|
||||
delete process.env.PURGE_REMOTE_BUILDER_CACHE;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
187
src/model/orchestrator/providers/ansible/index.ts
Normal file
187
src/model/orchestrator/providers/ansible/index.ts
Normal file
@@ -0,0 +1,187 @@
|
||||
import BuildParameters from '../../../build-parameters';
|
||||
import { OrchestratorSystem } from '../../services/core/orchestrator-system';
|
||||
import OrchestratorEnvironmentVariable from '../../options/orchestrator-environment-variable';
|
||||
import OrchestratorLogger from '../../services/core/orchestrator-logger';
|
||||
import { ProviderInterface } from '../provider-interface';
|
||||
import OrchestratorSecret from '../../options/orchestrator-secret';
|
||||
import { ProviderResource } from '../provider-resource';
|
||||
import { ProviderWorkflow } from '../provider-workflow';
|
||||
|
||||
/**
|
||||
* Ansible provider — executes Unity builds via Ansible playbooks
|
||||
* against managed inventory.
|
||||
*
|
||||
* Use case: Teams with existing Ansible infrastructure for server
|
||||
* management who want to leverage their inventory for build distribution.
|
||||
*/
|
||||
class AnsibleProvider implements ProviderInterface {
|
||||
private buildParameters: BuildParameters;
|
||||
private inventory: string;
|
||||
private playbook: string;
|
||||
private extraVariables: string;
|
||||
private vaultPassword: string;
|
||||
|
||||
constructor(buildParameters: BuildParameters) {
|
||||
this.buildParameters = buildParameters;
|
||||
this.inventory = buildParameters.ansibleInventory || '';
|
||||
this.playbook = buildParameters.ansiblePlaybook || '';
|
||||
this.extraVariables = buildParameters.ansibleExtraVars || '';
|
||||
this.vaultPassword = buildParameters.ansibleVaultPassword || '';
|
||||
}
|
||||
|
||||
async setupWorkflow(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildGuid: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildParameters: BuildParameters,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
branchName: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[],
|
||||
): Promise<void> {
|
||||
OrchestratorLogger.log(`[Ansible] Setting up playbook execution`);
|
||||
|
||||
if (!this.inventory) {
|
||||
throw new Error('ansibleInventory is required for the ansible provider');
|
||||
}
|
||||
|
||||
// Verify ansible is available
|
||||
try {
|
||||
const version = await OrchestratorSystem.Run('ansible --version | head -1');
|
||||
OrchestratorLogger.log(`[Ansible] ${version.trim()}`);
|
||||
} catch (error: any) {
|
||||
throw new Error(`Ansible not found on PATH: ${error.message || error}`);
|
||||
}
|
||||
|
||||
// Verify inventory exists
|
||||
try {
|
||||
await OrchestratorSystem.Run(`test -e "${this.inventory}"`);
|
||||
} catch {
|
||||
throw new Error(`Inventory not found: ${this.inventory}`);
|
||||
}
|
||||
}
|
||||
|
||||
async runTaskInWorkflow(
|
||||
buildGuid: string,
|
||||
image: string,
|
||||
commands: string,
|
||||
mountdir: string,
|
||||
workingdir: string,
|
||||
environment: OrchestratorEnvironmentVariable[],
|
||||
secrets: OrchestratorSecret[],
|
||||
): Promise<string> {
|
||||
OrchestratorLogger.log(`[Ansible] Running playbook against inventory ${this.inventory}`);
|
||||
|
||||
if (!this.playbook) {
|
||||
throw new Error(
|
||||
'ansiblePlaybook is required — no default playbook is provided yet. ' +
|
||||
'Provide a playbook that accepts build_guid, build_image, build_commands, mount_dir, and working_dir variables.',
|
||||
);
|
||||
}
|
||||
|
||||
// Build extra-vars JSON
|
||||
// These use snake_case because they are Ansible variable names passed to playbooks
|
||||
const playbookVariables: Record<string, string> = {
|
||||
// eslint-disable-next-line camelcase
|
||||
build_guid: buildGuid,
|
||||
// eslint-disable-next-line camelcase
|
||||
build_image: image,
|
||||
// eslint-disable-next-line camelcase
|
||||
build_commands: commands,
|
||||
// eslint-disable-next-line camelcase
|
||||
mount_dir: mountdir,
|
||||
// eslint-disable-next-line camelcase
|
||||
working_dir: workingdir,
|
||||
};
|
||||
|
||||
for (const element of environment) {
|
||||
playbookVariables[element.name.toLowerCase()] = element.value;
|
||||
}
|
||||
|
||||
// Merge user-provided extra vars
|
||||
if (this.extraVariables) {
|
||||
try {
|
||||
const userVariables = JSON.parse(this.extraVariables);
|
||||
Object.assign(playbookVariables, userVariables);
|
||||
} catch {
|
||||
OrchestratorLogger.logWarning(`[Ansible] Failed to parse ansibleExtraVars as JSON, using as-is`);
|
||||
}
|
||||
}
|
||||
|
||||
const extraVariablesJson = JSON.stringify(playbookVariables).replace(/'/g, "'\\''");
|
||||
|
||||
// Build ansible-playbook command
|
||||
const commandParts = [
|
||||
'ansible-playbook',
|
||||
`-i "${this.inventory}"`,
|
||||
`"${this.playbook}"`,
|
||||
`-e '${extraVariablesJson}'`,
|
||||
'--no-color',
|
||||
];
|
||||
|
||||
if (this.vaultPassword) {
|
||||
commandParts.push(`--vault-password-file "${this.vaultPassword}"`);
|
||||
}
|
||||
|
||||
// Add secret variables as extra environment
|
||||
const environmentPrefix = secrets
|
||||
.map((secret) => `${secret.EnvironmentVariable}='${secret.ParameterValue}'`)
|
||||
.join(' ');
|
||||
|
||||
const fullCommand = environmentPrefix ? `${environmentPrefix} ${commandParts.join(' ')}` : commandParts.join(' ');
|
||||
|
||||
try {
|
||||
const output = await OrchestratorSystem.Run(fullCommand);
|
||||
OrchestratorLogger.log(`[Ansible] Playbook completed successfully`);
|
||||
|
||||
return output;
|
||||
} catch (error: any) {
|
||||
OrchestratorLogger.logWarning(`[Ansible] Playbook failed: ${error.message || error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async cleanupWorkflow(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildParameters: BuildParameters,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
branchName: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[],
|
||||
): Promise<void> {
|
||||
OrchestratorLogger.log(`[Ansible] Cleanup complete`);
|
||||
}
|
||||
|
||||
async garbageCollect(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
filter: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
previewOnly: boolean,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
olderThan: Number,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
fullCache: boolean,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
baseDependencies: boolean,
|
||||
): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
async listResources(): Promise<ProviderResource[]> {
|
||||
if (!this.inventory) return [];
|
||||
|
||||
const resource = new ProviderResource();
|
||||
resource.Name = this.inventory;
|
||||
|
||||
return [resource];
|
||||
}
|
||||
|
||||
async listWorkflow(): Promise<ProviderWorkflow[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async watchWorkflow(): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
export default AnsibleProvider;
|
||||
267
src/model/orchestrator/providers/github-actions/index.ts
Normal file
267
src/model/orchestrator/providers/github-actions/index.ts
Normal file
@@ -0,0 +1,267 @@
|
||||
import BuildParameters from '../../../build-parameters';
|
||||
import { OrchestratorSystem } from '../../services/core/orchestrator-system';
|
||||
import OrchestratorEnvironmentVariable from '../../options/orchestrator-environment-variable';
|
||||
import OrchestratorLogger from '../../services/core/orchestrator-logger';
|
||||
import { ProviderInterface } from '../provider-interface';
|
||||
import OrchestratorSecret from '../../options/orchestrator-secret';
|
||||
import { ProviderResource } from '../provider-resource';
|
||||
import { ProviderWorkflow } from '../provider-workflow';
|
||||
|
||||
/**
|
||||
* GitHub Actions provider — triggers builds as workflow_dispatch events
|
||||
* on a target repository via the GitHub API.
|
||||
*
|
||||
* Use case: Distribute builds across orgs, use specialized runner pools,
|
||||
* or trigger builds in repos with Unity licenses.
|
||||
*/
|
||||
class GitHubActionsProvider implements ProviderInterface {
|
||||
private buildParameters: BuildParameters;
|
||||
private repo: string;
|
||||
private workflow: string;
|
||||
private token: string;
|
||||
private ref: string;
|
||||
private runId: number = 0;
|
||||
|
||||
constructor(buildParameters: BuildParameters) {
|
||||
this.buildParameters = buildParameters;
|
||||
this.repo = buildParameters.githubActionsRepo || '';
|
||||
this.workflow = buildParameters.githubActionsWorkflow || '';
|
||||
this.token = buildParameters.githubActionsToken || '';
|
||||
this.ref = buildParameters.githubActionsRef || 'main';
|
||||
}
|
||||
|
||||
async setupWorkflow(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildGuid: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildParameters: BuildParameters,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
branchName: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[],
|
||||
): Promise<void> {
|
||||
OrchestratorLogger.log(`[GitHubActions] Setting up workflow dispatch to ${this.repo}`);
|
||||
|
||||
if (!this.repo || !this.workflow) {
|
||||
throw new Error('githubActionsRepo and githubActionsWorkflow are required for the github-actions provider');
|
||||
}
|
||||
|
||||
if (!this.token) {
|
||||
throw new Error('githubActionsToken is required (PAT with actions:write scope)');
|
||||
}
|
||||
|
||||
// Verify repository and workflow exist
|
||||
try {
|
||||
const result = await OrchestratorSystem.Run(
|
||||
`GH_TOKEN=${this.token} gh api repos/${this.repo}/actions/workflows/${this.workflow} --jq '.id'`,
|
||||
);
|
||||
OrchestratorLogger.log(`[GitHubActions] Workflow verified: ${this.workflow} (ID: ${result.trim()})`);
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to verify workflow ${this.workflow} in ${this.repo}: ${error.message || error}`);
|
||||
}
|
||||
}
|
||||
|
||||
async runTaskInWorkflow(
|
||||
buildGuid: string,
|
||||
image: string,
|
||||
commands: string,
|
||||
mountdir: string,
|
||||
workingdir: string,
|
||||
environment: OrchestratorEnvironmentVariable[],
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
secrets: OrchestratorSecret[],
|
||||
): Promise<string> {
|
||||
OrchestratorLogger.log(`[GitHubActions] Dispatching workflow ${this.workflow} on ${this.repo}@${this.ref}`);
|
||||
|
||||
// Build inputs payload
|
||||
const inputs: Record<string, string> = {
|
||||
buildGuid,
|
||||
image,
|
||||
commands: Buffer.from(commands).toString('base64'),
|
||||
mountdir,
|
||||
workingdir,
|
||||
};
|
||||
|
||||
// Add environment variables as a JSON input
|
||||
if (environment.length > 0) {
|
||||
inputs.environment = JSON.stringify(environment.map((element) => ({ name: element.name, value: element.value })));
|
||||
}
|
||||
|
||||
// Record the time before dispatch to identify the run
|
||||
const beforeDispatch = new Date().toISOString();
|
||||
|
||||
// Dispatch the workflow
|
||||
const inputsJson = JSON.stringify(inputs).replace(/'/g, "'\\''");
|
||||
try {
|
||||
await OrchestratorSystem.Run(
|
||||
`GH_TOKEN=${this.token} gh api repos/${this.repo}/actions/workflows/${this.workflow}/dispatches -X POST -f ref='${this.ref}' -f "inputs=${inputsJson}"`,
|
||||
);
|
||||
OrchestratorLogger.log(`[GitHubActions] Workflow dispatched`);
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to dispatch workflow: ${error.message || error}`);
|
||||
}
|
||||
|
||||
// Poll for the run to appear
|
||||
OrchestratorLogger.log(`[GitHubActions] Waiting for workflow run to start...`);
|
||||
let attempts = 0;
|
||||
const maxAttempts = 30;
|
||||
|
||||
while (attempts < maxAttempts) {
|
||||
attempts++;
|
||||
await new Promise((resolve) => setTimeout(resolve, 10_000));
|
||||
|
||||
try {
|
||||
const runsJson = await OrchestratorSystem.Run(
|
||||
`GH_TOKEN=${this.token} gh api "repos/${this.repo}/actions/workflows/${this.workflow}/runs?created=>${beforeDispatch}&per_page=5" --jq '.workflow_runs[0] | {id, status, conclusion}'`,
|
||||
true,
|
||||
);
|
||||
|
||||
const run = JSON.parse(runsJson.trim());
|
||||
if (run.id) {
|
||||
this.runId = run.id;
|
||||
OrchestratorLogger.log(`[GitHubActions] Run started: ${this.runId} (status: ${run.status})`);
|
||||
break;
|
||||
}
|
||||
} catch {
|
||||
// Run not yet available
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.runId) {
|
||||
throw new Error(`Workflow run did not start within ${maxAttempts * 10}s`);
|
||||
}
|
||||
|
||||
// Poll until completion and stream logs
|
||||
let status = 'in_progress';
|
||||
while (status === 'in_progress' || status === 'queued') {
|
||||
await new Promise((resolve) => setTimeout(resolve, 15_000));
|
||||
|
||||
try {
|
||||
const statusJson = await OrchestratorSystem.Run(
|
||||
`GH_TOKEN=${this.token} gh api repos/${this.repo}/actions/runs/${this.runId} --jq '{status, conclusion}'`,
|
||||
true,
|
||||
);
|
||||
|
||||
const result = JSON.parse(statusJson.trim());
|
||||
status = result.status;
|
||||
|
||||
if (status === 'completed') {
|
||||
OrchestratorLogger.log(`[GitHubActions] Run ${this.runId} completed: ${result.conclusion}`);
|
||||
|
||||
if (result.conclusion !== 'success') {
|
||||
throw new Error(`Workflow run failed with conclusion: ${result.conclusion}`);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(`[GitHubActions] Run ${this.runId} status: ${status}`);
|
||||
} catch (error: any) {
|
||||
if (error.message && error.message.includes('conclusion')) {
|
||||
throw error;
|
||||
}
|
||||
OrchestratorLogger.logWarning(`[GitHubActions] Status check error: ${error.message || error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch logs
|
||||
try {
|
||||
const logs = await OrchestratorSystem.Run(
|
||||
`GH_TOKEN=${this.token} gh run view ${this.runId} --repo ${this.repo} --log`,
|
||||
true,
|
||||
);
|
||||
|
||||
return logs;
|
||||
} catch {
|
||||
return `Run ${this.runId} completed successfully (logs unavailable)`;
|
||||
}
|
||||
}
|
||||
|
||||
async cleanupWorkflow(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildParameters: BuildParameters,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
branchName: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[],
|
||||
): Promise<void> {
|
||||
OrchestratorLogger.log(`[GitHubActions] Cleanup complete (no resources to tear down)`);
|
||||
}
|
||||
|
||||
async garbageCollect(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
filter: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
previewOnly: boolean,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
olderThan: Number,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
fullCache: boolean,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
baseDependencies: boolean,
|
||||
): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
async listResources(): Promise<ProviderResource[]> {
|
||||
if (!this.repo || !this.token) return [];
|
||||
|
||||
try {
|
||||
const runnersJson = await OrchestratorSystem.Run(
|
||||
`GH_TOKEN=${this.token} gh api repos/${this.repo}/actions/runners --jq '.runners[] | .name'`,
|
||||
true,
|
||||
);
|
||||
|
||||
return runnersJson
|
||||
.trim()
|
||||
.split('\n')
|
||||
.filter(Boolean)
|
||||
.map((name) => {
|
||||
const resource = new ProviderResource();
|
||||
resource.Name = name.trim();
|
||||
|
||||
return resource;
|
||||
});
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async listWorkflow(): Promise<ProviderWorkflow[]> {
|
||||
if (!this.repo || !this.token) return [];
|
||||
|
||||
try {
|
||||
const runsJson = await OrchestratorSystem.Run(
|
||||
`GH_TOKEN=${this.token} gh api repos/${this.repo}/actions/runs?per_page=10 --jq '.workflow_runs[] | .name'`,
|
||||
true,
|
||||
);
|
||||
|
||||
return runsJson
|
||||
.trim()
|
||||
.split('\n')
|
||||
.filter(Boolean)
|
||||
.map((name) => {
|
||||
const workflow = new ProviderWorkflow();
|
||||
workflow.Name = name.trim();
|
||||
|
||||
return workflow;
|
||||
});
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async watchWorkflow(): Promise<string> {
|
||||
if (!this.runId) return 'No active run to watch';
|
||||
|
||||
try {
|
||||
return await OrchestratorSystem.Run(
|
||||
`GH_TOKEN=${this.token} gh run watch ${this.runId} --repo ${this.repo}`,
|
||||
true,
|
||||
);
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
export default GitHubActionsProvider;
|
||||
211
src/model/orchestrator/providers/gitlab-ci/index.ts
Normal file
211
src/model/orchestrator/providers/gitlab-ci/index.ts
Normal file
@@ -0,0 +1,211 @@
|
||||
import BuildParameters from '../../../build-parameters';
|
||||
import { OrchestratorSystem } from '../../services/core/orchestrator-system';
|
||||
import OrchestratorEnvironmentVariable from '../../options/orchestrator-environment-variable';
|
||||
import OrchestratorLogger from '../../services/core/orchestrator-logger';
|
||||
import { ProviderInterface } from '../provider-interface';
|
||||
import OrchestratorSecret from '../../options/orchestrator-secret';
|
||||
import { ProviderResource } from '../provider-resource';
|
||||
import { ProviderWorkflow } from '../provider-workflow';
|
||||
|
||||
/**
|
||||
* GitLab CI provider — triggers builds as GitLab CI pipelines
|
||||
* via the GitLab API.
|
||||
*
|
||||
* Use case: Teams using GitLab CI, hybrid GitHub/GitLab setups,
|
||||
* or GitLab runners with Unity licenses.
|
||||
*/
|
||||
class GitLabCIProvider implements ProviderInterface {
|
||||
private buildParameters: BuildParameters;
|
||||
private projectId: string;
|
||||
private triggerToken: string;
|
||||
private apiUrl: string;
|
||||
private ref: string;
|
||||
private pipelineId: number = 0;
|
||||
|
||||
constructor(buildParameters: BuildParameters) {
|
||||
this.buildParameters = buildParameters;
|
||||
this.projectId = buildParameters.gitlabProjectId || '';
|
||||
this.triggerToken = buildParameters.gitlabTriggerToken || '';
|
||||
this.apiUrl = (buildParameters.gitlabApiUrl || 'https://gitlab.com').replace(/\/+$/, '');
|
||||
this.ref = buildParameters.gitlabRef || 'main';
|
||||
}
|
||||
|
||||
async setupWorkflow(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildGuid: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildParameters: BuildParameters,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
branchName: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[],
|
||||
): Promise<void> {
|
||||
OrchestratorLogger.log(`[GitLabCI] Setting up pipeline trigger for project ${this.projectId}`);
|
||||
|
||||
if (!this.projectId || !this.triggerToken) {
|
||||
throw new Error('gitlabProjectId and gitlabTriggerToken are required for the gitlab-ci provider');
|
||||
}
|
||||
|
||||
// Verify project access
|
||||
const encodedProject = encodeURIComponent(this.projectId);
|
||||
try {
|
||||
await OrchestratorSystem.Run(
|
||||
`curl -sf -H "PRIVATE-TOKEN: ${this.triggerToken}" "${this.apiUrl}/api/v4/projects/${encodedProject}" -o /dev/null`,
|
||||
);
|
||||
OrchestratorLogger.log(`[GitLabCI] Project access verified`);
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to access GitLab project ${this.projectId}: ${error.message || error}`);
|
||||
}
|
||||
}
|
||||
|
||||
async runTaskInWorkflow(
|
||||
buildGuid: string,
|
||||
image: string,
|
||||
commands: string,
|
||||
mountdir: string,
|
||||
workingdir: string,
|
||||
environment: OrchestratorEnvironmentVariable[],
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
secrets: OrchestratorSecret[],
|
||||
): Promise<string> {
|
||||
OrchestratorLogger.log(`[GitLabCI] Triggering pipeline on project ${this.projectId}@${this.ref}`);
|
||||
|
||||
const encodedProject = encodeURIComponent(this.projectId);
|
||||
|
||||
// Build variables for the pipeline
|
||||
const pipelineVariables: string[] = [
|
||||
`-f "variables[BUILD_GUID]=${buildGuid}"`,
|
||||
`-f "variables[BUILD_IMAGE]=${image}"`,
|
||||
`-f "variables[BUILD_COMMANDS]=${Buffer.from(commands).toString('base64')}"`,
|
||||
`-f "variables[MOUNT_DIR]=${mountdir}"`,
|
||||
`-f "variables[WORKING_DIR]=${workingdir}"`,
|
||||
];
|
||||
|
||||
for (const element of environment) {
|
||||
pipelineVariables.push(`-f "variables[${element.name}]=${element.value}"`);
|
||||
}
|
||||
|
||||
// Trigger pipeline
|
||||
try {
|
||||
const response = await OrchestratorSystem.Run(
|
||||
`curl -sf -X POST "${this.apiUrl}/api/v4/projects/${encodedProject}/trigger/pipeline" -f "token=${
|
||||
this.triggerToken
|
||||
}" -f "ref=${this.ref}" ${pipelineVariables.join(' ')}`,
|
||||
);
|
||||
|
||||
const pipeline = JSON.parse(response);
|
||||
this.pipelineId = pipeline.id;
|
||||
OrchestratorLogger.log(`[GitLabCI] Pipeline triggered: ${this.pipelineId} (status: ${pipeline.status})`);
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to trigger pipeline: ${error.message || error}`);
|
||||
}
|
||||
|
||||
// Poll until completion
|
||||
let status = 'pending';
|
||||
const terminalStatuses = new Set(['success', 'failed', 'canceled', 'skipped']);
|
||||
|
||||
while (!terminalStatuses.has(status)) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 15_000));
|
||||
|
||||
try {
|
||||
const statusResponse = await OrchestratorSystem.Run(
|
||||
`curl -sf -H "PRIVATE-TOKEN: ${this.triggerToken}" "${this.apiUrl}/api/v4/projects/${encodedProject}/pipelines/${this.pipelineId}"`,
|
||||
true,
|
||||
);
|
||||
|
||||
const pipelineStatus = JSON.parse(statusResponse);
|
||||
status = pipelineStatus.status;
|
||||
OrchestratorLogger.log(`[GitLabCI] Pipeline ${this.pipelineId} status: ${status}`);
|
||||
} catch (error: any) {
|
||||
OrchestratorLogger.logWarning(`[GitLabCI] Status check error: ${error.message || error}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (status !== 'success') {
|
||||
throw new Error(`Pipeline ${this.pipelineId} finished with status: ${status}`);
|
||||
}
|
||||
|
||||
// Fetch job logs
|
||||
try {
|
||||
const jobsResponse = await OrchestratorSystem.Run(
|
||||
`curl -sf -H "PRIVATE-TOKEN: ${this.triggerToken}" "${this.apiUrl}/api/v4/projects/${encodedProject}/pipelines/${this.pipelineId}/jobs"`,
|
||||
true,
|
||||
);
|
||||
|
||||
const jobs = JSON.parse(jobsResponse);
|
||||
const logs: string[] = [];
|
||||
|
||||
for (const job of jobs) {
|
||||
try {
|
||||
const jobLog = await OrchestratorSystem.Run(
|
||||
`curl -sf -H "PRIVATE-TOKEN: ${this.triggerToken}" "${this.apiUrl}/api/v4/projects/${encodedProject}/jobs/${job.id}/trace"`,
|
||||
true,
|
||||
);
|
||||
logs.push(`=== Job: ${job.name} (${job.status}) ===\n${jobLog}`);
|
||||
} catch {
|
||||
logs.push(`=== Job: ${job.name} (${job.status}) === (logs unavailable)`);
|
||||
}
|
||||
}
|
||||
|
||||
return logs.join('\n\n');
|
||||
} catch {
|
||||
return `Pipeline ${this.pipelineId} completed successfully (logs unavailable)`;
|
||||
}
|
||||
}
|
||||
|
||||
async cleanupWorkflow(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildParameters: BuildParameters,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
branchName: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[],
|
||||
): Promise<void> {
|
||||
OrchestratorLogger.log(`[GitLabCI] Cleanup complete`);
|
||||
}
|
||||
|
||||
async garbageCollect(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
filter: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
previewOnly: boolean,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
olderThan: Number,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
fullCache: boolean,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
baseDependencies: boolean,
|
||||
): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
async listResources(): Promise<ProviderResource[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async listWorkflow(): Promise<ProviderWorkflow[]> {
|
||||
if (!this.projectId || !this.triggerToken) return [];
|
||||
|
||||
try {
|
||||
const encodedProject = encodeURIComponent(this.projectId);
|
||||
const response = await OrchestratorSystem.Run(
|
||||
`curl -sf -H "PRIVATE-TOKEN: ${this.triggerToken}" "${this.apiUrl}/api/v4/projects/${encodedProject}/pipelines?per_page=10"`,
|
||||
true,
|
||||
);
|
||||
|
||||
return JSON.parse(response).map((pipeline: any) => {
|
||||
const workflow = new ProviderWorkflow();
|
||||
workflow.Name = `Pipeline #${pipeline.id} (${pipeline.status})`;
|
||||
|
||||
return workflow;
|
||||
});
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async watchWorkflow(): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
export default GitLabCIProvider;
|
||||
160
src/model/orchestrator/providers/remote-powershell/index.ts
Normal file
160
src/model/orchestrator/providers/remote-powershell/index.ts
Normal file
@@ -0,0 +1,160 @@
|
||||
import BuildParameters from '../../../build-parameters';
|
||||
import { OrchestratorSystem } from '../../services/core/orchestrator-system';
|
||||
import OrchestratorEnvironmentVariable from '../../options/orchestrator-environment-variable';
|
||||
import OrchestratorLogger from '../../services/core/orchestrator-logger';
|
||||
import { ProviderInterface } from '../provider-interface';
|
||||
import OrchestratorSecret from '../../options/orchestrator-secret';
|
||||
import { ProviderResource } from '../provider-resource';
|
||||
import { ProviderWorkflow } from '../provider-workflow';
|
||||
|
||||
/**
|
||||
* Remote PowerShell provider — executes Unity builds on remote machines
|
||||
* via PowerShell Remoting (WinRM or SSH).
|
||||
*
|
||||
* Use case: Teams with dedicated build machines not part of a CI system.
|
||||
*/
|
||||
class RemotePowershellProvider implements ProviderInterface {
|
||||
private buildParameters: BuildParameters;
|
||||
private host: string;
|
||||
private transport: string;
|
||||
private credential: string;
|
||||
private sessionId: string = '';
|
||||
|
||||
constructor(buildParameters: BuildParameters) {
|
||||
this.buildParameters = buildParameters;
|
||||
this.host = buildParameters.remotePowershellHost || '';
|
||||
this.transport = buildParameters.remotePowershellTransport || 'wsman';
|
||||
this.credential = buildParameters.remotePowershellCredential || '';
|
||||
}
|
||||
|
||||
async setupWorkflow(
|
||||
buildGuid: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildParameters: BuildParameters,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
branchName: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[],
|
||||
): Promise<void> {
|
||||
OrchestratorLogger.log(`[RemotePowershell] Setting up remote session to ${this.host} via ${this.transport}`);
|
||||
|
||||
if (!this.host) {
|
||||
throw new Error('remotePowershellHost is required for the remote-powershell provider');
|
||||
}
|
||||
|
||||
// Test connectivity
|
||||
const testCommand = this.buildPwshCommand(`Test-WSMan -ComputerName "${this.host}" -ErrorAction Stop`);
|
||||
try {
|
||||
await OrchestratorSystem.Run(testCommand);
|
||||
OrchestratorLogger.log(`[RemotePowershell] Connection test passed`);
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to connect to remote host ${this.host}: ${error.message || error}`);
|
||||
}
|
||||
|
||||
this.sessionId = buildGuid;
|
||||
OrchestratorLogger.log(`[RemotePowershell] Session ${this.sessionId} ready`);
|
||||
}
|
||||
|
||||
async runTaskInWorkflow(
|
||||
buildGuid: string,
|
||||
image: string,
|
||||
commands: string,
|
||||
mountdir: string,
|
||||
workingdir: string,
|
||||
environment: OrchestratorEnvironmentVariable[],
|
||||
secrets: OrchestratorSecret[],
|
||||
): Promise<string> {
|
||||
OrchestratorLogger.log(`[RemotePowershell] Executing task on ${this.host}`);
|
||||
|
||||
// Build environment variable block for remote session
|
||||
const environmentBlock = environment.map((element) => `$env:${element.name} = '${element.value}'`).join('; ');
|
||||
|
||||
const secretBlock = secrets
|
||||
.map((secret) => `$env:${secret.EnvironmentVariable} = '${secret.ParameterValue}'`)
|
||||
.join('; ');
|
||||
|
||||
// Wrap commands for remote execution
|
||||
const remoteScript = [environmentBlock, secretBlock, `Set-Location "${workingdir}"`, commands]
|
||||
.filter(Boolean)
|
||||
.join('; ');
|
||||
|
||||
const invokeCommand = this.buildInvokeCommand(remoteScript);
|
||||
|
||||
try {
|
||||
const output = await OrchestratorSystem.Run(invokeCommand);
|
||||
OrchestratorLogger.log(`[RemotePowershell] Task completed successfully`);
|
||||
|
||||
return output;
|
||||
} catch (error: any) {
|
||||
OrchestratorLogger.logWarning(`[RemotePowershell] Task failed: ${error.message || error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async cleanupWorkflow(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
buildParameters: BuildParameters,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
branchName: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
defaultSecretsArray: { ParameterKey: string; EnvironmentVariable: string; ParameterValue: string }[],
|
||||
): Promise<void> {
|
||||
OrchestratorLogger.log(`[RemotePowershell] Cleaning up session ${this.sessionId}`);
|
||||
|
||||
// Remote sessions are stateless per invocation — no cleanup needed
|
||||
}
|
||||
|
||||
async garbageCollect(
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
filter: string,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
previewOnly: boolean,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
olderThan: Number,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
fullCache: boolean,
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
baseDependencies: boolean,
|
||||
): Promise<string> {
|
||||
OrchestratorLogger.log(`[RemotePowershell] Garbage collection not supported for remote PowerShell provider`);
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
async listResources(): Promise<ProviderResource[]> {
|
||||
const resource = new ProviderResource();
|
||||
resource.Name = this.host;
|
||||
|
||||
return [resource];
|
||||
}
|
||||
|
||||
async listWorkflow(): Promise<ProviderWorkflow[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async watchWorkflow(): Promise<string> {
|
||||
return '';
|
||||
}
|
||||
|
||||
private buildPwshCommand(script: string): string {
|
||||
return `pwsh -NoProfile -NonInteractive -Command "${script.replace(/"/g, '\\"')}"`;
|
||||
}
|
||||
|
||||
private buildInvokeCommand(remoteScript: string): string {
|
||||
const escapedScript = remoteScript.replace(/"/g, '\\"').replace(/'/g, "''");
|
||||
|
||||
if (this.transport === 'ssh') {
|
||||
return `pwsh -NoProfile -NonInteractive -Command "Invoke-Command -HostName '${this.host}' -ScriptBlock { ${escapedScript} }"`;
|
||||
}
|
||||
|
||||
// WinRM (default)
|
||||
const credentialPart = this.credential
|
||||
? `-Credential (New-Object PSCredential('${this.credential.split(':')[0]}', (ConvertTo-SecureString '${
|
||||
this.credential.split(':')[1]
|
||||
}' -AsPlainText -Force)))`
|
||||
: '';
|
||||
|
||||
return `pwsh -NoProfile -NonInteractive -Command "Invoke-Command -ComputerName '${this.host}' ${credentialPart} -ScriptBlock { ${escapedScript} }"`;
|
||||
}
|
||||
}
|
||||
export default RemotePowershellProvider;
|
||||
@@ -15,15 +15,24 @@ import BuildParameters from '../../build-parameters';
|
||||
import { Cli } from '../../cli/cli';
|
||||
import OrchestratorOptions from '../options/orchestrator-options';
|
||||
import ResourceTracking from '../services/core/resource-tracking';
|
||||
import { IncrementalSyncService } from '../services/sync';
|
||||
import { SyncStrategy } from '../services/sync/sync-state';
|
||||
|
||||
export class RemoteClient {
|
||||
@CliFunction(`remote-cli-pre-build`, `sets up a repository, usually before a game-ci build`)
|
||||
static async setupRemoteClient() {
|
||||
OrchestratorLogger.log(`bootstrap game ci orchestrator...`);
|
||||
await ResourceTracking.logDiskUsageSnapshot('remote-cli-pre-build (start)');
|
||||
if (!(await RemoteClient.handleRetainedWorkspace())) {
|
||||
|
||||
const syncStrategy = (Orchestrator.buildParameters.syncStrategy || 'full') as SyncStrategy;
|
||||
|
||||
if (syncStrategy !== 'full') {
|
||||
OrchestratorLogger.log(`[Sync] Using incremental sync strategy: ${syncStrategy}`);
|
||||
await RemoteClient.handleIncrementalSync(syncStrategy);
|
||||
} else if (!(await RemoteClient.handleRetainedWorkspace())) {
|
||||
await RemoteClient.bootstrapRepository();
|
||||
}
|
||||
|
||||
await RemoteClient.replaceLargePackageReferencesWithSharedReferences();
|
||||
await RemoteClient.runCustomHookFiles(`before-build`);
|
||||
}
|
||||
@@ -157,6 +166,20 @@ export class RemoteClient {
|
||||
|
||||
await RemoteClient.runCustomHookFiles(`after-build`);
|
||||
|
||||
// Revert sync overlays if configured
|
||||
const syncStrategy = (Orchestrator.buildParameters.syncStrategy || 'full') as SyncStrategy;
|
||||
if (Orchestrator.buildParameters.syncRevertAfter && syncStrategy !== 'full') {
|
||||
try {
|
||||
OrchestratorLogger.log('[Sync] Reverting overlay changes after job completion');
|
||||
await IncrementalSyncService.revertOverlays(
|
||||
OrchestratorFolders.repoPathAbsolute,
|
||||
Orchestrator.buildParameters.syncStatePath,
|
||||
);
|
||||
} catch (revertError: any) {
|
||||
RemoteClientLogger.logWarning(`[Sync] Overlay revert failed: ${revertError.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// WIP - need to give the pod permissions to create config map
|
||||
await RemoteClientLogger.handleLogManagementPostJob();
|
||||
} catch (error: any) {
|
||||
@@ -229,6 +252,78 @@ export class RemoteClient {
|
||||
RemoteClientLogger.log(JSON.stringify(error, undefined, 4));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle incremental sync strategies (git-delta, direct-input, storage-pull).
|
||||
*
|
||||
* For git-delta: requires an existing workspace with sync state; fetches and applies
|
||||
* only changed files.
|
||||
*
|
||||
* For direct-input and storage-pull: requires an existing workspace; applies overlay
|
||||
* content on top.
|
||||
*
|
||||
* Falls back to full bootstrapRepository() if incremental sync cannot proceed.
|
||||
*/
|
||||
private static async handleIncrementalSync(strategy: SyncStrategy): Promise<void> {
|
||||
const buildParameters = Orchestrator.buildParameters;
|
||||
const workspacePath = OrchestratorFolders.repoPathAbsolute;
|
||||
const statePath = buildParameters.syncStatePath;
|
||||
|
||||
// Resolve strategy — may fall back to 'full' if no state exists
|
||||
const resolvedStrategy = IncrementalSyncService.resolveStrategy(strategy, workspacePath, statePath);
|
||||
|
||||
if (resolvedStrategy === 'full') {
|
||||
OrchestratorLogger.log('[Sync] Falling back to full bootstrap');
|
||||
if (!(await RemoteClient.handleRetainedWorkspace())) {
|
||||
await RemoteClient.bootstrapRepository();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
switch (resolvedStrategy) {
|
||||
case 'git-delta': {
|
||||
const targetReference = buildParameters.gitSha || buildParameters.branch;
|
||||
OrchestratorLogger.log(`[Sync] Git delta sync to ${targetReference}`);
|
||||
const changedFiles = await IncrementalSyncService.syncGitDelta(workspacePath, targetReference, statePath);
|
||||
OrchestratorLogger.log(`[Sync] Git delta complete: ${changedFiles} file(s) updated`);
|
||||
break;
|
||||
}
|
||||
case 'direct-input': {
|
||||
const inputReference = buildParameters.syncInputRef;
|
||||
if (!inputReference) {
|
||||
throw new Error('[Sync] direct-input strategy requires syncInputRef');
|
||||
}
|
||||
OrchestratorLogger.log(`[Sync] Applying direct input: ${inputReference}`);
|
||||
await IncrementalSyncService.applyDirectInput(
|
||||
workspacePath,
|
||||
inputReference,
|
||||
buildParameters.syncStorageRemote || undefined,
|
||||
statePath,
|
||||
);
|
||||
break;
|
||||
}
|
||||
case 'storage-pull': {
|
||||
const storageUri = buildParameters.syncInputRef;
|
||||
if (!storageUri) {
|
||||
throw new Error('[Sync] storage-pull strategy requires syncInputRef');
|
||||
}
|
||||
OrchestratorLogger.log(`[Sync] Storage pull from: ${storageUri}`);
|
||||
await IncrementalSyncService.syncStoragePull(workspacePath, storageUri, {
|
||||
rcloneRemote: buildParameters.syncStorageRemote || undefined,
|
||||
syncRevertAfter: buildParameters.syncRevertAfter,
|
||||
statePath,
|
||||
});
|
||||
break;
|
||||
}
|
||||
default:
|
||||
OrchestratorLogger.logWarning(`[Sync] Unknown strategy: ${resolvedStrategy}, falling back to full`);
|
||||
if (!(await RemoteClient.handleRetainedWorkspace())) {
|
||||
await RemoteClient.bootstrapRepository();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async bootstrapRepository() {
|
||||
await OrchestratorSystem.Run(
|
||||
`mkdir -p ${OrchestratorFolders.ToLinuxFolder(OrchestratorFolders.uniqueOrchestratorJobFolderAbsolute)}`,
|
||||
|
||||
5
src/model/orchestrator/runners/README.md
Normal file
5
src/model/orchestrator/runners/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Hot Runner Protocol
|
||||
|
||||
Extensible runner registration and persistent Unity editor provider protocol.
|
||||
|
||||
See GitHub Issue for full specification.
|
||||
@@ -0,0 +1,159 @@
|
||||
import OrchestratorLogger from '../core/orchestrator-logger';
|
||||
import { HotRunnerRegistry } from './hot-runner-registry';
|
||||
import { HotRunnerJobRequest, HotRunnerJobResult, HotRunnerStatus, HotRunnerTransport } from './hot-runner-types';
|
||||
|
||||
const POLL_INTERVAL_MS = 1000;
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
export type OutputCallback = (output: string) => void;
|
||||
|
||||
export class HotRunnerDispatcher {
|
||||
private transports: Map<string, HotRunnerTransport>;
|
||||
|
||||
constructor(transports: Map<string, HotRunnerTransport>) {
|
||||
this.transports = transports;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch a job to an available hot runner matching the request's build target.
|
||||
* If no runner is immediately available, waits up to the request timeout.
|
||||
* Returns the job result, or throws if no runner becomes available in time.
|
||||
*/
|
||||
async dispatchJob(
|
||||
request: HotRunnerJobRequest,
|
||||
registry: HotRunnerRegistry,
|
||||
unityVersion: string,
|
||||
onOutput?: OutputCallback,
|
||||
): Promise<HotRunnerJobResult> {
|
||||
OrchestratorLogger.log(`[HotRunner] Dispatching job ${request.jobId} (target: ${request.buildTarget})`);
|
||||
|
||||
// Find or wait for an available runner
|
||||
let runner = registry.findAvailableRunner({
|
||||
unityVersion,
|
||||
platform: request.buildTarget,
|
||||
});
|
||||
|
||||
if (!runner) {
|
||||
OrchestratorLogger.log(
|
||||
`[HotRunner] No idle runner available for ${unityVersion}/${request.buildTarget}, waiting...`,
|
||||
);
|
||||
runner = await this.waitForRunner({ unityVersion, platform: request.buildTarget }, request.timeout, registry);
|
||||
}
|
||||
|
||||
// Mark runner as busy
|
||||
registry.updateRunner(runner.id, {
|
||||
state: 'busy',
|
||||
currentJob: request.jobId,
|
||||
});
|
||||
|
||||
const transport = this.transports.get(runner.id);
|
||||
if (!transport) {
|
||||
registry.updateRunner(runner.id, { state: 'idle', currentJob: undefined });
|
||||
throw new Error(`[HotRunner] No transport available for runner ${runner.id}`);
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(`[HotRunner] Sending job ${request.jobId} to runner ${runner.id}`);
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
const result = await this.executeWithTimeout(transport, request);
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
OrchestratorLogger.log(
|
||||
`[HotRunner] Job ${request.jobId} completed on runner ${runner.id} in ${duration}ms (exit: ${result.exitCode})`,
|
||||
);
|
||||
|
||||
if (onOutput && result.output) {
|
||||
onOutput(result.output);
|
||||
}
|
||||
|
||||
// Mark runner as idle and increment job count
|
||||
const currentStatus = registry.getRunner(runner.id);
|
||||
registry.updateRunner(runner.id, {
|
||||
state: 'idle',
|
||||
currentJob: undefined,
|
||||
lastJobCompleted: request.jobId,
|
||||
jobsCompleted: (currentStatus?.jobsCompleted ?? 0) + 1,
|
||||
});
|
||||
|
||||
return result;
|
||||
} catch (error: any) {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Job ${request.jobId} failed on runner ${runner.id}: ${error.message}`);
|
||||
|
||||
// Mark runner as idle despite failure -- the health monitor will recycle if needed
|
||||
registry.updateRunner(runner.id, {
|
||||
state: 'idle',
|
||||
currentJob: undefined,
|
||||
});
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for an available runner matching the requirements.
|
||||
* Polls the registry at a fixed interval until one becomes available or timeout expires.
|
||||
*/
|
||||
async waitForRunner(
|
||||
requirements: { unityVersion: string; platform: string },
|
||||
timeoutMs: number,
|
||||
registry: HotRunnerRegistry,
|
||||
): Promise<HotRunnerStatus> {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
|
||||
while (Date.now() < deadline) {
|
||||
const runner = registry.findAvailableRunner(requirements);
|
||||
if (runner) {
|
||||
OrchestratorLogger.log(`[HotRunner] Runner ${runner.id} became available`);
|
||||
|
||||
return runner;
|
||||
}
|
||||
|
||||
await this.sleep(Math.min(POLL_INTERVAL_MS, deadline - Date.now()));
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`[HotRunner] Timed out waiting for available runner (${requirements.unityVersion}/${requirements.platform}) after ${timeoutMs}ms`,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a job on a transport with a timeout guard.
|
||||
* On timeout, disconnects the transport to release the connection
|
||||
* and prevent the orphaned sendJob promise from holding resources.
|
||||
*/
|
||||
private async executeWithTimeout(
|
||||
transport: HotRunnerTransport,
|
||||
request: HotRunnerJobRequest,
|
||||
): Promise<HotRunnerJobResult> {
|
||||
const TIMEOUT_SENTINEL = Symbol('timeout');
|
||||
|
||||
const timeoutPromise = new Promise<typeof TIMEOUT_SENTINEL>((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve(TIMEOUT_SENTINEL);
|
||||
}, request.timeout);
|
||||
});
|
||||
|
||||
const result = await Promise.race([transport.sendJob(request), timeoutPromise]);
|
||||
|
||||
if (result === TIMEOUT_SENTINEL) {
|
||||
// Disconnect the transport to clean up the orphaned sendJob call
|
||||
try {
|
||||
await transport.disconnect();
|
||||
} catch (disconnectError: any) {
|
||||
OrchestratorLogger.logWarning(
|
||||
`[HotRunner] Error disconnecting transport after timeout for job ${request.jobId}: ${disconnectError.message}`,
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error(`[HotRunner] Job ${request.jobId} timed out after ${request.timeout}ms`);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
import OrchestratorLogger from '../core/orchestrator-logger';
|
||||
import { HotRunnerRegistry } from './hot-runner-registry';
|
||||
import { HotRunnerTransport } from './hot-runner-types';
|
||||
|
||||
export class HotRunnerHealthMonitor {
|
||||
private intervalHandle: ReturnType<typeof setInterval> | undefined;
|
||||
private registry: HotRunnerRegistry | undefined;
|
||||
private transports: Map<string, HotRunnerTransport> = new Map();
|
||||
|
||||
/**
|
||||
* Start periodic health monitoring for all registered runners.
|
||||
*/
|
||||
startMonitoring(registry: HotRunnerRegistry, interval: number, transports: Map<string, HotRunnerTransport>): void {
|
||||
if (this.intervalHandle) {
|
||||
this.stopMonitoring();
|
||||
}
|
||||
|
||||
this.registry = registry;
|
||||
this.transports = transports;
|
||||
|
||||
OrchestratorLogger.log(`[HotRunner] Starting health monitoring (interval: ${interval}s)`);
|
||||
|
||||
this.intervalHandle = setInterval(() => {
|
||||
this.runHealthChecks().catch((error: any) => {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Health check cycle failed: ${error.message}`);
|
||||
});
|
||||
}, interval * 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop periodic health monitoring.
|
||||
*/
|
||||
stopMonitoring(): void {
|
||||
if (this.intervalHandle) {
|
||||
clearInterval(this.intervalHandle);
|
||||
this.intervalHandle = undefined;
|
||||
OrchestratorLogger.log(`[HotRunner] Health monitoring stopped`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check health of a specific runner by ID. Returns true if healthy.
|
||||
*/
|
||||
async checkHealth(runnerId: string): Promise<boolean> {
|
||||
if (!this.registry) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const transport = this.transports.get(runnerId);
|
||||
if (!transport) {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] No transport for runner ${runnerId}`);
|
||||
this.registry.updateRunner(runnerId, {
|
||||
state: 'unhealthy',
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const healthy = await transport.healthCheck();
|
||||
if (healthy) {
|
||||
const status = await transport.getStatus();
|
||||
this.registry.updateRunner(runnerId, {
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
memoryUsageMB: status.memoryUsageMB,
|
||||
uptime: status.uptime,
|
||||
libraryHash: status.libraryHash,
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Runner ${runnerId} health check returned false`);
|
||||
this.registry.updateRunner(runnerId, {
|
||||
state: 'unhealthy',
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
});
|
||||
|
||||
return false;
|
||||
} catch (error: any) {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Runner ${runnerId} health check failed: ${error.message}`);
|
||||
this.registry.updateRunner(runnerId, {
|
||||
state: 'unhealthy',
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark an unhealthy runner for cleanup and disconnect its transport.
|
||||
*/
|
||||
async recycleUnhealthyRunner(runnerId: string): Promise<void> {
|
||||
if (!this.registry) {
|
||||
return;
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(`[HotRunner] Recycling unhealthy runner ${runnerId}`);
|
||||
this.registry.updateRunner(runnerId, { state: 'stopping' });
|
||||
|
||||
const transport = this.transports.get(runnerId);
|
||||
if (transport) {
|
||||
try {
|
||||
await transport.disconnect();
|
||||
} catch (error: any) {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Error disconnecting runner ${runnerId}: ${error.message}`);
|
||||
}
|
||||
this.transports.delete(runnerId);
|
||||
}
|
||||
|
||||
this.registry.unregisterRunner(runnerId);
|
||||
OrchestratorLogger.log(`[HotRunner] Runner ${runnerId} recycled and removed`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recycle a runner that has been idle longer than the maximum idle time.
|
||||
*/
|
||||
async recycleIdleRunner(runnerId: string, maxIdleTime: number): Promise<void> {
|
||||
if (!this.registry) {
|
||||
return;
|
||||
}
|
||||
|
||||
const runner = this.registry.getRunner(runnerId);
|
||||
if (!runner || runner.state !== 'idle') {
|
||||
return;
|
||||
}
|
||||
|
||||
const lastCheckTime = new Date(runner.lastHealthCheck).getTime();
|
||||
const now = Date.now();
|
||||
const idleSeconds = (now - lastCheckTime) / 1000;
|
||||
|
||||
if (idleSeconds >= maxIdleTime) {
|
||||
OrchestratorLogger.log(
|
||||
`[HotRunner] Runner ${runnerId} idle for ${Math.floor(idleSeconds)}s (max: ${maxIdleTime}s), recycling`,
|
||||
);
|
||||
await this.recycleUnhealthyRunner(runnerId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run health checks and idle-recycle checks for all registered runners.
|
||||
*/
|
||||
private async runHealthChecks(): Promise<void> {
|
||||
if (!this.registry) {
|
||||
return;
|
||||
}
|
||||
|
||||
const runners = this.registry.listRunners();
|
||||
|
||||
for (const runner of runners) {
|
||||
if (runner.state === 'stopping') {
|
||||
continue;
|
||||
}
|
||||
|
||||
const healthy = await this.checkHealth(runner.id);
|
||||
|
||||
if (!healthy && runner.state !== 'starting') {
|
||||
await this.recycleUnhealthyRunner(runner.id);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for idle timeout
|
||||
const config = this.registry.getConfig(runner.id);
|
||||
if (config && runner.state === 'idle') {
|
||||
await this.recycleIdleRunner(runner.id, config.maxIdleTime);
|
||||
}
|
||||
|
||||
// Check for max jobs before recycle
|
||||
if (config && config.maxJobsBeforeRecycle > 0 && runner.jobsCompleted >= config.maxJobsBeforeRecycle) {
|
||||
OrchestratorLogger.log(
|
||||
`[HotRunner] Runner ${runner.id} reached max jobs (${runner.jobsCompleted}/${config.maxJobsBeforeRecycle}), recycling`,
|
||||
);
|
||||
await this.recycleUnhealthyRunner(runner.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether health monitoring is currently active.
|
||||
*/
|
||||
get isMonitoring(): boolean {
|
||||
return this.intervalHandle !== undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { customAlphabet } from 'nanoid';
|
||||
import OrchestratorLogger from '../core/orchestrator-logger';
|
||||
import { HotRunnerConfig, HotRunnerStatus } from './hot-runner-types';
|
||||
|
||||
const generateId = customAlphabet('abcdefghijklmnopqrstuvwxyz0123456789', 12);
|
||||
|
||||
const PERSISTENCE_FILENAME = 'hot-runners.json';
|
||||
|
||||
const VALID_RUNNER_STATES: ReadonlySet<string> = new Set(['idle', 'busy', 'starting', 'stopping', 'unhealthy']);
|
||||
|
||||
export interface HotRunnerFilter {
|
||||
platform?: string;
|
||||
state?: string;
|
||||
unityVersion?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a restored runner entry has all required fields with correct types.
|
||||
* Returns true if the entry is a valid HotRunnerStatus, false otherwise.
|
||||
*/
|
||||
function isValidRunnerStatus(entry: unknown): entry is HotRunnerStatus {
|
||||
if (typeof entry !== 'object' || entry === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const record = entry as Record<string, unknown>;
|
||||
|
||||
return (
|
||||
typeof record.id === 'string' &&
|
||||
record.id.length > 0 &&
|
||||
typeof record.state === 'string' &&
|
||||
VALID_RUNNER_STATES.has(record.state) &&
|
||||
typeof record.unityVersion === 'string' &&
|
||||
typeof record.platform === 'string' &&
|
||||
typeof record.uptime === 'number' &&
|
||||
typeof record.jobsCompleted === 'number' &&
|
||||
typeof record.lastHealthCheck === 'string' &&
|
||||
typeof record.memoryUsageMB === 'number'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a restored config entry has all required fields with correct types.
|
||||
* Returns true if the entry is a valid HotRunnerConfig, false otherwise.
|
||||
*/
|
||||
function isValidRunnerConfig(entry: unknown): entry is HotRunnerConfig {
|
||||
if (typeof entry !== 'object' || entry === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const record = entry as Record<string, unknown>;
|
||||
|
||||
return (
|
||||
typeof record.enabled === 'boolean' &&
|
||||
typeof record.transport === 'string' &&
|
||||
['websocket', 'grpc', 'named-pipe'].includes(record.transport) &&
|
||||
typeof record.host === 'string' &&
|
||||
typeof record.port === 'number' &&
|
||||
typeof record.healthCheckInterval === 'number' &&
|
||||
typeof record.maxIdleTime === 'number' &&
|
||||
typeof record.maxJobsBeforeRecycle === 'number'
|
||||
);
|
||||
}
|
||||
|
||||
export class HotRunnerRegistry {
|
||||
private runners: Map<string, HotRunnerStatus> = new Map();
|
||||
private configs: Map<string, HotRunnerConfig> = new Map();
|
||||
private persistencePath: string;
|
||||
|
||||
constructor(persistenceDirectory?: string) {
|
||||
this.persistencePath = persistenceDirectory ? path.join(persistenceDirectory, PERSISTENCE_FILENAME) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new hot runner. Returns the generated runner ID.
|
||||
*/
|
||||
registerRunner(config: HotRunnerConfig): string {
|
||||
const id = `hr-${generateId()}`;
|
||||
|
||||
const status: HotRunnerStatus = {
|
||||
id,
|
||||
state: 'starting',
|
||||
unityVersion: config.unityVersion ?? 'unknown',
|
||||
platform: config.platform ?? 'unknown',
|
||||
uptime: 0,
|
||||
jobsCompleted: 0,
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
memoryUsageMB: 0,
|
||||
};
|
||||
|
||||
this.runners.set(id, status);
|
||||
this.configs.set(id, config);
|
||||
OrchestratorLogger.log(`[HotRunner] Registered runner ${id} (${status.unityVersion}/${status.platform})`);
|
||||
|
||||
this.persist();
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a runner from the registry.
|
||||
*/
|
||||
unregisterRunner(id: string): void {
|
||||
const existed = this.runners.delete(id);
|
||||
this.configs.delete(id);
|
||||
|
||||
if (existed) {
|
||||
OrchestratorLogger.log(`[HotRunner] Unregistered runner ${id}`);
|
||||
this.persist();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a runner's current status by ID.
|
||||
*/
|
||||
getRunner(id: string): HotRunnerStatus | undefined {
|
||||
return this.runners.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a runner's config by ID.
|
||||
*/
|
||||
getConfig(id: string): HotRunnerConfig | undefined {
|
||||
return this.configs.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* List all runners, optionally filtered by platform, state, or Unity version.
|
||||
*/
|
||||
listRunners(filter?: HotRunnerFilter): HotRunnerStatus[] {
|
||||
let results = [...this.runners.values()];
|
||||
|
||||
if (filter?.platform) {
|
||||
results = results.filter((runner) => runner.platform === filter.platform);
|
||||
}
|
||||
|
||||
if (filter?.state) {
|
||||
results = results.filter((runner) => runner.state === filter.state);
|
||||
}
|
||||
|
||||
if (filter?.unityVersion) {
|
||||
results = results.filter((runner) => runner.unityVersion === filter.unityVersion);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find an idle runner matching the given Unity version and platform requirements.
|
||||
*/
|
||||
findAvailableRunner(requirements: { unityVersion: string; platform: string }): HotRunnerStatus | undefined {
|
||||
return this.listRunners({
|
||||
state: 'idle',
|
||||
unityVersion: requirements.unityVersion,
|
||||
platform: requirements.platform,
|
||||
})[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a runner's status fields. Merges partial updates into existing status.
|
||||
*/
|
||||
updateRunner(id: string, update: Partial<HotRunnerStatus>): void {
|
||||
const existing = this.runners.get(id);
|
||||
if (!existing) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.runners.set(id, { ...existing, ...update, id });
|
||||
this.persist();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total number of registered runners.
|
||||
*/
|
||||
get size(): number {
|
||||
return this.runners.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate all runners in the registry and reset invalid ones to 'unhealthy'.
|
||||
* Returns the number of runners that were repaired.
|
||||
*/
|
||||
validateAndRepair(): number {
|
||||
let repaired = 0;
|
||||
|
||||
for (const [id, status] of this.runners) {
|
||||
// Cast to unknown to bypass the type guard narrowing to 'never',
|
||||
// since the Map is typed as HotRunnerStatus but entries may have
|
||||
// been corrupted via direct deserialization or unsafe casts.
|
||||
const entry = status as unknown as Record<string, unknown>;
|
||||
|
||||
if (!isValidRunnerStatus(entry)) {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Runner ${id} has invalid state, marking as unhealthy`);
|
||||
this.runners.set(id, {
|
||||
id,
|
||||
state: 'unhealthy',
|
||||
unityVersion: typeof entry.unityVersion === 'string' ? entry.unityVersion : 'unknown',
|
||||
platform: typeof entry.platform === 'string' ? entry.platform : 'unknown',
|
||||
uptime: typeof entry.uptime === 'number' ? entry.uptime : 0,
|
||||
jobsCompleted: typeof entry.jobsCompleted === 'number' ? entry.jobsCompleted : 0,
|
||||
lastHealthCheck: typeof entry.lastHealthCheck === 'string' ? entry.lastHealthCheck : new Date().toISOString(),
|
||||
memoryUsageMB: typeof entry.memoryUsageMB === 'number' ? entry.memoryUsageMB : 0,
|
||||
});
|
||||
repaired++;
|
||||
}
|
||||
}
|
||||
|
||||
if (repaired > 0) {
|
||||
this.persist();
|
||||
}
|
||||
|
||||
return repaired;
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist current registry state to disk for crash recovery.
|
||||
* Validates data before writing to prevent persisting corrupt state.
|
||||
*/
|
||||
private persist(): void {
|
||||
if (!this.persistencePath) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Validate data before persisting
|
||||
for (const [id, status] of this.runners) {
|
||||
if (!isValidRunnerStatus(status)) {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Skipping persistence -- runner ${id} has invalid state`);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const data = {
|
||||
runners: Object.fromEntries(this.runners),
|
||||
configs: Object.fromEntries(this.configs),
|
||||
};
|
||||
const directory = path.dirname(this.persistencePath);
|
||||
if (!fs.existsSync(directory)) {
|
||||
fs.mkdirSync(directory, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFileSync(this.persistencePath, JSON.stringify(data, undefined, 2));
|
||||
} catch (error: any) {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Failed to persist registry: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load registry state from disk. Returns the number of runners restored.
|
||||
* Validates each restored entry and discards corrupt entries with warnings.
|
||||
* If the persistence file itself is corrupt (invalid JSON), starts with
|
||||
* an empty registry.
|
||||
*/
|
||||
loadFromDisk(): number {
|
||||
if (!this.persistencePath || !fs.existsSync(this.persistencePath)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let data: any;
|
||||
|
||||
try {
|
||||
const raw = fs.readFileSync(this.persistencePath, 'utf8');
|
||||
data = JSON.parse(raw);
|
||||
} catch (error: any) {
|
||||
OrchestratorLogger.logWarning(
|
||||
`[HotRunner] Persistence file is corrupt, starting with empty registry: ${error.message}`,
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (typeof data !== 'object' || data === null) {
|
||||
OrchestratorLogger.logWarning('[HotRunner] Persistence file has invalid structure, starting with empty registry');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
let discarded = 0;
|
||||
|
||||
if (data.runners && typeof data.runners === 'object') {
|
||||
for (const [id, status] of Object.entries(data.runners)) {
|
||||
if (isValidRunnerStatus(status)) {
|
||||
this.runners.set(id, status);
|
||||
} else {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Discarding invalid runner entry '${id}' from persistence file`);
|
||||
discarded++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (data.configs && typeof data.configs === 'object') {
|
||||
for (const [id, config] of Object.entries(data.configs)) {
|
||||
// Only restore configs for runners that were successfully restored
|
||||
if (this.runners.has(id)) {
|
||||
if (isValidRunnerConfig(config)) {
|
||||
this.configs.set(id, config);
|
||||
} else {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Discarding invalid config entry '${id}' from persistence file`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (discarded > 0) {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Discarded ${discarded} invalid runner(s) from persistence file`);
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(`[HotRunner] Restored ${this.runners.size} runner(s) from disk`);
|
||||
|
||||
return this.runners.size;
|
||||
}
|
||||
}
|
||||
166
src/model/orchestrator/services/hot-runner/hot-runner-service.ts
Normal file
166
src/model/orchestrator/services/hot-runner/hot-runner-service.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
import OrchestratorLogger from '../core/orchestrator-logger';
|
||||
import { HotRunnerRegistry } from './hot-runner-registry';
|
||||
import { HotRunnerHealthMonitor } from './hot-runner-health-monitor';
|
||||
import { HotRunnerDispatcher, OutputCallback } from './hot-runner-dispatcher';
|
||||
import {
|
||||
HotRunnerConfig,
|
||||
HotRunnerJobRequest,
|
||||
HotRunnerJobResult,
|
||||
HotRunnerStatus,
|
||||
HotRunnerTransport,
|
||||
} from './hot-runner-types';
|
||||
import BuildParameters from '../../../build-parameters';
|
||||
|
||||
export class HotRunnerService {
|
||||
private registry: HotRunnerRegistry;
|
||||
private healthMonitor: HotRunnerHealthMonitor;
|
||||
private dispatcher: HotRunnerDispatcher;
|
||||
private transports: Map<string, HotRunnerTransport> = new Map();
|
||||
private config: HotRunnerConfig | undefined;
|
||||
|
||||
constructor(persistenceDirectory?: string) {
|
||||
this.registry = new HotRunnerRegistry(persistenceDirectory);
|
||||
this.healthMonitor = new HotRunnerHealthMonitor();
|
||||
this.dispatcher = new HotRunnerDispatcher(this.transports);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the hot runner service: load persisted state, start health monitoring.
|
||||
*/
|
||||
async initialize(config: HotRunnerConfig): Promise<void> {
|
||||
this.config = config;
|
||||
|
||||
OrchestratorLogger.log(
|
||||
`[HotRunner] Initializing service (transport: ${config.transport}, ${config.host}:${config.port})`,
|
||||
);
|
||||
|
||||
// Attempt to restore previously registered runners from disk
|
||||
const restored = this.registry.loadFromDisk();
|
||||
if (restored > 0) {
|
||||
OrchestratorLogger.log(`[HotRunner] Restored ${restored} runner(s) from persistence`);
|
||||
}
|
||||
|
||||
// Start health monitoring
|
||||
this.healthMonitor.startMonitoring(this.registry, config.healthCheckInterval, this.transports);
|
||||
|
||||
OrchestratorLogger.log(`[HotRunner] Service initialized`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a runner with a transport implementation.
|
||||
* Returns the runner ID.
|
||||
*/
|
||||
registerRunner(config: HotRunnerConfig, transport: HotRunnerTransport): string {
|
||||
const id = this.registry.registerRunner(config);
|
||||
this.transports.set(id, transport);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit a build job to an available hot runner.
|
||||
* Converts BuildParameters to a HotRunnerJobRequest and dispatches.
|
||||
*/
|
||||
async submitBuild(params: BuildParameters, onOutput?: OutputCallback): Promise<HotRunnerJobResult> {
|
||||
const request: HotRunnerJobRequest = {
|
||||
jobId: params.buildGuid || `build-${Date.now()}`,
|
||||
buildMethod: params.buildMethod || undefined,
|
||||
buildTarget: params.targetPlatform,
|
||||
buildPath: params.buildPath,
|
||||
customParameters: params.customParameters ? this.parseCustomParameters(params.customParameters) : undefined,
|
||||
timeout: 30 * 60 * 1000, // 30 minutes default
|
||||
};
|
||||
|
||||
OrchestratorLogger.log(`[HotRunner] Submitting build: ${request.jobId} (target: ${request.buildTarget})`);
|
||||
|
||||
return this.dispatcher.dispatchJob(request, this.registry, params.editorVersion, onOutput);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit a test job to an available hot runner.
|
||||
* Converts BuildParameters and optional suite config to a test-mode HotRunnerJobRequest.
|
||||
*/
|
||||
async submitTest(
|
||||
params: BuildParameters,
|
||||
suiteConfig?: { testMode?: 'editmode' | 'playmode'; testSuitePath?: string },
|
||||
onOutput?: OutputCallback,
|
||||
): Promise<HotRunnerJobResult> {
|
||||
const request: HotRunnerJobRequest = {
|
||||
jobId: params.buildGuid || `test-${Date.now()}`,
|
||||
buildTarget: params.targetPlatform,
|
||||
customParameters: params.customParameters ? this.parseCustomParameters(params.customParameters) : undefined,
|
||||
timeout: 30 * 60 * 1000, // 30 minutes default
|
||||
testMode: suiteConfig?.testMode ?? 'editmode',
|
||||
testSuitePath: suiteConfig?.testSuitePath,
|
||||
};
|
||||
|
||||
OrchestratorLogger.log(`[HotRunner] Submitting test: ${request.jobId} (mode: ${request.testMode})`);
|
||||
|
||||
return this.dispatcher.dispatchJob(request, this.registry, params.editorVersion, onOutput);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shut down the service: stop health monitoring, disconnect all transports,
|
||||
* and unregister all runners.
|
||||
*/
|
||||
async shutdown(): Promise<void> {
|
||||
OrchestratorLogger.log(`[HotRunner] Shutting down service`);
|
||||
|
||||
this.healthMonitor.stopMonitoring();
|
||||
|
||||
const disconnectPromises: Promise<void>[] = [];
|
||||
for (const [id, transport] of this.transports.entries()) {
|
||||
disconnectPromises.push(
|
||||
transport.disconnect().catch((error: any) => {
|
||||
OrchestratorLogger.logWarning(`[HotRunner] Error disconnecting runner ${id}: ${error.message}`);
|
||||
}),
|
||||
);
|
||||
}
|
||||
await Promise.all(disconnectPromises);
|
||||
|
||||
this.transports.clear();
|
||||
|
||||
OrchestratorLogger.log(`[HotRunner] Service shut down`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the status of all registered runners.
|
||||
*/
|
||||
getStatus(): HotRunnerStatus[] {
|
||||
return this.registry.listRunners();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the underlying registry (for testing or advanced use).
|
||||
*/
|
||||
getRegistry(): HotRunnerRegistry {
|
||||
return this.registry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a space-separated custom parameters string into a key-value map.
|
||||
* Handles `-key value` and `-key=value` formats.
|
||||
*/
|
||||
private parseCustomParameters(raw: string): Record<string, string> {
|
||||
const result: Record<string, string> = {};
|
||||
const parts = raw.trim().split(/\s+/);
|
||||
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const part = parts[i];
|
||||
if (part.startsWith('-')) {
|
||||
const key = part.replace(/^-+/, '');
|
||||
if (key.includes('=')) {
|
||||
const [k, ...v] = key.split('=');
|
||||
result[k] = v.join('=');
|
||||
} else if (i + 1 < parts.length && !parts[i + 1].startsWith('-')) {
|
||||
result[key] = parts[i + 1];
|
||||
i++;
|
||||
} else {
|
||||
result[key] = 'true';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
export interface HotRunnerConfig {
|
||||
enabled: boolean;
|
||||
transport: 'websocket' | 'grpc' | 'named-pipe';
|
||||
host: string;
|
||||
port: number;
|
||||
healthCheckInterval: number; // seconds
|
||||
maxIdleTime: number; // seconds before recycling
|
||||
maxJobsBeforeRecycle: number;
|
||||
unityVersion?: string;
|
||||
platform?: string;
|
||||
}
|
||||
|
||||
export interface HotRunnerStatus {
|
||||
id: string;
|
||||
state: 'idle' | 'busy' | 'starting' | 'stopping' | 'unhealthy';
|
||||
unityVersion: string;
|
||||
platform: string;
|
||||
currentJob?: string;
|
||||
lastJobCompleted?: string;
|
||||
uptime: number;
|
||||
jobsCompleted: number;
|
||||
lastHealthCheck: string;
|
||||
memoryUsageMB: number;
|
||||
libraryHash?: string;
|
||||
}
|
||||
|
||||
export interface HotRunnerJobRequest {
|
||||
jobId: string;
|
||||
buildMethod?: string;
|
||||
buildTarget: string;
|
||||
buildPath?: string;
|
||||
customParameters?: Record<string, string>;
|
||||
timeout: number;
|
||||
testMode?: 'editmode' | 'playmode';
|
||||
testSuitePath?: string;
|
||||
}
|
||||
|
||||
export interface HotRunnerJobResult {
|
||||
jobId: string;
|
||||
success: boolean;
|
||||
exitCode: number;
|
||||
duration: number;
|
||||
output: string;
|
||||
artifacts?: string[];
|
||||
testResults?: string; // path to test result file
|
||||
}
|
||||
|
||||
export interface HotRunnerTransport {
|
||||
connect(config: HotRunnerConfig): Promise<void>;
|
||||
disconnect(): Promise<void>;
|
||||
sendJob(request: HotRunnerJobRequest): Promise<HotRunnerJobResult>;
|
||||
getStatus(): Promise<HotRunnerStatus>;
|
||||
healthCheck(): Promise<boolean>;
|
||||
}
|
||||
740
src/model/orchestrator/services/hot-runner/hot-runner.test.ts
Normal file
740
src/model/orchestrator/services/hot-runner/hot-runner.test.ts
Normal file
@@ -0,0 +1,740 @@
|
||||
import fs from 'node:fs';
|
||||
import { HotRunnerRegistry } from './hot-runner-registry';
|
||||
import { HotRunnerHealthMonitor } from './hot-runner-health-monitor';
|
||||
import { HotRunnerDispatcher } from './hot-runner-dispatcher';
|
||||
import { HotRunnerService } from './hot-runner-service';
|
||||
import {
|
||||
HotRunnerConfig,
|
||||
HotRunnerJobRequest,
|
||||
HotRunnerJobResult,
|
||||
HotRunnerStatus,
|
||||
HotRunnerTransport,
|
||||
} from './hot-runner-types';
|
||||
|
||||
// Mock dependencies
|
||||
jest.mock('node:fs');
|
||||
jest.mock('../core/orchestrator-logger');
|
||||
|
||||
const mockFs = fs as jest.Mocked<typeof fs>;
|
||||
|
||||
function createMockConfig(overrides?: Partial<HotRunnerConfig>): HotRunnerConfig {
|
||||
return {
|
||||
enabled: true,
|
||||
transport: 'websocket',
|
||||
host: 'localhost',
|
||||
port: 9090,
|
||||
healthCheckInterval: 30,
|
||||
maxIdleTime: 3600,
|
||||
maxJobsBeforeRecycle: 100,
|
||||
unityVersion: '2022.3.0f1',
|
||||
platform: 'StandaloneWindows64',
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function createMockTransport(overrides?: Partial<HotRunnerTransport>): HotRunnerTransport {
|
||||
return {
|
||||
// eslint-disable-next-line unicorn/no-useless-undefined
|
||||
connect: jest.fn().mockResolvedValue(undefined),
|
||||
// eslint-disable-next-line unicorn/no-useless-undefined
|
||||
disconnect: jest.fn().mockResolvedValue(undefined),
|
||||
sendJob: jest.fn().mockResolvedValue({
|
||||
jobId: 'test-job',
|
||||
success: true,
|
||||
exitCode: 0,
|
||||
duration: 5000,
|
||||
output: 'Build succeeded',
|
||||
artifacts: ['build/output.exe'],
|
||||
} as HotRunnerJobResult),
|
||||
getStatus: jest.fn().mockResolvedValue({
|
||||
id: 'mock-runner',
|
||||
state: 'idle',
|
||||
unityVersion: '2022.3.0f1',
|
||||
platform: 'StandaloneWindows64',
|
||||
uptime: 3600,
|
||||
jobsCompleted: 5,
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
memoryUsageMB: 1024,
|
||||
} as HotRunnerStatus),
|
||||
healthCheck: jest.fn().mockResolvedValue(true),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function createMockJobRequest(overrides?: Partial<HotRunnerJobRequest>): HotRunnerJobRequest {
|
||||
return {
|
||||
jobId: 'job-001',
|
||||
buildTarget: 'StandaloneWindows64',
|
||||
timeout: 60000,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// --- Registry Tests ---
|
||||
|
||||
describe('HotRunnerRegistry', () => {
|
||||
let registry: HotRunnerRegistry;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
registry = new HotRunnerRegistry();
|
||||
});
|
||||
|
||||
it('should register a runner and return an ID', () => {
|
||||
const config = createMockConfig();
|
||||
const id = registry.registerRunner(config);
|
||||
|
||||
expect(id).toMatch(/^hr-/);
|
||||
expect(registry.size).toBe(1);
|
||||
});
|
||||
|
||||
it('should retrieve a registered runner by ID', () => {
|
||||
const config = createMockConfig();
|
||||
const id = registry.registerRunner(config);
|
||||
const runner = registry.getRunner(id);
|
||||
|
||||
expect(runner).toBeDefined();
|
||||
expect(runner!.id).toBe(id);
|
||||
expect(runner!.state).toBe('starting');
|
||||
expect(runner!.unityVersion).toBe('2022.3.0f1');
|
||||
expect(runner!.platform).toBe('StandaloneWindows64');
|
||||
});
|
||||
|
||||
it('should return undefined for unknown runner ID', () => {
|
||||
const runner = registry.getRunner('nonexistent');
|
||||
expect(runner).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should unregister a runner', () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
expect(registry.size).toBe(1);
|
||||
|
||||
registry.unregisterRunner(id);
|
||||
expect(registry.size).toBe(0);
|
||||
expect(registry.getRunner(id)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle unregistering a nonexistent runner gracefully', () => {
|
||||
registry.unregisterRunner('nonexistent');
|
||||
expect(registry.size).toBe(0);
|
||||
});
|
||||
|
||||
it('should list all runners without filter', () => {
|
||||
registry.registerRunner(createMockConfig({ platform: 'StandaloneWindows64' }));
|
||||
registry.registerRunner(createMockConfig({ platform: 'StandaloneLinux64' }));
|
||||
registry.registerRunner(createMockConfig({ platform: 'StandaloneOSX' }));
|
||||
|
||||
const all = registry.listRunners();
|
||||
expect(all).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('should filter runners by platform', () => {
|
||||
registry.registerRunner(createMockConfig({ platform: 'StandaloneWindows64' }));
|
||||
registry.registerRunner(createMockConfig({ platform: 'StandaloneLinux64' }));
|
||||
registry.registerRunner(createMockConfig({ platform: 'StandaloneWindows64' }));
|
||||
|
||||
const windows = registry.listRunners({ platform: 'StandaloneWindows64' });
|
||||
expect(windows).toHaveLength(2);
|
||||
|
||||
const linux = registry.listRunners({ platform: 'StandaloneLinux64' });
|
||||
expect(linux).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should filter runners by state', () => {
|
||||
const id1 = registry.registerRunner(createMockConfig());
|
||||
registry.registerRunner(createMockConfig());
|
||||
|
||||
registry.updateRunner(id1, { state: 'idle' });
|
||||
|
||||
// second runner remains in 'starting' state
|
||||
|
||||
const idle = registry.listRunners({ state: 'idle' });
|
||||
expect(idle).toHaveLength(1);
|
||||
expect(idle[0].id).toBe(id1);
|
||||
});
|
||||
|
||||
it('should filter runners by Unity version', () => {
|
||||
registry.registerRunner(createMockConfig({ unityVersion: '2022.3.0f1' }));
|
||||
registry.registerRunner(createMockConfig({ unityVersion: '2023.1.0f1' }));
|
||||
registry.registerRunner(createMockConfig({ unityVersion: '2022.3.0f1' }));
|
||||
|
||||
const v2022 = registry.listRunners({ unityVersion: '2022.3.0f1' });
|
||||
expect(v2022).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should find an available idle runner matching requirements', () => {
|
||||
const id1 = registry.registerRunner(
|
||||
createMockConfig({ unityVersion: '2022.3.0f1', platform: 'StandaloneWindows64' }),
|
||||
);
|
||||
registry.updateRunner(id1, { state: 'idle' });
|
||||
|
||||
const id2 = registry.registerRunner(
|
||||
createMockConfig({ unityVersion: '2023.1.0f1', platform: 'StandaloneLinux64' }),
|
||||
);
|
||||
registry.updateRunner(id2, { state: 'idle' });
|
||||
|
||||
const found = registry.findAvailableRunner({
|
||||
unityVersion: '2022.3.0f1',
|
||||
platform: 'StandaloneWindows64',
|
||||
});
|
||||
|
||||
expect(found).toBeDefined();
|
||||
expect(found!.id).toBe(id1);
|
||||
});
|
||||
|
||||
it('should return undefined when no runner matches requirements', () => {
|
||||
const id = registry.registerRunner(
|
||||
createMockConfig({ unityVersion: '2022.3.0f1', platform: 'StandaloneWindows64' }),
|
||||
);
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
const found = registry.findAvailableRunner({
|
||||
unityVersion: '2023.1.0f1',
|
||||
platform: 'StandaloneLinux64',
|
||||
});
|
||||
|
||||
expect(found).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should update runner status fields', () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle', memoryUsageMB: 2048 });
|
||||
|
||||
const runner = registry.getRunner(id);
|
||||
expect(runner!.state).toBe('idle');
|
||||
expect(runner!.memoryUsageMB).toBe(2048);
|
||||
|
||||
// ID should not be overridden by the update
|
||||
expect(runner!.id).toBe(id);
|
||||
});
|
||||
|
||||
it('should persist and load registry from disk', () => {
|
||||
const persistenceRegistry = new HotRunnerRegistry('/tmp/test');
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.writeFileSync.mockImplementation(() => {});
|
||||
mockFs.mkdirSync.mockImplementation(() => '' as any);
|
||||
|
||||
const id = persistenceRegistry.registerRunner(createMockConfig());
|
||||
|
||||
// Verify writeFileSync was called for persistence
|
||||
expect(mockFs.writeFileSync).toHaveBeenCalled();
|
||||
const writtenData = JSON.parse((mockFs.writeFileSync as jest.Mock).mock.calls[0][1] as string);
|
||||
expect(writtenData.runners).toBeDefined();
|
||||
expect(writtenData.runners[id]).toBeDefined();
|
||||
});
|
||||
|
||||
it('should load runners from disk on loadFromDisk', () => {
|
||||
const persistenceRegistry = new HotRunnerRegistry('/tmp/test');
|
||||
const storedData = {
|
||||
runners: {
|
||||
'hr-restored': {
|
||||
id: 'hr-restored',
|
||||
state: 'idle',
|
||||
unityVersion: '2022.3.0f1',
|
||||
platform: 'StandaloneWindows64',
|
||||
uptime: 100,
|
||||
jobsCompleted: 3,
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
memoryUsageMB: 512,
|
||||
},
|
||||
},
|
||||
configs: {
|
||||
'hr-restored': createMockConfig(),
|
||||
},
|
||||
};
|
||||
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(JSON.stringify(storedData));
|
||||
|
||||
const count = persistenceRegistry.loadFromDisk();
|
||||
expect(count).toBe(1);
|
||||
expect(persistenceRegistry.getRunner('hr-restored')).toBeDefined();
|
||||
});
|
||||
|
||||
it('should discard invalid runner entries when loading from disk', () => {
|
||||
const persistenceRegistry = new HotRunnerRegistry('/tmp/test');
|
||||
const storedData = {
|
||||
runners: {
|
||||
'hr-valid': {
|
||||
id: 'hr-valid',
|
||||
state: 'idle',
|
||||
unityVersion: '2022.3.0f1',
|
||||
platform: 'StandaloneWindows64',
|
||||
uptime: 100,
|
||||
jobsCompleted: 3,
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
memoryUsageMB: 512,
|
||||
},
|
||||
'hr-invalid': {
|
||||
// Missing required fields like state, unityVersion
|
||||
id: 'hr-invalid',
|
||||
},
|
||||
'hr-bad-state': {
|
||||
id: 'hr-bad-state',
|
||||
state: 'nonexistent-state',
|
||||
unityVersion: '2022.3.0f1',
|
||||
platform: 'StandaloneWindows64',
|
||||
uptime: 0,
|
||||
jobsCompleted: 0,
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
memoryUsageMB: 0,
|
||||
},
|
||||
},
|
||||
configs: {
|
||||
'hr-valid': createMockConfig(),
|
||||
'hr-invalid': createMockConfig(),
|
||||
'hr-bad-state': createMockConfig(),
|
||||
},
|
||||
};
|
||||
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(JSON.stringify(storedData));
|
||||
|
||||
const count = persistenceRegistry.loadFromDisk();
|
||||
expect(count).toBe(1);
|
||||
expect(persistenceRegistry.getRunner('hr-valid')).toBeDefined();
|
||||
expect(persistenceRegistry.getRunner('hr-invalid')).toBeUndefined();
|
||||
expect(persistenceRegistry.getRunner('hr-bad-state')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle corrupt JSON persistence file gracefully', () => {
|
||||
const persistenceRegistry = new HotRunnerRegistry('/tmp/test');
|
||||
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue('{ invalid json !!!');
|
||||
|
||||
const count = persistenceRegistry.loadFromDisk();
|
||||
expect(count).toBe(0);
|
||||
expect(persistenceRegistry.size).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle persistence file with invalid top-level structure', () => {
|
||||
const persistenceRegistry = new HotRunnerRegistry('/tmp/test');
|
||||
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue('"just a string"');
|
||||
|
||||
const count = persistenceRegistry.loadFromDisk();
|
||||
expect(count).toBe(0);
|
||||
});
|
||||
|
||||
it('should handle persistence file with null runners', () => {
|
||||
const persistenceRegistry = new HotRunnerRegistry('/tmp/test');
|
||||
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue('{"runners": null, "configs": null}');
|
||||
|
||||
const count = persistenceRegistry.loadFromDisk();
|
||||
expect(count).toBe(0);
|
||||
});
|
||||
|
||||
it('should validate and repair invalid runners', () => {
|
||||
const persistenceRegistry = new HotRunnerRegistry('/tmp/test');
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.writeFileSync.mockImplementation(() => {});
|
||||
mockFs.mkdirSync.mockImplementation(() => '' as any);
|
||||
|
||||
// Register a valid runner first
|
||||
const id = persistenceRegistry.registerRunner(createMockConfig());
|
||||
persistenceRegistry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
// Manually corrupt the runner's state by setting an invalid state
|
||||
// (we access via the public API -- updateRunner with a cast)
|
||||
persistenceRegistry.updateRunner(id, { state: 'invalid-state' as any });
|
||||
|
||||
const repaired = persistenceRegistry.validateAndRepair();
|
||||
expect(repaired).toBe(1);
|
||||
|
||||
const runner = persistenceRegistry.getRunner(id);
|
||||
expect(runner!.state).toBe('unhealthy');
|
||||
});
|
||||
|
||||
it('should not discard configs for valid runners when loading from disk', () => {
|
||||
const persistenceRegistry = new HotRunnerRegistry('/tmp/test');
|
||||
const storedData = {
|
||||
runners: {
|
||||
'hr-valid': {
|
||||
id: 'hr-valid',
|
||||
state: 'idle',
|
||||
unityVersion: '2022.3.0f1',
|
||||
platform: 'StandaloneWindows64',
|
||||
uptime: 100,
|
||||
jobsCompleted: 3,
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
memoryUsageMB: 512,
|
||||
},
|
||||
},
|
||||
configs: {
|
||||
'hr-valid': createMockConfig(),
|
||||
},
|
||||
};
|
||||
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(JSON.stringify(storedData));
|
||||
|
||||
persistenceRegistry.loadFromDisk();
|
||||
expect(persistenceRegistry.getConfig('hr-valid')).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
// --- Health Monitor Tests ---
|
||||
|
||||
describe('HotRunnerHealthMonitor', () => {
|
||||
let monitor: HotRunnerHealthMonitor;
|
||||
let registry: HotRunnerRegistry;
|
||||
let transports: Map<string, HotRunnerTransport>;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.useFakeTimers();
|
||||
monitor = new HotRunnerHealthMonitor();
|
||||
registry = new HotRunnerRegistry();
|
||||
transports = new Map();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
monitor.stopMonitoring();
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it('should start and stop monitoring', () => {
|
||||
monitor.startMonitoring(registry, 30, transports);
|
||||
expect(monitor.isMonitoring).toBe(true);
|
||||
|
||||
monitor.stopMonitoring();
|
||||
expect(monitor.isMonitoring).toBe(false);
|
||||
});
|
||||
|
||||
it('should report healthy when transport health check passes', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
const transport = createMockTransport();
|
||||
transports.set(id, transport);
|
||||
monitor.startMonitoring(registry, 30, transports);
|
||||
|
||||
const healthy = await monitor.checkHealth(id);
|
||||
expect(healthy).toBe(true);
|
||||
expect(transport.healthCheck).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should mark runner as unhealthy when health check fails', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
const transport = createMockTransport({
|
||||
healthCheck: jest.fn().mockResolvedValue(false),
|
||||
});
|
||||
transports.set(id, transport);
|
||||
monitor.startMonitoring(registry, 30, transports);
|
||||
|
||||
const healthy = await monitor.checkHealth(id);
|
||||
expect(healthy).toBe(false);
|
||||
|
||||
const runner = registry.getRunner(id);
|
||||
expect(runner!.state).toBe('unhealthy');
|
||||
});
|
||||
|
||||
it('should mark runner as unhealthy when health check throws', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
const transport = createMockTransport({
|
||||
healthCheck: jest.fn().mockRejectedValue(new Error('Connection refused')),
|
||||
});
|
||||
transports.set(id, transport);
|
||||
monitor.startMonitoring(registry, 30, transports);
|
||||
|
||||
const healthy = await monitor.checkHealth(id);
|
||||
expect(healthy).toBe(false);
|
||||
});
|
||||
|
||||
it('should recycle unhealthy runner and remove from registry', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
const transport = createMockTransport();
|
||||
transports.set(id, transport);
|
||||
monitor.startMonitoring(registry, 30, transports);
|
||||
|
||||
await monitor.recycleUnhealthyRunner(id);
|
||||
|
||||
expect(registry.getRunner(id)).toBeUndefined();
|
||||
expect(transport.disconnect).toHaveBeenCalled();
|
||||
expect(transports.has(id)).toBe(false);
|
||||
});
|
||||
|
||||
it('should recycle idle runner when max idle time exceeded', async () => {
|
||||
const id = registry.registerRunner(createMockConfig({ maxIdleTime: 60 }));
|
||||
|
||||
// Set lastHealthCheck to 120 seconds ago
|
||||
const oldDate = new Date(Date.now() - 120 * 1000).toISOString();
|
||||
registry.updateRunner(id, { state: 'idle', lastHealthCheck: oldDate });
|
||||
|
||||
const transport = createMockTransport();
|
||||
transports.set(id, transport);
|
||||
monitor.startMonitoring(registry, 30, transports);
|
||||
|
||||
await monitor.recycleIdleRunner(id, 60);
|
||||
|
||||
expect(registry.getRunner(id)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should not recycle idle runner when within max idle time', async () => {
|
||||
const id = registry.registerRunner(createMockConfig({ maxIdleTime: 3600 }));
|
||||
registry.updateRunner(id, {
|
||||
state: 'idle',
|
||||
lastHealthCheck: new Date().toISOString(),
|
||||
});
|
||||
|
||||
const transport = createMockTransport();
|
||||
transports.set(id, transport);
|
||||
monitor.startMonitoring(registry, 30, transports);
|
||||
|
||||
await monitor.recycleIdleRunner(id, 3600);
|
||||
|
||||
// Runner should still exist
|
||||
expect(registry.getRunner(id)).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return false when no transport exists for runner', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
|
||||
// Do not set any transport for this runner
|
||||
monitor.startMonitoring(registry, 30, transports);
|
||||
|
||||
const healthy = await monitor.checkHealth(id);
|
||||
expect(healthy).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Dispatcher Tests ---
|
||||
|
||||
describe('HotRunnerDispatcher', () => {
|
||||
let registry: HotRunnerRegistry;
|
||||
let transports: Map<string, HotRunnerTransport>;
|
||||
let dispatcher: HotRunnerDispatcher;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
registry = new HotRunnerRegistry();
|
||||
transports = new Map();
|
||||
dispatcher = new HotRunnerDispatcher(transports);
|
||||
});
|
||||
|
||||
it('should dispatch a job to an available runner', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
const transport = createMockTransport();
|
||||
transports.set(id, transport);
|
||||
|
||||
const request = createMockJobRequest();
|
||||
const result = await dispatcher.dispatchJob(request, registry, '2022.3.0f1');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(transport.sendJob).toHaveBeenCalledWith(request);
|
||||
});
|
||||
|
||||
it('should mark runner as busy during job execution', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
const statesDuringJob: string[] = [];
|
||||
const transport = createMockTransport({
|
||||
sendJob: jest.fn().mockImplementation(async () => {
|
||||
const runner = registry.getRunner(id);
|
||||
if (runner) statesDuringJob.push(runner.state);
|
||||
|
||||
return {
|
||||
jobId: 'job-001',
|
||||
success: true,
|
||||
exitCode: 0,
|
||||
duration: 1000,
|
||||
output: 'ok',
|
||||
};
|
||||
}),
|
||||
});
|
||||
transports.set(id, transport);
|
||||
|
||||
await dispatcher.dispatchJob(createMockJobRequest(), registry, '2022.3.0f1');
|
||||
|
||||
expect(statesDuringJob).toContain('busy');
|
||||
|
||||
// After completion, should be idle again
|
||||
const runner = registry.getRunner(id);
|
||||
expect(runner!.state).toBe('idle');
|
||||
});
|
||||
|
||||
it('should increment jobsCompleted after successful dispatch', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle', jobsCompleted: 5 });
|
||||
|
||||
const transport = createMockTransport();
|
||||
transports.set(id, transport);
|
||||
|
||||
await dispatcher.dispatchJob(createMockJobRequest(), registry, '2022.3.0f1');
|
||||
|
||||
const runner = registry.getRunner(id);
|
||||
expect(runner!.jobsCompleted).toBe(6);
|
||||
});
|
||||
|
||||
it('should throw when no runner is available and wait times out', async () => {
|
||||
// No runners registered at all
|
||||
const request = createMockJobRequest({ timeout: 100 });
|
||||
|
||||
await expect(dispatcher.dispatchJob(request, registry, '2022.3.0f1')).rejects.toThrow(/Timed out waiting/);
|
||||
});
|
||||
|
||||
it('should throw when runner has no transport', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
// No transport set for this runner
|
||||
|
||||
const request = createMockJobRequest();
|
||||
|
||||
await expect(dispatcher.dispatchJob(request, registry, '2022.3.0f1')).rejects.toThrow(/No transport available/);
|
||||
});
|
||||
|
||||
it('should handle job failure and return runner to idle', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
const transport = createMockTransport({
|
||||
sendJob: jest.fn().mockRejectedValue(new Error('Unity crashed')),
|
||||
});
|
||||
transports.set(id, transport);
|
||||
|
||||
await expect(dispatcher.dispatchJob(createMockJobRequest(), registry, '2022.3.0f1')).rejects.toThrow(
|
||||
'Unity crashed',
|
||||
);
|
||||
|
||||
// Runner should be back to idle despite failure
|
||||
const runner = registry.getRunner(id);
|
||||
expect(runner!.state).toBe('idle');
|
||||
});
|
||||
|
||||
it('should handle job timeout', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
const transport = createMockTransport({
|
||||
sendJob: jest.fn().mockImplementation(
|
||||
() => new Promise((resolve) => setTimeout(resolve, 60000)), // never resolves within timeout
|
||||
),
|
||||
});
|
||||
transports.set(id, transport);
|
||||
|
||||
const request = createMockJobRequest({ timeout: 50 });
|
||||
|
||||
await expect(dispatcher.dispatchJob(request, registry, '2022.3.0f1')).rejects.toThrow(/timed out/);
|
||||
});
|
||||
|
||||
it('should disconnect transport on job timeout', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
const transport = createMockTransport({
|
||||
sendJob: jest.fn().mockImplementation(
|
||||
() => new Promise((resolve) => setTimeout(resolve, 60000)), // never resolves within timeout
|
||||
),
|
||||
});
|
||||
transports.set(id, transport);
|
||||
|
||||
const request = createMockJobRequest({ timeout: 50 });
|
||||
|
||||
await expect(dispatcher.dispatchJob(request, registry, '2022.3.0f1')).rejects.toThrow(/timed out/);
|
||||
|
||||
// Transport should have been disconnected to clean up orphaned connection
|
||||
expect(transport.disconnect).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should call output callback with job output', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
|
||||
const transport = createMockTransport();
|
||||
transports.set(id, transport);
|
||||
|
||||
const outputCallback = jest.fn();
|
||||
await dispatcher.dispatchJob(createMockJobRequest(), registry, '2022.3.0f1', outputCallback);
|
||||
|
||||
expect(outputCallback).toHaveBeenCalledWith('Build succeeded');
|
||||
});
|
||||
|
||||
it('should wait for runner to become available', async () => {
|
||||
const id = registry.registerRunner(createMockConfig());
|
||||
|
||||
// Runner starts in 'starting' state, not idle
|
||||
|
||||
const transport = createMockTransport();
|
||||
transports.set(id, transport);
|
||||
|
||||
// Simulate runner becoming idle after a short delay
|
||||
setTimeout(() => {
|
||||
registry.updateRunner(id, { state: 'idle' });
|
||||
}, 50);
|
||||
|
||||
const request = createMockJobRequest({ timeout: 5000 });
|
||||
const result = await dispatcher.dispatchJob(request, registry, '2022.3.0f1');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Service Integration Tests ---
|
||||
|
||||
describe('HotRunnerService', () => {
|
||||
let service: HotRunnerService;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
service = new HotRunnerService();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await service.shutdown();
|
||||
});
|
||||
|
||||
it('should initialize and shut down cleanly', async () => {
|
||||
const config = createMockConfig();
|
||||
await service.initialize(config);
|
||||
|
||||
const status = service.getStatus();
|
||||
expect(status).toEqual([]);
|
||||
|
||||
await service.shutdown();
|
||||
});
|
||||
|
||||
it('should register a runner with transport', async () => {
|
||||
await service.initialize(createMockConfig());
|
||||
|
||||
const transport = createMockTransport();
|
||||
const id = service.registerRunner(createMockConfig(), transport);
|
||||
|
||||
expect(id).toMatch(/^hr-/);
|
||||
expect(service.getStatus()).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should disconnect all transports on shutdown', async () => {
|
||||
await service.initialize(createMockConfig());
|
||||
|
||||
const transport1 = createMockTransport();
|
||||
const transport2 = createMockTransport();
|
||||
service.registerRunner(createMockConfig(), transport1);
|
||||
service.registerRunner(createMockConfig(), transport2);
|
||||
|
||||
await service.shutdown();
|
||||
|
||||
expect(transport1.disconnect).toHaveBeenCalled();
|
||||
expect(transport2.disconnect).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should expose the underlying registry', async () => {
|
||||
await service.initialize(createMockConfig());
|
||||
const registry = service.getRegistry();
|
||||
|
||||
expect(registry).toBeInstanceOf(HotRunnerRegistry);
|
||||
});
|
||||
});
|
||||
11
src/model/orchestrator/services/hot-runner/index.ts
Normal file
11
src/model/orchestrator/services/hot-runner/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export { HotRunnerService } from './hot-runner-service';
|
||||
export { HotRunnerRegistry } from './hot-runner-registry';
|
||||
export { HotRunnerHealthMonitor } from './hot-runner-health-monitor';
|
||||
export { HotRunnerDispatcher } from './hot-runner-dispatcher';
|
||||
export type {
|
||||
HotRunnerConfig,
|
||||
HotRunnerStatus,
|
||||
HotRunnerJobRequest,
|
||||
HotRunnerJobResult,
|
||||
HotRunnerTransport,
|
||||
} from './hot-runner-types';
|
||||
607
src/model/orchestrator/services/output/artifact-service.test.ts
Normal file
607
src/model/orchestrator/services/output/artifact-service.test.ts
Normal file
@@ -0,0 +1,607 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { OutputTypeRegistry, OutputTypeDefinition } from './output-type-registry';
|
||||
import { OutputService } from './output-service';
|
||||
import { OutputManifest } from './output-manifest';
|
||||
import { ArtifactUploadHandler, ArtifactUploadConfig } from './artifact-upload-handler';
|
||||
|
||||
// Mock node:fs
|
||||
jest.mock('node:fs');
|
||||
const mockedFs = fs as jest.Mocked<typeof fs>;
|
||||
|
||||
// Mock @actions/core (used by OrchestratorLogger)
|
||||
jest.mock('@actions/core', () => ({
|
||||
info: jest.fn(),
|
||||
warning: jest.fn(),
|
||||
error: jest.fn(),
|
||||
setOutput: jest.fn(),
|
||||
getInput: jest.fn(),
|
||||
setFailed: jest.fn(),
|
||||
setSecret: jest.fn(),
|
||||
}));
|
||||
|
||||
// Mock @actions/exec (used by upload handler for rclone)
|
||||
jest.mock('@actions/exec', () => ({
|
||||
exec: jest.fn().mockResolvedValue(0),
|
||||
}));
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
OutputTypeRegistry.resetCustomTypes();
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// OutputTypeRegistry Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('OutputTypeRegistry', () => {
|
||||
describe('built-in types', () => {
|
||||
it('should have 8 built-in types', () => {
|
||||
const allTypes = OutputTypeRegistry.getAllTypes();
|
||||
const builtInTypes = allTypes.filter((t) => t.builtIn);
|
||||
expect(builtInTypes).toHaveLength(8);
|
||||
});
|
||||
|
||||
it.each(['build', 'test-results', 'server-build', 'data-export', 'images', 'logs', 'metrics', 'coverage'])(
|
||||
'should include built-in type "%s"',
|
||||
(typeName) => {
|
||||
const typeDef = OutputTypeRegistry.getType(typeName);
|
||||
expect(typeDef).toBeDefined();
|
||||
expect(typeDef!.name).toBe(typeName);
|
||||
expect(typeDef!.builtIn).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
it('should return undefined for unknown types', () => {
|
||||
const typeDef = OutputTypeRegistry.getType('nonexistent');
|
||||
expect(typeDef).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should include default paths for all built-in types', () => {
|
||||
const allTypes = OutputTypeRegistry.getAllTypes();
|
||||
for (const typeDef of allTypes) {
|
||||
expect(typeDef.defaultPath).toBeTruthy();
|
||||
expect(typeof typeDef.defaultPath).toBe('string');
|
||||
}
|
||||
});
|
||||
|
||||
it('should include descriptions for all built-in types', () => {
|
||||
const allTypes = OutputTypeRegistry.getAllTypes();
|
||||
for (const typeDef of allTypes) {
|
||||
expect(typeDef.description).toBeTruthy();
|
||||
expect(typeof typeDef.description).toBe('string');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('custom type registration', () => {
|
||||
it('should register a custom type', () => {
|
||||
const customType: OutputTypeDefinition = {
|
||||
name: 'custom-reports',
|
||||
defaultPath: './Reports/',
|
||||
description: 'Custom generated reports',
|
||||
builtIn: false,
|
||||
};
|
||||
|
||||
OutputTypeRegistry.registerType(customType);
|
||||
const retrieved = OutputTypeRegistry.getType('custom-reports');
|
||||
expect(retrieved).toBeDefined();
|
||||
expect(retrieved!.name).toBe('custom-reports');
|
||||
expect(retrieved!.builtIn).toBe(false);
|
||||
});
|
||||
|
||||
it('should not override built-in types', () => {
|
||||
const override: OutputTypeDefinition = {
|
||||
name: 'build',
|
||||
defaultPath: './Override/',
|
||||
description: 'Should not override',
|
||||
builtIn: false,
|
||||
};
|
||||
|
||||
OutputTypeRegistry.registerType(override);
|
||||
const buildType = OutputTypeRegistry.getType('build');
|
||||
expect(buildType!.defaultPath).not.toBe('./Override/');
|
||||
expect(buildType!.builtIn).toBe(true);
|
||||
});
|
||||
|
||||
it('should include custom types in getAllTypes', () => {
|
||||
OutputTypeRegistry.registerType({
|
||||
name: 'custom-a',
|
||||
defaultPath: './A/',
|
||||
description: 'Custom A',
|
||||
builtIn: false,
|
||||
});
|
||||
|
||||
const allTypes = OutputTypeRegistry.getAllTypes();
|
||||
expect(allTypes.length).toBe(9); // 8 built-in + 1 custom
|
||||
expect(allTypes.some((t) => t.name === 'custom-a')).toBe(true);
|
||||
});
|
||||
|
||||
it('should reset custom types', () => {
|
||||
OutputTypeRegistry.registerType({
|
||||
name: 'temp-type',
|
||||
defaultPath: './Temp/',
|
||||
description: 'Temporary type',
|
||||
builtIn: false,
|
||||
});
|
||||
|
||||
expect(OutputTypeRegistry.getType('temp-type')).toBeDefined();
|
||||
OutputTypeRegistry.resetCustomTypes();
|
||||
expect(OutputTypeRegistry.getType('temp-type')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should force builtIn to false when registering custom types', () => {
|
||||
OutputTypeRegistry.registerType({
|
||||
name: 'sneaky',
|
||||
defaultPath: './Sneaky/',
|
||||
description: 'Tries to be built-in',
|
||||
builtIn: true, // Intentionally setting to true
|
||||
});
|
||||
|
||||
const retrieved = OutputTypeRegistry.getType('sneaky');
|
||||
expect(retrieved).toBeDefined();
|
||||
expect(retrieved!.builtIn).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseOutputTypes', () => {
|
||||
it('should parse a comma-separated string of valid types', () => {
|
||||
const types = OutputTypeRegistry.parseOutputTypes('build,logs,coverage');
|
||||
expect(types).toHaveLength(3);
|
||||
expect(types.map((t) => t.name)).toEqual(['build', 'logs', 'coverage']);
|
||||
});
|
||||
|
||||
it('should skip unknown types', () => {
|
||||
const types = OutputTypeRegistry.parseOutputTypes('build,unknown,logs');
|
||||
expect(types).toHaveLength(2);
|
||||
expect(types.map((t) => t.name)).toEqual(['build', 'logs']);
|
||||
});
|
||||
|
||||
it('should handle empty string', () => {
|
||||
const types = OutputTypeRegistry.parseOutputTypes('');
|
||||
expect(types).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should handle whitespace in type names', () => {
|
||||
const types = OutputTypeRegistry.parseOutputTypes(' build , logs , coverage ');
|
||||
expect(types).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('should include custom types when parsing', () => {
|
||||
OutputTypeRegistry.registerType({
|
||||
name: 'my-reports',
|
||||
defaultPath: './Reports/',
|
||||
description: 'Custom reports',
|
||||
builtIn: false,
|
||||
});
|
||||
|
||||
const types = OutputTypeRegistry.parseOutputTypes('build,my-reports');
|
||||
expect(types).toHaveLength(2);
|
||||
expect(types[1].name).toBe('my-reports');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// OutputService Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('OutputService', () => {
|
||||
const projectPath = '/project';
|
||||
const buildGuid = 'test-guid-1234';
|
||||
|
||||
beforeEach(() => {
|
||||
// Reset all fs mocks
|
||||
mockedFs.existsSync.mockReset();
|
||||
mockedFs.statSync.mockReset();
|
||||
mockedFs.readdirSync.mockReset();
|
||||
mockedFs.writeFileSync.mockReset();
|
||||
mockedFs.mkdirSync.mockReset();
|
||||
});
|
||||
|
||||
describe('collectOutputs', () => {
|
||||
it('should return an empty manifest when no output types are declared', async () => {
|
||||
const manifest = await OutputService.collectOutputs(projectPath, buildGuid, '');
|
||||
expect(manifest.buildGuid).toBe(buildGuid);
|
||||
expect(manifest.outputs).toHaveLength(0);
|
||||
expect(manifest.timestamp).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should skip outputs where the path does not exist', async () => {
|
||||
mockedFs.existsSync.mockReturnValue(false);
|
||||
|
||||
const manifest = await OutputService.collectOutputs(projectPath, buildGuid, 'build,logs');
|
||||
expect(manifest.outputs).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should collect directory outputs with file listings', async () => {
|
||||
mockedFs.existsSync.mockReturnValue(true);
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => true, size: 0 } as any);
|
||||
mockedFs.readdirSync.mockImplementation((_dirPath: any, options?: any) => {
|
||||
if (options?.withFileTypes) {
|
||||
return [
|
||||
{ name: 'file1.txt', isDirectory: () => false },
|
||||
{ name: 'file2.txt', isDirectory: () => false },
|
||||
] as any;
|
||||
}
|
||||
|
||||
return ['file1.txt', 'file2.txt'] as any;
|
||||
});
|
||||
|
||||
const manifest = await OutputService.collectOutputs(projectPath, buildGuid, 'logs');
|
||||
expect(manifest.outputs).toHaveLength(1);
|
||||
expect(manifest.outputs[0].type).toBe('logs');
|
||||
expect(manifest.outputs[0].files).toEqual(['file1.txt', 'file2.txt']);
|
||||
});
|
||||
|
||||
it('should collect file output with correct size', async () => {
|
||||
mockedFs.existsSync.mockReturnValue(true);
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => false, size: 4096 } as any);
|
||||
|
||||
const manifest = await OutputService.collectOutputs(projectPath, buildGuid, 'coverage');
|
||||
expect(manifest.outputs).toHaveLength(1);
|
||||
expect(manifest.outputs[0].size).toBe(4096);
|
||||
});
|
||||
|
||||
it('should write manifest to disk when manifestPath is provided', async () => {
|
||||
// existsSync returns false for output paths (no outputs found) but mkdirSync/writeFileSync should still be called
|
||||
// The service only writes manifest when at least one output type is declared and types are resolved
|
||||
// So we need to provide a valid output type and have its path exist
|
||||
mockedFs.existsSync.mockReturnValue(true);
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => false, size: 100 } as any);
|
||||
mockedFs.mkdirSync.mockReturnValue(undefined);
|
||||
mockedFs.writeFileSync.mockImplementation(() => {});
|
||||
|
||||
const manifestPath = '/output/manifest.json';
|
||||
await OutputService.collectOutputs(projectPath, buildGuid, 'logs', manifestPath);
|
||||
|
||||
expect(mockedFs.mkdirSync).toHaveBeenCalledWith(path.dirname(manifestPath), { recursive: true });
|
||||
expect(mockedFs.writeFileSync).toHaveBeenCalledWith(manifestPath, expect.any(String), 'utf8');
|
||||
});
|
||||
|
||||
it('should generate valid JSON in the manifest file', async () => {
|
||||
mockedFs.existsSync.mockReturnValue(true);
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => false, size: 200 } as any);
|
||||
mockedFs.mkdirSync.mockReturnValue(undefined);
|
||||
mockedFs.writeFileSync.mockImplementation(() => {});
|
||||
|
||||
const manifestPath = '/output/manifest.json';
|
||||
await OutputService.collectOutputs(projectPath, buildGuid, 'coverage', manifestPath);
|
||||
|
||||
const writtenContent = (mockedFs.writeFileSync as jest.Mock).mock.calls[0][1];
|
||||
const parsed = JSON.parse(writtenContent);
|
||||
expect(parsed.buildGuid).toBe(buildGuid);
|
||||
expect(Array.isArray(parsed.outputs)).toBe(true);
|
||||
expect(parsed.outputs.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should set a valid ISO 8601 timestamp', async () => {
|
||||
const manifest = await OutputService.collectOutputs(projectPath, buildGuid, '');
|
||||
const parsed = new Date(manifest.timestamp);
|
||||
expect(parsed.toISOString()).toBe(manifest.timestamp);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ArtifactUploadHandler Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
describe('ArtifactUploadHandler', () => {
|
||||
const projectPath = '/project';
|
||||
|
||||
beforeEach(() => {
|
||||
mockedFs.existsSync.mockReset();
|
||||
mockedFs.statSync.mockReset();
|
||||
mockedFs.readdirSync.mockReset();
|
||||
mockedFs.mkdirSync.mockReset();
|
||||
mockedFs.copyFileSync.mockReset();
|
||||
});
|
||||
|
||||
describe('parseConfig', () => {
|
||||
it('should parse valid config values', () => {
|
||||
const config = ArtifactUploadHandler.parseConfig('github-artifacts', '/dest', 'gzip', '14');
|
||||
expect(config.target).toBe('github-artifacts');
|
||||
expect(config.destination).toBe('/dest');
|
||||
expect(config.compression).toBe('gzip');
|
||||
expect(config.retentionDays).toBe(14);
|
||||
});
|
||||
|
||||
it('should default invalid target to github-artifacts', () => {
|
||||
const config = ArtifactUploadHandler.parseConfig('invalid', undefined, 'none', '30');
|
||||
expect(config.target).toBe('github-artifacts');
|
||||
});
|
||||
|
||||
it('should default invalid compression to gzip', () => {
|
||||
const config = ArtifactUploadHandler.parseConfig('local', '/dest', 'brotli', '30');
|
||||
expect(config.compression).toBe('gzip');
|
||||
});
|
||||
|
||||
it('should default invalid retention to 30 days', () => {
|
||||
const config = ArtifactUploadHandler.parseConfig('local', '/dest', 'gzip', 'abc');
|
||||
expect(config.retentionDays).toBe(30);
|
||||
});
|
||||
|
||||
it('should default negative retention to 30 days', () => {
|
||||
const config = ArtifactUploadHandler.parseConfig('local', '/dest', 'gzip', '-5');
|
||||
expect(config.retentionDays).toBe(30);
|
||||
});
|
||||
|
||||
it('should set destination to undefined when empty string', () => {
|
||||
const config = ArtifactUploadHandler.parseConfig('storage', '', 'none', '7');
|
||||
expect(config.destination).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('uploadArtifacts', () => {
|
||||
it('should skip upload when target is none', async () => {
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid: 'test-guid',
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [{ type: 'build', path: './Builds/' }],
|
||||
};
|
||||
|
||||
const config: ArtifactUploadConfig = {
|
||||
target: 'none',
|
||||
compression: 'gzip',
|
||||
retentionDays: 30,
|
||||
};
|
||||
|
||||
const result = await ArtifactUploadHandler.uploadArtifacts(manifest, config, projectPath);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.entries).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should return success with no entries for empty manifest', async () => {
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid: 'test-guid',
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [],
|
||||
};
|
||||
|
||||
const config: ArtifactUploadConfig = {
|
||||
target: 'github-artifacts',
|
||||
compression: 'gzip',
|
||||
retentionDays: 30,
|
||||
};
|
||||
|
||||
const result = await ArtifactUploadHandler.uploadArtifacts(manifest, config, projectPath);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.entries).toHaveLength(0);
|
||||
expect(result.totalBytes).toBe(0);
|
||||
});
|
||||
|
||||
it('should fail entry when output path does not exist', async () => {
|
||||
mockedFs.existsSync.mockReturnValue(false);
|
||||
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid: 'test-guid',
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [{ type: 'build', path: './Builds/Missing/' }],
|
||||
};
|
||||
|
||||
const config: ArtifactUploadConfig = {
|
||||
target: 'local',
|
||||
destination: '/output',
|
||||
compression: 'none',
|
||||
retentionDays: 30,
|
||||
};
|
||||
|
||||
const result = await ArtifactUploadHandler.uploadArtifacts(manifest, config, projectPath);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.entries).toHaveLength(1);
|
||||
expect(result.entries[0].success).toBe(false);
|
||||
expect(result.entries[0].error).toContain('does not exist');
|
||||
});
|
||||
|
||||
it('should copy files for local upload target', async () => {
|
||||
mockedFs.existsSync.mockReturnValue(true);
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => false, size: 1024 } as any);
|
||||
mockedFs.mkdirSync.mockReturnValue(undefined);
|
||||
mockedFs.copyFileSync.mockReturnValue(undefined);
|
||||
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid: 'test-guid',
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [{ type: 'logs', path: './Logs/build.log', size: 1024 }],
|
||||
};
|
||||
|
||||
const config: ArtifactUploadConfig = {
|
||||
target: 'local',
|
||||
destination: '/output',
|
||||
compression: 'none',
|
||||
retentionDays: 30,
|
||||
};
|
||||
|
||||
const result = await ArtifactUploadHandler.uploadArtifacts(manifest, config, projectPath);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.entries).toHaveLength(1);
|
||||
expect(result.entries[0].success).toBe(true);
|
||||
expect(result.totalBytes).toBe(1024);
|
||||
});
|
||||
|
||||
it('should fail local upload when no destination is provided', async () => {
|
||||
mockedFs.existsSync.mockReturnValue(true);
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => false, size: 512 } as any);
|
||||
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid: 'test-guid',
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [{ type: 'logs', path: './Logs/build.log', size: 512 }],
|
||||
};
|
||||
|
||||
const config: ArtifactUploadConfig = {
|
||||
target: 'local',
|
||||
compression: 'none',
|
||||
retentionDays: 30,
|
||||
};
|
||||
|
||||
const result = await ArtifactUploadHandler.uploadArtifacts(manifest, config, projectPath);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.entries[0].success).toBe(false);
|
||||
expect(result.entries[0].error).toContain('destination path');
|
||||
});
|
||||
|
||||
it('should report correct duration', async () => {
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid: 'test-guid',
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [],
|
||||
};
|
||||
|
||||
const config: ArtifactUploadConfig = {
|
||||
target: 'none',
|
||||
compression: 'gzip',
|
||||
retentionDays: 30,
|
||||
};
|
||||
|
||||
const result = await ArtifactUploadHandler.uploadArtifacts(manifest, config, projectPath);
|
||||
expect(result.durationMs).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('collectFiles', () => {
|
||||
it('should return single file for a file path', () => {
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => false } as any);
|
||||
|
||||
const files = ArtifactUploadHandler.collectFiles('/path/to/file.txt');
|
||||
expect(files).toEqual(['/path/to/file.txt']);
|
||||
});
|
||||
|
||||
it('should return all files recursively for a directory', () => {
|
||||
mockedFs.statSync.mockImplementation((p: any) => {
|
||||
const pathStr = typeof p === 'string' ? p : p.toString();
|
||||
if (pathStr.endsWith('.txt') || pathStr.endsWith('.log')) {
|
||||
return { isDirectory: () => false } as any;
|
||||
}
|
||||
|
||||
return { isDirectory: () => true } as any;
|
||||
});
|
||||
|
||||
mockedFs.readdirSync.mockImplementation((dirPath: any, _options?: any) => {
|
||||
const dirStr = typeof dirPath === 'string' ? dirPath : dirPath.toString();
|
||||
if (dirStr === '/root') {
|
||||
return [
|
||||
{ name: 'file1.txt', isDirectory: () => false },
|
||||
{ name: 'sub', isDirectory: () => true },
|
||||
] as any;
|
||||
}
|
||||
if (dirStr.endsWith('sub')) {
|
||||
return [{ name: 'file2.log', isDirectory: () => false }] as any;
|
||||
}
|
||||
|
||||
return [] as any;
|
||||
});
|
||||
|
||||
const files = ArtifactUploadHandler.collectFiles('/root');
|
||||
expect(files).toHaveLength(2);
|
||||
expect(files).toContain(path.join('/root', 'file1.txt'));
|
||||
expect(files).toContain(path.join('/root', 'sub', 'file2.log'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('storage upload validation', () => {
|
||||
it('should fail storage upload when no destination is provided', async () => {
|
||||
mockedFs.existsSync.mockReturnValue(true);
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => false, size: 256 } as any);
|
||||
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid: 'test-guid',
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [{ type: 'build', path: './Builds/', size: 256 }],
|
||||
};
|
||||
|
||||
const config: ArtifactUploadConfig = {
|
||||
target: 'storage',
|
||||
compression: 'gzip',
|
||||
retentionDays: 30,
|
||||
};
|
||||
|
||||
const result = await ArtifactUploadHandler.uploadArtifacts(manifest, config, projectPath);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.entries[0].error).toContain('destination URI');
|
||||
});
|
||||
|
||||
it('should fail storage upload when destination URI has invalid format', async () => {
|
||||
mockedFs.existsSync.mockReturnValue(true);
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => false, size: 256 } as any);
|
||||
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid: 'test-guid',
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [{ type: 'build', path: './Builds/', size: 256 }],
|
||||
};
|
||||
|
||||
const config: ArtifactUploadConfig = {
|
||||
target: 'storage',
|
||||
destination: '/just/a/local/path',
|
||||
compression: 'gzip',
|
||||
retentionDays: 30,
|
||||
};
|
||||
|
||||
const result = await ArtifactUploadHandler.uploadArtifacts(manifest, config, projectPath);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.entries[0].error).toContain('Invalid storage destination URI');
|
||||
});
|
||||
|
||||
it('should fail storage upload when rclone is not installed', async () => {
|
||||
// Mock child_process.execFileSync to throw (rclone not found)
|
||||
const childProcess = require('node:child_process');
|
||||
const originalExecFileSync = childProcess.execFileSync;
|
||||
childProcess.execFileSync = jest.fn(() => {
|
||||
throw new Error('ENOENT');
|
||||
});
|
||||
|
||||
mockedFs.existsSync.mockReturnValue(true);
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => false, size: 256 } as any);
|
||||
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid: 'test-guid',
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [{ type: 'build', path: './Builds/', size: 256 }],
|
||||
};
|
||||
|
||||
const config: ArtifactUploadConfig = {
|
||||
target: 'storage',
|
||||
destination: 's3:my-bucket/artifacts',
|
||||
compression: 'gzip',
|
||||
retentionDays: 30,
|
||||
};
|
||||
|
||||
const result = await ArtifactUploadHandler.uploadArtifacts(manifest, config, projectPath);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.entries[0].error).toContain('rclone is not installed');
|
||||
|
||||
// Restore
|
||||
childProcess.execFileSync = originalExecFileSync;
|
||||
});
|
||||
|
||||
it('should accept valid rclone storage URI formats', async () => {
|
||||
// Mock child_process.execFileSync to succeed (rclone available)
|
||||
const childProcess = require('node:child_process');
|
||||
const originalExecFileSync = childProcess.execFileSync;
|
||||
childProcess.execFileSync = jest.fn(() => 'rclone v1.65.0');
|
||||
|
||||
mockedFs.existsSync.mockReturnValue(true);
|
||||
mockedFs.statSync.mockReturnValue({ isDirectory: () => false, size: 256 } as any);
|
||||
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid: 'test-guid',
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [{ type: 'build', path: './Builds/', size: 256 }],
|
||||
};
|
||||
|
||||
// s3:bucket format should pass URI validation and reach the exec call
|
||||
const config: ArtifactUploadConfig = {
|
||||
target: 'storage',
|
||||
destination: 's3:my-bucket/artifacts',
|
||||
compression: 'gzip',
|
||||
retentionDays: 30,
|
||||
};
|
||||
|
||||
const result = await ArtifactUploadHandler.uploadArtifacts(manifest, config, projectPath);
|
||||
// Should succeed because exec is mocked to return 0
|
||||
expect(result.entries[0].success).toBe(true);
|
||||
|
||||
// Restore
|
||||
childProcess.execFileSync = originalExecFileSync;
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,474 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { exec } from '@actions/exec';
|
||||
import OrchestratorLogger from '../core/orchestrator-logger';
|
||||
import { OutputManifest, OutputEntry } from './output-manifest';
|
||||
|
||||
/**
|
||||
* Configuration for artifact upload.
|
||||
*/
|
||||
export interface ArtifactUploadConfig {
|
||||
/** Upload target: 'github-artifacts', 'storage', 'local', 'none' */
|
||||
target: 'github-artifacts' | 'storage' | 'local' | 'none';
|
||||
|
||||
/** Destination path — storage URI for 'storage', local path for 'local' */
|
||||
destination?: string;
|
||||
|
||||
/** Compression method */
|
||||
compression: 'none' | 'gzip' | 'lz4';
|
||||
|
||||
/** Retention period in days (GitHub Artifacts only) */
|
||||
retentionDays: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of an artifact upload operation.
|
||||
*/
|
||||
export interface UploadResult {
|
||||
/** Whether the upload succeeded overall */
|
||||
success: boolean;
|
||||
|
||||
/** Per-entry upload results */
|
||||
entries: UploadEntryResult[];
|
||||
|
||||
/** Total bytes uploaded */
|
||||
totalBytes: number;
|
||||
|
||||
/** Duration in milliseconds */
|
||||
durationMs: number;
|
||||
}
|
||||
|
||||
export interface UploadEntryResult {
|
||||
/** The output type name */
|
||||
type: string;
|
||||
|
||||
/** The output path */
|
||||
path: string;
|
||||
|
||||
/** Whether this entry uploaded successfully */
|
||||
success: boolean;
|
||||
|
||||
/** Bytes uploaded for this entry */
|
||||
bytes: number;
|
||||
|
||||
/** Error message if upload failed */
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* GitHub Artifacts size limit per artifact (10 GB).
|
||||
* Files larger than this must be split.
|
||||
*/
|
||||
const GITHUB_ARTIFACT_SIZE_LIMIT = 10 * 1024 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* Minimum valid storage URI pattern: "remote:path" or "remote:".
|
||||
* rclone requires at least a remote name followed by a colon.
|
||||
*/
|
||||
const STORAGE_URI_PATTERN = /^[a-zA-Z][\w-]*:/;
|
||||
|
||||
/**
|
||||
* Check whether rclone is installed and available on PATH.
|
||||
* Returns true if `rclone version` executes successfully.
|
||||
*/
|
||||
function isRcloneAvailable(): boolean {
|
||||
try {
|
||||
execFileSync('rclone', ['version'], { stdio: 'pipe', timeout: 5000 });
|
||||
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a storage destination URI has the correct rclone format.
|
||||
* Valid format: "remoteName:path" (e.g., "s3:bucket/prefix", "gdrive:folder").
|
||||
*/
|
||||
function isValidStorageUri(uri: string): boolean {
|
||||
return STORAGE_URI_PATTERN.test(uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles uploading build artifacts to various targets.
|
||||
*/
|
||||
export class ArtifactUploadHandler {
|
||||
/**
|
||||
* Upload artifacts described by a manifest to the configured target.
|
||||
*/
|
||||
static async uploadArtifacts(
|
||||
manifest: OutputManifest,
|
||||
config: ArtifactUploadConfig,
|
||||
projectPath: string,
|
||||
): Promise<UploadResult> {
|
||||
const startTime = Date.now();
|
||||
const result: UploadResult = {
|
||||
success: true,
|
||||
entries: [],
|
||||
totalBytes: 0,
|
||||
durationMs: 0,
|
||||
};
|
||||
|
||||
if (config.target === 'none') {
|
||||
OrchestratorLogger.log('[ArtifactUpload] Upload target is "none", skipping upload');
|
||||
result.durationMs = Date.now() - startTime;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (manifest.outputs.length === 0) {
|
||||
OrchestratorLogger.log('[ArtifactUpload] No outputs in manifest, nothing to upload');
|
||||
result.durationMs = Date.now() - startTime;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(`[ArtifactUpload] Uploading ${manifest.outputs.length} output(s) to ${config.target}`);
|
||||
|
||||
for (const entry of manifest.outputs) {
|
||||
const entryResult = await ArtifactUploadHandler.uploadEntry(entry, config, projectPath);
|
||||
result.entries.push(entryResult);
|
||||
result.totalBytes += entryResult.bytes;
|
||||
|
||||
if (!entryResult.success) {
|
||||
result.success = false;
|
||||
}
|
||||
}
|
||||
|
||||
result.durationMs = Date.now() - startTime;
|
||||
|
||||
OrchestratorLogger.log(
|
||||
`[ArtifactUpload] Upload complete: ${result.entries.filter((e) => e.success).length}/${
|
||||
result.entries.length
|
||||
} succeeded, ${result.totalBytes} bytes, ${result.durationMs}ms`,
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a single output entry.
|
||||
*/
|
||||
private static async uploadEntry(
|
||||
entry: OutputEntry,
|
||||
config: ArtifactUploadConfig,
|
||||
projectPath: string,
|
||||
): Promise<UploadEntryResult> {
|
||||
const entryResult: UploadEntryResult = {
|
||||
type: entry.type,
|
||||
path: entry.path,
|
||||
success: false,
|
||||
bytes: entry.size || 0,
|
||||
};
|
||||
|
||||
const resolvedPath = path.resolve(
|
||||
projectPath,
|
||||
entry.path.replace('{platform}', process.env.BUILD_TARGET || 'Unknown'),
|
||||
);
|
||||
|
||||
if (!fs.existsSync(resolvedPath)) {
|
||||
entryResult.error = `Output path does not exist: ${resolvedPath}`;
|
||||
OrchestratorLogger.logWarning(`[ArtifactUpload] ${entryResult.error}`);
|
||||
|
||||
return entryResult;
|
||||
}
|
||||
|
||||
try {
|
||||
switch (config.target) {
|
||||
case 'github-artifacts':
|
||||
await ArtifactUploadHandler.uploadToGitHubArtifacts(entry, resolvedPath, config);
|
||||
break;
|
||||
case 'storage':
|
||||
await ArtifactUploadHandler.uploadToStorage(entry, resolvedPath, config);
|
||||
break;
|
||||
case 'local':
|
||||
await ArtifactUploadHandler.uploadToLocal(entry, resolvedPath, config);
|
||||
break;
|
||||
}
|
||||
entryResult.success = true;
|
||||
OrchestratorLogger.log(
|
||||
`[ArtifactUpload] Uploaded '${entry.type}' (${entryResult.bytes} bytes) to ${config.target}`,
|
||||
);
|
||||
} catch (error: any) {
|
||||
entryResult.error = error.message || String(error);
|
||||
OrchestratorLogger.logWarning(`[ArtifactUpload] Failed to upload '${entry.type}': ${entryResult.error}`);
|
||||
}
|
||||
|
||||
return entryResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload to GitHub Artifacts via @actions/artifact.
|
||||
* Handles large file splitting if artifacts exceed the size limit.
|
||||
*/
|
||||
private static async uploadToGitHubArtifacts(
|
||||
entry: OutputEntry,
|
||||
resolvedPath: string,
|
||||
config: ArtifactUploadConfig,
|
||||
): Promise<void> {
|
||||
// Dynamically require @actions/artifact — it may not be available in all environments.
|
||||
// Using a variable to prevent TypeScript from resolving the module at compile time.
|
||||
let artifact: any;
|
||||
try {
|
||||
const artifactModule = '@actions/artifact';
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
artifact = require(artifactModule);
|
||||
} catch {
|
||||
throw new Error('@actions/artifact package is not available. Install it to use github-artifacts upload target.');
|
||||
}
|
||||
|
||||
const artifactClient = artifact.DefaultArtifactClient
|
||||
? new artifact.DefaultArtifactClient()
|
||||
: artifact.default
|
||||
? new artifact.default()
|
||||
: artifact;
|
||||
|
||||
const files = ArtifactUploadHandler.collectFiles(resolvedPath);
|
||||
|
||||
if (files.length === 0) {
|
||||
OrchestratorLogger.logWarning(`[ArtifactUpload] No files found at ${resolvedPath} for '${entry.type}'`);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const totalSize = entry.size || 0;
|
||||
const artifactName = `unity-output-${entry.type}`;
|
||||
|
||||
if (totalSize > GITHUB_ARTIFACT_SIZE_LIMIT) {
|
||||
OrchestratorLogger.log(
|
||||
`[ArtifactUpload] Output '${entry.type}' exceeds GitHub Artifacts size limit (${totalSize} > ${GITHUB_ARTIFACT_SIZE_LIMIT}), splitting into chunks`,
|
||||
);
|
||||
await ArtifactUploadHandler.uploadChunked(artifactClient, artifactName, files, resolvedPath, config);
|
||||
} else {
|
||||
const rootDirectory = fs.statSync(resolvedPath).isDirectory() ? resolvedPath : path.dirname(resolvedPath);
|
||||
|
||||
if (typeof artifactClient.uploadArtifact === 'function') {
|
||||
await artifactClient.uploadArtifact(artifactName, files, rootDirectory, {
|
||||
retentionDays: config.retentionDays,
|
||||
compressionLevel: config.compression === 'none' ? 0 : 6,
|
||||
});
|
||||
} else {
|
||||
throw new Error(
|
||||
'@actions/artifact client does not have uploadArtifact method. Ensure the package version is compatible.',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload large artifacts in chunks to stay within GitHub size limits.
|
||||
*/
|
||||
private static async uploadChunked(
|
||||
artifactClient: any,
|
||||
baseName: string,
|
||||
files: string[],
|
||||
rootDirectory: string,
|
||||
config: ArtifactUploadConfig,
|
||||
): Promise<void> {
|
||||
const chunkSize = GITHUB_ARTIFACT_SIZE_LIMIT;
|
||||
let currentChunkFiles: string[] = [];
|
||||
let currentChunkSize = 0;
|
||||
let chunkIndex = 0;
|
||||
|
||||
for (const filePath of files) {
|
||||
const fileSize = fs.statSync(filePath).size;
|
||||
|
||||
if (currentChunkSize + fileSize > chunkSize && currentChunkFiles.length > 0) {
|
||||
await ArtifactUploadHandler.uploadSingleChunk(
|
||||
artifactClient,
|
||||
`${baseName}-part${chunkIndex}`,
|
||||
currentChunkFiles,
|
||||
rootDirectory,
|
||||
config,
|
||||
);
|
||||
chunkIndex++;
|
||||
currentChunkFiles = [];
|
||||
currentChunkSize = 0;
|
||||
}
|
||||
|
||||
currentChunkFiles.push(filePath);
|
||||
currentChunkSize += fileSize;
|
||||
}
|
||||
|
||||
if (currentChunkFiles.length > 0) {
|
||||
await ArtifactUploadHandler.uploadSingleChunk(
|
||||
artifactClient,
|
||||
chunkIndex > 0 ? `${baseName}-part${chunkIndex}` : baseName,
|
||||
currentChunkFiles,
|
||||
rootDirectory,
|
||||
config,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static async uploadSingleChunk(
|
||||
artifactClient: any,
|
||||
name: string,
|
||||
files: string[],
|
||||
rootDirectory: string,
|
||||
config: ArtifactUploadConfig,
|
||||
): Promise<void> {
|
||||
OrchestratorLogger.log(`[ArtifactUpload] Uploading chunk '${name}' with ${files.length} file(s)`);
|
||||
|
||||
if (typeof artifactClient.uploadArtifact === 'function') {
|
||||
await artifactClient.uploadArtifact(name, files, rootDirectory, {
|
||||
retentionDays: config.retentionDays,
|
||||
compressionLevel: config.compression === 'none' ? 0 : 6,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload to remote storage via rclone.
|
||||
*
|
||||
* Validates rclone availability and destination URI format before attempting
|
||||
* the upload. If rclone is not installed, falls back to local copy when a
|
||||
* local-compatible destination is provided, or skips with a clear error.
|
||||
*/
|
||||
private static async uploadToStorage(
|
||||
entry: OutputEntry,
|
||||
resolvedPath: string,
|
||||
config: ArtifactUploadConfig,
|
||||
): Promise<void> {
|
||||
if (!config.destination) {
|
||||
throw new Error('Storage upload requires a destination URI in artifactUploadPath');
|
||||
}
|
||||
|
||||
// Validate storage URI format before attempting upload
|
||||
if (!isValidStorageUri(config.destination)) {
|
||||
throw new Error(
|
||||
`Invalid storage destination URI: "${config.destination}". ` +
|
||||
'Expected rclone remote format "remoteName:path" (e.g., "s3:my-bucket/artifacts", "gdrive:builds").',
|
||||
);
|
||||
}
|
||||
|
||||
// Check rclone availability before attempting upload
|
||||
if (!isRcloneAvailable()) {
|
||||
OrchestratorLogger.error(
|
||||
'rclone is not installed or not in PATH. ' +
|
||||
'Install rclone (https://rclone.org/install/) to use storage-based artifact upload. ' +
|
||||
'Falling back to local copy.',
|
||||
);
|
||||
|
||||
// Attempt local copy fallback using the destination as a hint
|
||||
// Strip the remote prefix to get a local-ish path for fallback
|
||||
OrchestratorLogger.logWarning(
|
||||
`[ArtifactUpload] Storage upload skipped for '${entry.type}' — rclone not available`,
|
||||
);
|
||||
throw new Error(
|
||||
'rclone is not installed or not in PATH. ' +
|
||||
'Install rclone from https://rclone.org/install/ to use storage-based artifact upload.',
|
||||
);
|
||||
}
|
||||
|
||||
const destination = `${config.destination}/${entry.type}`;
|
||||
|
||||
OrchestratorLogger.log(`[ArtifactUpload] Uploading '${entry.type}' to storage: ${destination}`);
|
||||
|
||||
const args = ['copy', resolvedPath, destination, '--progress'];
|
||||
|
||||
if (config.compression !== 'none') {
|
||||
// rclone doesn't have built-in compression flags for copy;
|
||||
// compression is typically handled by the remote configuration.
|
||||
// Log as informational.
|
||||
OrchestratorLogger.log(
|
||||
`[ArtifactUpload] Note: compression '${config.compression}' is configured at the remote level for rclone`,
|
||||
);
|
||||
}
|
||||
|
||||
await exec('rclone', args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload to a local path (copy).
|
||||
*/
|
||||
private static async uploadToLocal(
|
||||
entry: OutputEntry,
|
||||
resolvedPath: string,
|
||||
config: ArtifactUploadConfig,
|
||||
): Promise<void> {
|
||||
if (!config.destination) {
|
||||
throw new Error('Local upload requires a destination path in artifactUploadPath');
|
||||
}
|
||||
|
||||
const destination = path.join(config.destination, entry.type);
|
||||
fs.mkdirSync(destination, { recursive: true });
|
||||
|
||||
OrchestratorLogger.log(`[ArtifactUpload] Copying '${entry.type}' to local path: ${destination}`);
|
||||
|
||||
ArtifactUploadHandler.copyRecursive(resolvedPath, destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively copy files from source to destination.
|
||||
*/
|
||||
private static copyRecursive(source: string, destination: string): void {
|
||||
const stat = fs.statSync(source);
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
fs.mkdirSync(destination, { recursive: true });
|
||||
const entries = fs.readdirSync(source);
|
||||
for (const entry of entries) {
|
||||
ArtifactUploadHandler.copyRecursive(path.join(source, entry), path.join(destination, entry));
|
||||
}
|
||||
} else {
|
||||
fs.copyFileSync(source, destination);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect all files at a given path (recursively if directory).
|
||||
*/
|
||||
static collectFiles(targetPath: string): string[] {
|
||||
const stat = fs.statSync(targetPath);
|
||||
|
||||
if (!stat.isDirectory()) {
|
||||
return [targetPath];
|
||||
}
|
||||
|
||||
const files: string[] = [];
|
||||
const entries = fs.readdirSync(targetPath, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(targetPath, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
files.push(...ArtifactUploadHandler.collectFiles(fullPath));
|
||||
} else {
|
||||
files.push(fullPath);
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an ArtifactUploadConfig from action inputs.
|
||||
*/
|
||||
static parseConfig(
|
||||
target: string,
|
||||
destination: string | undefined,
|
||||
compression: string,
|
||||
retentionDays: string,
|
||||
): ArtifactUploadConfig {
|
||||
const validTargets = ['github-artifacts', 'storage', 'local', 'none'] as const;
|
||||
const resolvedTarget = validTargets.includes(target as any)
|
||||
? (target as ArtifactUploadConfig['target'])
|
||||
: 'github-artifacts';
|
||||
|
||||
const validCompressions = ['none', 'gzip', 'lz4'] as const;
|
||||
const resolvedCompression = validCompressions.includes(compression as any)
|
||||
? (compression as ArtifactUploadConfig['compression'])
|
||||
: 'gzip';
|
||||
|
||||
const parsedRetention = Number.parseInt(retentionDays, 10);
|
||||
const resolvedRetention = Number.isNaN(parsedRetention) || parsedRetention <= 0 ? 30 : parsedRetention;
|
||||
|
||||
return {
|
||||
target: resolvedTarget,
|
||||
destination: destination || undefined,
|
||||
compression: resolvedCompression,
|
||||
retentionDays: resolvedRetention,
|
||||
};
|
||||
}
|
||||
}
|
||||
3
src/model/orchestrator/services/output/index.ts
Normal file
3
src/model/orchestrator/services/output/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { OutputManifest, OutputEntry } from './output-manifest';
|
||||
export { OutputTypeRegistry, OutputTypeDefinition } from './output-type-registry';
|
||||
export { OutputService } from './output-service';
|
||||
41
src/model/orchestrator/services/output/output-manifest.ts
Normal file
41
src/model/orchestrator/services/output/output-manifest.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Structured build output manifest.
|
||||
* Describes all artifacts produced by a build with type, path, size, hash, and metadata.
|
||||
*/
|
||||
|
||||
export interface OutputEntry {
|
||||
/** Output type identifier (e.g., 'build', 'test-results', 'images') */
|
||||
type: string;
|
||||
|
||||
/** Relative path to the output */
|
||||
path: string;
|
||||
|
||||
/** Output format (e.g., 'nunit3', 'junit', 'json') */
|
||||
format?: string;
|
||||
|
||||
/** File size in bytes */
|
||||
size?: number;
|
||||
|
||||
/** Content hash (e.g., 'sha256:abc...') */
|
||||
hash?: string;
|
||||
|
||||
/** Individual files within the output path */
|
||||
files?: string[];
|
||||
|
||||
/** Type-specific summary (e.g., test counts, build size) */
|
||||
summary?: Record<string, unknown>;
|
||||
|
||||
/** Arbitrary metadata */
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface OutputManifest {
|
||||
/** Unique build identifier */
|
||||
buildGuid: string;
|
||||
|
||||
/** ISO 8601 timestamp */
|
||||
timestamp: string;
|
||||
|
||||
/** All outputs produced by this build */
|
||||
outputs: OutputEntry[];
|
||||
}
|
||||
118
src/model/orchestrator/services/output/output-service.ts
Normal file
118
src/model/orchestrator/services/output/output-service.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import OrchestratorLogger from '../core/orchestrator-logger';
|
||||
import { OutputManifest, OutputEntry } from './output-manifest';
|
||||
import { OutputTypeRegistry } from './output-type-registry';
|
||||
|
||||
/**
|
||||
* Service for collecting, manifesting, and managing build outputs.
|
||||
*
|
||||
* After a build completes, this service scans declared output paths,
|
||||
* generates a structured manifest, and prepares outputs for post-processing.
|
||||
*/
|
||||
export class OutputService {
|
||||
/**
|
||||
* Collect outputs from the workspace and generate a manifest.
|
||||
*
|
||||
* @param projectPath - Path to the Unity project root
|
||||
* @param buildGuid - Unique build identifier
|
||||
* @param outputTypesInput - Comma-separated output type names
|
||||
* @param manifestPath - Where to write the manifest JSON (optional)
|
||||
* @returns The generated output manifest
|
||||
*/
|
||||
static async collectOutputs(
|
||||
projectPath: string,
|
||||
buildGuid: string,
|
||||
outputTypesInput: string,
|
||||
manifestPath?: string,
|
||||
): Promise<OutputManifest> {
|
||||
const types = OutputTypeRegistry.parseOutputTypes(outputTypesInput);
|
||||
const manifest: OutputManifest = {
|
||||
buildGuid,
|
||||
timestamp: new Date().toISOString(),
|
||||
outputs: [],
|
||||
};
|
||||
|
||||
if (types.length === 0) {
|
||||
OrchestratorLogger.log('[Output] No output types declared, skipping collection');
|
||||
|
||||
return manifest;
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(
|
||||
`[Output] Collecting ${types.length} output type(s): ${types.map((t) => t.name).join(', ')}`,
|
||||
);
|
||||
|
||||
for (const typeDef of types) {
|
||||
const outputPath = path.join(
|
||||
projectPath,
|
||||
typeDef.defaultPath.replace('{platform}', process.env.BUILD_TARGET || 'Unknown'),
|
||||
);
|
||||
|
||||
if (!fs.existsSync(outputPath)) {
|
||||
OrchestratorLogger.log(`[Output] No output found for '${typeDef.name}' at ${outputPath}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const entry: OutputEntry = {
|
||||
type: typeDef.name,
|
||||
path: typeDef.defaultPath,
|
||||
};
|
||||
|
||||
// Collect file listing for directory outputs
|
||||
try {
|
||||
const stat = fs.statSync(outputPath);
|
||||
if (stat.isDirectory()) {
|
||||
entry.files = fs.readdirSync(outputPath);
|
||||
entry.size = OutputService.getDirectorySize(outputPath);
|
||||
} else {
|
||||
entry.size = stat.size;
|
||||
}
|
||||
} catch {
|
||||
OrchestratorLogger.logWarning(`[Output] Failed to stat output '${typeDef.name}' at ${outputPath}`);
|
||||
}
|
||||
|
||||
manifest.outputs.push(entry);
|
||||
OrchestratorLogger.log(
|
||||
`[Output] Collected '${typeDef.name}': ${entry.files?.length || 1} file(s), ${entry.size || 0} bytes`,
|
||||
);
|
||||
}
|
||||
|
||||
// Write manifest to disk
|
||||
if (manifestPath) {
|
||||
try {
|
||||
const manifestDir = path.dirname(manifestPath);
|
||||
fs.mkdirSync(manifestDir, { recursive: true });
|
||||
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), 'utf8');
|
||||
OrchestratorLogger.log(`[Output] Manifest written to ${manifestPath}`);
|
||||
} catch (error: any) {
|
||||
OrchestratorLogger.logWarning(`[Output] Failed to write manifest: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
return manifest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate total size of a directory recursively.
|
||||
*/
|
||||
private static getDirectorySize(dirPath: string): number {
|
||||
let totalSize = 0;
|
||||
|
||||
try {
|
||||
const entries = fs.readdirSync(dirPath, { withFileTypes: true });
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(dirPath, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
totalSize += OutputService.getDirectorySize(fullPath);
|
||||
} else {
|
||||
totalSize += fs.statSync(fullPath).size;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Ignore errors in size calculation
|
||||
}
|
||||
|
||||
return totalSize;
|
||||
}
|
||||
}
|
||||
136
src/model/orchestrator/services/output/output-type-registry.ts
Normal file
136
src/model/orchestrator/services/output/output-type-registry.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import OrchestratorLogger from '../core/orchestrator-logger';
|
||||
|
||||
/**
|
||||
* Registry of known output types with default paths and processing hints.
|
||||
*/
|
||||
|
||||
export interface OutputTypeDefinition {
|
||||
/** Type identifier */
|
||||
name: string;
|
||||
|
||||
/** Default output path (relative to project root) */
|
||||
defaultPath: string;
|
||||
|
||||
/** Human-readable description */
|
||||
description: string;
|
||||
|
||||
/** Whether this type is built-in or user-registered */
|
||||
builtIn: boolean;
|
||||
}
|
||||
|
||||
export class OutputTypeRegistry {
|
||||
private static readonly builtInTypes: Record<string, OutputTypeDefinition> = {
|
||||
build: {
|
||||
name: 'build',
|
||||
defaultPath: './Builds/{platform}/',
|
||||
description: 'Standard game build artifact',
|
||||
builtIn: true,
|
||||
},
|
||||
'test-results': {
|
||||
name: 'test-results',
|
||||
defaultPath: './TestResults/',
|
||||
description: 'NUnit/JUnit XML test results',
|
||||
builtIn: true,
|
||||
},
|
||||
'server-build': {
|
||||
name: 'server-build',
|
||||
defaultPath: './Builds/{platform}-server/',
|
||||
description: 'Dedicated server build artifact',
|
||||
builtIn: true,
|
||||
},
|
||||
'data-export': {
|
||||
name: 'data-export',
|
||||
defaultPath: './Exports/',
|
||||
description: 'Exported data files (CSV, JSON, binary)',
|
||||
builtIn: true,
|
||||
},
|
||||
images: {
|
||||
name: 'images',
|
||||
defaultPath: './Captures/',
|
||||
description: 'Screenshots, render captures, atlas previews',
|
||||
builtIn: true,
|
||||
},
|
||||
logs: {
|
||||
name: 'logs',
|
||||
defaultPath: './Logs/',
|
||||
description: 'Structured build and test logs',
|
||||
builtIn: true,
|
||||
},
|
||||
metrics: {
|
||||
name: 'metrics',
|
||||
defaultPath: './Metrics/',
|
||||
description: 'Build performance metrics and asset statistics',
|
||||
builtIn: true,
|
||||
},
|
||||
coverage: {
|
||||
name: 'coverage',
|
||||
defaultPath: './Coverage/',
|
||||
description: 'Code coverage reports',
|
||||
builtIn: true,
|
||||
},
|
||||
};
|
||||
|
||||
private static customTypes: Record<string, OutputTypeDefinition> = {};
|
||||
|
||||
/**
|
||||
* Get a type definition by name. Checks custom types first, then built-in.
|
||||
*/
|
||||
static getType(name: string): OutputTypeDefinition | undefined {
|
||||
return OutputTypeRegistry.customTypes[name] || OutputTypeRegistry.builtInTypes[name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered types (built-in + custom).
|
||||
*/
|
||||
static getAllTypes(): OutputTypeDefinition[] {
|
||||
return [...Object.values(OutputTypeRegistry.builtInTypes), ...Object.values(OutputTypeRegistry.customTypes)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a custom output type.
|
||||
*/
|
||||
static registerType(definition: OutputTypeDefinition): void {
|
||||
if (OutputTypeRegistry.builtInTypes[definition.name]) {
|
||||
OrchestratorLogger.logWarning(`[OutputTypes] Cannot override built-in type '${definition.name}'`);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
OutputTypeRegistry.customTypes[definition.name] = { ...definition, builtIn: false };
|
||||
OrchestratorLogger.log(`[OutputTypes] Registered custom type '${definition.name}'`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a comma-separated output types string into type definitions.
|
||||
* Unknown types are logged as warnings and skipped.
|
||||
*/
|
||||
static parseOutputTypes(outputTypesInput: string): OutputTypeDefinition[] {
|
||||
if (!outputTypesInput) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const names = outputTypesInput
|
||||
.split(',')
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean);
|
||||
const types: OutputTypeDefinition[] = [];
|
||||
|
||||
for (const name of names) {
|
||||
const typeDef = OutputTypeRegistry.getType(name);
|
||||
if (typeDef) {
|
||||
types.push(typeDef);
|
||||
} else {
|
||||
OrchestratorLogger.logWarning(`[OutputTypes] Unknown output type '${name}', skipping`);
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset custom types (for testing).
|
||||
*/
|
||||
static resetCustomTypes(): void {
|
||||
OutputTypeRegistry.customTypes = {};
|
||||
}
|
||||
}
|
||||
315
src/model/orchestrator/services/sync/incremental-sync-service.ts
Normal file
315
src/model/orchestrator/services/sync/incremental-sync-service.ts
Normal file
@@ -0,0 +1,315 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { OrchestratorSystem } from '../core/orchestrator-system';
|
||||
import OrchestratorLogger from '../core/orchestrator-logger';
|
||||
import { SyncState, SyncStrategy } from './sync-state';
|
||||
import { SyncStateManager } from './sync-state-manager';
|
||||
|
||||
/**
|
||||
* Service for incremental workspace synchronization.
|
||||
*
|
||||
* Supports multiple sync strategies:
|
||||
* - full: Traditional clone + cache restore (default)
|
||||
* - git-delta: Fetch and apply only changed files since last sync
|
||||
* - direct-input: Apply file changes passed as job input (no git push required)
|
||||
* - storage-pull: Fetch changed files from rclone-backed generic storage
|
||||
*/
|
||||
export class IncrementalSyncService {
|
||||
/**
|
||||
* Load sync state from the workspace.
|
||||
*/
|
||||
static loadSyncState(workspacePath: string, statePath?: string): SyncState | undefined {
|
||||
return SyncStateManager.loadState(workspacePath, statePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save sync state to the workspace.
|
||||
*/
|
||||
static saveSyncState(workspacePath: string, state: SyncState, statePath?: string): void {
|
||||
SyncStateManager.saveState(workspacePath, state, statePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the appropriate sync strategy based on workspace state and configuration.
|
||||
*/
|
||||
static resolveStrategy(requestedStrategy: SyncStrategy, workspacePath: string, statePath?: string): SyncStrategy {
|
||||
if (requestedStrategy === 'full') {
|
||||
return 'full';
|
||||
}
|
||||
|
||||
// git-delta requires an existing sync state
|
||||
if (requestedStrategy === 'git-delta') {
|
||||
const state = SyncStateManager.loadState(workspacePath, statePath);
|
||||
if (!state) {
|
||||
OrchestratorLogger.log('[Sync] No sync state found, falling back to full sync');
|
||||
|
||||
return 'full';
|
||||
}
|
||||
|
||||
return 'git-delta';
|
||||
}
|
||||
|
||||
return requestedStrategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a git-delta sync: fetch latest and apply only changed files.
|
||||
*
|
||||
* @param workspacePath - Path to the git workspace
|
||||
* @param targetReference - Git ref to sync to (commit SHA, branch, tag)
|
||||
* @param statePath - Optional custom path for sync state file
|
||||
* @returns Number of files changed
|
||||
*/
|
||||
static async syncGitDelta(workspacePath: string, targetReference: string, statePath?: string): Promise<number> {
|
||||
const state = SyncStateManager.loadState(workspacePath, statePath);
|
||||
if (!state) {
|
||||
throw new Error('Cannot git-delta sync without existing sync state');
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(`[Sync] Git delta: ${state.lastSyncCommit.slice(0, 8)} -> ${targetReference.slice(0, 8)}`);
|
||||
|
||||
// Fetch latest
|
||||
await OrchestratorSystem.Run(`git -C "${workspacePath}" fetch origin`, true);
|
||||
|
||||
// Get list of changed files
|
||||
const diffOutput = await OrchestratorSystem.Run(
|
||||
`git -C "${workspacePath}" diff --name-only ${state.lastSyncCommit}..${targetReference}`,
|
||||
true,
|
||||
);
|
||||
|
||||
const changedFiles = diffOutput.split('\n').filter(Boolean);
|
||||
OrchestratorLogger.log(`[Sync] ${changedFiles.length} file(s) changed`);
|
||||
|
||||
if (changedFiles.length > 0) {
|
||||
// Checkout target ref
|
||||
await OrchestratorSystem.Run(`git -C "${workspacePath}" checkout ${targetReference}`, true);
|
||||
}
|
||||
|
||||
// Update sync state
|
||||
const newState: SyncState = {
|
||||
lastSyncCommit: targetReference,
|
||||
lastSyncTimestamp: new Date().toISOString(),
|
||||
workspaceHash: SyncStateManager.calculateWorkspaceHash(workspacePath),
|
||||
pendingOverlays: state.pendingOverlays,
|
||||
};
|
||||
SyncStateManager.saveState(workspacePath, newState, statePath);
|
||||
|
||||
return changedFiles.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a direct input overlay from a local archive or storage URI.
|
||||
*
|
||||
* For storage URIs (storage://remote:bucket/path), the archive is fetched via rclone.
|
||||
* For local paths, the archive is extracted directly.
|
||||
*
|
||||
* @param workspacePath - Path to the workspace
|
||||
* @param inputReference - Local path or storage:// URI to the input archive
|
||||
* @param rcloneRemote - rclone remote name for storage:// URIs (optional, uses URI-embedded remote)
|
||||
* @param statePath - Optional custom path for sync state file
|
||||
* @returns List of overlay paths applied
|
||||
*/
|
||||
static async applyDirectInput(
|
||||
workspacePath: string,
|
||||
inputReference: string,
|
||||
rcloneRemote?: string,
|
||||
statePath?: string,
|
||||
): Promise<string[]> {
|
||||
let localArchive = inputReference;
|
||||
|
||||
// If storage URI, fetch via rclone first
|
||||
if (inputReference.startsWith('storage://')) {
|
||||
const parsed = IncrementalSyncService.parseStorageUri(inputReference);
|
||||
const remote = rcloneRemote || parsed.remote;
|
||||
const remotePath = parsed.path;
|
||||
|
||||
localArchive = path.join(workspacePath, '.game-ci-input-overlay.tar');
|
||||
OrchestratorLogger.log(`[Sync] Fetching input from storage: ${inputReference}`);
|
||||
|
||||
await IncrementalSyncService.executeRcloneCopy(remote, remotePath, path.dirname(localArchive));
|
||||
}
|
||||
|
||||
if (!fs.existsSync(localArchive)) {
|
||||
throw new Error(`Input archive not found: ${localArchive}`);
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(`[Sync] Applying direct input overlay from ${localArchive}`);
|
||||
|
||||
// Extract overlay
|
||||
await OrchestratorSystem.Run(`tar -xf "${localArchive}" -C "${workspacePath}"`, true);
|
||||
|
||||
// Track overlay in sync state
|
||||
const state = SyncStateManager.loadState(workspacePath, statePath) || {
|
||||
lastSyncCommit: '',
|
||||
lastSyncTimestamp: new Date().toISOString(),
|
||||
pendingOverlays: [],
|
||||
};
|
||||
|
||||
state.pendingOverlays.push(localArchive);
|
||||
SyncStateManager.saveState(workspacePath, state, statePath);
|
||||
|
||||
return [localArchive];
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a storage-pull sync: pull changed files from an rclone remote.
|
||||
*
|
||||
* This strategy fetches content from a remote storage backend (S3, GCS, Azure, etc.)
|
||||
* and overlays it onto the workspace. Supports two modes:
|
||||
* - overlay: extract on top of existing workspace (default)
|
||||
* - clean: fresh git checkout, then apply overlay
|
||||
*
|
||||
* @param workspacePath - Path to the workspace
|
||||
* @param storageUri - storage://remote:bucket/path URI pointing to remote content
|
||||
* @param options - Configuration for the storage-pull operation
|
||||
* @returns List of files pulled from storage
|
||||
*/
|
||||
static async syncStoragePull(
|
||||
workspacePath: string,
|
||||
storageUri: string,
|
||||
options: {
|
||||
rcloneRemote?: string;
|
||||
cleanMode?: boolean;
|
||||
syncRevertAfter?: boolean;
|
||||
statePath?: string;
|
||||
} = {},
|
||||
): Promise<string[]> {
|
||||
if (!storageUri.startsWith('storage://')) {
|
||||
throw new Error(`Invalid storage URI: ${storageUri}. Must start with storage://`);
|
||||
}
|
||||
|
||||
// Verify rclone is available
|
||||
try {
|
||||
await OrchestratorSystem.Run('rclone version', true, true);
|
||||
} catch {
|
||||
throw new Error('rclone binary not found. Install rclone to use storage-pull sync strategy.');
|
||||
}
|
||||
|
||||
const parsed = IncrementalSyncService.parseStorageUri(storageUri);
|
||||
const remote = options.rcloneRemote || parsed.remote;
|
||||
const remotePath = parsed.path;
|
||||
|
||||
OrchestratorLogger.log(`[Sync] Storage pull: ${remote}:${remotePath} -> ${workspacePath}`);
|
||||
|
||||
// Clean mode: reset workspace to clean git state before applying overlay
|
||||
if (options.cleanMode) {
|
||||
OrchestratorLogger.log('[Sync] Clean mode: resetting workspace to HEAD');
|
||||
await OrchestratorSystem.Run(`git -C "${workspacePath}" checkout -- .`, true);
|
||||
await OrchestratorSystem.Run(`git -C "${workspacePath}" clean -fd`, true);
|
||||
}
|
||||
|
||||
// Pull from remote storage directly into workspace
|
||||
const rcloneSource = `${remote}:${remotePath}`;
|
||||
await OrchestratorSystem.Run(`rclone copy "${rcloneSource}" "${workspacePath}" --transfers 8 --checkers 16`, true);
|
||||
|
||||
// List what was pulled for tracking
|
||||
let pulledFiles: string[] = [];
|
||||
try {
|
||||
const lsOutput = await OrchestratorSystem.Run(`rclone ls "${rcloneSource}"`, true, true);
|
||||
pulledFiles = lsOutput
|
||||
.split('\n')
|
||||
.filter(Boolean)
|
||||
.map((line) => {
|
||||
// rclone ls outputs: " <size> <path>"
|
||||
const trimmed = line.trim();
|
||||
const spaceIndex = trimmed.indexOf(' ');
|
||||
|
||||
return spaceIndex >= 0 ? trimmed.slice(spaceIndex + 1).trim() : trimmed;
|
||||
})
|
||||
.filter(Boolean);
|
||||
} catch {
|
||||
OrchestratorLogger.logWarning('[Sync] Could not list pulled files from remote');
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(`[Sync] Pulled ${pulledFiles.length} file(s) from storage`);
|
||||
|
||||
// Update sync state with overlay tracking
|
||||
const state = SyncStateManager.loadState(workspacePath, options.statePath) || {
|
||||
lastSyncCommit: '',
|
||||
lastSyncTimestamp: new Date().toISOString(),
|
||||
pendingOverlays: [],
|
||||
};
|
||||
|
||||
state.pendingOverlays.push(storageUri);
|
||||
state.lastSyncTimestamp = new Date().toISOString();
|
||||
state.workspaceHash = SyncStateManager.calculateWorkspaceHash(workspacePath);
|
||||
SyncStateManager.saveState(workspacePath, state, options.statePath);
|
||||
|
||||
return pulledFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a storage:// URI into remote and path components.
|
||||
*
|
||||
* Supported formats:
|
||||
* - storage://remote:bucket/path (explicit remote with colon separator)
|
||||
* - storage://remote/path (remote name is first path segment)
|
||||
*
|
||||
* @param uri - The storage:// URI to parse
|
||||
* @returns Object with remote name and path
|
||||
*/
|
||||
static parseStorageUri(uri: string): { remote: string; path: string } {
|
||||
if (!uri.startsWith('storage://')) {
|
||||
throw new Error(`Invalid storage URI: ${uri}. Must start with storage://`);
|
||||
}
|
||||
|
||||
const stripped = uri.replace('storage://', '');
|
||||
|
||||
// Check for explicit remote:path format (e.g., "myremote:bucket/path")
|
||||
const colonIndex = stripped.indexOf(':');
|
||||
if (colonIndex > 0) {
|
||||
return {
|
||||
remote: stripped.slice(0, colonIndex),
|
||||
path: stripped.slice(colonIndex + 1),
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback: first segment is remote name (e.g., "myremote/bucket/path")
|
||||
const slashIndex = stripped.indexOf('/');
|
||||
if (slashIndex > 0) {
|
||||
return {
|
||||
remote: stripped.slice(0, slashIndex),
|
||||
path: stripped.slice(slashIndex + 1),
|
||||
};
|
||||
}
|
||||
|
||||
// Just a remote name with no path
|
||||
return {
|
||||
remote: stripped,
|
||||
path: '',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute rclone copy with standard flags.
|
||||
*/
|
||||
private static async executeRcloneCopy(remote: string, remotePath: string, destinationPath: string): Promise<void> {
|
||||
await OrchestratorSystem.Run(
|
||||
`rclone copy "${remote}:${remotePath}" "${destinationPath}" --transfers 8 --checkers 16`,
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert pending overlays by restoring git state.
|
||||
*/
|
||||
static async revertOverlays(workspacePath: string, statePath?: string): Promise<void> {
|
||||
const state = SyncStateManager.loadState(workspacePath, statePath);
|
||||
if (!state || state.pendingOverlays.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
OrchestratorLogger.log(`[Sync] Reverting ${state.pendingOverlays.length} overlay(s)`);
|
||||
|
||||
await OrchestratorSystem.Run(`git -C "${workspacePath}" checkout -- .`, true);
|
||||
|
||||
// Clean untracked files from overlays
|
||||
await OrchestratorSystem.Run(`git -C "${workspacePath}" clean -fd`, true);
|
||||
|
||||
state.pendingOverlays = [];
|
||||
state.workspaceHash = SyncStateManager.calculateWorkspaceHash(workspacePath);
|
||||
SyncStateManager.saveState(workspacePath, state, statePath);
|
||||
|
||||
OrchestratorLogger.log('[Sync] Overlays reverted');
|
||||
}
|
||||
}
|
||||
544
src/model/orchestrator/services/sync/incremental-sync.test.ts
Normal file
544
src/model/orchestrator/services/sync/incremental-sync.test.ts
Normal file
@@ -0,0 +1,544 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { IncrementalSyncService } from './incremental-sync-service';
|
||||
import { SyncStateManager } from './sync-state-manager';
|
||||
import { SyncState } from './sync-state';
|
||||
import { OrchestratorSystem } from '../core/orchestrator-system';
|
||||
import OrchestratorLogger from '../core/orchestrator-logger';
|
||||
|
||||
// Mock dependencies
|
||||
jest.mock('node:fs');
|
||||
jest.mock('../core/orchestrator-system');
|
||||
jest.mock('../core/orchestrator-logger');
|
||||
|
||||
const mockFs = fs as jest.Mocked<typeof fs>;
|
||||
const mockSystem = OrchestratorSystem as jest.Mocked<typeof OrchestratorSystem>;
|
||||
const mockLogger = OrchestratorLogger as jest.Mocked<typeof OrchestratorLogger>;
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('IncrementalSyncService', () => {
|
||||
const workspacePath = '/workspace/project';
|
||||
|
||||
describe('parseStorageUri', () => {
|
||||
it('parses storage://remote:bucket/path format', () => {
|
||||
const result = IncrementalSyncService.parseStorageUri('storage://myremote:mybucket/some/path');
|
||||
expect(result).toEqual({ remote: 'myremote', path: 'mybucket/some/path' });
|
||||
});
|
||||
|
||||
it('parses storage://remote/path format', () => {
|
||||
const result = IncrementalSyncService.parseStorageUri('storage://myremote/mybucket/path');
|
||||
expect(result).toEqual({ remote: 'myremote', path: 'mybucket/path' });
|
||||
});
|
||||
|
||||
it('parses storage://remote:bucket with no sub-path', () => {
|
||||
const result = IncrementalSyncService.parseStorageUri('storage://myremote:mybucket');
|
||||
expect(result).toEqual({ remote: 'myremote', path: 'mybucket' });
|
||||
});
|
||||
|
||||
it('handles remote-only URI without path', () => {
|
||||
const result = IncrementalSyncService.parseStorageUri('storage://myremote');
|
||||
expect(result).toEqual({ remote: 'myremote', path: '' });
|
||||
});
|
||||
|
||||
it('throws on invalid URI without storage:// prefix', () => {
|
||||
expect(() => IncrementalSyncService.parseStorageUri('http://example.com')).toThrow('Invalid storage URI');
|
||||
});
|
||||
|
||||
it('throws on empty URI', () => {
|
||||
expect(() => IncrementalSyncService.parseStorageUri('')).toThrow('Invalid storage URI');
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveStrategy', () => {
|
||||
it('returns full when full is requested', () => {
|
||||
const result = IncrementalSyncService.resolveStrategy('full', workspacePath);
|
||||
expect(result).toBe('full');
|
||||
});
|
||||
|
||||
it('returns git-delta when sync state exists', () => {
|
||||
const state: SyncState = {
|
||||
lastSyncCommit: 'abc123',
|
||||
lastSyncTimestamp: new Date().toISOString(),
|
||||
pendingOverlays: [],
|
||||
};
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(JSON.stringify(state));
|
||||
|
||||
const result = IncrementalSyncService.resolveStrategy('git-delta', workspacePath);
|
||||
expect(result).toBe('git-delta');
|
||||
});
|
||||
|
||||
it('falls back to full when git-delta requested but no sync state', () => {
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
|
||||
const result = IncrementalSyncService.resolveStrategy('git-delta', workspacePath);
|
||||
expect(result).toBe('full');
|
||||
});
|
||||
|
||||
it('returns direct-input as-is', () => {
|
||||
const result = IncrementalSyncService.resolveStrategy('direct-input', workspacePath);
|
||||
expect(result).toBe('direct-input');
|
||||
});
|
||||
|
||||
it('returns storage-pull as-is', () => {
|
||||
const result = IncrementalSyncService.resolveStrategy('storage-pull', workspacePath);
|
||||
expect(result).toBe('storage-pull');
|
||||
});
|
||||
});
|
||||
|
||||
describe('syncGitDelta', () => {
|
||||
const targetReference = 'def456789';
|
||||
|
||||
beforeEach(() => {
|
||||
const state: SyncState = {
|
||||
lastSyncCommit: 'abc123456',
|
||||
lastSyncTimestamp: new Date().toISOString(),
|
||||
pendingOverlays: [],
|
||||
};
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(JSON.stringify(state));
|
||||
});
|
||||
|
||||
it('fetches and checks out changed files', async () => {
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // git fetch
|
||||
mockSystem.Run.mockResolvedValueOnce('file1.txt\nfile2.cs\n'); // git diff
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // git checkout
|
||||
|
||||
const result = await IncrementalSyncService.syncGitDelta(workspacePath, targetReference);
|
||||
|
||||
expect(result).toBe(2);
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(
|
||||
expect.stringContaining('git -C "/workspace/project" fetch origin'),
|
||||
true,
|
||||
);
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(
|
||||
expect.stringContaining('diff --name-only abc123456..def456789'),
|
||||
true,
|
||||
);
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(expect.stringContaining('checkout def456789'), true);
|
||||
});
|
||||
|
||||
it('skips checkout when no files changed', async () => {
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // git fetch
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // git diff (empty)
|
||||
|
||||
const result = await IncrementalSyncService.syncGitDelta(workspacePath, targetReference);
|
||||
|
||||
expect(result).toBe(0);
|
||||
|
||||
// Should only have fetch + diff calls, no checkout
|
||||
expect(mockSystem.Run).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('throws when no sync state exists', async () => {
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
|
||||
await expect(IncrementalSyncService.syncGitDelta(workspacePath, targetReference)).rejects.toThrow(
|
||||
'Cannot git-delta sync without existing sync state',
|
||||
);
|
||||
});
|
||||
|
||||
it('saves updated sync state after delta sync', async () => {
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // git fetch
|
||||
mockSystem.Run.mockResolvedValueOnce('file1.txt\n'); // git diff
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // git checkout
|
||||
|
||||
await IncrementalSyncService.syncGitDelta(workspacePath, targetReference);
|
||||
|
||||
expect(mockFs.writeFileSync).toHaveBeenCalled();
|
||||
const writeCall = mockFs.writeFileSync.mock.calls[0];
|
||||
const savedState = JSON.parse(writeCall[1] as string) as SyncState;
|
||||
expect(savedState.lastSyncCommit).toBe(targetReference);
|
||||
});
|
||||
});
|
||||
|
||||
describe('applyDirectInput', () => {
|
||||
it('extracts a local archive to workspace', async () => {
|
||||
const archivePath = '/tmp/overlay.tar';
|
||||
mockFs.existsSync.mockImplementation((p: fs.PathLike) => {
|
||||
if (p === archivePath) return true;
|
||||
|
||||
// State file path does not exist
|
||||
return false;
|
||||
});
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // tar extract
|
||||
|
||||
const result = await IncrementalSyncService.applyDirectInput(workspacePath, archivePath);
|
||||
|
||||
expect(result).toEqual([archivePath]);
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(expect.stringContaining('tar -xf "/tmp/overlay.tar"'), true);
|
||||
});
|
||||
|
||||
it('fetches archive from storage URI via rclone then extracts', async () => {
|
||||
const storageUri = 'storage://s3remote:builds/overlay.tar';
|
||||
|
||||
mockFs.existsSync.mockImplementation((p: fs.PathLike) => {
|
||||
const pathString = p.toString();
|
||||
if (pathString.includes('.game-ci-input-overlay.tar')) return true;
|
||||
|
||||
return false;
|
||||
});
|
||||
mockSystem.Run.mockResolvedValue(''); // rclone copy + tar extract
|
||||
|
||||
const result = await IncrementalSyncService.applyDirectInput(workspacePath, storageUri);
|
||||
|
||||
expect(result.length).toBe(1);
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(
|
||||
expect.stringContaining('rclone copy "s3remote:builds/overlay.tar"'),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('throws when local archive does not exist', async () => {
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
|
||||
await expect(IncrementalSyncService.applyDirectInput(workspacePath, '/missing/archive.tar')).rejects.toThrow(
|
||||
'Input archive not found',
|
||||
);
|
||||
});
|
||||
|
||||
it('tracks overlay in sync state', async () => {
|
||||
const archivePath = '/tmp/overlay.tar';
|
||||
mockFs.existsSync.mockImplementation((p: fs.PathLike) => {
|
||||
if (p === archivePath) return true;
|
||||
|
||||
return false;
|
||||
});
|
||||
mockSystem.Run.mockResolvedValueOnce('');
|
||||
|
||||
await IncrementalSyncService.applyDirectInput(workspacePath, archivePath);
|
||||
|
||||
expect(mockFs.writeFileSync).toHaveBeenCalled();
|
||||
const writeCall = mockFs.writeFileSync.mock.calls[0];
|
||||
const savedState = JSON.parse(writeCall[1] as string) as SyncState;
|
||||
expect(savedState.pendingOverlays).toContain(archivePath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('syncStoragePull', () => {
|
||||
const storageUri = 'storage://s3:game-builds/latest';
|
||||
|
||||
beforeEach(() => {
|
||||
mockFs.existsSync.mockReturnValue(false); // no existing state
|
||||
});
|
||||
|
||||
it('pulls files from rclone remote into workspace', async () => {
|
||||
mockSystem.Run.mockResolvedValueOnce('rclone v1.60.0'); // version check
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // rclone copy
|
||||
mockSystem.Run.mockResolvedValueOnce(' 1234 file1.txt\n 5678 dir/file2.cs\n'); // rclone ls
|
||||
|
||||
const result = await IncrementalSyncService.syncStoragePull(workspacePath, storageUri);
|
||||
|
||||
expect(result).toEqual(['file1.txt', 'dir/file2.cs']);
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(
|
||||
expect.stringContaining('rclone copy "s3:game-builds/latest" "/workspace/project"'),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('uses custom rclone remote when provided', async () => {
|
||||
mockSystem.Run.mockResolvedValueOnce('rclone v1.60.0'); // version
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // rclone copy
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // rclone ls
|
||||
|
||||
await IncrementalSyncService.syncStoragePull(workspacePath, storageUri, {
|
||||
rcloneRemote: 'custom-remote',
|
||||
});
|
||||
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(
|
||||
expect.stringContaining('rclone copy "custom-remote:game-builds/latest"'),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('resets workspace in clean mode before pull', async () => {
|
||||
mockSystem.Run.mockResolvedValueOnce('rclone v1.60.0'); // version
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // git checkout -- .
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // git clean -fd
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // rclone copy
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // rclone ls
|
||||
|
||||
await IncrementalSyncService.syncStoragePull(workspacePath, storageUri, { cleanMode: true });
|
||||
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(
|
||||
expect.stringContaining('git -C "/workspace/project" checkout -- .'),
|
||||
true,
|
||||
);
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(
|
||||
expect.stringContaining('git -C "/workspace/project" clean -fd'),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('throws on invalid storage URI', async () => {
|
||||
await expect(IncrementalSyncService.syncStoragePull(workspacePath, 'http://example.com')).rejects.toThrow(
|
||||
'Invalid storage URI',
|
||||
);
|
||||
});
|
||||
|
||||
it('throws when rclone binary is not available', async () => {
|
||||
mockSystem.Run.mockRejectedValueOnce(new Error('command not found: rclone'));
|
||||
|
||||
await expect(IncrementalSyncService.syncStoragePull(workspacePath, storageUri)).rejects.toThrow(
|
||||
'rclone binary not found',
|
||||
);
|
||||
});
|
||||
|
||||
it('saves sync state with overlay tracking', async () => {
|
||||
mockSystem.Run.mockResolvedValueOnce('rclone v1.60.0'); // version
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // rclone copy
|
||||
mockSystem.Run.mockResolvedValueOnce(' 100 a.txt\n'); // rclone ls
|
||||
|
||||
await IncrementalSyncService.syncStoragePull(workspacePath, storageUri);
|
||||
|
||||
expect(mockFs.writeFileSync).toHaveBeenCalled();
|
||||
const writeCall = mockFs.writeFileSync.mock.calls[0];
|
||||
const savedState = JSON.parse(writeCall[1] as string) as SyncState;
|
||||
expect(savedState.pendingOverlays).toContain(storageUri);
|
||||
});
|
||||
|
||||
it('handles rclone ls failure gracefully', async () => {
|
||||
mockSystem.Run.mockResolvedValueOnce('rclone v1.60.0'); // version
|
||||
mockSystem.Run.mockResolvedValueOnce(''); // rclone copy
|
||||
mockSystem.Run.mockRejectedValueOnce(new Error('ls failed')); // rclone ls fails
|
||||
|
||||
const result = await IncrementalSyncService.syncStoragePull(workspacePath, storageUri);
|
||||
|
||||
expect(result).toEqual([]);
|
||||
expect(mockLogger.logWarning).toHaveBeenCalledWith(expect.stringContaining('Could not list pulled files'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('revertOverlays', () => {
|
||||
it('reverts git state and cleans untracked files', async () => {
|
||||
const state: SyncState = {
|
||||
lastSyncCommit: 'abc123',
|
||||
lastSyncTimestamp: new Date().toISOString(),
|
||||
pendingOverlays: ['/tmp/overlay.tar', 'storage://s3:builds/content'],
|
||||
};
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(JSON.stringify(state));
|
||||
mockSystem.Run.mockResolvedValue('');
|
||||
|
||||
await IncrementalSyncService.revertOverlays(workspacePath);
|
||||
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(
|
||||
expect.stringContaining('git -C "/workspace/project" checkout -- .'),
|
||||
true,
|
||||
);
|
||||
expect(mockSystem.Run).toHaveBeenCalledWith(
|
||||
expect.stringContaining('git -C "/workspace/project" clean -fd'),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('clears pending overlays in saved state', async () => {
|
||||
const state: SyncState = {
|
||||
lastSyncCommit: 'abc123',
|
||||
lastSyncTimestamp: new Date().toISOString(),
|
||||
pendingOverlays: ['/tmp/overlay.tar'],
|
||||
};
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(JSON.stringify(state));
|
||||
mockSystem.Run.mockResolvedValue('');
|
||||
|
||||
await IncrementalSyncService.revertOverlays(workspacePath);
|
||||
|
||||
expect(mockFs.writeFileSync).toHaveBeenCalled();
|
||||
const writeCall = mockFs.writeFileSync.mock.calls[0];
|
||||
const savedState = JSON.parse(writeCall[1] as string) as SyncState;
|
||||
expect(savedState.pendingOverlays).toEqual([]);
|
||||
});
|
||||
|
||||
it('does nothing when no overlays are pending', async () => {
|
||||
const state: SyncState = {
|
||||
lastSyncCommit: 'abc123',
|
||||
lastSyncTimestamp: new Date().toISOString(),
|
||||
pendingOverlays: [],
|
||||
};
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(JSON.stringify(state));
|
||||
|
||||
await IncrementalSyncService.revertOverlays(workspacePath);
|
||||
|
||||
expect(mockSystem.Run).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does nothing when no sync state exists', async () => {
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
|
||||
await IncrementalSyncService.revertOverlays(workspacePath);
|
||||
|
||||
expect(mockSystem.Run).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('SyncStateManager', () => {
|
||||
const workspacePath = '/workspace/project';
|
||||
|
||||
describe('loadState', () => {
|
||||
it('returns parsed state from default path', () => {
|
||||
const state: SyncState = {
|
||||
lastSyncCommit: 'abc123',
|
||||
lastSyncTimestamp: '2026-01-01T00:00:00.000Z',
|
||||
pendingOverlays: [],
|
||||
};
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(JSON.stringify(state));
|
||||
|
||||
const result = SyncStateManager.loadState(workspacePath);
|
||||
|
||||
expect(result).toEqual(state);
|
||||
expect(mockFs.readFileSync).toHaveBeenCalledWith(path.join(workspacePath, '.game-ci/sync-state.json'), 'utf8');
|
||||
});
|
||||
|
||||
it('uses custom state path when provided', () => {
|
||||
const state: SyncState = {
|
||||
lastSyncCommit: 'abc123',
|
||||
lastSyncTimestamp: '2026-01-01T00:00:00.000Z',
|
||||
pendingOverlays: [],
|
||||
};
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(JSON.stringify(state));
|
||||
|
||||
SyncStateManager.loadState(workspacePath, 'custom/state.json');
|
||||
|
||||
expect(mockFs.readFileSync).toHaveBeenCalledWith(path.join(workspacePath, 'custom/state.json'), 'utf8');
|
||||
});
|
||||
|
||||
it('returns undefined when state file does not exist', () => {
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
|
||||
const result = SyncStateManager.loadState(workspacePath);
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('returns undefined and logs warning on malformed JSON', () => {
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue('not-valid-json{{{');
|
||||
|
||||
const result = SyncStateManager.loadState(workspacePath);
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
expect(mockLogger.logWarning).toHaveBeenCalledWith(expect.stringContaining('Failed to load sync state'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveState', () => {
|
||||
it('writes state to default path with pretty JSON', () => {
|
||||
const state: SyncState = {
|
||||
lastSyncCommit: 'abc123',
|
||||
lastSyncTimestamp: '2026-01-01T00:00:00.000Z',
|
||||
pendingOverlays: ['overlay1'],
|
||||
};
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
|
||||
SyncStateManager.saveState(workspacePath, state);
|
||||
|
||||
expect(mockFs.writeFileSync).toHaveBeenCalledWith(
|
||||
path.join(workspacePath, '.game-ci/sync-state.json'),
|
||||
JSON.stringify(state, undefined, 2),
|
||||
'utf8',
|
||||
);
|
||||
});
|
||||
|
||||
it('creates parent directories if they do not exist', () => {
|
||||
const state: SyncState = {
|
||||
lastSyncCommit: 'abc123',
|
||||
lastSyncTimestamp: '2026-01-01T00:00:00.000Z',
|
||||
pendingOverlays: [],
|
||||
};
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
|
||||
SyncStateManager.saveState(workspacePath, state);
|
||||
|
||||
expect(mockFs.mkdirSync).toHaveBeenCalledWith(expect.stringContaining('.game-ci'), { recursive: true });
|
||||
});
|
||||
|
||||
it('logs warning on write failure instead of throwing', () => {
|
||||
const state: SyncState = {
|
||||
lastSyncCommit: 'abc123',
|
||||
lastSyncTimestamp: '2026-01-01T00:00:00.000Z',
|
||||
pendingOverlays: [],
|
||||
};
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.writeFileSync.mockImplementation(() => {
|
||||
throw new Error('Permission denied');
|
||||
});
|
||||
|
||||
// Should not throw
|
||||
SyncStateManager.saveState(workspacePath, state);
|
||||
|
||||
expect(mockLogger.logWarning).toHaveBeenCalledWith(expect.stringContaining('Failed to save sync state'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('calculateWorkspaceHash', () => {
|
||||
it('produces a deterministic SHA-256 hash from workspace files', () => {
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockImplementation((filePath: fs.PathOrFileDescriptor) => {
|
||||
const p = filePath.toString();
|
||||
if (p.includes('ProjectVersion.txt')) return 'm_EditorVersion: 2022.3.10f1';
|
||||
if (p.includes('manifest.json')) return '{"dependencies":{}}';
|
||||
if (p.includes('packages-lock.json')) return '{"dependencies":{}}';
|
||||
if (p.includes('csc.rsp')) return '-nullable+';
|
||||
|
||||
return '';
|
||||
});
|
||||
|
||||
const hash1 = SyncStateManager.calculateWorkspaceHash(workspacePath);
|
||||
const hash2 = SyncStateManager.calculateWorkspaceHash(workspacePath);
|
||||
|
||||
expect(hash1).toBe(hash2);
|
||||
expect(hash1).toHaveLength(64); // SHA-256 hex digest
|
||||
});
|
||||
|
||||
it('produces different hashes for different workspace content', () => {
|
||||
let callCount = 0;
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockImplementation(() => {
|
||||
callCount++;
|
||||
|
||||
return callCount <= 4 ? 'content-v1' : 'content-v2';
|
||||
});
|
||||
|
||||
const hash1 = SyncStateManager.calculateWorkspaceHash(workspacePath);
|
||||
const hash2 = SyncStateManager.calculateWorkspaceHash(workspacePath);
|
||||
|
||||
expect(hash1).not.toBe(hash2);
|
||||
});
|
||||
|
||||
it('includes missing file markers in hash for absent files', () => {
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
|
||||
const hash = SyncStateManager.calculateWorkspaceHash(workspacePath);
|
||||
|
||||
expect(hash).toHaveLength(64);
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasDrifted', () => {
|
||||
it('returns false when workspace hash matches', () => {
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
const savedHash = SyncStateManager.calculateWorkspaceHash(workspacePath);
|
||||
|
||||
const result = SyncStateManager.hasDrifted(workspacePath, savedHash);
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('returns true when workspace hash differs', () => {
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
|
||||
const result = SyncStateManager.hasDrifted(workspacePath, 'some-old-hash-that-will-not-match');
|
||||
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
3
src/model/orchestrator/services/sync/index.ts
Normal file
3
src/model/orchestrator/services/sync/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { SyncState, SyncStrategy } from './sync-state';
|
||||
export { IncrementalSyncService } from './incremental-sync-service';
|
||||
export { SyncStateManager } from './sync-state-manager';
|
||||
125
src/model/orchestrator/services/sync/sync-state-manager.ts
Normal file
125
src/model/orchestrator/services/sync/sync-state-manager.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import crypto from 'node:crypto';
|
||||
import OrchestratorLogger from '../core/orchestrator-logger';
|
||||
import { SyncState } from './sync-state';
|
||||
|
||||
/**
|
||||
* Manages persistent sync state for incremental workspace updates.
|
||||
*
|
||||
* The sync state tracks what has been synced to a workspace, enabling
|
||||
* delta-based updates instead of full clones. State is stored as a JSON
|
||||
* file in the workspace (default: .game-ci/sync-state.json).
|
||||
*/
|
||||
export class SyncStateManager {
|
||||
static readonly DEFAULT_STATE_PATH = '.game-ci/sync-state.json';
|
||||
|
||||
/**
|
||||
* Key workspace files whose content is hashed for drift detection.
|
||||
* Changes to any of these files indicate the workspace may have been
|
||||
* modified outside of the sync system.
|
||||
*/
|
||||
private static readonly WORKSPACE_HASH_FILES = [
|
||||
'ProjectSettings/ProjectVersion.txt',
|
||||
'Packages/manifest.json',
|
||||
'Packages/packages-lock.json',
|
||||
'Assets/csc.rsp',
|
||||
];
|
||||
|
||||
/**
|
||||
* Load sync state from the workspace.
|
||||
*
|
||||
* @param workspacePath - Root path of the workspace
|
||||
* @param statePath - Relative path to the state file (default: .game-ci/sync-state.json)
|
||||
* @returns The loaded sync state, or undefined if no state exists or parsing fails
|
||||
*/
|
||||
static loadState(workspacePath: string, statePath?: string): SyncState | undefined {
|
||||
const resolvedPath = path.join(workspacePath, statePath || SyncStateManager.DEFAULT_STATE_PATH);
|
||||
|
||||
if (!fs.existsSync(resolvedPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const content = fs.readFileSync(resolvedPath, 'utf8');
|
||||
|
||||
return JSON.parse(content) as SyncState;
|
||||
} catch {
|
||||
OrchestratorLogger.logWarning(`[SyncState] Failed to load sync state from ${resolvedPath}`);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save sync state to the workspace.
|
||||
*
|
||||
* Creates parent directories if they do not exist.
|
||||
*
|
||||
* @param workspacePath - Root path of the workspace
|
||||
* @param state - The sync state to persist
|
||||
* @param statePath - Relative path to the state file (default: .game-ci/sync-state.json)
|
||||
*/
|
||||
static saveState(workspacePath: string, state: SyncState, statePath?: string): void {
|
||||
const resolvedPath = path.join(workspacePath, statePath || SyncStateManager.DEFAULT_STATE_PATH);
|
||||
|
||||
try {
|
||||
const directory = path.dirname(resolvedPath);
|
||||
if (!fs.existsSync(directory)) {
|
||||
fs.mkdirSync(directory, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFileSync(resolvedPath, JSON.stringify(state, undefined, 2), 'utf8');
|
||||
OrchestratorLogger.log(
|
||||
`[SyncState] State saved: commit=${state.lastSyncCommit}, overlays=${state.pendingOverlays.length}`,
|
||||
);
|
||||
} catch (error: any) {
|
||||
OrchestratorLogger.logWarning(`[SyncState] Failed to save sync state: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate a SHA-256 hash of key workspace files for drift detection.
|
||||
*
|
||||
* Hashes the content of known workspace files (ProjectVersion.txt,
|
||||
* manifest.json, etc.) to produce a fingerprint. If the hash changes
|
||||
* between syncs, the workspace may have been modified externally.
|
||||
*
|
||||
* Files that do not exist are skipped (their absence is part of the hash).
|
||||
*
|
||||
* @param workspacePath - Root path of the workspace
|
||||
* @returns Hex-encoded SHA-256 hash string
|
||||
*/
|
||||
static calculateWorkspaceHash(workspacePath: string): string {
|
||||
const hash = crypto.createHash('sha256');
|
||||
|
||||
for (const relativePath of SyncStateManager.WORKSPACE_HASH_FILES) {
|
||||
const filePath = path.join(workspacePath, relativePath);
|
||||
try {
|
||||
if (fs.existsSync(filePath)) {
|
||||
const content = fs.readFileSync(filePath, 'utf8');
|
||||
hash.update(`${relativePath}:${content}`);
|
||||
} else {
|
||||
hash.update(`${relativePath}:__missing__`);
|
||||
}
|
||||
} catch {
|
||||
hash.update(`${relativePath}:__error__`);
|
||||
}
|
||||
}
|
||||
|
||||
return hash.digest('hex');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the workspace has drifted from a previously saved hash.
|
||||
*
|
||||
* @param workspacePath - Root path of the workspace
|
||||
* @param savedHash - The previously saved workspace hash to compare against
|
||||
* @returns true if the current workspace hash differs from the saved hash
|
||||
*/
|
||||
static hasDrifted(workspacePath: string, savedHash: string): boolean {
|
||||
const currentHash = SyncStateManager.calculateWorkspaceHash(workspacePath);
|
||||
|
||||
return currentHash !== savedHash;
|
||||
}
|
||||
}
|
||||
19
src/model/orchestrator/services/sync/sync-state.ts
Normal file
19
src/model/orchestrator/services/sync/sync-state.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Persistent sync state for incremental workspace updates.
|
||||
* Stored on the runner to track what has already been synced.
|
||||
*/
|
||||
export interface SyncState {
|
||||
/** Last successfully synced git commit SHA */
|
||||
lastSyncCommit: string;
|
||||
|
||||
/** ISO 8601 timestamp of last sync */
|
||||
lastSyncTimestamp: string;
|
||||
|
||||
/** SHA-256 hash of workspace state (optional) */
|
||||
workspaceHash?: string;
|
||||
|
||||
/** List of overlay paths that haven't been reverted */
|
||||
pendingOverlays: string[];
|
||||
}
|
||||
|
||||
export type SyncStrategy = 'full' | 'git-delta' | 'direct-input' | 'storage-pull';
|
||||
12
src/model/orchestrator/services/test-workflow/index.ts
Normal file
12
src/model/orchestrator/services/test-workflow/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export { TestSuiteParser } from './test-suite-parser';
|
||||
export { TaxonomyFilterService } from './taxonomy-filter-service';
|
||||
export { TestResultReporter } from './test-result-reporter';
|
||||
export { TestWorkflowService } from './test-workflow-service';
|
||||
export {
|
||||
TestSuiteDefinition,
|
||||
TestRunDefinition,
|
||||
TaxonomyDimension,
|
||||
TaxonomyDefinition,
|
||||
TestResult,
|
||||
TestFailure,
|
||||
} from './test-workflow-types';
|
||||
@@ -0,0 +1,198 @@
|
||||
import fs from 'node:fs';
|
||||
import YAML from 'yaml';
|
||||
import { TaxonomyDimension, TaxonomyDefinition } from './test-workflow-types';
|
||||
|
||||
/**
|
||||
* Manages test taxonomy dimensions and builds filter arguments for
|
||||
* the Unity test runner CLI. Supports comma-separated value lists,
|
||||
* regex patterns (/pattern/), and hierarchical dot-notation matching.
|
||||
*/
|
||||
export class TaxonomyFilterService {
|
||||
/**
|
||||
* Built-in taxonomy dimensions that are always available.
|
||||
* Projects may extend these via a custom taxonomy file.
|
||||
*/
|
||||
private static readonly BUILT_IN_DIMENSIONS: TaxonomyDimension[] = [
|
||||
{ name: 'Scope', values: ['Unit', 'Integration', 'System', 'End To End'] },
|
||||
{ name: 'Maturity', values: ['Trusted', 'Adolescent', 'Experimental'] },
|
||||
{ name: 'FeedbackSpeed', values: ['Fast', 'Moderate', 'Slow'] },
|
||||
{ name: 'Execution', values: ['Synchronous', 'Asynchronous', 'Coroutine'] },
|
||||
{ name: 'Rigor', values: ['Strict', 'Normal', 'Relaxed'] },
|
||||
{ name: 'Determinism', values: ['Deterministic', 'NonDeterministic'] },
|
||||
{ name: 'IsolationLevel', values: ['Full', 'Partial', 'None'] },
|
||||
];
|
||||
|
||||
/**
|
||||
* Load taxonomy dimensions: built-in dimensions plus any custom dimensions
|
||||
* from an optional taxonomy file.
|
||||
*/
|
||||
static loadTaxonomy(filePath?: string): TaxonomyDimension[] {
|
||||
const dimensions = [...TaxonomyFilterService.BUILT_IN_DIMENSIONS];
|
||||
|
||||
if (filePath && fs.existsSync(filePath)) {
|
||||
const content = fs.readFileSync(filePath, 'utf8');
|
||||
const parsed = YAML.parse(content) as TaxonomyDefinition;
|
||||
|
||||
if (parsed?.extensible_groups && Array.isArray(parsed.extensible_groups)) {
|
||||
for (const group of parsed.extensible_groups) {
|
||||
if (group.name && Array.isArray(group.values)) {
|
||||
// If a custom dimension has the same name as a built-in, merge values
|
||||
const existing = dimensions.find((d) => d.name === group.name);
|
||||
if (existing) {
|
||||
const existingValues = new Set(existing.values);
|
||||
for (const value of group.values) {
|
||||
if (!existingValues.has(value)) {
|
||||
existing.values.push(value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
dimensions.push({ name: group.name, values: [...group.values] });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dimensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a filter map to Unity test runner CLI args (--testFilter).
|
||||
*
|
||||
* Each filter dimension becomes a category expression. Multiple values in one
|
||||
* dimension are OR'd; multiple dimensions are AND'd. The result is a single
|
||||
* --testFilter string suitable for passing to Unity's test runner CLI.
|
||||
*
|
||||
* Regex patterns (values wrapped in /.../) are converted to category regex
|
||||
* expressions supported by the Unity test runner.
|
||||
*/
|
||||
static buildFilterArgs(filters: Record<string, string>): string {
|
||||
if (!filters || Object.keys(filters).length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const categoryExpressions: string[] = [];
|
||||
|
||||
for (const [dimension, valueSpec] of Object.entries(filters)) {
|
||||
const expression = TaxonomyFilterService.buildDimensionExpression(dimension, valueSpec);
|
||||
if (expression) {
|
||||
categoryExpressions.push(expression);
|
||||
}
|
||||
}
|
||||
|
||||
if (categoryExpressions.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Unity test runner uses --testFilter with category expressions
|
||||
// Multiple dimensions are AND'd by joining with ';'
|
||||
const filterString = categoryExpressions.join(';');
|
||||
return `--testFilter "${filterString}"`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a filter expression for a single taxonomy dimension.
|
||||
*/
|
||||
private static buildDimensionExpression(dimension: string, valueSpec: string): string {
|
||||
if (!valueSpec || valueSpec.trim() === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
const trimmed = valueSpec.trim();
|
||||
|
||||
// Check if the value is a regex pattern: /pattern/
|
||||
if (trimmed.startsWith('/') && trimmed.endsWith('/') && trimmed.length > 2) {
|
||||
const pattern = trimmed.slice(1, -1);
|
||||
return `${dimension}=~${pattern}`;
|
||||
}
|
||||
|
||||
// Comma-separated values: OR'd together
|
||||
const values = trimmed
|
||||
.split(',')
|
||||
.map((v) => v.trim())
|
||||
.filter((v) => v.length > 0);
|
||||
|
||||
if (values.length === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (values.length === 1) {
|
||||
return `${dimension}=${values[0]}`;
|
||||
}
|
||||
|
||||
// Multiple values: use pipe-separated OR syntax
|
||||
return `${dimension}=${values.join('|')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a test's taxonomy metadata matches the given filter criteria.
|
||||
*
|
||||
* A test matches if ALL filter dimensions match (AND across dimensions).
|
||||
* Within a single dimension, the test must match ANY of the specified values (OR).
|
||||
* Regex patterns are matched as regular expressions.
|
||||
* Hierarchical dot-notation supports prefix matching (e.g., filter "Combat.Melee"
|
||||
* matches test category "Combat.Melee.Sword").
|
||||
*/
|
||||
static matchesFilter(testCategories: Record<string, string>, filters: Record<string, string>): boolean {
|
||||
for (const [dimension, valueSpec] of Object.entries(filters)) {
|
||||
const testValue = testCategories[dimension];
|
||||
|
||||
// If the test has no value for this dimension, it does not match
|
||||
if (testValue === undefined || testValue === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TaxonomyFilterService.matchesDimensionFilter(testValue, valueSpec)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a single test category value matches a dimension filter spec.
|
||||
*/
|
||||
private static matchesDimensionFilter(testValue: string, valueSpec: string): boolean {
|
||||
const trimmed = valueSpec.trim();
|
||||
|
||||
// Regex pattern
|
||||
if (trimmed.startsWith('/') && trimmed.endsWith('/') && trimmed.length > 2) {
|
||||
const pattern = trimmed.slice(1, -1);
|
||||
try {
|
||||
const regex = new RegExp(pattern);
|
||||
return regex.test(testValue);
|
||||
} catch {
|
||||
// Invalid regex, treat as literal
|
||||
return testValue === trimmed;
|
||||
}
|
||||
}
|
||||
|
||||
// Comma-separated values
|
||||
const values = trimmed
|
||||
.split(',')
|
||||
.map((v) => v.trim())
|
||||
.filter((v) => v.length > 0);
|
||||
|
||||
return values.some((filterValue) => {
|
||||
// Exact match
|
||||
if (testValue === filterValue) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Hierarchical dot-notation prefix match
|
||||
// Filter "Combat.Melee" matches test "Combat.Melee" and "Combat.Melee.Sword"
|
||||
if (filterValue.includes('.') || testValue.includes('.')) {
|
||||
if (testValue.startsWith(filterValue + '.') || testValue === filterValue) {
|
||||
return true;
|
||||
}
|
||||
// Also allow the test to be a prefix of the filter for upward matching
|
||||
if (filterValue.startsWith(testValue + '.')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { TestResult, TestFailure } from './test-workflow-types';
|
||||
|
||||
/**
|
||||
* Parses test result files (JUnit XML, Unity JSON) and generates structured
|
||||
* summary reports. Supports writing results in multiple formats for CI
|
||||
* integration (GitHub Checks, artifact upload).
|
||||
*/
|
||||
export class TestResultReporter {
|
||||
/**
|
||||
* Parse a JUnit XML test result file into a TestResult.
|
||||
* JUnit XML is the standard format produced by Unity's test runner.
|
||||
*/
|
||||
static parseJUnitResults(xmlPath: string): TestResult {
|
||||
if (!fs.existsSync(xmlPath)) {
|
||||
throw new Error(`JUnit result file not found: ${xmlPath}`);
|
||||
}
|
||||
|
||||
const content = fs.readFileSync(xmlPath, 'utf8');
|
||||
return TestResultReporter.parseJUnitXml(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse JUnit XML content string into a TestResult.
|
||||
*/
|
||||
static parseJUnitXml(xmlContent: string): TestResult {
|
||||
// Extract the testsuite opening tag
|
||||
const suiteTagMatch = xmlContent.match(/<testsuite\s[^>]*>/);
|
||||
|
||||
let runName = 'unknown';
|
||||
let totalTests = 0;
|
||||
let failureCount = 0;
|
||||
let skippedCount = 0;
|
||||
let duration = 0;
|
||||
|
||||
if (suiteTagMatch) {
|
||||
const tag = suiteTagMatch[0];
|
||||
|
||||
// Extract individual attributes -- order-independent
|
||||
const nameMatch = tag.match(/\sname="([^"]*)"/);
|
||||
const testsMatch = tag.match(/\stests="(\d+)"/);
|
||||
const failuresMatch = tag.match(/\sfailures="(\d+)"/);
|
||||
const skippedMatch = tag.match(/\sskipped="(\d+)"/);
|
||||
const timeMatch = tag.match(/\stime="([^"]*)"/);
|
||||
|
||||
runName = nameMatch ? nameMatch[1] : 'unknown';
|
||||
totalTests = testsMatch ? Number.parseInt(testsMatch[1], 10) : 0;
|
||||
failureCount = failuresMatch ? Number.parseInt(failuresMatch[1], 10) : 0;
|
||||
skippedCount = skippedMatch ? Number.parseInt(skippedMatch[1], 10) : 0;
|
||||
duration = timeMatch ? Number.parseFloat(timeMatch[1]) : 0;
|
||||
}
|
||||
|
||||
// Extract individual test failures by splitting into testcase blocks
|
||||
const failures: TestFailure[] = [];
|
||||
const testcasePattern = /<testcase\s[^>]*>[\s\S]*?<\/testcase>/g;
|
||||
|
||||
let tcMatch;
|
||||
while ((tcMatch = testcasePattern.exec(xmlContent)) !== null) {
|
||||
const block = tcMatch[0];
|
||||
|
||||
// Only process testcases that contain a <failure> element
|
||||
if (!block.includes('<failure')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Extract testcase attributes (order-independent)
|
||||
const tcTag = block.match(/<testcase\s[^>]*>/);
|
||||
if (!tcTag) continue;
|
||||
|
||||
const cnMatch = tcTag[0].match(/\sclassname="([^"]*)"/);
|
||||
const tnMatch = tcTag[0].match(/\sname="([^"]*)"/);
|
||||
|
||||
const className = cnMatch ? cnMatch[1] : 'unknown';
|
||||
const testName = tnMatch ? tnMatch[1] : 'unknown';
|
||||
|
||||
// Extract failure message
|
||||
const failTag = block.match(/<failure\s[^>]*>/);
|
||||
const msgMatch = failTag ? failTag[0].match(/\smessage="([^"]*)"/) : null;
|
||||
const message = msgMatch ? msgMatch[1] : 'Test failed';
|
||||
|
||||
// Extract stack trace from CDATA or text content
|
||||
const cdataMatch = block.match(/<failure[^>]*>[\s\S]*?<!\[CDATA\[([\s\S]*?)\]\]>/);
|
||||
const textMatch = !cdataMatch ? block.match(/<failure[^>]*>([^<]*)<\/failure>/) : null;
|
||||
const stackTrace = cdataMatch ? cdataMatch[1].trim() : textMatch ? textMatch[1].trim() : undefined;
|
||||
|
||||
failures.push({ testName, className, message, stackTrace: stackTrace || undefined });
|
||||
}
|
||||
|
||||
const passed = totalTests - failureCount - skippedCount;
|
||||
|
||||
return {
|
||||
runName,
|
||||
passed: Math.max(0, passed),
|
||||
failed: failureCount,
|
||||
skipped: skippedCount,
|
||||
duration,
|
||||
failures,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a Unity JSON test result file into a TestResult.
|
||||
*/
|
||||
static parseJsonResults(jsonPath: string): TestResult {
|
||||
if (!fs.existsSync(jsonPath)) {
|
||||
throw new Error(`JSON result file not found: ${jsonPath}`);
|
||||
}
|
||||
|
||||
const content = fs.readFileSync(jsonPath, 'utf8');
|
||||
const data = JSON.parse(content);
|
||||
|
||||
return TestResultReporter.parseJsonData(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse Unity JSON test result data into a TestResult.
|
||||
*/
|
||||
static parseJsonData(data: any): TestResult {
|
||||
const runName = data.name ?? data.suiteName ?? 'unknown';
|
||||
const passed = data.passed ?? data.passCount ?? 0;
|
||||
const failed = data.failed ?? data.failCount ?? 0;
|
||||
const skipped = data.skipped ?? data.skipCount ?? data.inconclusive ?? 0;
|
||||
const duration = data.duration ?? data.time ?? 0;
|
||||
|
||||
const failures: TestFailure[] = [];
|
||||
|
||||
// Unity test results may have a 'testResults' or 'results' array
|
||||
const results = data.testResults ?? data.results ?? data.children ?? [];
|
||||
|
||||
if (Array.isArray(results)) {
|
||||
for (const result of results) {
|
||||
TestResultReporter.extractFailures(result, failures);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
runName,
|
||||
passed,
|
||||
failed,
|
||||
skipped,
|
||||
duration,
|
||||
failures,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively extract failures from nested Unity test result JSON.
|
||||
*/
|
||||
private static extractFailures(node: any, failures: TestFailure[]): void {
|
||||
if (!node) return;
|
||||
|
||||
const status = (node.result ?? node.status ?? '').toLowerCase();
|
||||
if (status === 'failed' || status === 'failure') {
|
||||
failures.push({
|
||||
testName: node.name ?? node.testName ?? 'unknown',
|
||||
className: node.className ?? node.fullName ?? node.name ?? 'unknown',
|
||||
message: node.message ?? node.output ?? 'Test failed',
|
||||
stackTrace: node.stackTrace ?? node.trace ?? undefined,
|
||||
});
|
||||
}
|
||||
|
||||
// Recurse into children (Unity nests test fixtures inside suites)
|
||||
const children = node.children ?? node.testResults ?? node.results ?? [];
|
||||
if (Array.isArray(children)) {
|
||||
for (const child of children) {
|
||||
TestResultReporter.extractFailures(child, failures);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a markdown summary table from an array of test results.
|
||||
*/
|
||||
static generateSummary(results: TestResult[]): string {
|
||||
if (results.length === 0) {
|
||||
return 'No test results available.';
|
||||
}
|
||||
|
||||
const lines: string[] = [];
|
||||
lines.push('## Test Results Summary');
|
||||
lines.push('');
|
||||
lines.push('| Run | Passed | Failed | Skipped | Duration |');
|
||||
lines.push('|-----|--------|--------|---------|----------|');
|
||||
|
||||
let totalPassed = 0;
|
||||
let totalFailed = 0;
|
||||
let totalSkipped = 0;
|
||||
let totalDuration = 0;
|
||||
|
||||
for (const result of results) {
|
||||
const status = result.failed > 0 ? 'X' : 'OK';
|
||||
const durationStr = TestResultReporter.formatDuration(result.duration);
|
||||
lines.push(
|
||||
`| ${status} ${result.runName} | ${result.passed} | ${result.failed} | ${result.skipped} | ${durationStr} |`,
|
||||
);
|
||||
totalPassed += result.passed;
|
||||
totalFailed += result.failed;
|
||||
totalSkipped += result.skipped;
|
||||
totalDuration += result.duration;
|
||||
}
|
||||
|
||||
lines.push(
|
||||
`| **Total** | **${totalPassed}** | **${totalFailed}** | **${totalSkipped}** | **${TestResultReporter.formatDuration(
|
||||
totalDuration,
|
||||
)}** |`,
|
||||
);
|
||||
lines.push('');
|
||||
|
||||
// Append failure details if any
|
||||
const allFailures = results.flatMap((r) => r.failures.map((f) => ({ ...f, run: r.runName })));
|
||||
if (allFailures.length > 0) {
|
||||
lines.push('### Failures');
|
||||
lines.push('');
|
||||
for (const failure of allFailures) {
|
||||
lines.push(`**${failure.run}** - \`${failure.className}.${failure.testName}\``);
|
||||
lines.push(`> ${failure.message}`);
|
||||
if (failure.stackTrace) {
|
||||
lines.push('```');
|
||||
lines.push(failure.stackTrace.slice(0, 500));
|
||||
lines.push('```');
|
||||
}
|
||||
lines.push('');
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Write test results to the output path in the specified format(s).
|
||||
*/
|
||||
static writeResults(results: TestResult[], outputPath: string, format: 'junit' | 'json' | 'both'): void {
|
||||
if (!fs.existsSync(outputPath)) {
|
||||
fs.mkdirSync(outputPath, { recursive: true });
|
||||
}
|
||||
|
||||
if (format === 'json' || format === 'both') {
|
||||
const jsonPath = path.join(outputPath, 'test-results.json');
|
||||
fs.writeFileSync(jsonPath, JSON.stringify(results, null, 2), 'utf8');
|
||||
}
|
||||
|
||||
if (format === 'junit' || format === 'both') {
|
||||
const junitPath = path.join(outputPath, 'test-results.xml');
|
||||
const xml = TestResultReporter.toJUnitXml(results);
|
||||
fs.writeFileSync(junitPath, xml, 'utf8');
|
||||
}
|
||||
|
||||
// Always write markdown summary
|
||||
const summaryPath = path.join(outputPath, 'test-summary.md');
|
||||
const summary = TestResultReporter.generateSummary(results);
|
||||
fs.writeFileSync(summaryPath, summary, 'utf8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert TestResult array to JUnit XML format.
|
||||
*/
|
||||
private static toJUnitXml(results: TestResult[]): string {
|
||||
const lines: string[] = [];
|
||||
lines.push('<?xml version="1.0" encoding="UTF-8"?>');
|
||||
lines.push('<testsuites>');
|
||||
|
||||
for (const result of results) {
|
||||
const total = result.passed + result.failed + result.skipped;
|
||||
lines.push(
|
||||
` <testsuite name="${TestResultReporter.escapeXml(result.runName)}" tests="${total}" failures="${
|
||||
result.failed
|
||||
}" skipped="${result.skipped}" time="${result.duration.toFixed(3)}">`,
|
||||
);
|
||||
|
||||
// Write failure test cases
|
||||
for (const failure of result.failures) {
|
||||
lines.push(
|
||||
` <testcase classname="${TestResultReporter.escapeXml(
|
||||
failure.className,
|
||||
)}" name="${TestResultReporter.escapeXml(failure.testName)}">`,
|
||||
);
|
||||
lines.push(` <failure message="${TestResultReporter.escapeXml(failure.message)}">`);
|
||||
if (failure.stackTrace) {
|
||||
lines.push(` <![CDATA[${failure.stackTrace}]]>`);
|
||||
}
|
||||
lines.push(' </failure>');
|
||||
lines.push(' </testcase>');
|
||||
}
|
||||
|
||||
lines.push(' </testsuite>');
|
||||
}
|
||||
|
||||
lines.push('</testsuites>');
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape special XML characters.
|
||||
*/
|
||||
private static escapeXml(str: string): string {
|
||||
return str
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a duration in seconds to a human-readable string.
|
||||
*/
|
||||
private static formatDuration(seconds: number): string {
|
||||
if (seconds < 60) {
|
||||
return `${seconds.toFixed(1)}s`;
|
||||
}
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
const remainingSeconds = seconds % 60;
|
||||
return `${minutes}m ${remainingSeconds.toFixed(0)}s`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
import fs from 'node:fs';
|
||||
import YAML from 'yaml';
|
||||
import { TestSuiteDefinition, TestRunDefinition } from './test-workflow-types';
|
||||
|
||||
/**
|
||||
* Parses and validates YAML-based test suite definition files.
|
||||
* Handles dependency resolution (topological sort) for ordered test run execution.
|
||||
*/
|
||||
export class TestSuiteParser {
|
||||
/**
|
||||
* Read and parse a YAML test suite definition file.
|
||||
* Validates the structure and returns a typed TestSuiteDefinition.
|
||||
*/
|
||||
static parseSuiteFile(filePath: string): TestSuiteDefinition {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
throw new Error(`Test suite file not found: ${filePath}`);
|
||||
}
|
||||
|
||||
const content = fs.readFileSync(filePath, 'utf8');
|
||||
const parsed = YAML.parse(content);
|
||||
|
||||
if (!parsed || typeof parsed !== 'object') {
|
||||
throw new Error(`Invalid YAML in test suite file: ${filePath}`);
|
||||
}
|
||||
|
||||
if (!parsed.name || typeof parsed.name !== 'string') {
|
||||
throw new Error(`Test suite must have a 'name' field (string): ${filePath}`);
|
||||
}
|
||||
|
||||
if (!Array.isArray(parsed.runs) || parsed.runs.length === 0) {
|
||||
throw new Error(`Test suite must have a non-empty 'runs' array: ${filePath}`);
|
||||
}
|
||||
|
||||
const suite: TestSuiteDefinition = {
|
||||
name: parsed.name,
|
||||
description: parsed.description,
|
||||
runs: parsed.runs.map((run: any) => TestSuiteParser.parseRun(run)),
|
||||
};
|
||||
|
||||
const errors = TestSuiteParser.validateSuite(suite);
|
||||
if (errors.length > 0) {
|
||||
throw new Error(`Test suite validation failed:\n ${errors.join('\n ')}`);
|
||||
}
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a single run definition from raw YAML data.
|
||||
*/
|
||||
private static parseRun(raw: any): TestRunDefinition {
|
||||
if (!raw || typeof raw !== 'object') {
|
||||
throw new Error(`Each run must be an object`);
|
||||
}
|
||||
|
||||
if (!raw.name || typeof raw.name !== 'string') {
|
||||
throw new Error(`Each run must have a 'name' field (string)`);
|
||||
}
|
||||
|
||||
const run: TestRunDefinition = {
|
||||
name: raw.name,
|
||||
};
|
||||
|
||||
if (raw.needs !== undefined) {
|
||||
if (!Array.isArray(raw.needs)) {
|
||||
throw new Error(`Run '${raw.name}': 'needs' must be an array of strings`);
|
||||
}
|
||||
run.needs = raw.needs;
|
||||
}
|
||||
|
||||
if (raw.editMode !== undefined) {
|
||||
run.editMode = Boolean(raw.editMode);
|
||||
}
|
||||
|
||||
if (raw.playMode !== undefined) {
|
||||
run.playMode = Boolean(raw.playMode);
|
||||
}
|
||||
|
||||
if (raw.builtClient !== undefined) {
|
||||
run.builtClient = Boolean(raw.builtClient);
|
||||
}
|
||||
|
||||
if (raw.builtClientPath !== undefined) {
|
||||
run.builtClientPath = String(raw.builtClientPath);
|
||||
}
|
||||
|
||||
if (raw.filters !== undefined) {
|
||||
if (typeof raw.filters !== 'object' || Array.isArray(raw.filters)) {
|
||||
throw new Error(`Run '${raw.name}': 'filters' must be a key-value object`);
|
||||
}
|
||||
run.filters = {};
|
||||
for (const [key, value] of Object.entries(raw.filters)) {
|
||||
run.filters[key] = String(value);
|
||||
}
|
||||
}
|
||||
|
||||
if (raw.timeout !== undefined) {
|
||||
const timeout = Number(raw.timeout);
|
||||
if (Number.isNaN(timeout) || timeout <= 0) {
|
||||
throw new Error(`Run '${raw.name}': 'timeout' must be a positive number`);
|
||||
}
|
||||
run.timeout = timeout;
|
||||
}
|
||||
|
||||
return run;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve run execution order via topological sort based on 'needs' dependencies.
|
||||
* Returns an array of parallel groups -- each group contains runs that can execute concurrently.
|
||||
* Runs within the same group have no inter-dependencies.
|
||||
*/
|
||||
static resolveRunOrder(suite: TestSuiteDefinition): TestRunDefinition[][] {
|
||||
const runMap = new Map<string, TestRunDefinition>();
|
||||
for (const run of suite.runs) {
|
||||
runMap.set(run.name, run);
|
||||
}
|
||||
|
||||
// Build adjacency: inDegree counts and dependents map
|
||||
const inDegree = new Map<string, number>();
|
||||
const dependents = new Map<string, string[]>();
|
||||
|
||||
for (const run of suite.runs) {
|
||||
if (!inDegree.has(run.name)) {
|
||||
inDegree.set(run.name, 0);
|
||||
}
|
||||
if (!dependents.has(run.name)) {
|
||||
dependents.set(run.name, []);
|
||||
}
|
||||
|
||||
if (run.needs) {
|
||||
for (const dep of run.needs) {
|
||||
inDegree.set(run.name, (inDegree.get(run.name) ?? 0) + 1);
|
||||
if (!dependents.has(dep)) {
|
||||
dependents.set(dep, []);
|
||||
}
|
||||
dependents.get(dep)!.push(run.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Kahn's algorithm producing parallel layers
|
||||
const groups: TestRunDefinition[][] = [];
|
||||
let ready = suite.runs.filter((r) => (inDegree.get(r.name) ?? 0) === 0);
|
||||
let processed = 0;
|
||||
|
||||
while (ready.length > 0) {
|
||||
groups.push(ready);
|
||||
processed += ready.length;
|
||||
|
||||
const nextReady: TestRunDefinition[] = [];
|
||||
for (const run of ready) {
|
||||
for (const dep of dependents.get(run.name) ?? []) {
|
||||
const newDegree = (inDegree.get(dep) ?? 1) - 1;
|
||||
inDegree.set(dep, newDegree);
|
||||
if (newDegree === 0) {
|
||||
nextReady.push(runMap.get(dep)!);
|
||||
}
|
||||
}
|
||||
}
|
||||
ready = nextReady;
|
||||
}
|
||||
|
||||
if (processed !== suite.runs.length) {
|
||||
throw new Error(`Circular dependency detected in test suite '${suite.name}'`);
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a parsed test suite definition.
|
||||
* Returns an array of validation error messages (empty = valid).
|
||||
*/
|
||||
static validateSuite(suite: TestSuiteDefinition): string[] {
|
||||
const errors: string[] = [];
|
||||
const runNames = new Set<string>();
|
||||
|
||||
// Check for duplicate run names
|
||||
for (const run of suite.runs) {
|
||||
if (runNames.has(run.name)) {
|
||||
errors.push(`Duplicate run name: '${run.name}'`);
|
||||
}
|
||||
runNames.add(run.name);
|
||||
}
|
||||
|
||||
// Check that all 'needs' references exist
|
||||
for (const run of suite.runs) {
|
||||
if (run.needs) {
|
||||
for (const dep of run.needs) {
|
||||
if (!runNames.has(dep)) {
|
||||
errors.push(`Run '${run.name}' depends on unknown run '${dep}'`);
|
||||
}
|
||||
}
|
||||
|
||||
// Self-dependency
|
||||
if (run.needs.includes(run.name)) {
|
||||
errors.push(`Run '${run.name}' depends on itself`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check that at least one test mode is specified per run
|
||||
for (const run of suite.runs) {
|
||||
if (!run.editMode && !run.playMode && !run.builtClient) {
|
||||
errors.push(`Run '${run.name}' must specify at least one of: editMode, playMode, builtClient`);
|
||||
}
|
||||
}
|
||||
|
||||
// Detect circular dependencies via DFS
|
||||
const circularError = TestSuiteParser.detectCircularDependencies(suite);
|
||||
if (circularError) {
|
||||
errors.push(circularError);
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect circular dependencies using DFS cycle detection.
|
||||
*/
|
||||
private static detectCircularDependencies(suite: TestSuiteDefinition): string | null {
|
||||
const adjacency = new Map<string, string[]>();
|
||||
for (const run of suite.runs) {
|
||||
adjacency.set(run.name, run.needs ?? []);
|
||||
}
|
||||
|
||||
const visited = new Set<string>();
|
||||
const visiting = new Set<string>();
|
||||
|
||||
const dfs = (node: string, path: string[]): string | null => {
|
||||
if (visiting.has(node)) {
|
||||
const cycleStart = path.indexOf(node);
|
||||
const cycle = path.slice(cycleStart).concat(node);
|
||||
return `Circular dependency: ${cycle.join(' -> ')}`;
|
||||
}
|
||||
if (visited.has(node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
visiting.add(node);
|
||||
path.push(node);
|
||||
|
||||
for (const dep of adjacency.get(node) ?? []) {
|
||||
if (adjacency.has(dep)) {
|
||||
const result = dfs(dep, [...path]);
|
||||
if (result) return result;
|
||||
}
|
||||
}
|
||||
|
||||
visiting.delete(node);
|
||||
visited.add(node);
|
||||
return null;
|
||||
};
|
||||
|
||||
for (const run of suite.runs) {
|
||||
const result = dfs(run.name, []);
|
||||
if (result) return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
import { exec } from 'node:child_process';
|
||||
import { promisify } from 'node:util';
|
||||
import path from 'node:path';
|
||||
import * as core from '@actions/core';
|
||||
import BuildParameters from '../../../build-parameters';
|
||||
import { TestSuiteParser } from './test-suite-parser';
|
||||
import { TaxonomyFilterService } from './taxonomy-filter-service';
|
||||
import { TestResultReporter } from './test-result-reporter';
|
||||
import { TestRunDefinition, TestResult } from './test-workflow-types';
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
/**
|
||||
* Main entry point for the test workflow engine.
|
||||
* Orchestrates parsing of YAML suite definitions, resolving run order,
|
||||
* executing test runs via Unity CLI, and collecting structured results.
|
||||
*/
|
||||
export class TestWorkflowService {
|
||||
/**
|
||||
* Execute a full test suite from a YAML definition file.
|
||||
* Parses the suite, resolves dependency order, executes each parallel
|
||||
* group sequentially (runs within a group execute concurrently), and
|
||||
* collects all results.
|
||||
*/
|
||||
static async executeTestSuite(suitePath: string, parameters: BuildParameters): Promise<TestResult[]> {
|
||||
core.info(`[TestWorkflow] Loading test suite from: ${suitePath}`);
|
||||
|
||||
const suite = TestSuiteParser.parseSuiteFile(suitePath);
|
||||
core.info(`[TestWorkflow] Suite '${suite.name}' loaded with ${suite.runs.length} run(s)`);
|
||||
|
||||
if (suite.description) {
|
||||
core.info(`[TestWorkflow] Description: ${suite.description}`);
|
||||
}
|
||||
|
||||
const groups = TestSuiteParser.resolveRunOrder(suite);
|
||||
core.info(`[TestWorkflow] Resolved into ${groups.length} execution group(s)`);
|
||||
|
||||
const allResults: TestResult[] = [];
|
||||
let groupIndex = 0;
|
||||
|
||||
for (const group of groups) {
|
||||
groupIndex++;
|
||||
const runNames = group.map((r) => r.name).join(', ');
|
||||
core.info(`[TestWorkflow] Executing group ${groupIndex}/${groups.length}: [${runNames}]`);
|
||||
|
||||
// Execute runs within a group concurrently
|
||||
const groupResults = await Promise.all(group.map((run) => TestWorkflowService.executeTestRun(run, parameters)));
|
||||
|
||||
allResults.push(...groupResults);
|
||||
|
||||
// Check for failures -- if any run in this group failed, log a warning
|
||||
// but continue to the next group (fail-forward for maximum feedback)
|
||||
const failedRuns = groupResults.filter((r) => r.failed > 0);
|
||||
if (failedRuns.length > 0) {
|
||||
const failedNames = failedRuns.map((r) => r.runName).join(', ');
|
||||
core.warning(`[TestWorkflow] Failures detected in group ${groupIndex}: [${failedNames}]`);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate and output summary
|
||||
const summary = TestResultReporter.generateSummary(allResults);
|
||||
core.info(summary);
|
||||
|
||||
// Write results if output path is configured
|
||||
const resultPath = parameters.testResultPath;
|
||||
const resultFormat = parameters.testResultFormat;
|
||||
if (resultPath) {
|
||||
TestResultReporter.writeResults(allResults, resultPath, resultFormat as 'junit' | 'json' | 'both');
|
||||
core.info(`[TestWorkflow] Results written to: ${resultPath}`);
|
||||
}
|
||||
|
||||
return allResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a single test run definition.
|
||||
* Builds the Unity CLI arguments based on the run configuration (edit mode,
|
||||
* play mode, built client) and taxonomy filters, executes the command
|
||||
* asynchronously, and parses the result output.
|
||||
*
|
||||
* Uses promisified exec instead of execSync so that Promise.all can
|
||||
* actually run multiple test groups in parallel without blocking the
|
||||
* Node.js event loop.
|
||||
*/
|
||||
static async executeTestRun(run: TestRunDefinition, parameters: BuildParameters): Promise<TestResult> {
|
||||
core.info(`[TestWorkflow] Starting run: '${run.name}'`);
|
||||
|
||||
const unityArguments = TestWorkflowService.buildUnityArgs(run, parameters);
|
||||
const timeoutMs = (run.timeout ?? 600) * 1000;
|
||||
|
||||
core.info(`[TestWorkflow] Unity args: ${unityArguments}`);
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
const resultDirectory = path.join(parameters.testResultPath ?? './test-results', run.name);
|
||||
const resultFile = path.join(resultDirectory, 'results.xml');
|
||||
|
||||
// Build the full Unity command
|
||||
const unityPath = TestWorkflowService.resolveUnityPath(parameters);
|
||||
const command = `"${unityPath}" ${unityArguments} -testResults "${resultFile}"`;
|
||||
|
||||
core.info(`[TestWorkflow] Executing: ${command}`);
|
||||
|
||||
await execAsync(command, {
|
||||
timeout: timeoutMs,
|
||||
maxBuffer: 50 * 1024 * 1024, // 50 MB to handle large Unity output
|
||||
cwd: parameters.projectPath || process.cwd(),
|
||||
});
|
||||
|
||||
const duration = (Date.now() - startTime) / 1000;
|
||||
|
||||
// Parse the result file
|
||||
try {
|
||||
const result = TestResultReporter.parseJUnitResults(resultFile);
|
||||
result.runName = run.name;
|
||||
result.duration = duration;
|
||||
|
||||
return result;
|
||||
} catch {
|
||||
// Result file may not exist if Unity exited early
|
||||
core.warning(`[TestWorkflow] Could not parse results for run '${run.name}' -- result file may be missing`);
|
||||
|
||||
return {
|
||||
runName: run.name,
|
||||
passed: 0,
|
||||
failed: 0,
|
||||
skipped: 0,
|
||||
duration,
|
||||
failures: [],
|
||||
};
|
||||
}
|
||||
} catch (error: any) {
|
||||
const duration = (Date.now() - startTime) / 1000;
|
||||
|
||||
// The promisified exec sets error.killed when the process is terminated
|
||||
// due to timeout, and error.signal will be 'SIGTERM'
|
||||
const isTimeout = error.killed === true || error.signal === 'SIGTERM';
|
||||
|
||||
if (isTimeout) {
|
||||
core.error(`[TestWorkflow] Run '${run.name}' timed out after ${run.timeout ?? 600}s`);
|
||||
} else {
|
||||
core.error(`[TestWorkflow] Run '${run.name}' failed: ${error.message}`);
|
||||
}
|
||||
|
||||
// Try to parse partial results even on failure
|
||||
const resultDirectory = path.join(parameters.testResultPath ?? './test-results', run.name);
|
||||
const resultFile = path.join(resultDirectory, 'results.xml');
|
||||
|
||||
try {
|
||||
const result = TestResultReporter.parseJUnitResults(resultFile);
|
||||
result.runName = run.name;
|
||||
result.duration = duration;
|
||||
|
||||
return result;
|
||||
} catch {
|
||||
return {
|
||||
runName: run.name,
|
||||
passed: 0,
|
||||
failed: 1,
|
||||
skipped: 0,
|
||||
duration,
|
||||
failures: [
|
||||
{
|
||||
testName: isTimeout ? 'Timeout' : 'ExecutionError',
|
||||
className: run.name,
|
||||
message: isTimeout
|
||||
? `Test run timed out after ${run.timeout ?? 600}s`
|
||||
: error.message ?? 'Unknown execution error',
|
||||
stackTrace: error.stderr ?? undefined,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build Unity CLI arguments for a test run based on its configuration.
|
||||
*/
|
||||
static buildUnityArgs(run: TestRunDefinition, parameters: BuildParameters): string {
|
||||
const unityArguments: string[] = ['-batchmode', '-nographics'];
|
||||
|
||||
// Project path
|
||||
if (parameters.projectPath) {
|
||||
unityArguments.push(`-projectPath "${parameters.projectPath}"`);
|
||||
}
|
||||
|
||||
// Test mode
|
||||
if (run.builtClient && run.builtClientPath) {
|
||||
// Built client testing: run tests against a built player
|
||||
unityArguments.push(
|
||||
'-runTests',
|
||||
`-testPlatform StandalonePlayer`,
|
||||
`-assemblyNames Assembly-CSharp-Tests`,
|
||||
`-builtPlayerPath "${run.builtClientPath}"`,
|
||||
);
|
||||
} else if (run.editMode && run.playMode) {
|
||||
// Both modes: run EditMode first, then PlayMode will require a separate invocation
|
||||
// For combined mode, use EditMode (the service handles sequencing)
|
||||
unityArguments.push('-runTests', '-testPlatform EditMode');
|
||||
} else if (run.playMode) {
|
||||
unityArguments.push('-runTests', '-testPlatform PlayMode');
|
||||
} else if (run.editMode) {
|
||||
unityArguments.push('-runTests', '-testPlatform EditMode');
|
||||
}
|
||||
|
||||
// Apply taxonomy filters
|
||||
if (run.filters && Object.keys(run.filters).length > 0) {
|
||||
const filterArguments = TaxonomyFilterService.buildFilterArgs(run.filters);
|
||||
if (filterArguments) {
|
||||
unityArguments.push(filterArguments);
|
||||
}
|
||||
}
|
||||
|
||||
// Target platform
|
||||
if (parameters.targetPlatform) {
|
||||
unityArguments.push(`-buildTarget ${parameters.targetPlatform}`);
|
||||
}
|
||||
|
||||
return unityArguments.join(' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the path to the Unity editor executable.
|
||||
*/
|
||||
private static resolveUnityPath(parameters: BuildParameters): string {
|
||||
// In CI, Unity path is typically set via environment or the docker container
|
||||
const environmentUnityPath = process.env.UNITY_PATH ?? process.env.UNITY_EDITOR;
|
||||
if (environmentUnityPath) {
|
||||
return environmentUnityPath;
|
||||
}
|
||||
|
||||
// Default paths by platform
|
||||
if (process.platform === 'win32') {
|
||||
return `C:/Program Files/Unity/Hub/Editor/${parameters.editorVersion}/Editor/Unity.exe`;
|
||||
}
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
return `/Applications/Unity/Hub/Editor/${parameters.editorVersion}/Unity.app/Contents/MacOS/Unity`;
|
||||
}
|
||||
|
||||
// Linux default (Docker container path)
|
||||
return '/opt/unity/Editor/Unity';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
export interface TestSuiteDefinition {
|
||||
name: string;
|
||||
description?: string;
|
||||
runs: TestRunDefinition[];
|
||||
}
|
||||
|
||||
export interface TestRunDefinition {
|
||||
name: string;
|
||||
needs?: string[];
|
||||
editMode?: boolean;
|
||||
playMode?: boolean;
|
||||
builtClient?: boolean;
|
||||
builtClientPath?: string;
|
||||
filters?: Record<string, string>; // dimension -> comma-separated values or /regex/
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export interface TaxonomyDimension {
|
||||
name: string;
|
||||
values: string[];
|
||||
}
|
||||
|
||||
export interface TaxonomyDefinition {
|
||||
extensible_groups: TaxonomyDimension[];
|
||||
}
|
||||
|
||||
export interface TestResult {
|
||||
runName: string;
|
||||
passed: number;
|
||||
failed: number;
|
||||
skipped: number;
|
||||
duration: number;
|
||||
failures: TestFailure[];
|
||||
}
|
||||
|
||||
export interface TestFailure {
|
||||
testName: string;
|
||||
className: string;
|
||||
message: string;
|
||||
stackTrace?: string;
|
||||
}
|
||||
@@ -0,0 +1,562 @@
|
||||
import fs from 'node:fs';
|
||||
import { TestSuiteParser } from './test-suite-parser';
|
||||
import { TaxonomyFilterService } from './taxonomy-filter-service';
|
||||
import { TestResultReporter } from './test-result-reporter';
|
||||
import { TestWorkflowService } from './test-workflow-service';
|
||||
import { TestSuiteDefinition, TestResult, TestRunDefinition } from './test-workflow-types';
|
||||
|
||||
jest.mock('node:fs');
|
||||
jest.mock('@actions/core');
|
||||
|
||||
const mockFs = fs as jest.Mocked<typeof fs>;
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Test Suite Parser
|
||||
// ============================================================================
|
||||
|
||||
describe('TestSuiteParser', () => {
|
||||
describe('parseSuiteFile', () => {
|
||||
it('should parse a valid YAML suite file', () => {
|
||||
const yaml = `
|
||||
name: pull-request
|
||||
description: Fast feedback for pull requests
|
||||
runs:
|
||||
- name: fast
|
||||
editMode: true
|
||||
filters:
|
||||
Maturity: Trusted
|
||||
FeedbackSpeed: Fast,Moderate
|
||||
timeout: 300
|
||||
- name: basic
|
||||
needs: [fast]
|
||||
editMode: true
|
||||
playMode: true
|
||||
filters:
|
||||
Maturity: Trusted,Adolescent
|
||||
timeout: 600
|
||||
`;
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(yaml);
|
||||
|
||||
const suite = TestSuiteParser.parseSuiteFile('/path/to/suite.yml');
|
||||
|
||||
expect(suite.name).toBe('pull-request');
|
||||
expect(suite.description).toBe('Fast feedback for pull requests');
|
||||
expect(suite.runs).toHaveLength(2);
|
||||
expect(suite.runs[0].name).toBe('fast');
|
||||
expect(suite.runs[0].editMode).toBe(true);
|
||||
expect(suite.runs[0].filters?.Maturity).toBe('Trusted');
|
||||
expect(suite.runs[0].timeout).toBe(300);
|
||||
expect(suite.runs[1].needs).toEqual(['fast']);
|
||||
});
|
||||
|
||||
it('should throw when file does not exist', () => {
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
|
||||
expect(() => TestSuiteParser.parseSuiteFile('/missing.yml')).toThrow('not found');
|
||||
});
|
||||
|
||||
it('should throw on invalid YAML', () => {
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue('not: valid: yaml: [');
|
||||
|
||||
expect(() => TestSuiteParser.parseSuiteFile('/bad.yml')).toThrow();
|
||||
});
|
||||
|
||||
it('should throw when suite has no name', () => {
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue('runs:\n - name: test\n editMode: true');
|
||||
|
||||
expect(() => TestSuiteParser.parseSuiteFile('/no-name.yml')).toThrow("'name'");
|
||||
});
|
||||
|
||||
it('should throw when suite has no runs', () => {
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue('name: test');
|
||||
|
||||
expect(() => TestSuiteParser.parseSuiteFile('/no-runs.yml')).toThrow("'runs'");
|
||||
});
|
||||
|
||||
it('should throw on invalid needs reference', () => {
|
||||
const yaml = `
|
||||
name: bad-deps
|
||||
runs:
|
||||
- name: first
|
||||
editMode: true
|
||||
needs: [nonexistent]
|
||||
`;
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(yaml);
|
||||
|
||||
expect(() => TestSuiteParser.parseSuiteFile('/bad-deps.yml')).toThrow("unknown run 'nonexistent'");
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveRunOrder', () => {
|
||||
it('should return single group when no dependencies exist', () => {
|
||||
const suite: TestSuiteDefinition = {
|
||||
name: 'test',
|
||||
runs: [
|
||||
{ name: 'a', editMode: true },
|
||||
{ name: 'b', playMode: true },
|
||||
],
|
||||
};
|
||||
|
||||
const groups = TestSuiteParser.resolveRunOrder(suite);
|
||||
expect(groups).toHaveLength(1);
|
||||
expect(groups[0]).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should resolve linear dependencies into sequential groups', () => {
|
||||
const suite: TestSuiteDefinition = {
|
||||
name: 'test',
|
||||
runs: [
|
||||
{ name: 'a', editMode: true },
|
||||
{ name: 'b', needs: ['a'], playMode: true },
|
||||
{ name: 'c', needs: ['b'], editMode: true },
|
||||
],
|
||||
};
|
||||
|
||||
const groups = TestSuiteParser.resolveRunOrder(suite);
|
||||
expect(groups).toHaveLength(3);
|
||||
expect(groups[0][0].name).toBe('a');
|
||||
expect(groups[1][0].name).toBe('b');
|
||||
expect(groups[2][0].name).toBe('c');
|
||||
});
|
||||
|
||||
it('should place independent runs in the same group', () => {
|
||||
const suite: TestSuiteDefinition = {
|
||||
name: 'test',
|
||||
runs: [
|
||||
{ name: 'root', editMode: true },
|
||||
{ name: 'branch-a', needs: ['root'], playMode: true },
|
||||
{ name: 'branch-b', needs: ['root'], editMode: true },
|
||||
],
|
||||
};
|
||||
|
||||
const groups = TestSuiteParser.resolveRunOrder(suite);
|
||||
expect(groups).toHaveLength(2);
|
||||
expect(groups[0]).toHaveLength(1);
|
||||
expect(groups[1]).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should detect circular dependencies', () => {
|
||||
const suite: TestSuiteDefinition = {
|
||||
name: 'circular',
|
||||
runs: [
|
||||
{ name: 'a', needs: ['b'], editMode: true },
|
||||
{ name: 'b', needs: ['a'], playMode: true },
|
||||
],
|
||||
};
|
||||
|
||||
expect(() => TestSuiteParser.resolveRunOrder(suite)).toThrow('Circular dependency');
|
||||
});
|
||||
|
||||
it('should detect three-way circular dependencies', () => {
|
||||
const suite: TestSuiteDefinition = {
|
||||
name: 'circular3',
|
||||
runs: [
|
||||
{ name: 'a', needs: ['c'], editMode: true },
|
||||
{ name: 'b', needs: ['a'], playMode: true },
|
||||
{ name: 'c', needs: ['b'], editMode: true },
|
||||
],
|
||||
};
|
||||
|
||||
expect(() => TestSuiteParser.resolveRunOrder(suite)).toThrow('Circular dependency');
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateSuite', () => {
|
||||
it('should return no errors for a valid suite', () => {
|
||||
const suite: TestSuiteDefinition = {
|
||||
name: 'valid',
|
||||
runs: [
|
||||
{ name: 'a', editMode: true },
|
||||
{ name: 'b', needs: ['a'], playMode: true },
|
||||
],
|
||||
};
|
||||
|
||||
const errors = TestSuiteParser.validateSuite(suite);
|
||||
expect(errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should detect duplicate run names', () => {
|
||||
const suite: TestSuiteDefinition = {
|
||||
name: 'dupes',
|
||||
runs: [
|
||||
{ name: 'a', editMode: true },
|
||||
{ name: 'a', playMode: true },
|
||||
],
|
||||
};
|
||||
|
||||
const errors = TestSuiteParser.validateSuite(suite);
|
||||
expect(errors.some((e) => e.includes('Duplicate'))).toBe(true);
|
||||
});
|
||||
|
||||
it('should detect missing test mode', () => {
|
||||
const suite: TestSuiteDefinition = {
|
||||
name: 'no-mode',
|
||||
runs: [{ name: 'empty' }],
|
||||
};
|
||||
|
||||
const errors = TestSuiteParser.validateSuite(suite);
|
||||
expect(errors.some((e) => e.includes('editMode'))).toBe(true);
|
||||
});
|
||||
|
||||
it('should detect self-dependency', () => {
|
||||
const suite: TestSuiteDefinition = {
|
||||
name: 'self-dep',
|
||||
runs: [{ name: 'a', needs: ['a'], editMode: true }],
|
||||
};
|
||||
|
||||
const errors = TestSuiteParser.validateSuite(suite);
|
||||
expect(errors.some((e) => e.includes('depends on itself'))).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Taxonomy Filter Service
|
||||
// ============================================================================
|
||||
|
||||
describe('TaxonomyFilterService', () => {
|
||||
describe('loadTaxonomy', () => {
|
||||
it('should return built-in dimensions when no file provided', () => {
|
||||
const dimensions = TaxonomyFilterService.loadTaxonomy();
|
||||
|
||||
expect(dimensions.length).toBeGreaterThanOrEqual(7);
|
||||
expect(dimensions.find((d) => d.name === 'Scope')).toBeDefined();
|
||||
expect(dimensions.find((d) => d.name === 'Maturity')).toBeDefined();
|
||||
expect(dimensions.find((d) => d.name === 'FeedbackSpeed')).toBeDefined();
|
||||
expect(dimensions.find((d) => d.name === 'Execution')).toBeDefined();
|
||||
expect(dimensions.find((d) => d.name === 'Rigor')).toBeDefined();
|
||||
expect(dimensions.find((d) => d.name === 'Determinism')).toBeDefined();
|
||||
expect(dimensions.find((d) => d.name === 'IsolationLevel')).toBeDefined();
|
||||
});
|
||||
|
||||
it('should merge custom dimensions from file', () => {
|
||||
const yaml = `
|
||||
extensible_groups:
|
||||
- name: SubjectLevel
|
||||
values: [Class, Feature, System, Product]
|
||||
- name: DataScenario
|
||||
values: [HappyPath, EdgeCase]
|
||||
`;
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(yaml);
|
||||
|
||||
const dimensions = TaxonomyFilterService.loadTaxonomy('/taxonomy.yml');
|
||||
|
||||
expect(dimensions.find((d) => d.name === 'SubjectLevel')).toBeDefined();
|
||||
expect(dimensions.find((d) => d.name === 'DataScenario')).toBeDefined();
|
||||
// Built-ins should still exist
|
||||
expect(dimensions.find((d) => d.name === 'Scope')).toBeDefined();
|
||||
});
|
||||
|
||||
it('should merge values for existing dimensions', () => {
|
||||
const yaml = `
|
||||
extensible_groups:
|
||||
- name: Scope
|
||||
values: [Unit, Integration, Acceptance]
|
||||
`;
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.readFileSync.mockReturnValue(yaml);
|
||||
|
||||
const dimensions = TaxonomyFilterService.loadTaxonomy('/taxonomy.yml');
|
||||
const scope = dimensions.find((d) => d.name === 'Scope');
|
||||
|
||||
expect(scope).toBeDefined();
|
||||
// Should contain built-in + custom values without duplicates
|
||||
expect(scope!.values).toContain('Unit');
|
||||
expect(scope!.values).toContain('Integration');
|
||||
expect(scope!.values).toContain('Acceptance');
|
||||
expect(scope!.values).toContain('System');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildFilterArgs', () => {
|
||||
it('should return empty string for empty filters', () => {
|
||||
expect(TaxonomyFilterService.buildFilterArgs({})).toBe('');
|
||||
});
|
||||
|
||||
it('should build single-value filter', () => {
|
||||
const result = TaxonomyFilterService.buildFilterArgs({ Maturity: 'Trusted' });
|
||||
expect(result).toBe('--testFilter "Maturity=Trusted"');
|
||||
});
|
||||
|
||||
it('should build multi-value filter with pipe separator', () => {
|
||||
const result = TaxonomyFilterService.buildFilterArgs({ Scope: 'Unit,Integration' });
|
||||
expect(result).toBe('--testFilter "Scope=Unit|Integration"');
|
||||
});
|
||||
|
||||
it('should build regex filter', () => {
|
||||
const result = TaxonomyFilterService.buildFilterArgs({ Maturity: '/Trusted|Adolescent/' });
|
||||
expect(result).toBe('--testFilter "Maturity=~Trusted|Adolescent"');
|
||||
});
|
||||
|
||||
it('should AND multiple dimensions with semicolon', () => {
|
||||
const result = TaxonomyFilterService.buildFilterArgs({
|
||||
Maturity: 'Trusted',
|
||||
Scope: 'Unit',
|
||||
});
|
||||
expect(result).toContain(';');
|
||||
expect(result).toContain('Maturity=Trusted');
|
||||
expect(result).toContain('Scope=Unit');
|
||||
});
|
||||
});
|
||||
|
||||
describe('matchesFilter', () => {
|
||||
it('should match exact value', () => {
|
||||
const match = TaxonomyFilterService.matchesFilter({ Scope: 'Unit', Maturity: 'Trusted' }, { Scope: 'Unit' });
|
||||
expect(match).toBe(true);
|
||||
});
|
||||
|
||||
it('should match comma-separated values', () => {
|
||||
const match = TaxonomyFilterService.matchesFilter({ Scope: 'Integration' }, { Scope: 'Unit,Integration' });
|
||||
expect(match).toBe(true);
|
||||
});
|
||||
|
||||
it('should not match when value is not in list', () => {
|
||||
const match = TaxonomyFilterService.matchesFilter({ Scope: 'End To End' }, { Scope: 'Unit,Integration' });
|
||||
expect(match).toBe(false);
|
||||
});
|
||||
|
||||
it('should require all dimensions to match (AND)', () => {
|
||||
const match = TaxonomyFilterService.matchesFilter(
|
||||
{ Scope: 'Unit', Maturity: 'Experimental' },
|
||||
{ Scope: 'Unit', Maturity: 'Trusted' },
|
||||
);
|
||||
expect(match).toBe(false);
|
||||
});
|
||||
|
||||
it('should match regex patterns', () => {
|
||||
const match = TaxonomyFilterService.matchesFilter({ Maturity: 'Trusted' }, { Maturity: '/Trusted|Adolescent/' });
|
||||
expect(match).toBe(true);
|
||||
});
|
||||
|
||||
it('should not match when regex does not match', () => {
|
||||
const match = TaxonomyFilterService.matchesFilter(
|
||||
{ Maturity: 'Experimental' },
|
||||
{ Maturity: '/Trusted|Adolescent/' },
|
||||
);
|
||||
expect(match).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false when test lacks a required dimension', () => {
|
||||
const match = TaxonomyFilterService.matchesFilter({}, { Scope: 'Unit' });
|
||||
expect(match).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle hierarchical dot-notation matching', () => {
|
||||
const match = TaxonomyFilterService.matchesFilter({ Domain: 'Combat.Melee.Sword' }, { Domain: 'Combat.Melee' });
|
||||
expect(match).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Test Result Reporter
|
||||
// ============================================================================
|
||||
|
||||
describe('TestResultReporter', () => {
|
||||
describe('parseJUnitXml', () => {
|
||||
it('should parse a valid JUnit XML string', () => {
|
||||
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<testsuite name="EditMode" tests="10" failures="2" skipped="1" time="5.432">
|
||||
<testcase classname="MyTests" name="TestA">
|
||||
</testcase>
|
||||
<testcase classname="MyTests" name="TestB">
|
||||
<failure message="Expected true">
|
||||
<![CDATA[at MyTests.TestB() in TestFile.cs:42]]>
|
||||
</failure>
|
||||
</testcase>
|
||||
</testsuite>`;
|
||||
|
||||
const result = TestResultReporter.parseJUnitXml(xml);
|
||||
|
||||
expect(result.runName).toBe('EditMode');
|
||||
expect(result.passed).toBe(7);
|
||||
expect(result.failed).toBe(2);
|
||||
expect(result.skipped).toBe(1);
|
||||
expect(result.duration).toBeCloseTo(5.432);
|
||||
expect(result.failures).toHaveLength(1);
|
||||
expect(result.failures[0].testName).toBe('TestB');
|
||||
expect(result.failures[0].className).toBe('MyTests');
|
||||
expect(result.failures[0].message).toBe('Expected true');
|
||||
expect(result.failures[0].stackTrace).toContain('TestFile.cs:42');
|
||||
});
|
||||
|
||||
it('should handle empty test suite', () => {
|
||||
const xml = `<testsuite name="Empty" tests="0" failures="0" time="0.0"></testsuite>`;
|
||||
|
||||
const result = TestResultReporter.parseJUnitXml(xml);
|
||||
expect(result.passed).toBe(0);
|
||||
expect(result.failed).toBe(0);
|
||||
expect(result.failures).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseJsonData', () => {
|
||||
it('should parse Unity JSON test result data', () => {
|
||||
const data = {
|
||||
name: 'PlayMode',
|
||||
passed: 15,
|
||||
failed: 2,
|
||||
skipped: 3,
|
||||
duration: 12.5,
|
||||
testResults: [{ name: 'FailingTest', className: 'MyClass', result: 'Failed', message: 'Assert failed' }],
|
||||
};
|
||||
|
||||
const result = TestResultReporter.parseJsonData(data);
|
||||
expect(result.runName).toBe('PlayMode');
|
||||
expect(result.passed).toBe(15);
|
||||
expect(result.failed).toBe(2);
|
||||
expect(result.skipped).toBe(3);
|
||||
expect(result.failures).toHaveLength(1);
|
||||
expect(result.failures[0].testName).toBe('FailingTest');
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateSummary', () => {
|
||||
it('should generate a markdown summary table', () => {
|
||||
const results: TestResult[] = [
|
||||
{
|
||||
runName: 'fast',
|
||||
passed: 10,
|
||||
failed: 0,
|
||||
skipped: 2,
|
||||
duration: 5.0,
|
||||
failures: [],
|
||||
},
|
||||
{
|
||||
runName: 'basic',
|
||||
passed: 20,
|
||||
failed: 1,
|
||||
skipped: 0,
|
||||
duration: 30.0,
|
||||
failures: [{ testName: 'TestX', className: 'ClassX', message: 'Expected 1 but got 2' }],
|
||||
},
|
||||
];
|
||||
|
||||
const summary = TestResultReporter.generateSummary(results);
|
||||
|
||||
expect(summary).toContain('Test Results Summary');
|
||||
expect(summary).toContain('fast');
|
||||
expect(summary).toContain('basic');
|
||||
expect(summary).toContain('Total');
|
||||
expect(summary).toContain('Failures');
|
||||
expect(summary).toContain('TestX');
|
||||
});
|
||||
|
||||
it('should return message when no results available', () => {
|
||||
const summary = TestResultReporter.generateSummary([]);
|
||||
expect(summary).toContain('No test results');
|
||||
});
|
||||
});
|
||||
|
||||
describe('writeResults', () => {
|
||||
it('should create output directory and write files for json format', () => {
|
||||
mockFs.existsSync.mockReturnValue(false);
|
||||
mockFs.mkdirSync.mockReturnValue(undefined as any);
|
||||
mockFs.writeFileSync.mockReturnValue(undefined);
|
||||
|
||||
const results: TestResult[] = [
|
||||
{ runName: 'test', passed: 5, failed: 0, skipped: 0, duration: 1.0, failures: [] },
|
||||
];
|
||||
|
||||
TestResultReporter.writeResults(results, '/output', 'json');
|
||||
|
||||
expect(mockFs.mkdirSync).toHaveBeenCalledWith('/output', { recursive: true });
|
||||
// Should write JSON + summary
|
||||
expect(mockFs.writeFileSync).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('should write both formats when specified', () => {
|
||||
mockFs.existsSync.mockReturnValue(true);
|
||||
mockFs.writeFileSync.mockReturnValue(undefined);
|
||||
|
||||
const results: TestResult[] = [
|
||||
{ runName: 'test', passed: 5, failed: 0, skipped: 0, duration: 1.0, failures: [] },
|
||||
];
|
||||
|
||||
TestResultReporter.writeResults(results, '/output', 'both');
|
||||
|
||||
// Should write JSON + JUnit XML + summary = 3 calls
|
||||
expect(mockFs.writeFileSync).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Test Workflow Service - buildUnityArgs
|
||||
// ============================================================================
|
||||
|
||||
describe('TestWorkflowService', () => {
|
||||
describe('buildUnityArgs', () => {
|
||||
const baseParams = {
|
||||
projectPath: '/project',
|
||||
targetPlatform: 'StandaloneLinux64',
|
||||
testResultPath: './test-results',
|
||||
testResultFormat: 'junit',
|
||||
} as any;
|
||||
|
||||
it('should build EditMode args', () => {
|
||||
const run: TestRunDefinition = { name: 'edit', editMode: true };
|
||||
|
||||
const args = TestWorkflowService.buildUnityArgs(run, baseParams);
|
||||
|
||||
expect(args).toContain('-batchmode');
|
||||
expect(args).toContain('-nographics');
|
||||
expect(args).toContain('-runTests');
|
||||
expect(args).toContain('-testPlatform EditMode');
|
||||
expect(args).toContain('-projectPath');
|
||||
});
|
||||
|
||||
it('should build PlayMode args', () => {
|
||||
const run: TestRunDefinition = { name: 'play', playMode: true };
|
||||
|
||||
const args = TestWorkflowService.buildUnityArgs(run, baseParams);
|
||||
|
||||
expect(args).toContain('-testPlatform PlayMode');
|
||||
});
|
||||
|
||||
it('should build built-client args', () => {
|
||||
const run: TestRunDefinition = {
|
||||
name: 'client',
|
||||
builtClient: true,
|
||||
builtClientPath: './Builds/Linux',
|
||||
};
|
||||
|
||||
const args = TestWorkflowService.buildUnityArgs(run, baseParams);
|
||||
|
||||
expect(args).toContain('-testPlatform StandalonePlayer');
|
||||
expect(args).toContain('-builtPlayerPath');
|
||||
expect(args).toContain('./Builds/Linux');
|
||||
});
|
||||
|
||||
it('should include taxonomy filter args', () => {
|
||||
const run: TestRunDefinition = {
|
||||
name: 'filtered',
|
||||
editMode: true,
|
||||
filters: { Maturity: 'Trusted', Scope: 'Unit,Integration' },
|
||||
};
|
||||
|
||||
const args = TestWorkflowService.buildUnityArgs(run, baseParams);
|
||||
|
||||
expect(args).toContain('--testFilter');
|
||||
expect(args).toContain('Maturity=Trusted');
|
||||
});
|
||||
|
||||
it('should include build target', () => {
|
||||
const run: TestRunDefinition = { name: 'test', editMode: true };
|
||||
|
||||
const args = TestWorkflowService.buildUnityArgs(run, baseParams);
|
||||
|
||||
expect(args).toContain('-buildTarget StandaloneLinux64');
|
||||
});
|
||||
});
|
||||
});
|
||||
5
src/model/orchestrator/services/test/README.md
Normal file
5
src/model/orchestrator/services/test/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Test Workflow Engine
|
||||
|
||||
Service for YAML-based test suite definitions, taxonomy filtering, and structured test results.
|
||||
|
||||
See GitHub Issue for full specification.
|
||||
@@ -30,7 +30,7 @@ describe('Orchestrator Caching', () => {
|
||||
targetPlatform: 'StandaloneLinux64',
|
||||
cacheKey: `test-case-${uuidv4()}`,
|
||||
containerHookFiles: `debug-cache`,
|
||||
orchestratorBranch: `orchestrator-develop`,
|
||||
orchestratorBranch: `main`,
|
||||
orchestratorDebug: true,
|
||||
};
|
||||
|
||||
|
||||
@@ -33,8 +33,7 @@ if [ -n "$(git ls-remote --heads "$REPO" "$BRANCH" 2>/dev/null)" ]; then
|
||||
git clone -q -b "$BRANCH" "$REPO" /builder
|
||||
else
|
||||
echo "Remote branch $BRANCH not found in $REPO; falling back to a known branch"
|
||||
git clone -q -b orchestrator-develop "$REPO" /builder \
|
||||
|| git clone -q -b main "$REPO" /builder \
|
||||
git clone -q -b main "$REPO" /builder \
|
||||
|| git clone -q "$REPO" /builder
|
||||
fi
|
||||
git clone -q -b ${Orchestrator.buildParameters.branch} ${OrchestratorFolders.targetBuildRepoUrl} /repo
|
||||
|
||||
@@ -99,8 +99,7 @@ if [ -n "$(git ls-remote --heads "$REPO" "$BRANCH" 2>/dev/null)" ]; then
|
||||
git clone -q -b "$BRANCH" "$REPO" "$DEST"
|
||||
else
|
||||
echo "Remote branch $BRANCH not found in $REPO; falling back to a known branch"
|
||||
git clone -q -b orchestrator-develop "$REPO" "$DEST" \
|
||||
|| git clone -q -b main "$REPO" "$DEST" \
|
||||
git clone -q -b main "$REPO" "$DEST" \
|
||||
|| git clone -q "$REPO" "$DEST"
|
||||
fi
|
||||
chmod +x ${builderPath}`;
|
||||
|
||||
82
yarn.lock
82
yarn.lock
@@ -3003,11 +3003,6 @@ ansi-regex@^5.0.1:
|
||||
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304"
|
||||
integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==
|
||||
|
||||
ansi-regex@^6.2.2:
|
||||
version "6.2.2"
|
||||
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-6.2.2.tgz#60216eea464d864597ce2832000738a0589650c1"
|
||||
integrity sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg==
|
||||
|
||||
ansi-styles@^3.2.1:
|
||||
version "3.2.1"
|
||||
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
|
||||
@@ -3027,11 +3022,6 @@ ansi-styles@^5.0.0:
|
||||
resolved "https://registry.npmjs.org/ansi-styles/-/ansi-styles-5.2.0.tgz"
|
||||
integrity sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==
|
||||
|
||||
ansi-styles@^6.2.1:
|
||||
version "6.2.3"
|
||||
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.2.3.tgz#c044d5dcc521a076413472597a1acb1f103c4041"
|
||||
integrity sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg==
|
||||
|
||||
anymatch@^3.0.3:
|
||||
version "3.1.3"
|
||||
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.3.tgz#790c58b19ba1720a84205b57c618d5ad8524973e"
|
||||
@@ -3589,14 +3579,14 @@ cliui@^7.0.2:
|
||||
strip-ansi "^6.0.0"
|
||||
wrap-ansi "^7.0.0"
|
||||
|
||||
cliui@^9.0.1:
|
||||
version "9.0.1"
|
||||
resolved "https://registry.yarnpkg.com/cliui/-/cliui-9.0.1.tgz#6f7890f386f6f1f79953adc1f78dec46fcc2d291"
|
||||
integrity sha512-k7ndgKhwoQveBL+/1tqGJYNz097I7WOvwbmmU2AR5+magtbjPWQTS1C5vzGkBC8Ym8UWRzfKUzUUqFLypY4Q+w==
|
||||
cliui@^8.0.1:
|
||||
version "8.0.1"
|
||||
resolved "https://registry.yarnpkg.com/cliui/-/cliui-8.0.1.tgz#0c04b075db02cbfe60dc8e6cf2f5486b1a3608aa"
|
||||
integrity sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==
|
||||
dependencies:
|
||||
string-width "^7.2.0"
|
||||
strip-ansi "^7.1.0"
|
||||
wrap-ansi "^9.0.0"
|
||||
string-width "^4.2.0"
|
||||
strip-ansi "^6.0.1"
|
||||
wrap-ansi "^7.0.0"
|
||||
|
||||
clone-response@^1.0.2:
|
||||
version "1.0.3"
|
||||
@@ -3991,11 +3981,6 @@ emittery@^0.8.1:
|
||||
resolved "https://registry.npmjs.org/emittery/-/emittery-0.8.1.tgz"
|
||||
integrity sha512-uDfvUjVrfGJJhymx/kz6prltenw1u7WrCg1oa94zYY8xxVpLLUu045LAT0dhDZdXG58/EpPL/5kA180fQ/qudg==
|
||||
|
||||
emoji-regex@^10.3.0:
|
||||
version "10.6.0"
|
||||
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-10.6.0.tgz#bf3d6e8f7f8fd22a65d9703475bc0147357a6b0d"
|
||||
integrity sha512-toUI84YS5YmxW219erniWD0CIVOo46xGKColeNQRgOzDorgBi1v4D71/OFzgD9GO2UGKIv1C3Sp8DAn0+j5w7A==
|
||||
|
||||
emoji-regex@^8.0.0:
|
||||
version "8.0.0"
|
||||
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
|
||||
@@ -4796,11 +4781,6 @@ get-caller-file@^2.0.5:
|
||||
resolved "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz"
|
||||
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
|
||||
|
||||
get-east-asian-width@^1.0.0:
|
||||
version "1.5.0"
|
||||
resolved "https://registry.yarnpkg.com/get-east-asian-width/-/get-east-asian-width-1.5.0.tgz#ce7008fe345edcf5497a6f557cfa54bc318a9ce7"
|
||||
integrity sha512-CQ+bEO+Tva/qlmw24dCejulK5pMzVnUOFOijVogd3KQs07HnRIgp8TGipvCCRT06xeYEbpbgwaCxglFyiuIcmA==
|
||||
|
||||
get-intrinsic@^1.0.2, get-intrinsic@^1.1.1, get-intrinsic@^1.1.3, get-intrinsic@^1.2.0, get-intrinsic@^1.2.1:
|
||||
version "1.2.1"
|
||||
resolved "https://registry.yarnpkg.com/get-intrinsic/-/get-intrinsic-1.2.1.tgz#d295644fed4505fc9cde952c37ee12b477a83d82"
|
||||
@@ -7548,15 +7528,6 @@ string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
|
||||
is-fullwidth-code-point "^3.0.0"
|
||||
strip-ansi "^6.0.1"
|
||||
|
||||
string-width@^7.0.0, string-width@^7.2.0:
|
||||
version "7.2.0"
|
||||
resolved "https://registry.yarnpkg.com/string-width/-/string-width-7.2.0.tgz#b5bb8e2165ce275d4d43476dd2700ad9091db6dc"
|
||||
integrity sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ==
|
||||
dependencies:
|
||||
emoji-regex "^10.3.0"
|
||||
get-east-asian-width "^1.0.0"
|
||||
strip-ansi "^7.1.0"
|
||||
|
||||
string.prototype.trim@^1.2.7:
|
||||
version "1.2.7"
|
||||
resolved "https://registry.yarnpkg.com/string.prototype.trim/-/string.prototype.trim-1.2.7.tgz#a68352740859f6893f14ce3ef1bb3037f7a90533"
|
||||
@@ -7605,13 +7576,6 @@ strip-ansi@^6.0.0, strip-ansi@^6.0.1:
|
||||
dependencies:
|
||||
ansi-regex "^5.0.1"
|
||||
|
||||
strip-ansi@^7.1.0:
|
||||
version "7.2.0"
|
||||
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.2.0.tgz#d22a269522836a627af8d04b5c3fd2c7fa3e32e3"
|
||||
integrity sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w==
|
||||
dependencies:
|
||||
ansi-regex "^6.2.2"
|
||||
|
||||
strip-bom@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"
|
||||
@@ -8353,15 +8317,6 @@ wrap-ansi@^7.0.0:
|
||||
string-width "^4.1.0"
|
||||
strip-ansi "^6.0.0"
|
||||
|
||||
wrap-ansi@^9.0.0:
|
||||
version "9.0.2"
|
||||
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-9.0.2.tgz#956832dea9494306e6d209eb871643bb873d7c98"
|
||||
integrity sha512-42AtmgqjV+X1VpdOfyTGOYRi0/zsoLqtXQckTmqTeybT+BDIbM/Guxo7x3pE2vtpr1ok6xRqM9OpBe+Jyoqyww==
|
||||
dependencies:
|
||||
ansi-styles "^6.2.1"
|
||||
string-width "^7.0.0"
|
||||
strip-ansi "^7.1.0"
|
||||
|
||||
wrappy@1:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
|
||||
@@ -8447,10 +8402,10 @@ yargs-parser@20.x, yargs-parser@^20.2.2:
|
||||
resolved "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz"
|
||||
integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==
|
||||
|
||||
yargs-parser@^22.0.0:
|
||||
version "22.0.0"
|
||||
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-22.0.0.tgz#87b82094051b0567717346ecd00fd14804b357c8"
|
||||
integrity sha512-rwu/ClNdSMpkSrUb+d6BRsSkLUq1fmfsY6TOpYzTwvwkg1/NRG85KBy3kq++A8LKQwX6lsu+aWad+2khvuXrqw==
|
||||
yargs-parser@^21.1.1:
|
||||
version "21.1.1"
|
||||
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.1.1.tgz#9096bceebf990d21bb31fa9516e0ede294a77d35"
|
||||
integrity sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==
|
||||
|
||||
yargs@^16.2.0:
|
||||
version "16.2.0"
|
||||
@@ -8465,17 +8420,18 @@ yargs@^16.2.0:
|
||||
y18n "^5.0.5"
|
||||
yargs-parser "^20.2.2"
|
||||
|
||||
yargs@^18.0.0:
|
||||
version "18.0.0"
|
||||
resolved "https://registry.yarnpkg.com/yargs/-/yargs-18.0.0.tgz#6c84259806273a746b09f579087b68a3c2d25bd1"
|
||||
integrity sha512-4UEqdc2RYGHZc7Doyqkrqiln3p9X2DZVxaGbwhn2pi7MrRagKaOcIKe8L3OxYcbhXLgLFUS3zAYuQjKBQgmuNg==
|
||||
yargs@^17.7.2:
|
||||
version "17.7.2"
|
||||
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.7.2.tgz#991df39aca675a192b816e1e0363f9d75d2aa269"
|
||||
integrity sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==
|
||||
dependencies:
|
||||
cliui "^9.0.1"
|
||||
cliui "^8.0.1"
|
||||
escalade "^3.1.1"
|
||||
get-caller-file "^2.0.5"
|
||||
string-width "^7.2.0"
|
||||
require-directory "^2.1.1"
|
||||
string-width "^4.2.3"
|
||||
y18n "^5.0.5"
|
||||
yargs-parser "^22.0.0"
|
||||
yargs-parser "^21.1.1"
|
||||
|
||||
yarn-audit-fix@^9.3.8:
|
||||
version "9.3.12"
|
||||
|
||||
Reference in New Issue
Block a user