mirror of
https://github.com/game-ci/unity-builder.git
synced 2026-06-01 06:16:14 -07:00
Compare commits
4 Commits
feature/te
...
feature/in
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d503b0b61a | ||
|
|
07eec62d28 | ||
|
|
4870fb5a5c | ||
|
|
3033ee0067 |
@@ -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",
|
||||
|
||||
25
action.yml
25
action.yml
@@ -279,23 +279,24 @@ 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'
|
||||
syncStrategy:
|
||||
description: 'Workspace sync strategy: full, git-delta, direct-input, storage-pull'
|
||||
required: false
|
||||
testSuiteEvent:
|
||||
description: 'CI event name for suite selection (pr, push, release)'
|
||||
default: 'full'
|
||||
syncInputRef:
|
||||
description: 'URI for direct-input or storage-pull content (storage://remote/path or file path)'
|
||||
required: false
|
||||
testTaxonomyPath:
|
||||
description: 'Path to custom taxonomy definition YAML'
|
||||
syncStorageRemote:
|
||||
description: 'rclone remote name for storage-backed inputs (defaults to rcloneRemote)'
|
||||
required: false
|
||||
testResultFormat:
|
||||
description: 'Test result output format: junit, json, or both'
|
||||
syncRevertAfter:
|
||||
description: 'Revert overlaid changes after job completion'
|
||||
required: false
|
||||
default: 'junit'
|
||||
testResultPath:
|
||||
description: 'Directory for structured test result output'
|
||||
default: 'true'
|
||||
syncStatePath:
|
||||
description: 'Path to sync state file for delta tracking'
|
||||
required: false
|
||||
default: './test-results'
|
||||
default: '.game-ci/sync-state.json'
|
||||
|
||||
outputs:
|
||||
volume:
|
||||
|
||||
1468
dist/index.js
generated
vendored
1468
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
92
src/index.ts
92
src/index.ts
@@ -3,7 +3,8 @@ import { Action, BuildParameters, Cache, Orchestrator, Docker, ImageTag, Output
|
||||
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 { IncrementalSyncService } from './model/orchestrator/services/sync';
|
||||
import { SyncStrategy } from './model/orchestrator/services/sync/sync-state';
|
||||
|
||||
async function runMain() {
|
||||
try {
|
||||
@@ -18,29 +19,20 @@ 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') {
|
||||
core.info('Building locally');
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
await PlatformSetup.setup(buildParameters, actionFolder);
|
||||
exitCode =
|
||||
process.platform === 'darwin'
|
||||
@@ -50,6 +42,16 @@ async function runMain() {
|
||||
actionFolder,
|
||||
...buildParameters,
|
||||
});
|
||||
|
||||
// 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;
|
||||
@@ -68,4 +70,58 @@ async function runMain() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,12 +106,11 @@ class BuildParameters {
|
||||
public cacheUnityInstallationOnMac!: boolean;
|
||||
public unityHubVersionOnMac!: string;
|
||||
public dockerWorkspacePath!: string;
|
||||
|
||||
public testSuitePath!: string;
|
||||
public testSuiteEvent!: string;
|
||||
public testTaxonomyPath!: string;
|
||||
public testResultFormat!: string;
|
||||
public testResultPath!: 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 !== ``;
|
||||
@@ -248,11 +247,11 @@ 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,
|
||||
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();
|
||||
}
|
||||
@@ -282,26 +304,6 @@ class Input {
|
||||
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';
|
||||
}
|
||||
|
||||
public static ToEnvVarFormat(input: string) {
|
||||
if (input.toUpperCase() === input) {
|
||||
return input;
|
||||
|
||||
@@ -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)}`,
|
||||
|
||||
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';
|
||||
@@ -1,12 +0,0 @@
|
||||
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';
|
||||
@@ -1,198 +0,0 @@
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,316 +0,0 @@
|
||||
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`;
|
||||
}
|
||||
}
|
||||
@@ -1,263 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,246 +0,0 @@
|
||||
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';
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
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;
|
||||
}
|
||||
@@ -1,562 +0,0 @@
|
||||
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');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +0,0 @@
|
||||
# Test Workflow Engine
|
||||
|
||||
Service for YAML-based test suite definitions, taxonomy filtering, and structured test results.
|
||||
|
||||
See GitHub Issue for full specification.
|
||||
Reference in New Issue
Block a user