Skip to content

ci: 修正環境相依測試 + 新增 GitHub Actions CI#1

Merged
poterpan merged 3 commits into
mainfrom
ci/fix-env-dependent-tests-and-add-ci
Jun 7, 2026
Merged

ci: 修正環境相依測試 + 新增 GitHub Actions CI#1
poterpan merged 3 commits into
mainfrom
ci/fix-env-dependent-tests-and-add-ci

Conversation

@poterpan

@poterpan poterpan commented Jun 7, 2026

Copy link
Copy Markdown
Owner

背景

夥伴在乾淨本地環境跑 pnpm test 時有 8 個測試失敗。經 systematic-debugging 調查 + Codex 獨立複核(高信心),確認沒有任何產品程式 bug,失敗源於兩個與環境/時間相依的測試問題。本 PR 修正測試,並補上 CI 防止類似問題再次溜進來。

根因與修正

A. 7 個失敗 — 測試依賴 gitignored 的 .dev.vars

送通知的程式碼 gate 在 env.DISCORD_BOT_TOKENscheduled.tsbilling.tsadmin.ts)。該 token 只透過本地 .dev.vars(被 gitignore、含真實 token)提供;乾淨 clone / CI 沒有此檔 → token undefined → 斷言「通知有送出/slot 被佔」的 7 個測試失敗。

  • billing-initiate / scheduledbeforeAll 設假 token(notifier 是假的,零網路)。沿用 discord-initiate / discord-notify 既有範式。
  • admin只在兩個已 stub fetch 的測試內設 token 並還原,避免污染後面用真 discordNotifier、未 stub fetch 的 billing/initiate 測試(否則會打出真實 Discord 請求)。

B. 1 個失敗 — 時間相依測試

discord-initiate > "opens a modal"taipeiPeriod()(當前月)斷言,但「發起繳費」modal 實際用 nextBillingPeriod(billing_day),過了 billing day 會進位到下月。舊假設只在每月 1–5 號成立。改用 nextBillingPeriod(5) 與 handler 對齊。

文件

新增 packages/worker/.dev.vars.example 範本,並於 docs/DEPLOY.md §6 註明:跑測試不需要 .dev.vars

CI(本 PR 第二個 commit)

新增 .github/workflows/ci.yml:PR 與 push 到 main 時跑 pnpm install --frozen-lockfilepnpm -r typecheck / test / build(涵蓋 worker/web/admin)。紅燈擋合併。本次不含 CD(部署仍走 Cloudflare 網頁介面)。

附帶修正:把 pnpm 的 onlyBuiltDependenciespackage.jsonpnpm 欄位搬到 pnpm-workspace.yaml——pnpm 10 已不再讀前者(設定失效並有警告),否則 CI 全新安裝會跳過編譯 esbuild/workerd/sharp,導致測試池與 vite build 失敗。

驗證(皆實測)

  • 移走 .dev.vars(= CI/夥伴環境):31 files, 158 passed, 0 failed(修正前為 8 failed)
  • 保留 .dev.vars(本地開發):158 passed, 0 failed
  • pnpm install --frozen-lockfile:lockfile 同步
  • pnpm -r typecheck / build:全綠(pnpm -r build 對無 build script 的 worker 優雅跳過,exit 0)

🤖 Generated with Claude Code

poterpan added 3 commits June 7, 2026 17:32
…eriod test

The suite assumed a local .dev.vars (gitignored, holds a real bot token) supplied
DISCORD_BOT_TOKEN. On a clean clone / in CI the token is undefined, so 7 tests whose
assertions depend on a notification actually being sent/claimed failed — sending is gated
on env.DISCORD_BOT_TOKEN (scheduled.ts, billing.ts, admin.ts).

- billing-initiate / scheduled: set a dummy DISCORD_BOT_TOKEN in beforeAll (fake notifier,
  no network). Mirrors the existing pattern in discord-initiate / discord-notify tests.
- admin: set+restore the token only inside the two fetch-stubbed tests, so the later
  billing/initiate test (real discordNotifier, no fetch stub) keeps its no-real-send behavior.
- discord-initiate 'opens a modal': compute PERIOD via nextBillingPeriod(5) to match the
  handler instead of taipeiPeriod() — the old assumption only held on days 1–5 of a month.
- add .dev.vars.example template + DEPLOY.md note that tests don't need .dev.vars.

Verified: full suite green both with and without .dev.vars present (158 passed).
Runs on PRs and pushes to main: pnpm install --frozen-lockfile, then
`pnpm -r` typecheck / test / build across the worker/web/admin packages.
A red run blocks merge — this is exactly the class of breakage (env-dependent
tests, type errors, build failures) that previously slipped through.

Also move pnpm's onlyBuiltDependencies from package.json's "pnpm" field to
pnpm-workspace.yaml: pnpm 10 no longer reads the former (it logged a warning and
the setting was inert), so a fresh CI install would skip building esbuild/workerd/
sharp and break the test pool + vite builds. Add a root "build" script for symmetry.

Verified locally: frozen-lockfile install in sync; typecheck/test/build all green
with .dev.vars absent (the CI condition).
actions/checkout, actions/setup-node, pnpm/action-setup all run on Node.js 20 at
their v4 pins; GitHub forces JS actions to Node 24 on 2026-06-16. v6 of each runs
on node24, clearing the deprecation warning.

Verified safe for this workflow:
- checkout v6: bare usage (no inputs), only internal cred-handling change.
- setup-node v6: the v6 breaking change limits *automatic* caching to npm; we pass
  an explicit `cache: pnpm` (still a supported value), unaffected. pnpm/action-setup
  still runs first so pnpm is on PATH for the cache step.
- pnpm/action-setup v6: version still auto-detected from packageManager; adds pnpm 11.

Node 22 (app runtime) unchanged — the deprecation was about the action runtime, not it.
@poterpan poterpan merged commit 944079b into main Jun 7, 2026
2 checks passed
@poterpan poterpan deleted the ci/fix-env-dependent-tests-and-add-ci branch June 7, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant