#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

require_cmd() {
  local cmd="$1"
  command -v "$cmd" >/dev/null 2>&1 || {
    echo "missing dependency: $cmd" >&2
    exit 1
  }
}

require_cmd go
export GOMAXPROCS=12

cd "$ROOT_DIR"

echo "==> Wiki stale projection service regressions"
go test -p 12 -parallel 12 ./internal/service -count=1 -run 'Test(ListWikiTreeAtRef_UsesCatalogRowsWhenGitProjectionLagsLiveHead|ListWikiTreeAtRef_FallsBackToGitWithoutCatalogRows|WikiTreeUsesCatalogRowsWhenGitProjectionLagsCurrentHead|WikiTreeUsesCatalogRowsWhenV2IndexTracksLaggingGitProjection|ListWikiBacklinksDoesNotReturnGitOnlySourceWhenCatalogHasLiveRows)$'

echo "==> Wiki stale projection search regressions"
go test -p 12 -parallel 12 ./internal/service -count=1 -run 'TestWikiSearch(DoesNotReturnGitOnlyPageWhenCatalogHasLiveRows|FallsBackToGitWithoutCatalogRows|UsesCatalogBodyWhenSearchIndexLagsCatalogPage|UsesCatalogLexicalWhenIndexedRowsMissLivePage)$'

echo "==> Wiki publication recovery and read consistency regressions"
go test -p 12 -parallel 12 ./internal/service ./internal/rest -count=1 -run 'TestWiki(Publication|Reconcile)'

echo "==> Wiki REST transform link regressions"
go test -p 12 -parallel 12 ./internal/rest/transform -count=1 -run 'TestWikiTreeEntryCarriesRefInPageURLs$'

echo "Wiki regression gate passed."
