#!/usr/bin/env bash # Shell smoke tests for skills/jira-workflow/scripts/create-mr.sh. # Network-free: uses MR_DRY_RUN=1 and a throwaway git repo. # Run via `npm test` (after guardrails.test.ts) or directly: bash test/shell.test.sh set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" SCRIPT="$ROOT/skills/jira-workflow/scripts/create-mr.sh" FAILS=0 check() { # local name="$1" expected="$2" actual="$3" if grep -qF -- "$expected" <<<"$actual"; then echo "PASS $name" else echo "FAIL $name (expected substring: $expected)" echo "--- actual ---" echo "$actual" FAILS=$((FAILS + 1)) fi } # Fresh throwaway repo with an empty commit. DIR="$(mktemp -d)" trap 'rm -rf "$DIR"' EXIT cd "$DIR" git init -q git config user.email test@example.com git config user.name Test git remote add origin git@gitlab.tech.mvideo.ru:ai/foo.git git commit --allow-empty -q -m "chore: init" git checkout -q -b feature/PROJ-1-smoke # 1. scp-like origin → API URL with encoded namespace. OUT="$(GITLAB_TOKEN=dummy MR_DRY_RUN=1 bash "$SCRIPT" "Test MR" 2>&1)" check "create-mr: scp-like origin parse" "POST https://gitlab.tech.mvideo.ru/api/v4/projects/ai%2Ffoo/merge_requests" "$OUT" check "create-mr: dry-run shows would-push" "would push: git push -u origin feature/PROJ-1-smoke" "$OUT" # 2. https-like origin → same API shape, different host. git remote set-url origin "https://git.codelab.vc/ai/bar.git" OUT="$(GITLAB_TOKEN=dummy MR_DRY_RUN=1 bash "$SCRIPT" "Test MR" 2>&1)" check "create-mr: https origin parse" "POST https://git.codelab.vc/api/v4/projects/ai%2Fbar/merge_requests" "$OUT" # 3. GITLAB_HOST overrides the derived host. OUT="$(GITLAB_TOKEN=dummy GITLAB_HOST=gitlab.example.com MR_DRY_RUN=1 bash "$SCRIPT" "Test MR" 2>&1)" check "create-mr: GITLAB_HOST override" "POST https://gitlab.example.com/api/v4/projects/ai%2Fbar/merge_requests" "$OUT" # 4. Refuses to create MR from a protected/release source branch. git checkout -q -b release-42 OUT="$(GITLAB_TOKEN=dummy MR_DRY_RUN=1 bash "$SCRIPT" "Test MR" 2>&1 || true)" check "create-mr: refuses release-* source" "Refusing: source branch" "$OUT" echo "" if [ "$FAILS" -eq 0 ]; then echo "shell tests: ALL PASS" exit 0 else echo "shell tests: $FAILS FAILED" exit 1 fi