#!/usr/bin/env bats setup() { source "$(dirname "$BATS_TEST_DIRNAME")/files/retention-lib.sh" declare -gA REPO_BRANCHES_CACHE declare -gA REPO_STATUS declare -gA REASON_MAP MAXAGE_DELETED=0 KEEPMIN_DELETED=0 CONFIG=$(mktemp) } teardown() { rm -f "$CONFIG" } write_config() { cat > "$CONFIG" } # --------------------------------------------------------------------------- # read_rule # --------------------------------------------------------------------------- @test "read_rule returns default when branch has no override" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":90,"keepMin":5}}} EOF result=$(read_rule "$CONFIG" "nonexistent" "maxAgeDays" 90) [ "$result" = "90" ] } @test "read_rule returns branch-specific value" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":90,"keepMin":5},"main":{"maxAgeDays":365,"keepMin":20}}} EOF result=$(read_rule "$CONFIG" "main" "keepMin" 5) [ "$result" = "20" ] } @test "read_rule returns default for undefined key even if branch exists" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":90,"keepMin":5},"main":{"maxAgeDays":365}}} EOF result=$(read_rule "$CONFIG" "main" "keepMin" 5) [ "$result" = "5" ] } # --------------------------------------------------------------------------- # parse_path # --------------------------------------------------------------------------- @test "parse_path extracts owner and repo" { parse_path "my-owner/my-repo/reports/abc123/go-test-unit" [ "$OWNER" = "my-owner" ] [ "$REPO" = "my-repo" ] } @test "parse_path handles owner with hyphens" { parse_path "niko/agent-platform/reports/abc1234/go-test-bdd" [ "$OWNER" = "niko" ] [ "$REPO" = "agent-platform" ] } # --------------------------------------------------------------------------- # apply_retention — keepMin per commit # --------------------------------------------------------------------------- @test "keepMin: 6 commits × 4 types, keepMin=10 → all kept (6 commits < 10)" { # With old per-file counting, 24 files > 10 keepMin would delete 14. # With per-commit counting, 6 commits < 10 keepMin keeps everything. write_config <<'EOF' {"branches":{"default":{"maxAgeDays":365,"keepMin":10}}} EOF KEEP=() local -a commits=(c1 c2 c3 c4 c5 c6) local -a ages=(100 80 60 40 20 5) local -a types=(go-test-bdd go-test-unit helm-lint helm-kubeconform) for i in "${!commits[@]}"; do for t in "${types[@]}"; do KEEP+=("niko/agent-platform/reports/${commits[$i]}/$t|niko|agent-platform|main|${ages[$i]}") done done TO_DELETE=() apply_retention "$CONFIG" [ "${#TO_DELETE[@]}" -eq 0 ] } @test "keepMin: 8 commits × 1 type, keepMin=5 → deletes 3 oldest" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":365,"keepMin":5}}} EOF KEEP=() local -a ages=(80 70 60 50 40 30 20 10) for i in "${!ages[@]}"; do KEEP+=("niko/r/reports/c$((i+1))/test|niko|r|main|${ages[$i]}") done TO_DELETE=() apply_retention "$CONFIG" # 5 newest (c8-c4) kept, 3 oldest (c3,c2,c1) deleted [ "${#TO_DELETE[@]}" -eq 3 ] [[ "${TO_DELETE[0]}" == "niko/r/reports/c3" ]] [[ "${TO_DELETE[1]}" == "niko/r/reports/c2" ]] [[ "${TO_DELETE[2]}" == "niko/r/reports/c1" ]] } @test "keepMin: 12 commits × 1 type, keepMin=5 → keeps 5 newest, deletes 7 oldest" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":90,"keepMin":5}}} EOF KEEP=() for i in $(seq 1 12); do KEEP+=("niko/r/reports/c${i}/test|niko|r|feature/foo|$(( 13 - i ))") done TO_DELETE=() apply_retention "$CONFIG" # 7 oldest commits deleted (12 - 5 = 7) [ "${#TO_DELETE[@]}" -eq 7 ] # Oldest 7 should be c1..c7 (highest days = oldest = processed last after sort) # Sort is ascending by days, so processed as c12(1d), c11(2d), ..., c1(12d) # keepMin=5: c12-c8 kept, c7-c1 deleted [[ "${TO_DELETE[0]}" == "niko/r/reports/c7" ]] [[ "${TO_DELETE[6]}" == "niko/r/reports/c1" ]] } @test "keepMin: 2 commits × 3 types, keepMin=5 → all kept (2 < 5)" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":90,"keepMin":5}}} EOF KEEP=() KEEP+=("niko/r/reports/c1/test-a|niko|r|main|30") KEEP+=("niko/r/reports/c1/test-b|niko|r|main|30") KEEP+=("niko/r/reports/c1/test-c|niko|r|main|30") KEEP+=("niko/r/reports/c2/test-a|niko|r|main|10") KEEP+=("niko/r/reports/c2/test-b|niko|r|main|10") KEEP+=("niko/r/reports/c2/test-c|niko|r|main|10") TO_DELETE=() apply_retention "$CONFIG" [ "${#TO_DELETE[@]}" -eq 0 ] } @test "keepMin: 6 commits × 2 types, keepMin=3 → keeps 3 newest, deletes 3 oldest" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":365,"keepMin":3}}} EOF KEEP=() local -a commits=(c1 c2 c3 c4 c5 c6) local -a ages=(60 50 40 30 20 10) local -a types=(jest pytest) for i in "${!commits[@]}"; do for t in "${types[@]}"; do KEEP+=("niko/r/reports/${commits[$i]}/$t|niko|r|feature/x|${ages[$i]}") done done TO_DELETE=() apply_retention "$CONFIG" # Sort by days ascending: c6(10d), c5(20d), c4(30d), c3(40d), c2(50d), c1(60d) # keepMin=3: c6,c5,c4 kept; c3,c2,c1 deleted [ "${#TO_DELETE[@]}" -eq 3 ] [[ "${TO_DELETE[0]}" == "niko/r/reports/c3" ]] [[ "${TO_DELETE[1]}" == "niko/r/reports/c2" ]] [[ "${TO_DELETE[2]}" == "niko/r/reports/c1" ]] } # --------------------------------------------------------------------------- # apply_retention — maxAge # --------------------------------------------------------------------------- @test "maxAge: report exceeding maxAge is deleted" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":90,"keepMin":5}}} EOF KEEP=( "niko/r/reports/c1/test|niko|r|main|100" "niko/r/reports/c2/test|niko|r|main|50" ) TO_DELETE=() apply_retention "$CONFIG" # c1 (100d) > 90, deleted; c2 (50d) < 90, kept [ "${#TO_DELETE[@]}" -eq 1 ] [[ "${TO_DELETE[0]}" == "niko/r/reports/c1/test" ]] } @test "maxAge deletes report-level dir, not commit-level" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":30,"keepMin":5}}} EOF KEEP=( "niko/r/reports/c1/test-a|niko|r|main|100" "niko/r/reports/c1/test-b|niko|r|main|20" "niko/r/reports/c2/test-a|niko|r|main|10" ) TO_DELETE=() apply_retention "$CONFIG" # Only test-a for c1 is old; test-b for c1 is young, c2 is young [ "${#TO_DELETE[@]}" -eq 1 ] [[ "${TO_DELETE[0]}" == "niko/r/reports/c1/test-a" ]] } # --------------------------------------------------------------------------- # apply_retention — maxAge + keepMin interaction # --------------------------------------------------------------------------- @test "maxAge takes precedence over keepMin — aged report deleted, not counted in keepMin" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":30,"keepMin":2}}} EOF # 3 commits, 1 type each. c1 is old (100d), c2 and c3 are young. # With keepMin=2: c1 should be deleted by maxAge, c2 and c3 kept. # Without the continue after maxAge check, c1 would consume a keepMin slot. KEEP=( "niko/r/reports/c1/test|niko|r|main|100" "niko/r/reports/c2/test|niko|r|main|10" "niko/r/reports/c3/test|niko|r|main|5" ) TO_DELETE=() apply_retention "$CONFIG" # c1 deleted by maxAge, c2 and c3 within keepMin=2 [ "${#TO_DELETE[@]}" -eq 1 ] } # --------------------------------------------------------------------------- # apply_retention — sorting (newest first) # --------------------------------------------------------------------------- @test "sort order: newest commits processed first within same branch" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":365,"keepMin":2}}} EOF KEEP=( "niko/r/reports/c1/test|niko|r|main|100" "niko/r/reports/c2/test|niko|r|main|50" "niko/r/reports/c3/test|niko|r|main|10" ) TO_DELETE=() apply_retention "$CONFIG" # Sort by days ascending: c3(10d) 1st, c2(50d) 2nd, c1(100d) 3rd # keepMin=2: c3 and c2 kept, c1 deleted [ "${#TO_DELETE[@]}" -eq 1 ] [[ "${TO_DELETE[0]}" == "niko/r/reports/c1" ]] } @test "sort order: branches sorted alphabetically" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":365,"keepMin":1}}} EOF KEEP=( "niko/r/reports/c1/test|niko|r|z-branch|50" "niko/r/reports/c2/test|niko|r|a-branch|60" "niko/r/reports/c3/test|niko|r|m-branch|10" ) TO_DELETE=() apply_retention "$CONFIG" # Alphabetical: a-branch, m-branch, z-branch # Each has 1 commit, keepMin=1 → nothing deleted [ "${#TO_DELETE[@]}" -eq 0 ] } @test "multi-branch: each branch has own keepMin counter" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":90,"keepMin":2}}} EOF KEEP=( "niko/r/reports/c1/test|niko|r|branch-a|30" "niko/r/reports/c2/test|niko|r|branch-a|20" "niko/r/reports/c3/test|niko|r|branch-a|10" "niko/r/reports/c4/test|niko|r|branch-b|60" "niko/r/reports/c5/test|niko|r|branch-b|50" "niko/r/reports/c6/test|niko|r|branch-b|40" "niko/r/reports/c7/test|niko|r|branch-b|30" ) TO_DELETE=() apply_retention "$CONFIG" # branch-a: 3 reports → keep 2 newest (c2,c3), delete 1 oldest (c1) # branch-b: 4 reports → keep 2 newest (c6,c7), delete 2 oldest (c4,c5) # Actually: Sort is by branch, then by days ascending # branch-a processed first: c3(10d) 1st, c2(20d) 2nd (keep), c1(30d) 3rd (delete) # branch-b processed next: c7(30d) 1st, c6(40d) 2nd (keep), c5(50d) 3rd (delete), c4(60d) 4th (delete) [ "${#TO_DELETE[@]}" -eq 3 ] [[ "${TO_DELETE[0]}" == "niko/r/reports/c1" ]] [[ "${TO_DELETE[1]}" == "niko/r/reports/c5" ]] [[ "${TO_DELETE[2]}" == "niko/r/reports/c4" ]] } # --------------------------------------------------------------------------- # apply_retention — empty / edge cases # --------------------------------------------------------------------------- @test "empty KEEP array → nothing deleted" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":90,"keepMin":5}}} EOF KEEP=() TO_DELETE=() apply_retention "$CONFIG" [ "${#TO_DELETE[@]}" -eq 0 ] } @test "TO_DELETE preserves Phase 2 entries after apply_retention (no new deletions)" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":365,"keepMin":10}}} EOF KEEP=( "niko/r/reports/c1/test|niko|r|main|10" "niko/r/reports/c2/test|niko|r|main|5" ) TO_DELETE=( "niko/r/reports/abc/branch-gone" "niko/r/reports/def/repo-gone" ) apply_retention "$CONFIG" [ "${#TO_DELETE[@]}" -eq 2 ] [[ "${TO_DELETE[0]}" == "niko/r/reports/abc/branch-gone" ]] [[ "${TO_DELETE[1]}" == "niko/r/reports/def/repo-gone" ]] } @test "TO_DELETE preserves Phase 2 entries AND adds retention deletions" { write_config <<'EOF' {"branches":{"default":{"maxAgeDays":365,"keepMin":3}}} EOF KEEP=( "niko/r/reports/c1/test|niko|r|main|40" "niko/r/reports/c2/test|niko|r|main|30" "niko/r/reports/c3/test|niko|r|main|20" "niko/r/reports/c4/test|niko|r|main|10" ) TO_DELETE=( "niko/r/reports/abc/branch-gone" ) apply_retention "$CONFIG" # 1 pre-existing + 1 commit deleted (c1, oldest of 4, keepMin=3) [ "${#TO_DELETE[@]}" -eq 2 ] [[ "${TO_DELETE[0]}" == "niko/r/reports/abc/branch-gone" ]] } # --------------------------------------------------------------------------- # branch_exists — mocking REPO_STATUS / REPO_BRANCHES_CACHE # --------------------------------------------------------------------------- @test "branch_exists: branch in list → return 0" { GITEA_API_URL="https://gitea.example.com" GITEA_TOKEN="test-token" REPO_BRANCHES_CACHE["owner/repo"]=$'main\nfeature/x' REPO_STATUS["owner/repo"]="ok" run branch_exists "owner" "repo" "main" [ "$status" -eq 0 ] } @test "branch_exists: branch not in list → return 1" { GITEA_API_URL="https://gitea.example.com" GITEA_TOKEN="test-token" REPO_BRANCHES_CACHE["owner/repo"]=$'main\nfeature/x' REPO_STATUS["owner/repo"]="ok" run branch_exists "owner" "repo" "nonexistent" [ "$status" -eq 1 ] } @test "branch_exists: cert error → exit 1 with [FATAL]" { GITEA_API_URL="https://gitea.example.com" GITEA_TOKEN="test" REPO_STATUS["owner/repo"]="cert_error" run branch_exists "owner" "repo" "any-branch" [ "$status" -eq 1 ] [[ "$output" == *"[FATAL]"* ]] } @test "branch_exists: repo deleted → return 1" { GITEA_API_URL="https://gitea.example.com" GITEA_TOKEN="test-token" REPO_BRANCHES_CACHE["owner/repo"]="__REPO_DELETED__" REPO_STATUS["owner/repo"]="deleted" run branch_exists "owner" "repo" "any-branch" [ "$status" -eq 1 ] } @test "branch_exists: network error → return 0 (fail-safe keep)" { GITEA_API_URL="https://gitea.example.com" GITEA_TOKEN="test-token" REPO_BRANCHES_CACHE["owner/repo"]="__REPO_ERROR__" REPO_STATUS["owner/repo"]="error" run branch_exists "owner" "repo" "any-branch" [ "$status" -eq 0 ] } @test "branch_exists: empty GITEA_API_URL → return 0 (skip)" { GITEA_API_URL="" GITEA_TOKEN="test-token" run branch_exists "owner" "repo" "any-branch" [ "$status" -eq 0 ] } @test "branch_exists: empty GITEA_TOKEN → return 0 (skip)" { GITEA_API_URL="https://gitea.example.com" GITEA_TOKEN="" run branch_exists "owner" "repo" "any-branch" [ "$status" -eq 0 ] } # --------------------------------------------------------------------------- # repo_branches — git ls-remote error detection patterns # --------------------------------------------------------------------------- @test "error detection: 'command not found' does NOT trigger repo deleted" { # This must NOT match — "bash: git: command not found" is NOT a repo deletion local msg="bash: git: command not found" run grep -qiE "fatal:.*(not found|repository.*not|could not read)" <<< "$msg" [ "$status" -eq 1 ] } @test "error detection: 'fatal: repo not found' triggers repo deleted" { # This MUST match — genuine git error for deleted/missing repo local msg="fatal: repository 'https://gitea.app/owner/repo.git' not found" run grep -qiE "fatal:.*(not found|repository.*not|could not read)" <<< "$msg" [ "$status" -eq 0 ] } @test "error detection: 'could not read from remote' triggers repo deleted" { local msg="fatal: could not read from remote repository" run grep -qiE "fatal:.*(not found|repository.*not|could not read)" <<< "$msg" [ "$status" -eq 0 ] } # --------------------------------------------------------------------------- # git ls-remote integration (real git, temp repo) # --------------------------------------------------------------------------- @test "git ls-remote parsing: lists branches correctly" { local tmpdir=$(mktemp -d) git -C "$tmpdir" init -b main source >/dev/null 2>&1 git -C "$tmpdir/source" config user.email "test@test" git -C "$tmpdir/source" config user.name "test" git -C "$tmpdir/source" commit --allow-empty -m "init" >/dev/null 2>&1 git -C "$tmpdir/source" branch feature/x >/dev/null 2>&1 git clone --bare "$tmpdir/source" "$tmpdir/repo.git" >/dev/null 2>&1 local url="file://$tmpdir/repo.git" local output output=$(git ls-remote --heads "$url" 2>&1) local branches branches=$(echo "$output" | sed -n 's|.*refs/heads/||p') echo "$branches" | grep -qxF "main" [ "$?" -eq 0 ] echo "$branches" | grep -qxF "feature/x" [ "$?" -eq 0 ] ! echo "$branches" | grep -qxF "nonexistent" rm -rf "$tmpdir" } # --------------------------------------------------------------------------- # repo_branches — retry + error output (using git mock) # --------------------------------------------------------------------------- @test "repo_branches: success returns branches immediately" { local mockdir=$(mktemp -d) cat > "$mockdir/git" << 'SCRIPT' #!/usr/bin/env bash echo "abc123 refs/heads/main" echo "def456 refs/heads/feature/x" SCRIPT chmod +x "$mockdir/git" local save_PATH="$PATH" export PATH="$mockdir:$PATH" GITEA_API_URL="https://gitea.example.com" GITEA_TOKEN="test" REPO_BRANCHES_CACHE=() REPO_STATUS=() run repo_branches "owner" "repo" [ "$status" -eq 0 ] [[ "$output" == *"main"* ]] [[ "$output" == *"feature/x"* ]] export PATH="$save_PATH" rm -rf "$mockdir" } @test "repo_branches: retries 3 times on transient error" { local mockdir=$(mktemp -d) cat > "$mockdir/git" << 'SCRIPT' #!/usr/bin/env bash echo "call" >> "$MOCKDIR/count" echo "fatal: unable to access 'https://...'" >&2 exit 1 SCRIPT chmod +x "$mockdir/git" # Inject mockdir path into mock script via env var sed -i '' "s|\$MOCKDIR|$mockdir|g" "$mockdir/git" local save_PATH="$PATH" export PATH="$mockdir:$PATH" GITEA_API_URL="https://gitea.example.com" GITEA_TOKEN="test" REPO_BRANCHES_CACHE=() REPO_STATUS=() local start=$SECONDS run repo_branches "owner" "repo" [ "$status" -eq 3 ] [[ "$output" == *"[WARN] git-pages.retention"* ]] [[ "$output" == *"keeping all reports"* ]] [[ "$output" == *"git output:"* ]] [[ "$output" == *"unable to access"* ]] [ $(cat "$mockdir/count" | wc -l) -eq 3 ] [ $(( SECONDS - start )) -ge 18 ] export PATH="$save_PATH" rm -rf "$mockdir" } @test "repo_branches: certificate error → [ERROR] + return 1" { local mockdir=$(mktemp -d) cat > "$mockdir/git" << 'SCRIPT' #!/usr/bin/env bash echo "call" >> "$MOCKDIR/count" echo "fatal: unable to access 'https://gitea.app/owner/repo.git/': server certificate verification failed. CAfile: none CRLfile: none" >&2 exit 1 SCRIPT chmod +x "$mockdir/git" sed -i '' "s|\$MOCKDIR|$mockdir|g" "$mockdir/git" local save_PATH="$PATH" export PATH="$mockdir:$PATH" GITEA_API_URL="https://gitea.example.com" GITEA_TOKEN="test" REPO_BRANCHES_CACHE=() REPO_STATUS=() run repo_branches "owner" "repo" [ "$status" -eq 2 ] [[ "$output" == *"[ERROR]"* ]] [[ "$output" == *"certificate verification"* ]] [[ "$output" == *"git output:"* ]] [[ "$output" == *"unable to access"* ]] export PATH="$save_PATH" rm -rf "$mockdir" } @test "repo_branches: repo not found returns immediately (no retry)" { local mockdir=$(mktemp -d) cat > "$mockdir/git" << 'SCRIPT' #!/usr/bin/env bash echo "call" >> "$MOCKDIR/count" echo "fatal: repository 'https://gitea.app/owner/repo.git' not found" >&2 exit 1 SCRIPT chmod +x "$mockdir/git" sed -i '' "s|\$MOCKDIR|$mockdir|g" "$mockdir/git" local save_PATH="$PATH" export PATH="$mockdir:$PATH" GITEA_API_URL="https://gitea.example.com" GITEA_TOKEN="test" REPO_BRANCHES_CACHE=() REPO_STATUS=() run repo_branches "owner" "repo" [ "$status" -eq 1 ] [[ "$output" == *"REPO DELETED"* ]] [[ "$output" != *"[WARN]"* ]] [ $(cat "$mockdir/count" | wc -l) -eq 1 ] export PATH="$save_PATH" rm -rf "$mockdir" }