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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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_not_matches() {
  64. local haystack="$1"
  65. local pattern="$2"
  66. local description="$3"
  67. if printf '%s' "$haystack" | grep -Eq -- "$pattern"; then
  68. fail "$description"
  69. echo " did not expect to match: $pattern"
  70. else
  71. pass "$description"
  72. fi
  73. }
  74. assert_path_absent() {
  75. local path="$1"
  76. local description="$2"
  77. if [[ ! -e "$path" ]]; then
  78. pass "$description"
  79. else
  80. fail "$description"
  81. echo " did not expect path to exist: $path"
  82. fi
  83. }
  84. assert_branch_absent() {
  85. local repo="$1"
  86. local pattern="$2"
  87. local description="$3"
  88. local branches
  89. branches="$(git -C "$repo" branch --list "$pattern")"
  90. if [[ -z "$branches" ]]; then
  91. pass "$description"
  92. else
  93. fail "$description"
  94. echo " did not expect matching branches:"
  95. echo "$branches" | sed 's/^/ /'
  96. fi
  97. }
  98. assert_current_branch() {
  99. local repo="$1"
  100. local expected="$2"
  101. local description="$3"
  102. local actual
  103. actual="$(git -C "$repo" branch --show-current)"
  104. assert_equals "$actual" "$expected" "$description"
  105. }
  106. assert_file_equals() {
  107. local path="$1"
  108. local expected="$2"
  109. local description="$3"
  110. local actual
  111. actual="$(cat "$path")"
  112. assert_equals "$actual" "$expected" "$description"
  113. }
  114. cleanup() {
  115. if [[ -n "$TEST_ROOT" && -d "$TEST_ROOT" ]]; then
  116. rm -rf "$TEST_ROOT"
  117. fi
  118. }
  119. configure_git_identity() {
  120. local repo="$1"
  121. git -C "$repo" config user.name "Test Bot"
  122. git -C "$repo" config user.email "test@example.com"
  123. }
  124. init_repo() {
  125. local repo="$1"
  126. git init -q -b main "$repo"
  127. configure_git_identity "$repo"
  128. }
  129. commit_fixture() {
  130. local repo="$1"
  131. local message="$2"
  132. git -C "$repo" commit -q -m "$message"
  133. }
  134. checkout_fixture_branch() {
  135. local repo="$1"
  136. local branch="$2"
  137. git -C "$repo" checkout -q -b "$branch"
  138. }
  139. write_upstream_fixture() {
  140. local repo="$1"
  141. local with_pure_ignored="${2:-1}"
  142. mkdir -p \
  143. "$repo/.codex-plugin" \
  144. "$repo/.private-journal" \
  145. "$repo/assets" \
  146. "$repo/evals/drill" \
  147. "$repo/hooks" \
  148. "$repo/scripts" \
  149. "$repo/skills/brainstorming/scripts/vendor" \
  150. "$repo/skills/example"
  151. if [[ "$with_pure_ignored" == "1" ]]; then
  152. mkdir -p "$repo/ignored-cache/tmp"
  153. fi
  154. cp "$SYNC_SCRIPT_SOURCE" "$repo/scripts/sync-to-codex-plugin.sh"
  155. cat > "$repo/package.json" <<EOF
  156. {
  157. "name": "fixture-upstream",
  158. "version": "$PACKAGE_VERSION"
  159. }
  160. EOF
  161. cat > "$repo/.gitignore" <<'EOF'
  162. .private-journal/
  163. EOF
  164. if [[ "$with_pure_ignored" == "1" ]]; then
  165. cat >> "$repo/.gitignore" <<'EOF'
  166. ignored-cache/
  167. EOF
  168. fi
  169. cat > "$repo/.codex-plugin/plugin.json" <<EOF
  170. {
  171. "name": "superpowers",
  172. "version": "$MANIFEST_VERSION"
  173. }
  174. EOF
  175. cat > "$repo/assets/superpowers-small.svg" <<'EOF'
  176. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>
  177. EOF
  178. printf 'png fixture\n' > "$repo/assets/app-icon.png"
  179. printf 'eval harness fixture\n' > "$repo/evals/drill/README.md"
  180. cat > "$repo/hooks/hooks-codex.json" <<'EOF'
  181. {
  182. "hooks": {
  183. "SessionStart": [
  184. {
  185. "matcher": "startup|resume|clear",
  186. "hooks": [
  187. {
  188. "type": "command",
  189. "command": "\"${PLUGIN_ROOT}/hooks/run-hook.cmd\" session-start-codex",
  190. "async": false
  191. }
  192. ]
  193. }
  194. ]
  195. }
  196. }
  197. EOF
  198. cat > "$repo/hooks/session-start" <<'EOF'
  199. #!/usr/bin/env sh
  200. echo "session-start fixture"
  201. EOF
  202. cat > "$repo/hooks/session-start-codex" <<'EOF'
  203. #!/usr/bin/env sh
  204. echo "session-start-codex fixture"
  205. EOF
  206. cat > "$repo/hooks/run-hook.cmd" <<'EOF'
  207. @echo off
  208. echo run-hook fixture
  209. EOF
  210. chmod +x "$repo/hooks/session-start" "$repo/hooks/session-start-codex" "$repo/hooks/run-hook.cmd"
  211. cat > "$repo/skills/example/SKILL.md" <<'EOF'
  212. # Example Skill
  213. Fixture content.
  214. EOF
  215. cat > "$repo/skills/brainstorming/scripts/server.cjs" <<'EOF'
  216. console.log('fixture server')
  217. EOF
  218. cat > "$repo/skills/brainstorming/scripts/helper.js" <<'EOF'
  219. window.fixtureHelper = true
  220. EOF
  221. cat > "$repo/skills/brainstorming/scripts/frame-template.html" <<'EOF'
  222. <html><body><!-- CONTENT --></body></html>
  223. EOF
  224. printf 'fixture alpine\n' > "$repo/skills/brainstorming/scripts/vendor/alpine.js"
  225. cat > "$repo/skills/brainstorming/scripts/vendor/alpine.provenance.json" <<'EOF'
  226. {"name":"alpinejs","version":"3.15.12","localPath":"skills/brainstorming/scripts/vendor/alpine.js","sha256":"fixture","approvalArtifact":"SUP-215"}
  227. EOF
  228. cat > "$repo/skills/brainstorming/scripts/vendor/THIRD_PARTY_NOTICES.md" <<'EOF'
  229. # Third-Party Notices
  230. Alpine.js fixture notice.
  231. EOF
  232. printf 'tracked keep\n' > "$repo/.private-journal/keep.txt"
  233. printf 'ignored leak\n' > "$repo/.private-journal/leak.txt"
  234. if [[ "$with_pure_ignored" == "1" ]]; then
  235. printf 'ignored cache state\n' > "$repo/ignored-cache/tmp/state.json"
  236. fi
  237. git -C "$repo" add \
  238. .codex-plugin/plugin.json \
  239. .gitignore \
  240. assets/app-icon.png \
  241. assets/superpowers-small.svg \
  242. evals/drill/README.md \
  243. hooks/hooks-codex.json \
  244. hooks/run-hook.cmd \
  245. hooks/session-start \
  246. hooks/session-start-codex \
  247. package.json \
  248. scripts/sync-to-codex-plugin.sh \
  249. skills/brainstorming/scripts/server.cjs \
  250. skills/brainstorming/scripts/helper.js \
  251. skills/brainstorming/scripts/frame-template.html \
  252. skills/brainstorming/scripts/vendor/alpine.js \
  253. skills/brainstorming/scripts/vendor/alpine.provenance.json \
  254. skills/brainstorming/scripts/vendor/THIRD_PARTY_NOTICES.md \
  255. skills/example/SKILL.md
  256. git -C "$repo" add -f .private-journal/keep.txt
  257. commit_fixture "$repo" "Initial upstream fixture"
  258. }
  259. write_destination_fixture() {
  260. local repo="$1"
  261. mkdir -p "$repo/plugins/superpowers/skills/example"
  262. printf 'fixture keep\n' > "$repo/plugins/superpowers/.fixture-keep"
  263. cat > "$repo/plugins/superpowers/skills/example/SKILL.md" <<'EOF'
  264. # Example Skill
  265. Fixture content.
  266. EOF
  267. git -C "$repo" add plugins/superpowers/.fixture-keep
  268. git -C "$repo" add plugins/superpowers/skills/example/SKILL.md
  269. commit_fixture "$repo" "Initial destination fixture"
  270. }
  271. add_openai_agent_metadata_fixture() {
  272. local repo="$1"
  273. mkdir -p "$repo/plugins/superpowers/skills/example/agents"
  274. cat > "$repo/plugins/superpowers/skills/example/agents/openai.yaml" <<'EOF'
  275. interface:
  276. display_name: "Example"
  277. short_description: "Destination-owned OpenAI metadata"
  278. EOF
  279. git -C "$repo" add plugins/superpowers/skills/example/agents/openai.yaml
  280. commit_fixture "$repo" "Add OpenAI agent metadata fixture"
  281. }
  282. dirty_tracked_destination_skill() {
  283. local repo="$1"
  284. cat > "$repo/plugins/superpowers/skills/example/SKILL.md" <<'EOF'
  285. # Example Skill
  286. Locally modified fixture content.
  287. EOF
  288. }
  289. write_synced_destination_fixture() {
  290. local repo="$1"
  291. mkdir -p \
  292. "$repo/plugins/superpowers/.codex-plugin" \
  293. "$repo/plugins/superpowers/.private-journal" \
  294. "$repo/plugins/superpowers/assets" \
  295. "$repo/plugins/superpowers/hooks" \
  296. "$repo/plugins/superpowers/skills/brainstorming/scripts/vendor" \
  297. "$repo/plugins/superpowers/skills/example/agents" \
  298. "$repo/plugins/superpowers/skills/example"
  299. cat > "$repo/plugins/superpowers/.codex-plugin/plugin.json" <<EOF
  300. {
  301. "name": "superpowers",
  302. "version": "$MANIFEST_VERSION"
  303. }
  304. EOF
  305. cat > "$repo/plugins/superpowers/assets/superpowers-small.svg" <<'EOF'
  306. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"></svg>
  307. EOF
  308. printf 'png fixture\n' > "$repo/plugins/superpowers/assets/app-icon.png"
  309. cat > "$repo/plugins/superpowers/hooks/hooks-codex.json" <<'EOF'
  310. {
  311. "hooks": {
  312. "SessionStart": [
  313. {
  314. "matcher": "startup|resume|clear",
  315. "hooks": [
  316. {
  317. "type": "command",
  318. "command": "\"${PLUGIN_ROOT}/hooks/run-hook.cmd\" session-start-codex",
  319. "async": false
  320. }
  321. ]
  322. }
  323. ]
  324. }
  325. }
  326. EOF
  327. cat > "$repo/plugins/superpowers/hooks/session-start" <<'EOF'
  328. #!/usr/bin/env sh
  329. echo "session-start fixture"
  330. EOF
  331. cat > "$repo/plugins/superpowers/hooks/session-start-codex" <<'EOF'
  332. #!/usr/bin/env sh
  333. echo "session-start-codex fixture"
  334. EOF
  335. cat > "$repo/plugins/superpowers/hooks/run-hook.cmd" <<'EOF'
  336. @echo off
  337. echo run-hook fixture
  338. EOF
  339. chmod +x "$repo/plugins/superpowers/hooks/session-start" "$repo/plugins/superpowers/hooks/session-start-codex" "$repo/plugins/superpowers/hooks/run-hook.cmd"
  340. cat > "$repo/plugins/superpowers/skills/example/SKILL.md" <<'EOF'
  341. # Example Skill
  342. Fixture content.
  343. EOF
  344. cat > "$repo/plugins/superpowers/skills/brainstorming/scripts/server.cjs" <<'EOF'
  345. console.log('fixture server')
  346. EOF
  347. cat > "$repo/plugins/superpowers/skills/brainstorming/scripts/helper.js" <<'EOF'
  348. window.fixtureHelper = true
  349. EOF
  350. cat > "$repo/plugins/superpowers/skills/brainstorming/scripts/frame-template.html" <<'EOF'
  351. <html><body><!-- CONTENT --></body></html>
  352. EOF
  353. printf 'fixture alpine\n' > "$repo/plugins/superpowers/skills/brainstorming/scripts/vendor/alpine.js"
  354. cat > "$repo/plugins/superpowers/skills/brainstorming/scripts/vendor/alpine.provenance.json" <<'EOF'
  355. {"name":"alpinejs","version":"3.15.12","localPath":"skills/brainstorming/scripts/vendor/alpine.js","sha256":"fixture","approvalArtifact":"SUP-215"}
  356. EOF
  357. cat > "$repo/plugins/superpowers/skills/brainstorming/scripts/vendor/THIRD_PARTY_NOTICES.md" <<'EOF'
  358. # Third-Party Notices
  359. Alpine.js fixture notice.
  360. EOF
  361. cat > "$repo/plugins/superpowers/skills/example/agents/openai.yaml" <<'EOF'
  362. interface:
  363. display_name: "Example"
  364. short_description: "Destination-owned OpenAI metadata"
  365. EOF
  366. printf 'tracked keep\n' > "$repo/plugins/superpowers/.private-journal/keep.txt"
  367. git -C "$repo" add \
  368. plugins/superpowers/.codex-plugin/plugin.json \
  369. plugins/superpowers/assets/app-icon.png \
  370. plugins/superpowers/assets/superpowers-small.svg \
  371. plugins/superpowers/hooks/hooks-codex.json \
  372. plugins/superpowers/hooks/run-hook.cmd \
  373. plugins/superpowers/hooks/session-start \
  374. plugins/superpowers/hooks/session-start-codex \
  375. plugins/superpowers/skills/brainstorming/scripts/server.cjs \
  376. plugins/superpowers/skills/brainstorming/scripts/helper.js \
  377. plugins/superpowers/skills/brainstorming/scripts/frame-template.html \
  378. plugins/superpowers/skills/brainstorming/scripts/vendor/alpine.js \
  379. plugins/superpowers/skills/brainstorming/scripts/vendor/alpine.provenance.json \
  380. plugins/superpowers/skills/brainstorming/scripts/vendor/THIRD_PARTY_NOTICES.md \
  381. plugins/superpowers/skills/example/agents/openai.yaml \
  382. plugins/superpowers/skills/example/SKILL.md \
  383. plugins/superpowers/.private-journal/keep.txt
  384. commit_fixture "$repo" "Initial synced destination fixture"
  385. }
  386. write_stale_ignored_destination_fixture() {
  387. local repo="$1"
  388. mkdir -p "$repo/plugins/superpowers/.private-journal"
  389. printf 'fixture keep\n' > "$repo/plugins/superpowers/.fixture-keep"
  390. printf 'stale ignored leak\n' > "$repo/plugins/superpowers/.private-journal/leak.txt"
  391. git -C "$repo" add plugins/superpowers/.fixture-keep
  392. commit_fixture "$repo" "Initial stale ignored destination fixture"
  393. }
  394. write_outdated_destination_fixture() {
  395. local repo="$1"
  396. mkdir -p \
  397. "$repo/plugins/superpowers/.codex-plugin" \
  398. "$repo/plugins/superpowers/assets" \
  399. "$repo/plugins/superpowers/skills/example"
  400. cat > "$repo/plugins/superpowers/.codex-plugin/plugin.json" <<'EOF'
  401. {
  402. "name": "superpowers",
  403. "version": "0.0.1"
  404. }
  405. EOF
  406. printf 'old png fixture\n' > "$repo/plugins/superpowers/assets/app-icon.png"
  407. cat > "$repo/plugins/superpowers/skills/example/SKILL.md" <<'EOF'
  408. # Example Skill
  409. Old destination content.
  410. EOF
  411. git -C "$repo" add \
  412. plugins/superpowers/.codex-plugin/plugin.json \
  413. plugins/superpowers/assets/app-icon.png \
  414. plugins/superpowers/skills/example/SKILL.md
  415. commit_fixture "$repo" "Initial outdated destination fixture"
  416. }
  417. attach_origin_remote() {
  418. local repo="$1"
  419. local remote="$2"
  420. git init -q --bare "$remote"
  421. git -C "$repo" remote add origin "$remote"
  422. git -C "$repo" push -u origin main --quiet
  423. }
  424. write_fake_gh() {
  425. local bin_dir="$1"
  426. mkdir -p "$bin_dir"
  427. cat > "$bin_dir/gh" <<'EOF'
  428. #!/usr/bin/env bash
  429. set -euo pipefail
  430. if [[ "${1:-}" == "auth" && "${2:-}" == "status" ]]; then
  431. exit 0
  432. fi
  433. if [[ "${1:-}" == "pr" && "${2:-}" == "create" ]]; then
  434. shift 2
  435. body=""
  436. while [[ $# -gt 0 ]]; do
  437. case "$1" in
  438. --body)
  439. body="${2:-}"
  440. shift 2
  441. ;;
  442. *)
  443. shift
  444. ;;
  445. esac
  446. done
  447. if [[ -n "${FAKE_GH_PR_BODY_FILE:-}" ]]; then
  448. printf '%s' "$body" > "$FAKE_GH_PR_BODY_FILE"
  449. fi
  450. echo "https://github.com/prime-radiant-inc/openai-codex-plugins/pull/123"
  451. exit 0
  452. fi
  453. echo "unexpected gh invocation: $*" >&2
  454. exit 1
  455. EOF
  456. chmod +x "$bin_dir/gh"
  457. }
  458. run_preview() {
  459. local upstream="$1"
  460. local dest="$2"
  461. local fake_bin="$3"
  462. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -n --local "$dest" 2>&1
  463. }
  464. run_bootstrap_preview() {
  465. local upstream="$1"
  466. local dest="$2"
  467. local fake_bin="$3"
  468. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -n --bootstrap --local "$dest" 2>&1
  469. }
  470. run_preview_without_manifest() {
  471. local upstream="$1"
  472. local dest="$2"
  473. local fake_bin="$3"
  474. rm -f "$upstream/.codex-plugin/plugin.json"
  475. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -n --local "$dest" 2>&1
  476. }
  477. run_preview_with_stale_ignored_destination() {
  478. local upstream="$1"
  479. local dest="$2"
  480. local fake_bin="$3"
  481. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -n --local "$dest" 2>&1
  482. }
  483. run_apply() {
  484. local upstream="$1"
  485. local dest="$2"
  486. local fake_bin="$3"
  487. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -y --local "$dest" 2>&1
  488. }
  489. run_apply_with_pr_capture() {
  490. local upstream="$1"
  491. local dest="$2"
  492. local fake_bin="$3"
  493. local body_file="$4"
  494. FAKE_GH_PR_BODY_FILE="$body_file" PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -y --local "$dest" 2>&1
  495. }
  496. run_bootstrap_apply_with_pr_capture() {
  497. local upstream="$1"
  498. local dest="$2"
  499. local fake_bin="$3"
  500. local body_file="$4"
  501. FAKE_GH_PR_BODY_FILE="$body_file" PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" -y --bootstrap --local "$dest" 2>&1
  502. }
  503. run_help() {
  504. local upstream="$1"
  505. local fake_bin="$2"
  506. PATH="$fake_bin:$PATH" "$BASH_UNDER_TEST" "$upstream/scripts/sync-to-codex-plugin.sh" --help 2>&1
  507. }
  508. write_bootstrap_destination_fixture() {
  509. local repo="$1"
  510. printf 'bootstrap fixture\n' > "$repo/README.md"
  511. git -C "$repo" add README.md
  512. commit_fixture "$repo" "Initial bootstrap destination fixture"
  513. }
  514. main() {
  515. local upstream
  516. local mixed_only_upstream
  517. local dest
  518. local dest_branch
  519. local mixed_only_dest
  520. local stale_dest
  521. local dirty_apply_dest
  522. local dirty_apply_dest_branch
  523. local changed_apply_dest
  524. local changed_apply_remote
  525. local noop_apply_dest
  526. local noop_apply_dest_branch
  527. local fake_bin
  528. local bootstrap_dest
  529. local bootstrap_dest_branch
  530. local bootstrap_apply_dest
  531. local bootstrap_apply_remote
  532. local preview_status
  533. local preview_output
  534. local preview_section
  535. local bootstrap_status
  536. local bootstrap_output
  537. local missing_manifest_status
  538. local missing_manifest_output
  539. local mixed_only_status
  540. local mixed_only_output
  541. local stale_preview_status
  542. local stale_preview_output
  543. local stale_preview_section
  544. local dirty_apply_status
  545. local dirty_apply_output
  546. local changed_apply_status
  547. local changed_apply_output
  548. local changed_apply_pr_body_path
  549. local changed_apply_pr_body
  550. local bootstrap_apply_status
  551. local bootstrap_apply_output
  552. local bootstrap_apply_pr_body_path
  553. local bootstrap_apply_pr_body
  554. local noop_apply_status
  555. local noop_apply_output
  556. local help_output
  557. local script_source
  558. local dirty_skill_path
  559. local changed_apply_alpine_path
  560. local changed_apply_alpine_provenance_path
  561. local changed_apply_alpine_notice_path
  562. local noop_openai_metadata_path
  563. local noop_alpine_path
  564. local noop_alpine_provenance_path
  565. local noop_alpine_notice_path
  566. echo "=== Test: sync-to-codex-plugin dry-run regression ==="
  567. TEST_ROOT="$(mktemp -d)"
  568. trap cleanup EXIT
  569. upstream="$TEST_ROOT/upstream"
  570. mixed_only_upstream="$TEST_ROOT/mixed-only-upstream"
  571. dest="$TEST_ROOT/destination"
  572. mixed_only_dest="$TEST_ROOT/mixed-only-destination"
  573. stale_dest="$TEST_ROOT/stale-destination"
  574. dirty_apply_dest="$TEST_ROOT/dirty-apply-destination"
  575. dirty_apply_dest_branch="fixture/dirty-apply-target"
  576. changed_apply_dest="$TEST_ROOT/changed-apply-destination"
  577. changed_apply_remote="$TEST_ROOT/changed-apply-remote.git"
  578. noop_apply_dest="$TEST_ROOT/noop-apply-destination"
  579. noop_apply_dest_branch="fixture/noop-apply-target"
  580. bootstrap_dest="$TEST_ROOT/bootstrap-destination"
  581. bootstrap_apply_dest="$TEST_ROOT/bootstrap-apply-destination"
  582. bootstrap_apply_remote="$TEST_ROOT/bootstrap-apply-remote.git"
  583. dest_branch="fixture/preview-target"
  584. bootstrap_dest_branch="fixture/bootstrap-preview-target"
  585. fake_bin="$TEST_ROOT/bin"
  586. init_repo "$upstream"
  587. write_upstream_fixture "$upstream"
  588. init_repo "$mixed_only_upstream"
  589. write_upstream_fixture "$mixed_only_upstream" 0
  590. init_repo "$dest"
  591. write_destination_fixture "$dest"
  592. add_openai_agent_metadata_fixture "$dest"
  593. checkout_fixture_branch "$dest" "$dest_branch"
  594. dirty_tracked_destination_skill "$dest"
  595. init_repo "$mixed_only_dest"
  596. write_destination_fixture "$mixed_only_dest"
  597. init_repo "$stale_dest"
  598. write_stale_ignored_destination_fixture "$stale_dest"
  599. init_repo "$dirty_apply_dest"
  600. write_synced_destination_fixture "$dirty_apply_dest"
  601. checkout_fixture_branch "$dirty_apply_dest" "$dirty_apply_dest_branch"
  602. dirty_tracked_destination_skill "$dirty_apply_dest"
  603. init_repo "$changed_apply_dest"
  604. write_outdated_destination_fixture "$changed_apply_dest"
  605. attach_origin_remote "$changed_apply_dest" "$changed_apply_remote"
  606. init_repo "$noop_apply_dest"
  607. write_synced_destination_fixture "$noop_apply_dest"
  608. checkout_fixture_branch "$noop_apply_dest" "$noop_apply_dest_branch"
  609. init_repo "$bootstrap_dest"
  610. write_bootstrap_destination_fixture "$bootstrap_dest"
  611. checkout_fixture_branch "$bootstrap_dest" "$bootstrap_dest_branch"
  612. init_repo "$bootstrap_apply_dest"
  613. write_bootstrap_destination_fixture "$bootstrap_apply_dest"
  614. attach_origin_remote "$bootstrap_apply_dest" "$bootstrap_apply_remote"
  615. write_fake_gh "$fake_bin"
  616. # This regression test is about dry-run content, so capture the preview
  617. # output even if the current script exits nonzero in --local mode.
  618. set +e
  619. preview_output="$(run_preview "$upstream" "$dest" "$fake_bin")"
  620. preview_status=$?
  621. bootstrap_output="$(run_bootstrap_preview "$upstream" "$bootstrap_dest" "$fake_bin")"
  622. bootstrap_status=$?
  623. mixed_only_output="$(run_preview "$mixed_only_upstream" "$mixed_only_dest" "$fake_bin")"
  624. mixed_only_status=$?
  625. stale_preview_output="$(run_preview_with_stale_ignored_destination "$upstream" "$stale_dest" "$fake_bin")"
  626. stale_preview_status=$?
  627. dirty_apply_output="$(run_apply "$upstream" "$dirty_apply_dest" "$fake_bin")"
  628. dirty_apply_status=$?
  629. changed_apply_pr_body_path="$TEST_ROOT/changed-apply-pr-body.md"
  630. changed_apply_output="$(run_apply_with_pr_capture "$upstream" "$changed_apply_dest" "$fake_bin" "$changed_apply_pr_body_path")"
  631. changed_apply_status=$?
  632. bootstrap_apply_pr_body_path="$TEST_ROOT/bootstrap-apply-pr-body.md"
  633. bootstrap_apply_output="$(run_bootstrap_apply_with_pr_capture "$upstream" "$bootstrap_apply_dest" "$fake_bin" "$bootstrap_apply_pr_body_path")"
  634. bootstrap_apply_status=$?
  635. noop_apply_output="$(run_apply "$upstream" "$noop_apply_dest" "$fake_bin")"
  636. noop_apply_status=$?
  637. missing_manifest_output="$(run_preview_without_manifest "$upstream" "$dest" "$fake_bin")"
  638. missing_manifest_status=$?
  639. set -e
  640. help_output="$(run_help "$upstream" "$fake_bin")"
  641. script_source="$(cat "$upstream/scripts/sync-to-codex-plugin.sh")"
  642. preview_section="$(printf '%s\n' "$preview_output" | sed -n '/^=== Preview (rsync --dry-run) ===$/,/^=== End preview ===$/p')"
  643. stale_preview_section="$(printf '%s\n' "$stale_preview_output" | sed -n '/^=== Preview (rsync --dry-run) ===$/,/^=== End preview ===$/p')"
  644. dirty_skill_path="$dirty_apply_dest/plugins/superpowers/skills/example/SKILL.md"
  645. changed_apply_alpine_path="$changed_apply_dest/plugins/superpowers/skills/brainstorming/scripts/vendor/alpine.js"
  646. changed_apply_alpine_provenance_path="$changed_apply_dest/plugins/superpowers/skills/brainstorming/scripts/vendor/alpine.provenance.json"
  647. changed_apply_alpine_notice_path="$changed_apply_dest/plugins/superpowers/skills/brainstorming/scripts/vendor/THIRD_PARTY_NOTICES.md"
  648. changed_apply_pr_body="$(cat "$changed_apply_pr_body_path" 2>/dev/null || true)"
  649. bootstrap_apply_pr_body="$(cat "$bootstrap_apply_pr_body_path" 2>/dev/null || true)"
  650. noop_openai_metadata_path="$noop_apply_dest/plugins/superpowers/skills/example/agents/openai.yaml"
  651. noop_alpine_path="$noop_apply_dest/plugins/superpowers/skills/brainstorming/scripts/vendor/alpine.js"
  652. noop_alpine_provenance_path="$noop_apply_dest/plugins/superpowers/skills/brainstorming/scripts/vendor/alpine.provenance.json"
  653. noop_alpine_notice_path="$noop_apply_dest/plugins/superpowers/skills/brainstorming/scripts/vendor/THIRD_PARTY_NOTICES.md"
  654. echo ""
  655. echo "Preview assertions..."
  656. assert_equals "$preview_status" "0" "Preview exits successfully"
  657. assert_contains "$preview_output" "Version: $MANIFEST_VERSION" "Preview uses manifest version"
  658. assert_not_contains "$preview_output" "Version: $PACKAGE_VERSION" "Preview does not use package.json version"
  659. assert_contains "$preview_section" ".codex-plugin/plugin.json" "Preview includes manifest path"
  660. assert_contains "$preview_section" "assets/superpowers-small.svg" "Preview includes SVG asset"
  661. assert_contains "$preview_section" "assets/app-icon.png" "Preview includes PNG asset"
  662. assert_contains "$preview_section" "hooks/hooks-codex.json" "Preview includes Codex hook manifest"
  663. assert_contains "$preview_section" "hooks/session-start" "Preview includes session-start hook"
  664. assert_contains "$preview_section" "hooks/session-start-codex" "Preview includes Codex session-start hook"
  665. assert_contains "$preview_section" "hooks/run-hook.cmd" "Preview includes hook command wrapper"
  666. assert_contains "$preview_section" ".private-journal/keep.txt" "Preview includes tracked ignored file"
  667. assert_not_contains "$preview_section" ".private-journal/leak.txt" "Preview excludes ignored untracked file"
  668. assert_not_contains "$preview_section" "ignored-cache/" "Preview excludes pure ignored directories"
  669. assert_not_contains "$preview_section" "evals/" "Preview excludes eval harness"
  670. assert_not_contains "$preview_output" "Overlay file (.codex-plugin/plugin.json) will be regenerated" "Preview omits overlay regeneration note"
  671. assert_not_contains "$preview_output" "Assets (superpowers-small.svg, app-icon.png) will be seeded from" "Preview omits assets seeding note"
  672. assert_contains "$preview_section" "skills/example/SKILL.md" "Preview reflects dirty tracked destination file"
  673. assert_contains "$preview_section" "skills/brainstorming/scripts/server.cjs" "Preview includes skill-local server runtime"
  674. assert_contains "$preview_section" "skills/brainstorming/scripts/helper.js" "Preview includes skill-local helper runtime"
  675. assert_contains "$preview_section" "skills/brainstorming/scripts/frame-template.html" "Preview includes skill-local frame template"
  676. assert_contains "$preview_section" "skills/brainstorming/scripts/vendor/alpine.js" "Preview includes vendored Alpine"
  677. assert_contains "$preview_section" "skills/brainstorming/scripts/vendor/alpine.provenance.json" "Preview includes Alpine provenance"
  678. assert_contains "$preview_section" "skills/brainstorming/scripts/vendor/THIRD_PARTY_NOTICES.md" "Preview includes Alpine notice"
  679. assert_not_matches "$preview_section" "\\*deleting +skills/example/agents/openai\\.yaml" "Preview preserves destination-owned OpenAI agent metadata"
  680. assert_current_branch "$dest" "$dest_branch" "Preview leaves destination checkout on its original branch"
  681. assert_branch_absent "$dest" "sync/superpowers-*" "Preview does not create sync branch in destination checkout"
  682. echo ""
  683. echo "Mixed-directory assertions..."
  684. assert_equals "$mixed_only_status" "0" "Mixed ignored directory preview exits successfully under /bin/bash"
  685. assert_contains "$mixed_only_output" ".private-journal/keep.txt" "Mixed ignored directory preview still includes tracked ignored file"
  686. assert_not_contains "$mixed_only_output" "ignored-cache/" "Mixed ignored directory preview has no pure ignored directory fixture"
  687. echo ""
  688. echo "Convergence assertions..."
  689. assert_equals "$stale_preview_status" "0" "Stale ignored destination preview exits successfully"
  690. assert_matches "$stale_preview_section" "\\*deleting +\\.private-journal/leak\\.txt" "Preview deletes stale ignored destination file"
  691. echo ""
  692. echo "Bootstrap assertions..."
  693. assert_equals "$bootstrap_status" "0" "Bootstrap preview exits successfully"
  694. assert_contains "$bootstrap_output" "Mode: BOOTSTRAP (creating plugins/superpowers/ when absent)" "Bootstrap preview describes directory creation"
  695. assert_not_contains "$bootstrap_output" "Assets:" "Bootstrap preview omits external assets path"
  696. assert_contains "$bootstrap_output" "Dry run only. Nothing was changed or pushed." "Bootstrap preview remains dry-run only"
  697. assert_path_absent "$bootstrap_dest/plugins/superpowers" "Bootstrap preview does not create destination plugin directory"
  698. assert_current_branch "$bootstrap_dest" "$bootstrap_dest_branch" "Bootstrap preview leaves destination checkout on its original branch"
  699. assert_branch_absent "$bootstrap_dest" "bootstrap/superpowers-*" "Bootstrap preview does not create bootstrap branch in destination checkout"
  700. echo ""
  701. echo "Apply assertions..."
  702. assert_equals "$dirty_apply_status" "1" "Dirty local apply exits with failure"
  703. assert_contains "$dirty_apply_output" "ERROR: local checkout has uncommitted changes under 'plugins/superpowers'" "Dirty local apply reports protected destination path"
  704. assert_current_branch "$dirty_apply_dest" "$dirty_apply_dest_branch" "Dirty local apply leaves destination checkout on its original branch"
  705. assert_branch_absent "$dirty_apply_dest" "sync/superpowers-*" "Dirty local apply does not create sync branch in destination checkout"
  706. assert_file_equals "$dirty_skill_path" "# Example Skill
  707. Locally modified fixture content." "Dirty local apply preserves tracked working-tree file content"
  708. assert_equals "$changed_apply_status" "0" "Changed local apply exits successfully"
  709. assert_contains "$changed_apply_output" "PR opened: https://github.com/prime-radiant-inc/openai-codex-plugins/pull/123" "Changed local apply opens PR through fake gh"
  710. assert_contains "$changed_apply_pr_body" $'tool is behaving.\n\nVendored third-party code included in this sync' "Changed local apply PR body separates vendored section"
  711. assert_contains "$changed_apply_pr_body" "Vendored third-party code included in this sync" "Changed local apply PR body includes vendored section"
  712. assert_contains "$changed_apply_pr_body" "skills/brainstorming/scripts/vendor/alpine.js" "Changed local apply PR body includes vendored Alpine path"
  713. assert_contains "$changed_apply_pr_body" "alpinejs 3.15.12" "Changed local apply PR body includes Alpine package/version"
  714. assert_contains "$changed_apply_pr_body" "Approval artifact: SUP-215" "Changed local apply PR body includes approval artifact"
  715. assert_contains "$changed_apply_pr_body" 'License notice: `skills/brainstorming/scripts/vendor/THIRD_PARTY_NOTICES.md`' "Changed local apply PR body includes license notice path"
  716. assert_contains "$changed_apply_pr_body" 'Provenance: `skills/brainstorming/scripts/vendor/alpine.provenance.json`' "Changed local apply PR body includes provenance path"
  717. assert_contains "$changed_apply_pr_body" 'SHA256: `fixture`' "Changed local apply PR body includes SHA256"
  718. assert_file_equals "$changed_apply_alpine_path" "fixture alpine" "Changed local apply writes vendored Alpine"
  719. assert_file_equals "$changed_apply_alpine_provenance_path" "{\"name\":\"alpinejs\",\"version\":\"3.15.12\",\"localPath\":\"skills/brainstorming/scripts/vendor/alpine.js\",\"sha256\":\"fixture\",\"approvalArtifact\":\"SUP-215\"}" "Changed local apply writes Alpine provenance"
  720. assert_contains "$(cat "$changed_apply_alpine_notice_path")" "Alpine.js fixture notice." "Changed local apply writes Alpine notice"
  721. assert_equals "$bootstrap_apply_status" "0" "Bootstrap local apply exits successfully"
  722. assert_contains "$bootstrap_apply_output" "PR opened: https://github.com/prime-radiant-inc/openai-codex-plugins/pull/123" "Bootstrap local apply opens PR through fake gh"
  723. assert_contains "$bootstrap_apply_pr_body" "Vendored third-party code included in this sync" "Bootstrap local apply PR body includes vendored section"
  724. assert_contains "$bootstrap_apply_pr_body" "Approval artifact: SUP-215" "Bootstrap local apply PR body includes approval artifact"
  725. assert_equals "$noop_apply_status" "0" "Clean no-op local apply exits successfully"
  726. assert_contains "$noop_apply_output" "No changes — embedded plugin was already in sync with upstream" "Clean no-op local apply reports no changes"
  727. assert_current_branch "$noop_apply_dest" "$noop_apply_dest_branch" "Clean no-op local apply leaves destination checkout on its original branch"
  728. assert_branch_absent "$noop_apply_dest" "sync/superpowers-*" "Clean no-op local apply does not create sync branch in destination checkout"
  729. assert_file_equals "$noop_openai_metadata_path" "interface:
  730. display_name: \"Example\"
  731. short_description: \"Destination-owned OpenAI metadata\"" "Clean no-op local apply preserves OpenAI agent metadata"
  732. assert_file_equals "$noop_alpine_path" "fixture alpine" "Clean no-op local apply preserves vendored Alpine"
  733. assert_file_equals "$noop_alpine_provenance_path" "{\"name\":\"alpinejs\",\"version\":\"3.15.12\",\"localPath\":\"skills/brainstorming/scripts/vendor/alpine.js\",\"sha256\":\"fixture\",\"approvalArtifact\":\"SUP-215\"}" "Clean no-op local apply preserves Alpine provenance"
  734. assert_contains "$(cat "$noop_alpine_notice_path")" "Alpine.js fixture notice." "Clean no-op local apply preserves Alpine notice"
  735. echo ""
  736. echo "Missing manifest assertions..."
  737. assert_equals "$missing_manifest_status" "1" "Missing manifest exits with failure"
  738. assert_contains "$missing_manifest_output" "ERROR: committed Codex manifest missing at" "Missing manifest reports committed manifest path"
  739. echo ""
  740. echo "Help assertions..."
  741. assert_not_contains "$help_output" "--assets-src" "Help omits --assets-src"
  742. echo ""
  743. echo "Source assertions..."
  744. assert_not_contains "$script_source" "regenerated inline" "Source drops regenerated inline phrasing"
  745. assert_not_contains "$script_source" "Brand Assets directory" "Source drops Brand Assets directory phrasing"
  746. assert_not_contains "$script_source" "--assets-src" "Source drops --assets-src"
  747. assert_contains "$script_source" "Vendored third-party code included in this sync" "Source calls out vendored third-party code in sync PR body"
  748. if [[ $FAILURES -ne 0 ]]; then
  749. echo ""
  750. echo "FAILED: $FAILURES assertion(s) failed."
  751. exit 1
  752. fi
  753. echo ""
  754. echo "PASS"
  755. }
  756. main "$@"