Skip to content

test: 100% coverage for index.js + close categoryNo numeric validation gap - #14

Merged
terry90918 merged 1 commit into
developfrom
claude/init-58da86
Jul 11, 2026
Merged

test: 100% coverage for index.js + close categoryNo numeric validation gap#14
terry90918 merged 1 commit into
developfrom
claude/init-58da86

Conversation

@terry90918

@terry90918 terry90918 commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

Note: this PR was reworked after develop completed a full TypeScript + Vitest migration mid-review (the original diff targeted the now-obsolete CommonJS/Jest codebase). The two logical changes below are the same intent, ported onto the current src/*.ts / Vitest test suite.

  • Wire the previously-unported validateInput.numericString into list_resources (src/tools.ts), rejecting a non-digit categoryNo before the upstream request. Requires typeof value === 'string' before the regex test, since RegExp.test() silently coerces non-string input (e.g. a JSON number) — caught in review, see reply below.
  • Add the matching "pattern": "^\d+$" to categoryNo's inputSchema, and update the openspec/specs/open-data.md GIVEN/WHEN/THEN + JSON schema example accordingly.
  • Bring src/index.ts and src/server.ts — previously untested entry-point/bootstrap code — to 100% coverage: new __tests__/index.test.ts (console redirection + safeSerialize branches), new __tests__/server-startup-failure.test.ts (server.connect() rejecting → process.exit(1)), and an extended __tests__/server.test.ts (registered ListTools/CallTool handlers, server.onerror, main()'s transport/connect call).
  • Close a few remaining branch gaps in __tests__/tools.test.ts / __tests__/response.test.ts (missing content-type header, createBlobResponse's Buffer-vs-string branch, createErrorResponse's String(error) fallback).
  • Correct src/tools.ts:N line references across openspec/specs/*.md that shifted because of the new code.

src/index.ts, src/response.ts, src/server.ts, src/tools.ts are now all at 100% coverage (bin/judicial-mcp.ts's one-line re-export remains untested, pre-existing and out of scope here).

Review

Ran /code-review (multi-angle + verification) both before the original diff and again after the TS rework. Findings addressed in the rework:

  • numericString's implicit type coercion via RegExp.test() (also independently flagged by Copilot on this PR) — fixed with an explicit typeof check + test.
  • __tests__/index.test.ts's original structure had each test redundantly re-import()ing src/index.ts, relying on ESM module caching so only the first import actually ran — restructured to import once in beforeAll with a shared beforeEach/afterEach spy.
  • A weak assertion in the circular-reference test (String(circularObj) coincides with String(plainObj), so it didn't actually prove the catch branch ran) — strengthened by asserting JSON.stringify was called and threw.

Deferred (accepted tradeoff, not blocking): __tests__/server.test.ts/__tests__/server-startup-failure.test.ts mock the MCP SDK's Server/StdioServerTransport classes directly rather than using the SDK's InMemoryTransport.createLinkedPair() to drive a real Client/Server pair. Real but larger-scope improvement for a future PR.

Test plan

  • bun run typecheck — clean
  • bun run test:coverage — 77 tests pass, 100% coverage on src/index.ts, src/response.ts, src/server.ts, src/tools.ts
  • bun run lint — clean

Copilot AI review requested due to automatic review settings July 11, 2026 05:29
@terry90918 terry90918 added the enhancement New feature or request label Jul 11, 2026
@terry90918 terry90918 self-assigned this Jul 11, 2026
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (1)
  • ^main$

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4a28bb1d-c1cd-4e8a-9619-9cdd1d662aa6

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/init-58da86

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

此 PR 針對 judicial-mcp 的開放資料工具做輸入驗證補強,並補齊入口 src/index.js 的測試覆蓋,讓核心模組(index.js/tools.js/response.js)達到完整覆蓋,同時同步更新 openspec 文件與型別註記。

Changes:

  • 新增 __tests__/index.test.js,覆蓋 MCP 伺服器入口的 handler 註冊、onerror、啟動成功/失敗路徑。
  • list_resources 補上 validateInput.numericString(categoryNo) 的 runtime 驗證,並同步更新 schema pattern 與 openspec 範例。
  • 補齊 download_file 缺少 content-type header 的 fallback 測試,並調整/擴充 response 測試覆蓋。

Reviewed changes

Copilot reviewed 7 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
types/index.d.ts list_resources.categoryNo 補上 runtime 驗證註解,降低型別/行為誤解
src/tools.js list_resources 增加 categoryNo 純數字驗證;schema 加上 pattern;額外 export validateInput
openspec/specs/open-data.md 同步補上 numericString 驗證步驟與 schema pattern,移除已修補的 Known Gap 說明
openspec/changes/typescript-migration/design.md 新增 TS/ESM/Vitest 遷移設計文件(與本 PR 主題關聯度需釐清)
CLAUDE.md 更新測試檔案清單與說明,補充 index.test.js 的 mock 範圍
bun.lock 新增/提交 Bun lockfile 以固定依賴解析結果
tests/tools.test.js 配合 categoryNo 新驗證更新測資,並新增 numericStringcontent-type fallback 測試
tests/response.test.js 增加 createErrorResponsecreateBlobResponse 的分支覆蓋
tests/index.test.js 新增入口測試,驗證 ListTools/CallTool/onerror 與啟動失敗處理

Comment thread src/tools.js Outdated
Comment thread openspec/changes/typescript-migration/design.md
…egoryNo numeric validation

Rebases the prior JS/Jest-era PR onto the TypeScript/Vitest codebase that
develop migrated to in the meantime. Ports the same two logical changes:

- src/tools.ts: add validateInput.numericString and wire it into
  list_resources, rejecting a non-digit categoryNo before the upstream
  request (closing the gap already present in the migrated code — the JS
  version's numericString validator never survived the TS port). Also add
  the matching "pattern": "^\d+$" to categoryNo's inputSchema. The check
  requires typeof value === 'string' before the regex test, since
  RegExp.test() silently coerces non-string input (e.g. a JSON number).
- __tests__/index.test.ts (new): covers src/index.ts's console.log/info/warn
  -> stderr redirection and the safeSerialize helper's branches (string,
  JSON.stringify success, undefined-returning values, circular references).
- __tests__/server-startup-failure.test.ts (new): covers server.connect()
  rejecting -> process.exit(1), in its own file with static top-level mocks
  rather than vi.resetModules()/vi.doMock() in a shared file.
- __tests__/server.test.ts (extended): covers the registered ListTools/
  CallTool handlers (including the non-Error-throw branch), server.onerror,
  and that main() constructed a transport and called server.connect().
- __tests__/tools.test.ts, __tests__/response.test.ts: close remaining
  branch gaps (missing content-type header, createBlobResponse's
  Buffer-vs-string branch, createErrorResponse's String(error) fallback).
- openspec/specs/*.md: document the new validateInput.numericString step,
  add the schema pattern example, and correct src/tools.ts:N line references
  that shifted because of the new code.

src/, response.ts, server.ts, tools.ts, index.ts now at 100% coverage
(bin/judicial-mcp.ts's one-line re-export remains untested, pre-existing
and out of scope here).
@terry90918
terry90918 force-pushed the claude/init-58da86 branch from 618f8d2 to 215b8db Compare July 11, 2026 05:57
@terry90918
terry90918 merged commit 46511d0 into develop Jul 11, 2026
3 checks passed
@terry90918
terry90918 deleted the claude/init-58da86 branch July 11, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants