Skip to content

test(worker,shared): unit tests for summarize, rate-limit, github-ip core logic (patch)#224

Merged
liplus-lin-lay merged 1 commit into
mainfrom
223-core-logic-tests
Jun 21, 2026
Merged

test(worker,shared): unit tests for summarize, rate-limit, github-ip core logic (patch)#224
liplus-lin-lay merged 1 commit into
mainfrom
223-core-logic-tests

Conversation

@liplus-lin-lay

Copy link
Copy Markdown
Member

Closes #223

概要

webhook 本流の核心ロジックに単体テストを追加。これまでテストは OAuth (worker/oauth.ts) と mcp-server クライアント契約に偏っており、payload 要約・レート制限・IP 許可リストはテストの外だった。新インフラを足さず、既存ハーネス (tsx --test) で決定的にテストできる純ロジック層をカバーする。

影響スコープ

  • テスト追加のみ(新規ファイル3本)+ worker/src/github-ip.ts に visibility 追加の export 1行。ランタイム挙動の変更なし。
  • ユーザー/システムから観測可能な変更なし → patch

追加テスト(34本、全47本 pass)

ファイル 対象 主なケース
worker/test/summarize.test.ts shared/summarize.ts number/title/url のフォールバック階層、workflow_run 固有フィールドの type ゲート、空 payload の null 安全、各種 passthrough
worker/test/rate-limit.test.ts worker/rate-limit.ts sliding window (webhook 300 / api 120)、窓リセット、checkTenantQuota の 200/429/404 分岐、rateLimitResponse
worker/test/github-ip.test.ts worker/github-ip.ts CIDR 一致の境界・/32・/0・IPv6 CIDR skip・IPv6/不正IP passthrough、isGitHubWebhookIP の fallback 経路

リファクタ

github-ip.ts の内部純関数 ipMatchesCIDRsexport(CIDR 演算を直接単体テストするため)。ロジック不変。

スコープ外(別 issue 予定)

worker/src/store.ts / tenant.ts は Durable Object + ctx.storage.sqltsx --test では実行不可。本格テストには vitest-pool-workers(Miniflare) 導入=テストハーネス変更+workers-types ピン留めが必要なため分離する。

確認

  • npm test (worker): 47 pass / 0 fail
  • npx wrangler deploy --dry-run: build 成功

本流の核心ロジックに単体テストを追加する。これまで OAuth と mcp-server
クライアント契約に偏っていたカバレッジを是正し、webhook の payload 要約・
レート制限・IP 許可リストの純ロジックを既存ハーネスでカバーする。

- shared/summarize.ts: summarizeEvent の number/title/url フォールバック階層、
  workflow_run 固有フィールドの type ゲート、空 payload の null 安全
- worker/rate-limit.ts: sliding window (webhook 300 / api 120)、窓リセット、
  checkTenantQuota の 200/429/404 分岐、rateLimitResponse
- worker/github-ip.ts: CIDR 一致判定の境界・/32・/0・IPv6 passthrough、
  isGitHubWebhookIP の fallback 経路。テスト容易化のため ipMatchesCIDRs を export

新インフラ追加なし。既存の `tsx --test` で 34 本追加し全 47 本 pass。
Durable Object 層 (store/tenant) は Miniflare が要るため別 issue に分離。

Refs #223
@liplus-lin-lay liplus-lin-lay added this to the v0.11.6 milestone Jun 21, 2026
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
github-webhook-mcp 72d7c0d Jun 21 2026, 04:03 AM

@liplus-lin-lay liplus-lin-lay left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI セルフレビュー (auto mode)

検証結果

  • npm test (worker): 47 pass / 0 fail(新規34本込み)。CI gate test pass、CI gate pass、Workers Builds pass。
  • wrangler deploy --dry-run: build 成功(github-ip.ts の export 追加が build を壊さないことを確認)。

妥当性(now: 問題なし)

  • テストは実コードの分岐を反映:summarize の fallback 階層・workflow_run type ゲート、rate-limit の境界(300/120)・窓リセット・quota 200/429/404、github-ip の CIDR 境界/IPv6 passthrough。
  • 決定性を確保:github-ip は global fetch を stub して FALLBACK 経路に固定(実ネット不使用)、rate-limit の窓リセットは Date.now を mock し finally で復元。
  • ipMatchesCIDRs の export は visibility のみ、ロジック不変。
  • ランタイム挙動の変更なし → patch 分類は妥当(observable impact 無し)。

留意(later: 別 issue)

  • store.ts / tenant.ts(Durable Object + ctx.storage.sql)、index.ts の fetch handler、verifyGitHubSignaturetsx --test で実行不可。Miniflare(vitest-pool-workers)導入が要るため本 PR スコープ外。フォローアップ issue を起票予定。

受容済みトレードオフ (accepted)

  • summarize.test.tsshared 専用ハーネスではなく worker/test に配置。新インフラ回避のため(worker は summarize を実依存)。

self-review pass。auto mode に従い squash merge する。

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.

test(worker,shared): add unit tests for core logic (summarize, rate-limit, github-ip)

1 participant