Files
homeclaw/tests/kakao-geocode.test.ts
kimandClaude Sonnet 5.5 e4fe7edd61 feat(weather): 한글 지명을 카카오 로컬 주소검색으로 지오코딩 (디스크 캐시·폴백)
Open-Meteo 지오코더가 읍면동 단위 한글 지명을 잘못 풀던 문제(현풍→경주)를 막는다.
주소검색 REGION만 사용하고 행정 단계가 높은 후보를 고르며(구미→구미시), 한반도 범위 밖은
버린다. 성공 결과는 geocode-kakao-cache.json에 캐시, 키 없음·실패 시 기존 경로로 폴백.
순서: KR_CITY_COORDS → 지점표 → 카카오 로컬 → Open-Meteo.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 11:30:31 +09:00

88 lines
4.2 KiB
TypeScript

/**
* 카카오 로컬 주소검색 지오코딩 — 순수 선택 로직과 캐시/폴백 동작(네트워크는 주입한 fetch로 대체).
*/
import { describe, test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { adminLevel, inKoreaBox, pickKakaoRegion, kakaoGeocode } from '../src/tools/kakao-geocode';
const doc = (address_name: string, y: number, x: number, address_type = 'REGION') =>
({ address_type, address_name, y: String(y), x: String(x) });
describe('adminLevel', () => {
test('시·군·구 > 읍·면 > 동·리', () => {
assert.equal(adminLevel('경북 구미시'), 3);
assert.equal(adminLevel('대구 달성군'), 3);
assert.equal(adminLevel('대구 달성군 현풍읍'), 2);
assert.equal(adminLevel('경기 성남시 분당구 구미동'), 1);
assert.equal(adminLevel('경기도'), 0);
});
});
describe('pickKakaoRegion', () => {
test('"구미": 성남 구미동보다 구미시를 고른다', () => {
const r = pickKakaoRegion([
doc('경기 성남시 분당구 구미동', 37.34, 127.12),
doc('경기 성남시 분당구 구미1동', 37.35, 127.11),
doc('경북 구미시', 36.12, 128.34),
])!;
assert.match(r.displayName, /경북 구미시/);
assert.match(r.displayName, /대한민국/);
});
test('같은 단계면 API 순서(첫째)를 유지', () => {
const r = pickKakaoRegion([doc('서울 성동구 송정동', 37.55, 127.07), doc('경기 광주시 송정동', 37.42, 127.26)])!;
assert.match(r.displayName, /서울 성동구/);
});
test('REGION이 아닌 도로명/지번 결과와 범위 밖 좌표는 버린다', () => {
assert.equal(pickKakaoRegion([doc('서울 중구 세종대로 110', 37.56, 126.97, 'ROAD')]), null);
assert.equal(pickKakaoRegion([doc('어딘가', 10, 100)]), null);
assert.equal(pickKakaoRegion([]), null);
});
});
describe('inKoreaBox', () => {
test('한반도·제주 안/밖', () => {
assert.equal(inKoreaBox(35.7, 128.45), true);
assert.equal(inKoreaBox(33.4, 126.5), true);
assert.equal(inKoreaBox(35.68, 139.69), false);
});
});
describe('kakaoGeocode', () => {
const tmp = () => path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'kgeo-')), 'cache.json');
const okFetch = (docs: any[], counter: { n: number }) =>
(async () => { counter.n++; return { ok: true, json: async () => ({ documents: docs }) } as any; }) as unknown as typeof fetch;
test('한글이 없으면 호출하지 않는다', async () => {
const c = { n: 0 };
const r = await kakaoGeocode('Tokyo', { getKey: () => 'k', cacheFile: tmp(), fetchImpl: okFetch([], c) });
assert.equal(r, null); assert.equal(c.n, 0);
});
test('키가 없으면 null', async () => {
const c = { n: 0 };
assert.equal(await kakaoGeocode('현풍', { getKey: () => undefined, cacheFile: tmp(), fetchImpl: okFetch([], c) }), null);
assert.equal(c.n, 0);
});
test('성공 결과는 캐시되어 두 번째는 호출하지 않는다', async () => {
const c = { n: 0 }; const file = tmp();
const docs = [doc('대구 달성군 현풍읍', 35.697, 128.4476)];
const a = await kakaoGeocode('현풍x', { getKey: () => 'k', cacheFile: file, fetchImpl: okFetch(docs, c) });
const b = await kakaoGeocode('현풍x', { getKey: () => 'k', cacheFile: file, fetchImpl: okFetch(docs, c) });
assert.deepEqual(a, b); assert.equal(c.n, 1);
});
test('REGION이 없으면 null이고 캐시하지 않는다', async () => {
const c = { n: 0 }; const file = tmp();
assert.equal(await kakaoGeocode('도쿄', { getKey: () => 'k', cacheFile: file, fetchImpl: okFetch([], c) }), null);
assert.equal(await kakaoGeocode('도쿄', { getKey: () => 'k', cacheFile: file, fetchImpl: okFetch([], c) }), null);
assert.equal(c.n, 2);
});
test('HTTP 오류·예외는 null', async () => {
const bad = (async () => ({ ok: false }) as any) as unknown as typeof fetch;
assert.equal(await kakaoGeocode('현풍', { getKey: () => 'k', cacheFile: tmp(), fetchImpl: bad }), null);
const boom = (async () => { throw new Error('net'); }) as unknown as typeof fetch;
assert.equal(await kakaoGeocode('현풍', { getKey: () => 'k', cacheFile: tmp(), fetchImpl: boom }), null);
});
});