Skip to content

(refs #469) refactor tile choosing function#470

Open
niryuu wants to merge 1 commit intogeolonia:masterfrom
niryuu:469-refactor-tile-selection
Open

(refs #469) refactor tile choosing function#470
niryuu wants to merge 1 commit intogeolonia:masterfrom
niryuu:469-refactor-tile-selection

Conversation

@niryuu
Copy link
Contributor

@niryuu niryuu commented Mar 13, 2026

Fixes #469

Summary

Refactored tile selection function in geolonia-map.ts

Checklist (optional)

  • Tests added/updated

Summary by CodeRabbit

リリースノート

  • Tests

    • URL変換機能の包括的なテストスイートを追加しました。
  • Refactor

    • URL変換ロジックを整理し、複数の専用関数に分割しました。
    • 埋め込み属性に段階設定オプションを追加しました。

@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

📝 Walkthrough

ウォークスルー

タイルのエンドポイント選択とURL変換ロジックを新しい専用モジュール transform-request.ts に抽出し、既存の geolonia-map.ts 内のインライン処理を置き換えました。これにより、3つの変換関数(API、タイル、スプライト用)を再利用可能かつテスト可能にし、ステージごとのホスト切り替えとパラメータ注入の処理を一元化しました。

変更内容

Cohort / File(s) 概要
変換ロジックの新規抽出
src/lib/transform-request.ts, src/lib/geolonia-map.test.ts
タイルエンドポイント選択とURL変換機能を新しいモジュールとして実装し、API ソース、タイル URL、スプライト URL の変換を行う 3 つの関数 (transformGeoloniaApiSource, transformGeoloniaTileSource, transformGeoloniaSprite) を追加。テストスイート で URL リモデリング、パラメータ注入、条件付きホスト/パス調整を検証。
既存コンポーネントのリファクタリング
src/lib/geolonia-map.ts
インライン URL 変換ロジックを新しいヘルパー関数への呼び出しに置き換え。ソースリソースは transformGeoloniaApiSource または transformGeoloniaTileSource の結果を利用し、スプライトリソースは transformGeoloniaSprite にデリゲート。フォールバック処理を統一。
型定義の拡張
src/types.ts
EmbedAttributes 型に stage: string プロパティを追加。

推定コードレビュー工数

🎯 3 (Moderate) | ⏱️ ~20 minutes

ポエム

🐰✨ タイルの道をきれいに並べて
変換の関数、つなぎなおし
dev と本番、ホストを切り替え
テストで守る、堅牢な流れ
メンテナンスの扉、開きました 🗝️

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed プルリクエストのタイトルは、変更セットの主要な意図を正確に反映しており、タイル選択関数のリファクタリングという中心的な変更を明確に示しています。
Linked Issues check ✅ Passed 変更セットは、transform-request.ts の新しいモジュール化、geolonia-map.ts での URL 変換ロジックの抽出、および段階別のホスト切り替えに対応することで、#469 の目標である保守性の向上を達成しています。
Out of Scope Changes check ✅ Passed すべての変更はタイル選択とエンドポイント変換のリファクタリングに関連しており、#469 の範囲内です。EmbedAttributes.stage プロパティの追加も、dev ステージのホスト切り替えをサポートするために必要な変更です。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Tip

You can customize the high-level summary generated by CodeRabbit.

Configure the reviews.high_level_summary_instructions setting to provide custom instructions for generating the high-level summary.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/lib/transform-request.ts (1)

26-28: username キャプチャグループが未使用です。

正規表現で (?<username>.+) をキャプチャしていますが、Line 30 では customtileId のみを使用しています。これは意図的な設計でしょうか?将来の使用のために残しているのか、または不要なキャプチャグループであれば削除を検討してください。

♻️ 不要な場合の修正案
     const tilesMatch = url.match(
-      /^geolonia:\/\/tiles\/(?<username>.+)\/(?<customtileId>.+)/,
+      /^geolonia:\/\/tiles\/.+\/(?<customtileId>.+)/,
     );
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib/transform-request.ts` around lines 26 - 28, The regex used to build
tilesMatch captures both username and customtileId but only customtileId is
used; either remove the unused username capture from the pattern (change the
regex in transform-request.ts to /^geolonia:\/\/tiles\/(?<customtileId>.+)/) or
actually consume it by reading tilesMatch.groups.username where appropriate
(e.g., reference tilesMatch.groups.username alongside
tilesMatch.groups.customtileId) so the captured group is not unused.
src/lib/geolonia-map.test.ts (1)

30-91: geolonia:// URLがパターンにマッチしない場合のテストケースを追加してください。

transform-request.ts で指摘した問題に関連しますが、geolonia://invalid-format のようなURLを入力した場合の動作を検証するテストケースがありません。現在の実装では、このケースでランタイムエラーが発生する可能性があります。

💡 追加テストケースの例
it('should handle geolonia:// URLs that do not match the expected pattern', () => {
  const result = transformGeoloniaTileSource(
    'geolonia://invalid-format',
    atts,
    sessionId,
  );
  // 期待される動作に応じてアサーションを調整
  assert.strictEqual(result, null);
});
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib/geolonia-map.test.ts` around lines 30 - 91, Add a test to verify
transformGeoloniaTileSource safely handles malformed geolonia:// URLs (e.g.,
"geolonia://invalid-format") by returning null (or the expected safe value)
instead of throwing; update src/lib/geolonia-map.test.ts by adding an it block
that calls transformGeoloniaTileSource with 'geolonia://invalid-format' using
the existing atts and sessionId constants and asserts the result is null (or the
chosen expected behavior), and if the current implementation throws, adjust
transformGeoloniaTileSource to validate the geolonia:// pattern and return null
on non-matching inputs.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/lib/transform-request.ts`:
- Around line 25-32: The code currently leaves transformedUrl as the original
geolonia:// string when a geolonia:// URL doesn't match the tiles pattern,
causing new URL(transformedUrl) to throw; update the geolonia:// handling in
transform-request.ts (the block that uses url.startsWith('geolonia://') and
tilesMatch) to return early or set a safe transformedUrl when tilesMatch is
falsy (e.g. reject/throw a controlled error or return the original
Request/Response) so that new URL(...) is never called with an unsupported
scheme; ensure you reference the tilesMatch check and the transformedUrl
variable when making the change.

---

Nitpick comments:
In `@src/lib/geolonia-map.test.ts`:
- Around line 30-91: Add a test to verify transformGeoloniaTileSource safely
handles malformed geolonia:// URLs (e.g., "geolonia://invalid-format") by
returning null (or the expected safe value) instead of throwing; update
src/lib/geolonia-map.test.ts by adding an it block that calls
transformGeoloniaTileSource with 'geolonia://invalid-format' using the existing
atts and sessionId constants and asserts the result is null (or the chosen
expected behavior), and if the current implementation throws, adjust
transformGeoloniaTileSource to validate the geolonia:// pattern and return null
on non-matching inputs.

In `@src/lib/transform-request.ts`:
- Around line 26-28: The regex used to build tilesMatch captures both username
and customtileId but only customtileId is used; either remove the unused
username capture from the pattern (change the regex in transform-request.ts to
/^geolonia:\/\/tiles\/(?<customtileId>.+)/) or actually consume it by reading
tilesMatch.groups.username where appropriate (e.g., reference
tilesMatch.groups.username alongside tilesMatch.groups.customtileId) so the
captured group is not unused.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b87d13a1-373f-493f-80a0-aa2890286204

📥 Commits

Reviewing files that changed from the base of the PR and between 3e62985 and 2821415.

📒 Files selected for processing (4)
  • src/lib/geolonia-map.test.ts
  • src/lib/geolonia-map.ts
  • src/lib/transform-request.ts
  • src/types.ts

Comment on lines +25 to +32
if (url.startsWith('geolonia://')) {
const tilesMatch = url.match(
/^geolonia:\/\/tiles\/(?<username>.+)\/(?<customtileId>.+)/,
);
if (tilesMatch) {
transformedUrl = `https://tileserver.geolonia.com/customtiles/${tilesMatch.groups.customtileId}/tiles.json`;
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

geolonia:// URLがパターンにマッチしない場合にランタイムエラーが発生する可能性があります。

URLが geolonia:// で始まるが、tilesMatch のパターン(geolonia://tiles/username/customtileId)にマッチしない場合、transformedUrl は変換されずに geolonia://... のまま残ります。その後、Line 34 で new URL(transformedUrl) を呼び出すと、geolonia:// は有効なURLスキームではないため、TypeError がスローされます。

🐛 修正案: マッチしない場合は早期リターン
   if (url.startsWith('geolonia://')) {
     const tilesMatch = url.match(
       /^geolonia:\/\/tiles\/(?<username>.+)\/(?<customtileId>.+)/,
     );
     if (tilesMatch) {
       transformedUrl = `https://tileserver.geolonia.com/customtiles/${tilesMatch.groups.customtileId}/tiles.json`;
+    } else {
+      // geolonia:// URLがパターンにマッチしない場合は変換をスキップ
+      return null;
     }
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (url.startsWith('geolonia://')) {
const tilesMatch = url.match(
/^geolonia:\/\/tiles\/(?<username>.+)\/(?<customtileId>.+)/,
);
if (tilesMatch) {
transformedUrl = `https://tileserver.geolonia.com/customtiles/${tilesMatch.groups.customtileId}/tiles.json`;
}
}
if (url.startsWith('geolonia://')) {
const tilesMatch = url.match(
/^geolonia:\/\/tiles\/(?<username>.+)\/(?<customtileId>.+)/,
);
if (tilesMatch) {
transformedUrl = `https://tileserver.geolonia.com/customtiles/${tilesMatch.groups.customtileId}/tiles.json`;
} else {
// geolonia:// URLがパターンにマッチしない場合は変換をスキップ
return null;
}
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib/transform-request.ts` around lines 25 - 32, The code currently leaves
transformedUrl as the original geolonia:// string when a geolonia:// URL doesn't
match the tiles pattern, causing new URL(transformedUrl) to throw; update the
geolonia:// handling in transform-request.ts (the block that uses
url.startsWith('geolonia://') and tilesMatch) to return early or set a safe
transformedUrl when tilesMatch is falsy (e.g. reject/throw a controlled error or
return the original Request/Response) so that new URL(...) is never called with
an unsupported scheme; ensure you reference the tilesMatch check and the
transformedUrl variable when making the change.

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.

[Feature]: タイルのエンドポイント選択のリファクタリング

1 participant