ci: 修正環境相依測試 + 新增 GitHub Actions CI#1
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
夥伴在乾淨本地環境跑
pnpm test時有 8 個測試失敗。經 systematic-debugging 調查 + Codex 獨立複核(高信心),確認沒有任何產品程式 bug,失敗源於兩個與環境/時間相依的測試問題。本 PR 修正測試,並補上 CI 防止類似問題再次溜進來。根因與修正
A. 7 個失敗 — 測試依賴 gitignored 的
.dev.vars送通知的程式碼 gate 在
env.DISCORD_BOT_TOKEN(scheduled.ts、billing.ts、admin.ts)。該 token 只透過本地.dev.vars(被 gitignore、含真實 token)提供;乾淨 clone / CI 沒有此檔 → token undefined → 斷言「通知有送出/slot 被佔」的 7 個測試失敗。billing-initiate/scheduled:beforeAll設假 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-lockfile→pnpm -rtypecheck / test / build(涵蓋 worker/web/admin)。紅燈擋合併。本次不含 CD(部署仍走 Cloudflare 網頁介面)。附帶修正:把 pnpm 的
onlyBuiltDependencies從package.json的pnpm欄位搬到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 failedpnpm install --frozen-lockfile:lockfile 同步pnpm -r typecheck/build:全綠(pnpm -r build對無 build script 的 worker 優雅跳過,exit 0)🤖 Generated with Claude Code