|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("../utils/learningData.js", () => ({ |
| 4 | + fetchProblemCategories: vi.fn(), |
| 5 | + fetchCohortUserSolutions: vi.fn(), |
| 6 | + fetchPRSubmissions: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("../utils/openai.js", () => ({ |
| 10 | + generateApproachAnalysis: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +import { postLearningStatus } from "./learning-status.js"; |
| 14 | +import { |
| 15 | + fetchProblemCategories, |
| 16 | + fetchCohortUserSolutions, |
| 17 | + fetchPRSubmissions, |
| 18 | +} from "../utils/learningData.js"; |
| 19 | +import { generateApproachAnalysis } from "../utils/openai.js"; |
| 20 | + |
| 21 | +describe("postLearningStatus — 누적 풀이 집계", () => { |
| 22 | + let originalFetch; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + vi.clearAllMocks(); |
| 26 | + originalFetch = globalThis.fetch; |
| 27 | + fetchProblemCategories.mockResolvedValue({ |
| 28 | + "two-sum": { |
| 29 | + difficulty: "Easy", |
| 30 | + blindCategories: ["Array"], |
| 31 | + intended_approach: "Hash Table", |
| 32 | + }, |
| 33 | + }); |
| 34 | + fetchPRSubmissions.mockResolvedValue([ |
| 35 | + { problemName: "two-sum", rawUrl: "https://example.com/solution.py" }, |
| 36 | + ]); |
| 37 | + generateApproachAnalysis.mockResolvedValue({ |
| 38 | + matches: true, |
| 39 | + explanation: "Uses a hash table", |
| 40 | + }); |
| 41 | + globalThis.fetch = vi |
| 42 | + .fn() |
| 43 | + .mockResolvedValueOnce({ |
| 44 | + ok: true, |
| 45 | + text: () => Promise.resolve("class Solution: pass"), |
| 46 | + }) |
| 47 | + .mockResolvedValueOnce({ |
| 48 | + ok: true, |
| 49 | + json: () => Promise.resolve([]), |
| 50 | + }) |
| 51 | + .mockResolvedValueOnce({ ok: true }); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + globalThis.fetch = originalFetch; |
| 56 | + }); |
| 57 | + |
| 58 | + it.each([ |
| 59 | + { |
| 60 | + name: "등록된 문제의 풀이 수를 유지한다", |
| 61 | + solved: ["two-sum"], |
| 62 | + expected: 1, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "미등록 문제가 섞여 있으면 등록된 문제만 집계한다", |
| 66 | + solved: ["to-lower-case", "two-sum"], |
| 67 | + expected: 1, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "미등록 문제만 풀었으면 풀이 수는 0이다", |
| 71 | + solved: ["to-lower-case"], |
| 72 | + expected: 0, |
| 73 | + }, |
| 74 | + ])("$name", async ({ solved, expected }) => { |
| 75 | + fetchCohortUserSolutions.mockResolvedValue(solved); |
| 76 | + |
| 77 | + await postLearningStatus( |
| 78 | + "DaleStudy", |
| 79 | + "leetcode-study", |
| 80 | + 42, |
| 81 | + "learner", |
| 82 | + "token", |
| 83 | + {} |
| 84 | + ); |
| 85 | + |
| 86 | + const post = globalThis.fetch.mock.calls.find( |
| 87 | + ([, options]) => options?.method === "POST" |
| 88 | + ); |
| 89 | + expect(post).toBeDefined(); |
| 90 | + |
| 91 | + const body = JSON.parse(post[1].body).body; |
| 92 | + expect(body).toContain(`풀이한 문제: ${expected} / 1개`); |
| 93 | + }); |
| 94 | +}); |
0 commit comments