From df95ea81b31d9df8a650b73ff90c7c9c53f7f3f9 Mon Sep 17 00:00:00 2001 From: Woohyuck Jeong Date: Thu, 22 Oct 2026 13:50:47 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=A9=94=EA=B0=80=EB=B0=95=EC=8A=A4=20?= =?UTF-8?q?=EA=B2=BD=EA=B8=B0=20=EA=B4=91=EC=A3=BC=EC=8B=9C=20=EC=A7=80?= =?UTF-8?q?=EC=97=AD=20=EC=BD=94=EB=93=9C=20=EC=98=A4=ED=8C=90=EC=A0=95=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 지역 목록 순서대로 부분 일치를 찾아 "경기 광주시" 주소가 광주광역시(29)로 판정되던 문제를 고쳤습니다. 텍스트에서 가장 앞에 나온 지역명을 사용해 경기(41)로 판정합니다. --- src/services/megabox/location.ts | 11 ++++++++--- tests/services/megabox/location.test.ts | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/services/megabox/location.ts b/src/services/megabox/location.ts index 1a5d96ae..8e4fd104 100644 --- a/src/services/megabox/location.ts +++ b/src/services/megabox/location.ts @@ -107,12 +107,17 @@ function isValidCoordinate(value: number | undefined): value is number { function resolveAreaCodeFromText(text: string): string | null { const normalized = text.replace(/\s+/g, ''); + // 목록 순서가 아니라 텍스트에서 가장 앞에 나온 지역을 사용합니다. (예: "경기 광주시"는 광주광역시가 아닌 경기도) + let matchedAreaCode: string | null = null; + let matchedIndex = Number.POSITIVE_INFINITY; for (const [region, areaCode] of MEGABOX_AREA_CODE_BY_REGION) { - if (normalized.includes(region.replace(/\s+/g, ''))) { - return areaCode; + const index = normalized.indexOf(region); + if (index !== -1 && index < matchedIndex) { + matchedAreaCode = areaCode; + matchedIndex = index; } } - return null; + return matchedAreaCode; } export async function resolveMegaboxLocation( diff --git a/tests/services/megabox/location.test.ts b/tests/services/megabox/location.test.ts index 3fd27afe..83b7ef01 100644 --- a/tests/services/megabox/location.test.ts +++ b/tests/services/megabox/location.test.ts @@ -211,4 +211,12 @@ describe('무료 위치 해석 계약', () => { mockFetch.mockResolvedValueOnce(new Response(JSON.stringify({ documents: [{ place_name: '부산역', address_name: '부산 동구', x: '129.041', y: '35.115' }] }))); expect(await resolveMegaboxLocation({ keyword: '부산역' }, { kakaoRestApiKey: 'free' })).toMatchObject({ areaCode: '26', latitude: 35.115 }); }); + it('경기 광주시 주소는 광주광역시가 아닌 경기 지역으로 결정한다', async () => { + mockFetch.mockResolvedValueOnce(new Response(JSON.stringify({ documents: [{ place_name: '경안동', address_name: '경기 광주시 경안동', x: '127.255', y: '37.41' }] }))); + expect(await resolveMegaboxLocation({ keyword: '경안동' }, { kakaoRestApiKey: 'free' })).toMatchObject({ areaCode: '41' }); + }); + it('광주광역시 주소는 광주 지역으로 결정한다', async () => { + mockFetch.mockResolvedValueOnce(new Response(JSON.stringify({ documents: [{ place_name: '광주송정역', address_name: '광주 광산구 송정동', x: '126.793', y: '35.137' }] }))); + expect(await resolveMegaboxLocation({ keyword: '광주송정역' }, { kakaoRestApiKey: 'free' })).toMatchObject({ areaCode: '29' }); + }); });