Skip to content

環状路線のETA取得時にdirectionIdを明示的に指定して進行方向の弧を確定させる - #6334

Merged
TinyKitten merged 2 commits into
devfrom
claude/graphql-eta-directionid-e9ov0b
Jul 3, 2026
Merged

TinyKitten merged 2 commits into
devfrom
claude/graphql-eta-directionid-e9ov0b

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

概要

環状路線(山手線・大阪環状線・名城線など)でETAを取得する際、EstimateArrivalTimes クエリに directionId を明示的に指定するようにする。

StationAPI 側の TrainLCD/StationAPI#1581 で環状路線の弧選択が実装され、direction_id 未指定時は「直線距離が短い方の弧」を選ぶヒューリスティックにフォールバックするため、遠回り方向に乗車中は逆回りのETAが返ることがある。同PRで推奨されているとおり、クライアントから進行方向を明示して送ることでこれを防ぐ。

変更の種類

  • バグ修正
  • 新機能
  • リファクタリング
  • ドキュメント
  • CI/CD
  • その他

変更内容

  • ESTIMATE_ARRIVAL_TIMES クエリに $directionId: Int 変数を追加(src/lib/graphql/queries.ts)
  • useEstimateArrivalTimes で useLoopLine() の isLoopLine が true かつ方面選択済みのときだけ directionId を送信するように変更。非環状路線では undefined のためシリアライズ時に落ち、従来どおり未指定で送信される(src/hooks/useEstimateArrivalTimes.ts)
  • マッピングは StationAPI の proto 定義(EstimateArrivalTimesRequest.direction_id: 0 = default order, 1 = reverse order)に基づき、stations 配列の格納順に進む INBOUND → 0、逆順に進む OUTBOUND → 1。INBOUND でも 0 を明示的に送らないとバックエンドが短い弧ヒューリスティックに落ちるため、環状路線では両方向とも必ず送信する
  • 生成型 EstimateArrivalTimesQueryVariables に directionId を追加(src/@types/graphql.d.ts。環境に GQL_API_URL が無く npm run gql:codegen を実行できないため、codegen の出力形式に合わせて手動反映。次回 codegen 実行で同一内容が再生成される想定)
  • テスト4本追加: 非環状路線では directionId を送信しない / 環状路線かつ INBOUND で 0 / OUTBOUND で 1 / 環状路線でも方面未選択なら送信しない(src/hooks/useEstimateArrivalTimes.test.tsx)

テスト

ローカルで npm run lint && npm test && npm run typecheck を実行し、すべて通過。

  • npm run lint が通ること
  • npm test が通ること
  • npm run typecheck が通ること

関連Issue

参考: TrainLCD/StationAPI#1581

スクリーンショット(任意)

Summary by CodeRabbit

  • 新機能
    • 推定到着時刻の取得で、進行方向IDを任意に指定できるようになりました。
    • 環状路線では、選択中の方向に応じて適切な値が自動で送信されます(未選択時は送信されません)。
  • バグ修正
    • 環状路線で逆回り方向の予測が返る可能性を低減しました。
  • テスト
    • 進行方向の有無と、環状路線(INBOUND/OUTBOUND/未選択)の送信内容を検証するテストを追加しました。

@github-actions github-actions Bot added the react label Jul 3, 2026
@TinyKitten TinyKitten self-assigned this Jul 3, 2026
@coderabbitai

coderabbitai Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

EstimateArrivalTimes の GraphQL クエリと型定義に directionId 変数を追加し、useEstimateArrivalTimes フックで環状路線判定と選択方向から directionId を算出してクエリに渡すよう変更。対応するテストケースも追加された。

Changes

directionId 送信対応

Layer / File(s) Summary
GraphQL クエリと型定義の更新
src/lib/graphql/queries.ts, src/@types/graphql.d.ts
ESTIMATE_ARRIVAL_TIMES クエリに $directionId: Int 変数と directionId: $directionId 引数を追加し、対応する型定義にも directionId フィールドを追加。
フック内 directionId 決定ロジック
src/hooks/useEstimateArrivalTimes.ts
LOOP_LINE_DIRECTION_ID 定数を定義し、useLoopLine による環状路線判定と selectedDirection から directionId を算出、クエリ variables に含める。
directionId 送信テスト
src/hooks/useEstimateArrivalTimes.test.tsx
useLoopLine をモック化し、環状路線でない場合・環状かつ INBOUND・環状かつ OUTBOUND・方面未選択の各条件で directionId の送信有無と値を検証するテストを追加。

