#!/usr/bin/env sh # check-wiki-rules — 驗證 wiki repo 解析與 hash-id 規則。 # 涵蓋 TYPES 列的每一種頁面類型,每種三項:專用變數優先、退回共用變數、不得跨類型代用。 # 全部通過印 OK 並 exit 0;任一項不符印出差異並 exit 1。 set -eu dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) gitea="$dir/gitea.sh" hash_id="$dir/hash-id" TYPES='QUESTION PLAN ANALYZE DELIVER MAINTAIN REPO LOG LEARN ERROR CHECK REPORT SKILLSET' fail() { printf '%s\n' "$1" >&2 exit 1 } expect_eq() { got=$1 want=$2 label=$3 [ "$got" = "$want" ] || fail "$label: want=$want got=$got" } check_repo() { # type label want [VAR=值...] type=$1 label=$2 want=$3 shift 3 got=$(env -i PATH="${PATH:-/usr/bin:/bin}" "$@" "$gitea" wiki-repo "$type") expect_eq "$got" "$want" "$label" } check_no_repo() { # type label [VAR=值...]:期望 exit 3 且不印出任何 {owner}/{repo} type=$1 label=$2 shift 2 if got=$(env -i PATH="${PATH:-/usr/bin:/bin}" "$@" "$gitea" wiki-repo "$type" 2>/dev/null); then code=0 else code=$? fi [ "$code" -eq 3 ] || fail "$label: want exit=3 got exit=$code output=$got" [ -z "$got" ] || fail "$label: want no output got=$got" } check_hash() { input=$1 want=$2 got=$("$hash_id" "$input") expect_eq "$got" "$want" "hash-id $input" } # 其餘每一個類型的誘餌值。跨類型代用一旦發生,回傳的就會是 decoy/{別的類型}。 decoys() { # $1=要排除的類型 for t in $TYPES; do [ "$t" = "$1" ] || printf 'JSC_WIKI_REPO_%s=decoy/%s ' "$t" "$t" done } for ty in $TYPES; do # 1. 自己的變數贏過共用變數,也贏過其他類型的誘餌。 check_repo "$ty" "$ty specific wins" "own/$ty" \ $(decoys "$ty") "JSC_WIKI_REPO_$ty=own/$ty" JSC_WIKI_REPO='shared/wiki' # 2. 自己的變數未設定時,只退回共用變數。 check_repo "$ty" "$ty falls back to shared only" 'shared/wiki' \ $(decoys "$ty") JSC_WIKI_REPO='shared/wiki' # 3. 自己的變數與共用變數都沒有時,exit 3,不借用別的類型。 check_no_repo "$ty" "$ty never borrows another type" $(decoys "$ty") done # 未知類型 exit 2,與「設定不足」的 exit 3 分開。 if got=$(env -i PATH="${PATH:-/usr/bin:/bin}" JSC_WIKI_REPO='shared/wiki' \ "$gitea" wiki-repo NOSUCH 2>/dev/null); then code=0 else code=$? fi [ "$code" -eq 2 ] || fail "unknown type: want exit=2 got exit=$code" [ -z "$got" ] || fail "unknown type: want no output got=$got" # hash-id:首碼 0-9/A/B/C 改成 H 加前 7 碼;其餘首碼原樣輸出 8 碼大寫。 check_hash 'case-2' 'H5172CB7' check_hash 'case-11' 'HA9A6662' check_hash 'case-1' 'HB6EC7FD' check_hash 'case-12' 'HCCA8D42' check_hash 'case-3' 'D3E5AA27' check_hash 'case-7' 'D794C002' printf '%s\n' 'OK'