#!/usr/bin/env bash # # Negative tests for the duplication ratchet. # # The ratchet's whole value is that it can go RED. If it silently passes when # duplication rises, it is worse than not having it — it certifies the thing it # was built to stop, which is exactly how `continue-on-error` let evig ship 25 # failing tests under a green tick for three weeks. # # The counting half needs the GitHub API. The DECIDING half — compare current # against baseline, and which way the ratchet turns — is pure text, so it is # tested here against fixture files with no network at all. A rule only # exercisable by a live API call is a rule nobody re-tests after editing it. set -uo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" TMP="$(mktemp -d)" trap 'rm -rf "$TMP"' EXIT PASS=0 FAIL=0 ok() { printf ' ✓ %s\n' "$1"; PASS=$((PASS + 1)); } no() { printf ' ✗ %s\n' "$1"; FAIL=$((FAIL + 1)); } # The ratchet comparison, extracted verbatim in behaviour from # shared-inventory.sh so the fixtures exercise the real decision. # (baseline, current) -> prints findings, returns 1 if duplication rose. ratchet() { local baseline="$1" current="$2" fail=0 while IFS=$'\t' read -r concern _ was_copies; do [ -n "$concern" ] || continue local now_line now_copies now_line=$(grep "^${concern} " "$current" 2>/dev/null) [ -n "$now_line" ] || continue now_copies=$(printf '%s' "$now_line" | cut -f3) [ "$now_copies" -gt "$was_copies" ] && { echo "ROSE $concern $was_copies->$now_copies"; fail=1; } done < "$baseline" while IFS=$'\t' read -r concern _ now_copies; do [ -n "$concern" ] || continue cut -f1 "$baseline" | grep -qx "$concern" || { echo "NEW $concern $now_copies"; fail=1; } done < "$current" return $fail } printf 'ratchet\n' printf 'rate-limit\t9\t14\nlogger\t6\t10\n' > "$TMP/base" # ── it must go red ─────────────────────────────────────────────────────────── printf 'rate-limit\t9\t15\nlogger\t6\t10\n' > "$TMP/up" if ratchet "$TMP/base" "$TMP/up" >/dev/null; then no 'a RISE in duplication is caught' else ok 'a RISE in duplication is caught' fi printf 'rate-limit\t9\t14\nlogger\t6\t10\nnew-thing\t3\t3\n' > "$TMP/new" if ratchet "$TMP/base" "$TMP/new" >/dev/null; then no 'a NEW unmeasured concern is caught' else ok 'a NEW unmeasured concern is caught' fi # ── and it must stay quiet when it should ──────────────────────────────────── if ratchet "$TMP/base" "$TMP/base" >/dev/null; then ok 'an unchanged fleet passes' else no 'an unchanged fleet passes' fi printf 'rate-limit\t9\t9\nlogger\t6\t10\n' > "$TMP/down" if ratchet "$TMP/base" "$TMP/down" >/dev/null; then ok 'a DECREASE passes (progress is not a failure)' else no 'a DECREASE passes (progress is not a failure)' fi # A concern dropped from the script must not be read as "rose to zero". printf 'rate-limit\t9\t14\n' > "$TMP/dropped" if ratchet "$TMP/base" "$TMP/dropped" >/dev/null; then ok 'a concern removed from the script does not fail the ratchet' else no 'a concern removed from the script does not fail the ratchet' fi # ── an incomplete read must never be certified ─────────────────────────────── # # A repo the API would not hand over contributes zero files, which looks exactly # like a repo that deleted its duplicate. On 2026-09-15 one sweep over 43 repos # silently dropped nine and three rows appeared to fall that nobody had earned. # So the guard is the same shape as the truncation guard: refuse to judge, and # refuse to WRITE a baseline from counts known to be low. printf 'incomplete reads\n' # (truncated, unreadable) -> 2 when either is non-empty, 0 otherwise. incomplete_guard() { [ -n "$1$2" ] && return 2 return 0 } if incomplete_guard "" ""; then ok 'a complete read is judged' else no 'a complete read is judged'; fi if incomplete_guard " evig" ""; then no 'a truncated tree refuses judgement' else ok 'a truncated tree refuses judgement'; fi if incomplete_guard "" " kivvi"; then no 'an unreadable repo refuses judgement' else ok 'an unreadable repo refuses judgement'; fi # The fixtures above prove the rule; these prove the script still applies it. if grep -q 'fetch_tree()' "$HERE/shared-inventory.sh"; then ok 'the tree fetch retries instead of skipping on first failure' else no 'the tree fetch retries instead of skipping on first failure' fi if grep -q 'refusing to write a baseline' "$HERE/shared-inventory.sh"; then ok '--update refuses an incomplete read' else no '--update refuses an incomplete read' fi if grep -q 'could not read:\$unreadable' "$HERE/shared-inventory.sh"; then ok '--check refuses an incomplete read' else no '--check refuses an incomplete read' fi # ── the patterns count what they say ───────────────────────────────────────── printf 'patterns\n' pattern() { grep "^$1|" "$HERE/shared-inventory.sh" | head -1 | cut -d'|' -f2-; } matches() { grep -qiE "$(pattern "$1")" <<<"$2"; } for path in src/lib/dates.ts lib/i18n/dates.ts src/services/cat/handlers/date-utils.ts; do if matches date-utils "$path"; then ok "date-utils counts $path"; else no "date-utils counts $path"; fi done # Files about UPDATES end in "dates.ts" too; they are not date helpers. for path in src/lib/domain/methods/ballot-updates.ts lib/auto-updates.ts src/components/x/useActivityUpdates.ts; do if matches date-utils "$path"; then no "date-utils ignores $path"; else ok "date-utils ignores $path"; fi done # Build output and the packages themselves are not copies. EXCLUDE=$(grep "^EXCLUDE_PATHS=" "$HERE/shared-inventory.sh" | cut -d"'" -f2) for path in dist/composer.js dist/react/Composer.js apps/web/dist/x.js node_modules/a/logger.ts; do if grep -qiE "$EXCLUDE" <<<"$path"; then ok "not counted: $path"; else no "not counted: $path"; fi done for path in src/composer.ts src/lib/distance.ts lib/distribution/slug.ts; do if grep -qiE "$EXCLUDE" <<<"$path"; then no "still counted: $path"; else ok "still counted: $path"; fi done KITS_FIXTURE='{"packages":[{"repo":"https://github.com/bitbaum/chatkit"},{"repo":"https://github.com/bitbaum/bip-kit"},{"repo":"https://github.com/someone-else/chatkit"},{}]}' kits=$(jq -r --arg o bitbaum '.packages[].repo // empty | select(startswith("https://github.com/" + $o + "/")) | sub("^https://github.com/[^/]+/"; "")' <<<"$KITS_FIXTURE") if [ "$kits" = "$(printf 'chatkit\nbip-kit')" ]; then ok 'fleet packages are read from the register (own org only)' else no "fleet packages are read from the register (own org only): got $(tr '\n' ' ' <<<"$kits")" fi if grep -q 'registers/packages.json' "$HERE/shared-inventory.sh" && grep -q 'kits_skipped="$kits_skipped' "$HERE/shared-inventory.sh"; then ok 'the inventory skips the fleet packages' else no 'the inventory skips the fleet packages' fi if grep -q '\.where"' "$HERE/shared-inventory.sh"; then ok 'a rise names the files behind it' else no 'a rise names the files behind it' fi # ── the baseline in the repo must match the script's concerns ──────────────── printf 'baseline integrity\n' BASE="$HERE/shared-inventory.baseline" if [ -f "$BASE" ]; then bad=$(awk -F'\t' 'NF!=3 {print NR": "$0}' "$BASE") if [ -z "$bad" ]; then ok 'baseline rows are namereposfiles'; else no "malformed baseline rows: $bad"; fi missing="" while IFS=$'\t' read -r concern _ _; do [ -n "$concern" ] || continue grep -q "^${concern}|" "$HERE/shared-inventory.sh" || missing="$missing $concern" done < "$BASE" if [ -z "$missing" ]; then ok 'every baseline concern still exists in the script' else no "baseline names concerns the script no longer measures:$missing" fi else no "no baseline committed at $BASE" fi # ── automatic triggers must not depend on a manual-only input ─────────────── printf 'workflow admission\n' workflow="$HERE/../../.github/workflows/shared-inventory.yml" automatic_gate="github.event_name != 'workflow_dispatch' || inputs.strict != false" gate_count=$(grep -Fc "$automatic_gate" "$workflow" || true) if [ "$gate_count" -eq 2 ]; then ok 'both ratchets admit schedule and release events without workflow_dispatch inputs' else no "both ratchets admit automatic events (found $gate_count guarded steps)" fi should_check() { local event="$1" strict="$2" [ "$event" != workflow_dispatch ] || [ "$strict" != false ] } if should_check repository_dispatch false && should_check schedule false && should_check workflow_dispatch true && ! should_check workflow_dispatch false; then ok 'automatic events run strict checks; only an explicit manual false opts out' else no 'strict-check admission truth table' fi printf '\n%d passed, %d failed\n' "$PASS" "$FAIL" [ "$FAIL" -eq 0 ]