const { execSync } = require('child_process'); const { When, Then } = require('@cucumber/cucumber'); const path = require('path'); const PROJECT_ROOT = path.resolve(__dirname, '..', '..', '..'); const MOCK_SCRIPT = path.join(PROJECT_ROOT, 'tests', 'helpers', 'mock-api.sh'); const GITOPS_SCRIPT = path.join(PROJECT_ROOT, 'scripts', 'gitops-update.sh'); function bash(cmd) { try { const out = execSync(`bash -c '${cmd}'`, { cwd: PROJECT_ROOT, encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'], }); return { status: 0, stdout: out }; } catch (e) { return { status: e.status, stdout: e.stdout || '', stderr: e.stderr || '' }; } } function getFirstBody() { return bash(`source "${MOCK_SCRIPT}" && _get_request_file && mock_get_first_request_body`).stdout.trim(); } function getFirstPath() { return bash(`source "${MOCK_SCRIPT}" && _get_request_file && mock_get_first_request_path`).stdout.trim(); } function getLastBody() { return bash(`source "${MOCK_SCRIPT}" && _get_request_file && mock_get_request_body`).stdout.trim(); } function getLastPath() { return bash(`source "${MOCK_SCRIPT}" && _get_request_file && mock_get_request_path`).stdout.trim(); } When('a build completes successfully and dispatches a GitOps update', function () { const env = [ 'INPUT_FILE=dev/Chart.yaml', 'YQ_TPL=\'(.version) = "{{VERSION}}"\'', 'VERSION=0.2.3', 'SOURCE_REPO=niko/app', 'SOURCE_COMMIT=abc123def456', 'GITOPS_REPO=niko/app-gitops', 'GITEA_API_URL=http://localhost:18080', 'GITEA_TOKEN=test-token', ].join(' '); const r = bash(`${env} bash "${GITOPS_SCRIPT}"`); if (r.status !== 0) throw new Error(`Expected exit 0, got ${r.status}: ${r.stderr}`); }); Then('the GitOps repo has a new commit with the updated version', function () { const out = bash(`git -C /tmp log --oneline -1 2>/dev/null || echo "no-git-log"`); }); Then('the code repo shows a gitops status link to the GitOps commit', function () { const body = getFirstBody(); if (!body.includes('"state":"success"')) throw new Error('Expected success status'); if (!body.includes('"context":"gitops/niko/app"')) throw new Error('Expected gitops context'); const pathStr = getFirstPath(); if (!pathStr.includes('/repos/niko/app/statuses/')) throw new Error('Expected source repo status path'); }); Then('the GitOps repo shows a source status link to the code commit', function () { const body = getLastBody(); if (!body.includes('"state":"success"')) throw new Error('Expected success status'); if (!body.includes('"context":"source/niko/app"')) throw new Error('Expected source context'); const pathStr = getLastPath(); if (!pathStr.includes('/repos/niko/app-gitops/statuses/')) throw new Error('Expected gitops repo status path'); });