import { beforeEach, describe, expect, it } from "vitest"; import { firestore } from "../../src/firebase.js"; import { fetchRankFromKbo, fetchScheduleFromKbo, } from "../../src/repositories/kboRepository.js"; describe("kboRepository (실제 KBO 사이트 호출)", () => { beforeEach(async () => { console.log("[setup] 캐시/락 컬렉션 초기화"); await firestore.recursiveDelete(firestore.collection("kboCache")); await firestore.recursiveDelete(firestore.collection("kboLocks")); }); it("팀 순위를 정상적으로 fetch한다", async () => { console.log("\n[rank] 2025년 팀 순위 fetch 시작"); const t0 = Date.now(); const [result] = await fetchRankFromKbo([2025]); console.log(`[rank] fetch 완료 (${Date.now() - t0}ms)`); console.log(`[rank] year=${result.year}, teams=${result.teams.length}`); console.table( result.teams.map((t) => ({ rank: t.rank, team: t.team, winRate: t.winRate, games: t.games, })) ); expect(result.year).toBe(2025); expect(Array.isArray(result.teams)).toBe(true); expect(result.teams.length).toBe(10); expect(result.teams[0]).toHaveProperty("team"); expect(result.teams[0]).toHaveProperty("rank"); }, 30000); it("경기 일정을 정상적으로 fetch한다", async () => { console.log("\n[schedule] 2025-04 일정 fetch 시작"); const t0 = Date.now(); const result = await fetchScheduleFromKbo({ year: 2025, month: 4 }); console.log(`[schedule] fetch 완료 (${Date.now() - t0}ms)`); console.log( `[schedule] year=${result.year}, month=${result.month}, games=${result.games.length}` ); console.log("[schedule] 첫 3개 경기:"); console.table(result.games.slice(0, 3)); expect(result.year).toBe(2025); expect(result.month).toBe(4); expect(Array.isArray(result.games)).toBe(true); expect(result.games.length).toBeGreaterThan(0); expect(result.games[0]).toHaveProperty("gameId"); expect(result.games[0]).toHaveProperty("homeTeamCode"); expect(result.games[0]).toHaveProperty("awayTeamCode"); }, 30000); it("같은 요청을 두 번 하면 두 번째는 캐시에서 온다", async () => { console.log("\n[cache] 1차 호출 (실제 fetch 예상)"); const start1 = Date.now(); await fetchScheduleFromKbo({ year: 2025, month: 5 }); const firstDuration = Date.now() - start1; console.log(`[cache] 1차 소요: ${firstDuration}ms`); console.log("[cache] 2차 호출 (캐시 hit 예상)"); const start2 = Date.now(); await fetchScheduleFromKbo({ year: 2025, month: 5 }); const secondDuration = Date.now() - start2; console.log(`[cache] 2차 소요: ${secondDuration}ms`); console.log( `[cache] 속도 비교: ${firstDuration}ms → ${secondDuration}ms (${( (1 - secondDuration / firstDuration) * 100 ).toFixed(1)}% 빨라짐)` ); expect(secondDuration).toBeLessThan(firstDuration); }, 30000); });