處理 AI review findings 並改寫 Node.js entrypoint #1

Merged
jiantw83 merged 58 commits from ai-review-resolve/20260624102518 into develop 2026-06-24 14:09:27 +00:00
Showing only changes of commit ed4ac005b2 - Show all commits
+145
View File
@@ -0,0 +1,145 @@
#!/bin/bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
fail() {
Ghost marked this conversation as resolved Outdated
Outdated
Review

嚴重等級🔵 建議
審查員:Bard
問題:專案在 entrypoint.sh 中使用 die 函數處理致命錯誤,但測試腳本中卻定義了名稱不同的 fail 函數,這使得專案內的錯誤處理語彙不夠一致,略顯突兀。
建議:建議將 tests/entrypoint_test.sh 中的 fail 函數更名為 die,使錯誤處理的語彙在專案各處保持一致,讓樂章的節奏更為統一。

**嚴重等級**:🔵 建議 **審查員**:Bard **問題**:專案在 `entrypoint.sh` 中使用 `die` 函數處理致命錯誤,但測試腳本中卻定義了名稱不同的 `fail` 函數,這使得專案內的錯誤處理語彙不夠一致,略顯突兀。 **建議**:建議將 `tests/entrypoint_test.sh` 中的 `fail` 函數更名為 `die`,使錯誤處理的語彙在專案各處保持一致,讓樂章的節奏更為統一。
echo "FAIL: $1" >&2
exit 1
}
make_fake_codex() {
local dir="$1"
local exit_code="$2"
local message="$3"
cat > "$dir/codex" <<SH
Ghost marked this conversation as resolved
Review

嚴重等級🟡 警告
審查員:Bard
問題:在測試中建立 codex 指令時,Here-document 與命令混雜,可讀性較低。
建議:建議使用更整齊的縮排格式,或將其抽離為獨立的測試輔助檔案。

**嚴重等級**:🟡 警告 **審查員**:Bard **問題**:在測試中建立 `codex` 指令時,Here-document 與命令混雜,可讀性較低。 **建議**:建議使用更整齊的縮排格式,或將其抽離為獨立的測試輔助檔案。
#!/bin/sh
echo "$message"
exit $exit_code
SH
chmod +x "$dir/codex"
}
encoded_json() {
printf '%s' "$1" | base64 | tr -d '\n'
}
run_entrypoint() {
local tmpdir="$1"
shift
env \
PATH="$tmpdir:$PATH" \
CODEX_HOME="$tmpdir/codex-home" \
GITHUB_OUTPUT="$tmpdir/github-output" \
"$@" \
bash "$ROOT_DIR/entrypoint.sh" > "$tmpdir/stdout" 2> "$tmpdir/stderr"
}
assert_status() {
local actual="$1"
local expected="$2"
[[ "$actual" -eq "$expected" ]] || fail "expected status $expected, got $actual"
}
test_missing_oauth() {
local tmpdir
tmpdir="$(mktemp -d)"
make_fake_codex "$tmpdir" 0 "unused"
set +e
run_entrypoint "$tmpdir" MODEL="gpt-test" PROMPT="hello"
local status="$?"
set -e
assert_status "$status" 1
grep -q "OAUTH is required" "$tmpdir/stderr" || fail "missing OAUTH error"
rm -rf "$tmpdir"
}
test_missing_model() {
local tmpdir
tmpdir="$(mktemp -d)"
Ghost marked this conversation as resolved
Review

嚴重等級🔵 建議
審查員:Maya
問題:測試案例對於輸入參數的邊界測試(例如 PROMPT 為空字串、極長字串)不足。
建議:增加針對 PROMPT 輸入為空字串或是包含特殊 Shell 跳脫字元的測試案例。

**嚴重等級**:🔵 建議 **審查員**:Maya **問題**:測試案例對於輸入參數的邊界測試(例如 `PROMPT` 為空字串、極長字串)不足。 **建議**:增加針對 `PROMPT` 輸入為空字串或是包含特殊 Shell 跳脫字元的測試案例。
make_fake_codex "$tmpdir" 0 "unused"
set +e
run_entrypoint "$tmpdir" OAUTH="$(encoded_json '{}')" PROMPT="hello"
local status="$?"
set -e
assert_status "$status" 1
grep -q "MODEL is required" "$tmpdir/stderr" || fail "missing MODEL error"
rm -rf "$tmpdir"
}
test_invalid_base64() {
local tmpdir
tmpdir="$(mktemp -d)"
make_fake_codex "$tmpdir" 0 "unused"
set +e
run_entrypoint "$tmpdir" OAUTH="not-base64" MODEL="gpt-test" PROMPT="hello"
local status="$?"
set -e
assert_status "$status" 1
grep -q "valid base64" "$tmpdir/stderr" || fail "invalid base64 error"
rm -rf "$tmpdir"
}
test_non_object_json() {
local tmpdir
tmpdir="$(mktemp -d)"
make_fake_codex "$tmpdir" 0 "unused"
set +e
run_entrypoint "$tmpdir" OAUTH="$(encoded_json '[]')" MODEL="gpt-test" PROMPT="hello"
local status="$?"
set -e
assert_status "$status" 1
grep -q "JSON object" "$tmpdir/stderr" || fail "non-object JSON error"
rm -rf "$tmpdir"
}
test_success_output() {
local tmpdir
tmpdir="$(mktemp -d)"
make_fake_codex "$tmpdir" 0 "codex ok"
run_entrypoint "$tmpdir" OAUTH="$(encoded_json '{}')" MODEL="gpt-test" PROMPT="hello"
grep -q "status=completed" "$tmpdir/github-output" || fail "completed status output"
grep -q "codex ok" "$tmpdir/github-output" || fail "codex output"
[[ ! -e "$tmpdir/codex-home/auth.json" ]] || fail "auth.json was not cleaned up"
rm -rf "$tmpdir"
}
test_failure_output() {
local tmpdir
tmpdir="$(mktemp -d)"
make_fake_codex "$tmpdir" 7 "codex failed"
set +e
run_entrypoint "$tmpdir" OAUTH="$(encoded_json '{}')" MODEL="gpt-test" PROMPT="hello"
local status="$?"
set -e
assert_status "$status" 7
grep -q "status=failed" "$tmpdir/github-output" || fail "failed status output"
grep -q "codex failed" "$tmpdir/github-output" || fail "failed codex output"
[[ ! -e "$tmpdir/codex-home/auth.json" ]] || fail "auth.json was not cleaned up after failure"
rm -rf "$tmpdir"
}
test_missing_oauth
test_missing_model
test_invalid_base64
test_non_object_json
test_success_output
test_failure_output
echo "entrypoint tests passed"