Repository navigation
feat: Kakao 키워드 장소 검색 구현 #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5086f9c
feat: geocoder provider 기반 흐름 및 타입 정리
minsour c560b7a
feat: Kakao 키워드 장소 검색 구현
minsour 63e49d4
chore: Kakao geocoder 확인용 API 추가
minsour 588a0e6
refactor: Kakao API 키를 env 검증에서 fail-fast로 강제
minsour 3daa0ad
chore: CodeRabbit 리뷰 반영 (실패 로깅·fetch 테스트·AreaType 단일화)
minsour 714f9f0
Merge origin/main into feature/GM-63-kakao-keyword-search
minsour d5a42d4
test: e2e 실행에 KAKAO_REST_API_KEY 주입
minsour 53a5768
Merge remote-tracking branch 'origin/main' into feature/GM-63-kakao-k…
minsour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
src/infrastructures/geocoder/providers/kakao.provider.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { afterEach, describe, expect, it, jest } from "bun:test"; | ||
| import type { ConfigService } from "@nestjs/config"; | ||
| import { AppException } from "../../../common/exceptions/app.exception"; | ||
| import type { Env } from "../../../config/env.schema"; | ||
| import type { GeoQuery } from "../geocoder.type"; | ||
| import { KakaoProvider } from "./kakao.provider"; | ||
|
|
||
| const originalFetch = globalThis.fetch; | ||
| const query: GeoQuery = { | ||
| areaName: "서울 강남구", | ||
| areaType: "landmark", | ||
| placeName: "카카오프렌즈", | ||
| }; | ||
|
|
||
| function createProvider() { | ||
| const configService = { | ||
| getOrThrow: jest.fn(() => "test-api-key"), | ||
| } as unknown as ConfigService<Env>; | ||
|
|
||
| return new KakaoProvider(configService); | ||
| } | ||
|
|
||
| function createKakaoResponse(overrides: Record<string, unknown> = {}) { | ||
| return { | ||
| meta: { | ||
| same_name: { | ||
| region: [], | ||
| keyword: "카카오프렌즈", | ||
| selected_region: "", | ||
| }, | ||
| pageable_count: 1, | ||
| total_count: 1, | ||
| is_end: true, | ||
| }, | ||
| documents: [ | ||
| { | ||
| id: "26338954", | ||
| place_name: "카카오프렌즈 코엑스점", | ||
| category_name: "가정,생활 > 문구,사무용품 > 디자인문구 > 카카오프렌즈", | ||
| category_group_code: "", | ||
| category_group_name: "", | ||
| phone: "02-6002-1880", | ||
| address_name: "서울 강남구 삼성동 159", | ||
| road_address_name: "서울 강남구 영동대로 513", | ||
| x: "127.05902969025047", | ||
| y: "37.51207412593136", | ||
| place_url: "http://place.map.kakao.com/26338954", | ||
| distance: "418", | ||
| ...overrides, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| function mockFetchJson(body: unknown, init: ResponseInit = {}) { | ||
| const fetchMock = jest.fn().mockResolvedValue( | ||
| new Response(JSON.stringify(body), { | ||
| status: 200, | ||
| headers: { "content-type": "application/json" }, | ||
| ...init, | ||
| }), | ||
| ); | ||
| globalThis.fetch = fetchMock as unknown as typeof fetch; | ||
| return fetchMock; | ||
| } | ||
|
|
||
| async function expectAppException( | ||
| promise: Promise<unknown>, | ||
| errorCode: string, | ||
| ) { | ||
| const error = await promise.then( | ||
| () => undefined, | ||
| (error: unknown) => error, | ||
| ); | ||
|
|
||
| expect(error).toBeInstanceOf(AppException); | ||
| expect(error).toMatchObject({ errorCode }); | ||
| } | ||
|
|
||
| describe("KakaoProvider", () => { | ||
| afterEach(() => { | ||
| globalThis.fetch = originalFetch; | ||
| }); | ||
|
|
||
| it("keyword search API를 올바른 query와 인증 헤더로 호출한다", async () => { | ||
| const fetchMock = mockFetchJson(createKakaoResponse()); | ||
| const provider = createProvider(); | ||
|
|
||
| await provider.search(query); | ||
|
|
||
| const [url, init] = fetchMock.mock.calls[0] as [ | ||
| string, | ||
| RequestInit | undefined, | ||
| ]; | ||
| const requestUrl = new URL(url); | ||
| expect(requestUrl.origin).toBe("https://dapi.kakao.com"); | ||
| expect(requestUrl.pathname).toBe("/v2/local/search/keyword.json"); | ||
| expect(requestUrl.searchParams.get("query")).toBe( | ||
| "서울 강남구 카카오프렌즈", | ||
| ); | ||
| expect(requestUrl.searchParams.get("size")).toBe("15"); | ||
| expect(init?.headers).toEqual({ | ||
| Authorization: "KakaoAK test-api-key", | ||
| }); | ||
| }); | ||
|
|
||
| it("Kakao 응답을 GeoCandidate로 정규화한다", async () => { | ||
| mockFetchJson(createKakaoResponse()); | ||
| const provider = createProvider(); | ||
|
|
||
| const result = await provider.search(query); | ||
|
|
||
| expect(result).toEqual([ | ||
| { | ||
| provider: "kakao", | ||
| providerPlaceId: "26338954", | ||
| placeName: "카카오프렌즈 코엑스점", | ||
| address: "서울 강남구 삼성동 159", | ||
| coordinate: { | ||
| lat: 37.51207412593136, | ||
| lng: 127.05902969025047, | ||
| }, | ||
| distance: 418, | ||
| mapUrl: "http://place.map.kakao.com/26338954", | ||
| phone: "02-6002-1880", | ||
| category: "가정,생활 > 문구,사무용품 > 디자인문구 > 카카오프렌즈", | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("distance가 없거나 빈 문자열이면 distance를 생략한다", async () => { | ||
| mockFetchJson(createKakaoResponse({ distance: "" })); | ||
| const provider = createProvider(); | ||
|
|
||
| const [candidate] = await provider.search(query); | ||
|
|
||
| expect(candidate.distance).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("429 응답이면 KAKAO_RATE_LIMITED를 던진다", async () => { | ||
| mockFetchJson({ error: "rate limited" }, { status: 429 }); | ||
| const provider = createProvider(); | ||
|
|
||
| await expectAppException(provider.search(query), "KAKAO_RATE_LIMITED"); | ||
| }); | ||
|
|
||
| it("2xx 응답이 아니면 KAKAO_REQUEST_FAILED를 던진다", async () => { | ||
| mockFetchJson({ error: "bad gateway" }, { status: 502 }); | ||
| const provider = createProvider(); | ||
|
|
||
| await expectAppException(provider.search(query), "KAKAO_REQUEST_FAILED"); | ||
| }); | ||
|
|
||
| it("응답 형식이 다르면 KAKAO_RESPONSE_INVALID를 던진다", async () => { | ||
| mockFetchJson(createKakaoResponse({ x: "not-a-number" })); | ||
| const provider = createProvider(); | ||
|
|
||
| await expectAppException(provider.search(query), "KAKAO_RESPONSE_INVALID"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.