Estimated code review effort: 2 (Simple) | ~12 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Component
  participant useEstimateArrivalTimes
  participant useLoopLine
  participant GraphQLAPI

  Component->>useEstimateArrivalTimes: selectedDirection を渡す
  useEstimateArrivalTimes->>useLoopLine: isLoopLine 判定
  useLoopLine-->>useEstimateArrivalTimes: isLoopLine
  useEstimateArrivalTimes->>useEstimateArrivalTimes: directionId 算出(LOOP_LINE_DIRECTION_ID)
  useEstimateArrivalTimes->>GraphQLAPI: ESTIMATE_ARRIVAL_TIMES(variables含むdirectionId)
  GraphQLAPI-->>useEstimateArrivalTimes: 推定到着時刻レスポンス
Loading

Possibly related PRs

  • TrainLCD/MobileApp#6050: useEstimateArrivalTimes が依存する useLoopLine の環状路線判定ロジックを変更しており、directionId 算出の挙動に連動する。
  • TrainLCD/MobileApp#6320: 同じ ESTIMATE_ARRIVAL_TIMES クエリと useEstimateArrivalTimes フック(およびそのテスト)を対象としており、本PRはその実装を directionId 対応に拡張している。

Poem

ぴょんと跳ねて 方向決める
環状線なら 0か1
クエリに乗せて 届けよう
うさぎも回る 駅めぐり
directionId で ぴったり到着 🐰🚃

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed 環状路線でETA取得時にdirectionIdを明示する変更を端的に表しており、主要な変更内容と一致しています。
Description check ✅ Passed 概要、変更の種類、変更内容、テストが揃っており、テンプレートの必須項目を概ね満たしています。
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/graphql-eta-directionid-e9ov0b

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/hooks/useEstimateArrivalTimes.test.tsx (1)

199-251: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

環状路線かつ方面未選択のケースのテストが未追加。

directionId の算出条件は isLoopLine && selectedDirection != null ですが、現在のテストは「非環状」「環状+INBOUND」「環状+OUTBOUND」の3パターンのみで、「環状路線だが selectedDirection が null(方面未選択)」のケースが未検証です。この分岐が正しく directionId: undefined になることを確認するテストを追加すると、条件式の両方の要素(isLoopLine と selectedDirection != null)がそれぞれ独立してテストされ、より安心です。

As per path instructions: "When modifying behavior, update or add tests in the same change set; document skipped tests with TODOs and owner rationale."

♻️ 追加テスト案
+  it('環状路線でも方面未選択のとき directionId を送信しない', async () => {
+    mockUseLoopLine.mockReturnValue({
+      isLoopLine: true,
+    } as ReturnType<typeof useLoopLine>);
+    setupAtoms({ selectedDirection: null });
+    mockGqlRequest.mockResolvedValue({
+      estimateArrivalTimes: { routes: [] },
+    });
+
+    renderHook();
+
+    await waitFor(() => {
+      expect(mockGqlRequest).toHaveBeenCalled();
+    });
+
+    const variables = mockGqlRequest.mock.calls[0][1];
+    expect(variables.directionId).toBeUndefined();
+  });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/hooks/useEstimateArrivalTimes.test.tsx` around lines 199 - 251, The
current tests cover non-loop, loop+INBOUND, and loop+OUTBOUND cases, but miss
the branch where useLoopLine().isLoopLine is true and selectedDirection is null.
Add a test in useEstimateArrivalTimes.test.tsx that sets mockUseLoopLine to loop
mode without setting selectedDirection, then assert the gql request variables
from mockGqlRequest include directionId as undefined. This should verify the
isLoopLine && selectedDirection != null logic in the hook’s directionId
calculation alongside the existing direction cases.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/hooks/useEstimateArrivalTimes.test.tsx`:
- Around line 199-251: The current tests cover non-loop, loop+INBOUND, and
loop+OUTBOUND cases, but miss the branch where useLoopLine().isLoopLine is true
and selectedDirection is null. Add a test in useEstimateArrivalTimes.test.tsx
that sets mockUseLoopLine to loop mode without setting selectedDirection, then
assert the gql request variables from mockGqlRequest include directionId as
undefined. This should verify the isLoopLine && selectedDirection != null logic
in the hook’s directionId calculation alongside the existing direction cases.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7b232493-a0fa-4374-849f-afadeb77e61f

📥 Commits

Reviewing files that changed from the base of the PR and between 12f25d4 and 87c2696.

📒 Files selected for processing (4)
  • src/@types/graphql.d.ts
  • src/hooks/useEstimateArrivalTimes.test.tsx
  • src/hooks/useEstimateArrivalTimes.ts
  • src/lib/graphql/queries.ts

@TinyKitten
TinyKitten merged commit b188f47 into dev Jul 3, 2026
5 checks passed
@TinyKitten
TinyKitten deleted the claude/graphql-eta-directionid-e9ov0b branch July 3, 2026 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants