test-sync-to-codex-plugin.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  4. REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
  5. SYNC_SCRIPT_SOURCE="$REPO_ROOT/scripts/sync-to-codex-plugin.sh"
  6. BASH_UNDER_TEST="/bin/bash"
  7. PACKAGE_VERSION="1.2.3"
  8. MANIFEST_VERSION="9.8.7"
  9. FAILURES=0
  10. TEST_ROOT=""
  11. pass() {
  12. echo " [PASS] $1"
  13. }
  14. fail() {
  15. echo " [FAIL] $1"
  16. FAILURES=$((FAILURES + 1))
  17. }
  18. assert_equals() {
  19. local actual="$1"
  20. local expected="$2"
  21. local description="$3"
  22. if [[ "$actual" == "$expected" ]]; then
  23. pass "$description"
  24. else
  25. fail "$description"
  26. echo " expected: $expected"
  27. echo " actual: $actual"
  28. fi
  29. }
  30. assert_contains() {
  31. local haystack="$1"
  32. local needle="$2"
  33. local description="$3"
  34. if printf '%s' "$haystack" | grep -Fq -- "$needle"; then
  35. pass "$description"
  36. else
  37. fail "$description"
  38. echo " expected to find: $needle"
  39. fi
  40. }
  41. assert_not_contains() {
  42. local haystack="$1"
  43. local needle="$2"
  44. local description="$3"
  45. if printf '%s' "$haystack" | grep -Fq -- "$needle"; then
  46. fail "$description"
  47. echo " did not expect to find: $needle"
  48. else
  49. pass "$description"
  50. fi
  51. }
  52. assert_matches() {
  53. local haystack="$1"
  54. local pattern="$2"
  55. local description="$3"
  56. if printf '%s' "$haystack" | grep -Eq -- "$pattern"; then
  57. pass "$description"
  58. else
  59. fail "$description"
  60. echo " expected to match: $pattern"
  61. fi
  62. }
  63. assert_path_absent() {
  64. local path="$1"
  65. local description="$2"
  66. if [[ ! -e "$path" ]]; then
  67. pass "$description"
  68. else
  69. fail "$description"
  70. echo " did not expect path to exist: $path"
  71. fi
  72. }
  73. assert_branch_absent() {
  74. local repo="$1"
  75. local pattern="$2"
  76. local description="$3"
  77. local branches
  78. branches="$(git -C "$repo" branch --list "$pattern")"
  79. if [[ -z "$branches" ]]; then
  80. pass "$description"
  81. else
  82. fail "$description"
  83. echo " did not expect matching branches:"
  84. echo "$branches" | sed 's/^/ /'
  85. fi
  86. }
  87. assert_current_branch() {
  88. local repo="$1"
  89. local expected="$2"
  90. local description="$3"
  91. local actual
  92. actual="$(git -C "$repo" branch --show-current)"
  93. assert_equals "$actual" "$expected" "$description"
  94. }
  95. assert_file_equals() {
  96. local path="$1"
  97. local expected="$2"
  98. local description="$3"
  99. local actual
  100. actual="$(cat "$path")"
  101. assert_equals "$actual" "$expected" "$description"
  102. }
  103. cleanup() {
  104. if [[ -n "$TEST_ROOT" && -d "$TEST_ROOT" ]]; then
  105. rm -rf "$TEST_ROOT"
  106. fi
  107. }
  108. configure_git_identity() {
  109. local repo="$1"
  110. git -C "$repo" config user.name "Test Bot"
  111. git -C "$repo" config user.email "test@example.com"
  112. }
  113. init_repo() {
  114. local repo="$1"
  115. git init -q -b main "$repo"
  116. configure_git_identity "$repo"
  117. }
  118. commit_fixture() {
  119. local repo="$1"
  120. local message="$2"
  121. git -C "$repo" commit -q -m "$message"
  122. }
  123. checkout_fixture_branch() {
  124. local repo="$1"
  125. local branch="$2"
  126. git -C "$repo" checkout -q -b "$branch"
  127. }
  128. write_upstream_fixture() {
  129. local repo="$1"
  130. local with_pure_ignored="${2:-1}"
  131. mkdir -p \
  132. "$repo/.codex-plugin" \
  133. "$repo/.private-journal" \
  134. "$repo/assets" \
  135. "$repo/scripts" \
  136. "$repo/skills/example"
  137. if [[ "$with_pure_ignored" == "1" ]]; then
  138. mkdir -p "$repo/ignored-cache/tmp"
  139. fi
  140. cp "$SYNC_SCRIPT_SOURCE" "$repo/scripts/sync-to-codex-plugin.sh"
  141. cat > "$repo/package.json" <<EOF
  142. {
  143. "name": "fixture-upstream",
  144. "version": "$PACKAGE_VERSION"
  145. }
  146. EOF
  147. cat > "$repo/.gitignore" <<'EOF'
  148. .private-journal/
  149. EOF
  150. if [[ "$with_pure_ignored" == "1" ]]; then
  151. cat >> "$repo/.gitignore" <<'EOF'
  152. ignored-cache/
  153. EOF
  154. fi
  155. cat > "$repo/.codex-plugin/plugin.json" <<EOF
  156. {
  157. "name": "superpowers",
  158. "version": "$MANIFEST_VERSION"
  159. }
  160. EOF
  161. cat > "$repo/assets/superpowers-small.svg" <<'EOF'
  162. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>
  163. EOF
  164. printf 'png fixture\n' > "$repo/assets/app-icon.png"
  165. cat > "$repo/skills/example/SKILL.md" <<'EOF'
  166. # Example Skill
  167. Fixture content.
  168. EOF
  169. printf 'tracked keep\n' > "$repo/.private-journal/keep.txt"
  170. printf 'ignored leak\n' > "$repo/.private-journal/leak.txt"
  171. if [[ "$with_pure_ignored" == "1" ]]; then
  172. printf 'ignored cache state\n' > "$repo/ignored-cache/tmp/state.json"
  173. fi
  174. git -C "$repo" add \
  175. .codex-plugin/plugin.json \
  176. .gitignore \
  177. assets/app-icon.png \
  178. assets/superpowers-small.svg \
  179. package.json \
  180. scripts/sync-to-codex-plugin.sh \
  181. skills/example/SKILL.md
  182. git -C "$repo" add -f .private-journal/keep.txt
  183. commit_fixture "$repo" "Initial upstream fixture"
  184. }
  185. write_destination_fixture() {
  186. local repo="$1"
  187. mkdir -p "$repo/plugins/superpowers/skills/example"
  188. printf 'fixture keep\n' > "$repo/plugins/superpowers/.fixture-keep"
  189. cat > "$repo/plugins/superpowers/skills/example/SKILL.md" <<'EOF'
  190. # Example Skill
  191. Fixture content.
  192. EOF
  193. git -C "$repo" add plugins/superpowers/.fixture-keep
  194. git -C "$repo" add plugins/superpowers/skills/example/SKILL.md
  195. commit_fixture "$repo" "Initial destination fixture"
  196. }
  197. dirty_tracked_destination_skill() {
  198. local repo="$1"
  199. cat > "$repo/plugins/superpowers/skills/example/SKILL.md" <<'EOF'
  200. # Example Skill
  201. Locally modified fixture content.
  202. EOF
  203. }
  204. write_synced_destination_fixture() {
  205. local repo="$1"
  206. mkdir -p \
  207. "$repo/plugins/superpowers/.codex-plugin" \
  208. "$repo/plugins/superpowers/.private-journal" \
  209. "$repo/plugins/superpowers/assets" \
  210. "$repo/plugins/superpowers/skills/example"
  211. cat > "$repo/plugins/superpowers/.codex-plugin/plugin.json" <<EOF
  212. {
  213. "name": "superpowers",
  214. "version": "$MANIFEST_VERSION"
  215. }
  216. EOF
  217. cat > "$repo/plugins/superpowers/assets/superpowers-small.svg" <<'EOF'
  218. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>
  219. EOF
  220. printf 'png fixture\n' > "$repo/plugins/superpowers/assets/app-icon.png"
  221. cat > "$repo/plugins/superpowers/skills/example/SKILL.md" <<'EOF'
  222. # Example Skill
  223. Fixture content.
  224. EOF
  225. printf 'tracked keep\n' > "$repo/plugins/superpowers/.private-journal/keep.txt"
  226. git -C "$repo" add \
  227. plugins/superpowers/.codex-plugin/plugin.json \
  228. plugins/superpowers/assets/app-icon.png \
  229. plugins/superpowers/assets/superpowers-small.svg \
  230. plugins/superpowers/skills/example/SKILL.md \
  231. plugins/superpowers/.private-journal/keep.txt
  232. commit_fixture "$repo" "Initial synced destination fixture"
  233. }
  234. write_stale_ignored_destination_fixture() {
  235. local repo="$1"
  236. mkdir -p "$repo/plugins/superpowers/.private-journal"
  237. printf 'fixture keep\n' > "$repo/plugins/superpowers/.fixture-keep"
  238. printf 'stale ignored leak\n' > "$repo/plugins/superpowers/.private-journal/leak.txt"
  239. git -C "$repo" add plugins/superpowers/.fixture-keep
  240. commit_fixture "$repo" "Initial stale ignored destination fixture"
  241. }
  242. write_fake_gh() {
  243. local bin_dir="$1"
  244. mkdir -p "$bin_dir"
  245. cat > "$bin_dir/gh" <<'EOF'
  246. #!/usr/bin/env bash
  247. set -euo pipefail
  248. if [[ "${1:-}" == "auth" && "${2:-}" == "status" ]]; then
  249. exit 0
  250. fi
  251. echo "unexpected gh invocation: $*" >&2
  252. exit 1
  253. EOF
  254. chmod +x "$bin_dir/gh"
  255. }
  256. run_preview() {
  257. local upstream="$1"
  258. local dest="$2"
  259. local fake_bin="$3"
  260. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -n --local "$dest" 2>&1
  261. }
  262. run_bootstrap_preview() {
  263. local upstream="$1"
  264. local dest="$2"
  265. local fake_bin="$3"
  266. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -n --bootstrap --local "$dest" 2>&1
  267. }
  268. run_preview_without_manifest() {
  269. local upstream="$1"
  270. local dest="$2"
  271. local fake_bin="$3"
  272. rm -f "$upstream/.codex-plugin/plugin.json"
  273. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -n --local "$dest" 2>&1
  274. }
  275. run_preview_with_stale_ignored_destination() {
  276. local upstream="$1"
  277. local dest="$2"
  278. local fake_bin="$3"
  279. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -n --local "$dest" 2>&1
  280. }
  281. run_apply() {
  282. local upstream="$1"
  283. local dest="$2"
  284. local fake_bin="$3"
  285. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -y --local "$dest" 2>&1
  286. }
  287. run_help() {
  288. local upstream="$1"
  289. local fake_bin="$2"
  290. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" --help 2>&1
  291. }
  292. write_bootstrap_destination_fixture() {
  293. local repo="$1"
  294. printf 'bootstrap fixture\n' > "$repo/README.md"
  295. git -C "$repo" add README.md
  296. commit_fixture "$repo" "Initial bootstrap destination fixture"
  297. }
  298. main() {
  299. local upstream
  300. local mixed_only_upstream
  301. local dest
  302. local dest_branch
  303. local mixed_only_dest
  304. local stale_dest
  305. local dirty_apply_dest
  306. local dirty_apply_dest_branch
  307. local noop_apply_dest
  308. local noop_apply_dest_branch
  309. local fake_bin
  310. local bootstrap_dest
  311. local bootstrap_dest_branch
  312. local preview_status
  313. local preview_output
  314. local preview_section
  315. local bootstrap_status
  316. local bootstrap_output
  317. local missing_manifest_status
  318. local missing_manifest_output
  319. local mixed_only_status
  320. local mixed_only_output
  321. local stale_preview_status
  322. local stale_preview_output
  323. local stale_preview_section
  324. local dirty_apply_status
  325. local dirty_apply_output
  326. local noop_apply_status
  327. local noop_apply_output
  328. local help_output
  329. local script_source
  330. local dirty_skill_path
  331. echo "=== Test: sync-to-codex-plugin dry-run regression ==="
  332. TEST_ROOT="$(mktemp -d)"
  333. trap cleanup EXIT
  334. upstream="$TEST_ROOT/upstream"
  335. mixed_only_upstream="$TEST_ROOT/mixed-only-upstream"
  336. dest="$TEST_ROOT/destination"
  337. mixed_only_dest="$TEST_ROOT/mixed-only-destination"
  338. stale_dest="$TEST_ROOT/stale-destination"
  339. dirty_apply_dest="$TEST_ROOT/dirty-apply-destination"
  340. dirty_apply_dest_branch="fixture/dirty-apply-target"
  341. noop_apply_dest="$TEST_ROOT/noop-apply-destination"
  342. noop_apply_dest_branch="fixture/noop-apply-target"
  343. bootstrap_dest="$TEST_ROOT/bootstrap-destination"
  344. dest_branch="fixture/preview-target"
  345. bootstrap_dest_branch="fixture/bootstrap-preview-target"
  346. fake_bin="$TEST_ROOT/bin"
  347. init_repo "$upstream"
  348. write_upstream_fixture "$upstream"
  349. init_repo "$mixed_only_upstream"
  350. write_upstream_fixture "$mixed_only_upstream" 0
  351. init_repo "$dest"
  352. write_destination_fixture "$dest"
  353. checkout_fixture_branch "$dest" "$dest_branch"
  354. dirty_tracked_destination_skill "$dest"
  355. init_repo "$mixed_only_dest"
  356. write_destination_fixture "$mixed_only_dest"
  357. init_repo "$stale_dest"
  358. write_stale_ignored_destination_fixture "$stale_dest"
  359. init_repo "$dirty_apply_dest"
  360. write_synced_destination_fixture "$dirty_apply_dest"
  361. checkout_fixture_branch "$dirty_apply_dest" "$dirty_apply_dest_branch"
  362. dirty_tracked_destination_skill "$dirty_apply_dest"
  363. init_repo "$noop_apply_dest"
  364. write_synced_destination_fixture "$noop_apply_dest"
  365. checkout_fixture_branch "$noop_apply_dest" "$noop_apply_dest_branch"
  366. init_repo "$bootstrap_dest"
  367. write_bootstrap_destination_fixture "$bootstrap_dest"
  368. checkout_fixture_branch "$bootstrap_dest" "$bootstrap_dest_branch"
  369. write_fake_gh "$fake_bin"
  370. # This regression test is about dry-run content, so capture the preview
  371. # output even if the current script exits nonzero in --local mode.
  372. set +e
  373. preview_output="$(run_preview "$upstream" "$dest" "$fake_bin")"
  374. preview_status=$?
  375. bootstrap_output="$(run_bootstrap_preview "$upstream" "$bootstrap_dest" "$fake_bin")"
  376. bootstrap_status=$?
  377. mixed_only_output="$(run_preview "$mixed_only_upstream" "$mixed_only_dest" "$fake_bin")"
  378. mixed_only_status=$?
  379. stale_preview_output="$(run_preview_with_stale_ignored_destination "$upstream" "$stale_dest" "$fake_bin")"
  380. stale_preview_status=$?
  381. dirty_apply_output="$(run_apply "$upstream" "$dirty_apply_dest" "$fake_bin")"
  382. dirty_apply_status=$?
  383. noop_apply_output="$(run_apply "$upstream" "$noop_apply_dest" "$fake_bin")"
  384. noop_apply_status=$?
  385. missing_manifest_output="$(run_preview_without_manifest "$upstream" "$dest" "$fake_bin")"
  386. missing_manifest_status=$?
  387. set -e
  388. help_output="$(run_help "$upstream" "$fake_bin")"
  389. script_source="$(cat "$upstream/scripts/sync-to-codex-plugin.sh")"
  390. preview_section="$(printf '%s\n' "$preview_output" | sed -n '/^=== Preview (rsync --dry-run) ===$/,/^=== End preview ===$/p')"
  391. stale_preview_section="$(printf '%s\n' "$stale_preview_output" | sed -n '/^=== Preview (rsync --dry-run) ===$/,/^=== End preview ===$/p')"
  392. dirty_skill_path="$dirty_apply_dest/plugins/superpowers/skills/example/SKILL.md"
  393. echo ""
  394. echo "Preview assertions..."
  395. assert_equals "$preview_status" "0" "Preview exits successfully"
  396. assert_contains "$preview_output" "Version: $MANIFEST_VERSION" "Preview uses manifest version"
  397. assert_not_contains "$preview_output" "Version: $PACKAGE_VERSION" "Preview does not use package.json version"
  398. assert_contains "$preview_section" ".codex-plugin/plugin.json" "Preview includes manifest path"
  399. assert_contains "$preview_section" "assets/superpowers-small.svg" "Preview includes SVG asset"
  400. assert_contains "$preview_section" "assets/app-icon.png" "Preview includes PNG asset"
  401. assert_contains "$preview_section" ".private-journal/keep.txt" "Preview includes tracked ignored file"
  402. assert_not_contains "$preview_section" ".private-journal/leak.txt" "Preview excludes ignored untracked file"
  403. assert_not_contains "$preview_section" "ignored-cache/" "Preview excludes pure ignored directories"
  404. assert_not_contains "$preview_output" "Overlay file (.codex-plugin/plugin.json) will be regenerated" "Preview omits overlay regeneration note"
  405. assert_not_contains "$preview_output" "Assets (superpowers-small.svg, app-icon.png) will be seeded from" "Preview omits assets seeding note"
  406. assert_contains "$preview_section" "skills/example/SKILL.md" "Preview reflects dirty tracked destination file"
  407. assert_current_branch "$dest" "$dest_branch" "Preview leaves destination checkout on its original branch"
  408. assert_branch_absent "$dest" "sync/superpowers-*" "Preview does not create sync branch in destination checkout"
  409. echo ""
  410. echo "Mixed-directory assertions..."
  411. assert_equals "$mixed_only_status" "0" "Mixed ignored directory preview exits successfully under /bin/bash"
  412. assert_contains "$mixed_only_output" ".private-journal/keep.txt" "Mixed ignored directory preview still includes tracked ignored file"
  413. assert_not_contains "$mixed_only_output" "ignored-cache/" "Mixed ignored directory preview has no pure ignored directory fixture"
  414. echo ""
  415. echo "Convergence assertions..."
  416. assert_equals "$stale_preview_status" "0" "Stale ignored destination preview exits successfully"
  417. assert_matches "$stale_preview_section" "\\*deleting +\\.private-journal/leak\\.txt" "Preview deletes stale ignored destination file"
  418. echo ""
  419. echo "Bootstrap assertions..."
  420. assert_equals "$bootstrap_status" "0" "Bootstrap preview exits successfully"
  421. assert_contains "$bootstrap_output" "Mode: BOOTSTRAP (creating plugins/superpowers/ when absent)" "Bootstrap preview describes directory creation"
  422. assert_not_contains "$bootstrap_output" "Assets:" "Bootstrap preview omits external assets path"
  423. assert_contains "$bootstrap_output" "Dry run only. Nothing was changed or pushed." "Bootstrap preview remains dry-run only"
  424. assert_path_absent "$bootstrap_dest/plugins/superpowers" "Bootstrap preview does not create destination plugin directory"
  425. assert_current_branch "$bootstrap_dest" "$bootstrap_dest_branch" "Bootstrap preview leaves destination checkout on its original branch"
  426. assert_branch_absent "$bootstrap_dest" "bootstrap/superpowers-*" "Bootstrap preview does not create bootstrap branch in destination checkout"
  427. echo ""
  428. echo "Apply assertions..."
  429. assert_equals "$dirty_apply_status" "1" "Dirty local apply exits with failure"
  430. assert_contains "$dirty_apply_output" "ERROR: local checkout has uncommitted changes under 'plugins/superpowers'" "Dirty local apply reports protected destination path"
  431. assert_current_branch "$dirty_apply_dest" "$dirty_apply_dest_branch" "Dirty local apply leaves destination checkout on its original branch"
  432. assert_branch_absent "$dirty_apply_dest" "sync/superpowers-*" "Dirty local apply does not create sync branch in destination checkout"
  433. assert_file_equals "$dirty_skill_path" "# Example Skill
  434. Locally modified fixture content." "Dirty local apply preserves tracked working-tree file content"
  435. assert_equals "$noop_apply_status" "0" "Clean no-op local apply exits successfully"
  436. assert_contains "$noop_apply_output" "No changes — embedded plugin was already in sync with upstream" "Clean no-op local apply reports no changes"
  437. assert_current_branch "$noop_apply_dest" "$noop_apply_dest_branch" "Clean no-op local apply leaves destination checkout on its original branch"
  438. assert_branch_absent "$noop_apply_dest" "sync/superpowers-*" "Clean no-op local apply does not create sync branch in destination checkout"
  439. echo ""
  440. echo "Missing manifest assertions..."
  441. assert_equals "$missing_manifest_status" "1" "Missing manifest exits with failure"
  442. assert_contains "$missing_manifest_output" "ERROR: committed Codex manifest missing at" "Missing manifest reports committed manifest path"
  443. echo ""
  444. echo "Help assertions..."
  445. assert_not_contains "$help_output" "--assets-src" "Help omits --assets-src"
  446. echo ""
  447. echo "Source assertions..."
  448. assert_not_contains "$script_source" "regenerated inline" "Source drops regenerated inline phrasing"
  449. assert_not_contains "$script_source" "Brand Assets directory" "Source drops Brand Assets directory phrasing"
  450. assert_not_contains "$script_source" "--assets-src" "Source drops --assets-src"
  451. if [[ $FAILURES -ne 0 ]]; then
  452. echo ""
  453. echo "FAILED: $FAILURES assertion(s) failed."
  454. exit 1
  455. fi
  456. echo ""
  457. echo "PASS"
  458. }
  459. main "$@"