|
| 1 | +import { Credential, LeetCode, LeetCodeCN } from "leetcode-query"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { LeetCodeCNService } from "../../src/leetcode/leetcode-cn-service.js"; |
| 4 | +import { LeetCodeGlobalService } from "../../src/leetcode/leetcode-global-service.js"; |
| 5 | + |
| 6 | +describe("LeetCode Problem Services", () => { |
| 7 | + describe("LeetCodeGlobalService", () => { |
| 8 | + const credential = new Credential(); |
| 9 | + const leetCodeApi = new LeetCode(credential); |
| 10 | + const service = new LeetCodeGlobalService(leetCodeApi, credential); |
| 11 | + |
| 12 | + describe("fetchProblemSimplified", () => { |
| 13 | + it("should return simplified problem data", async () => { |
| 14 | + const titleSlug = "two-sum"; |
| 15 | + const result = await service.fetchProblemSimplified(titleSlug); |
| 16 | + |
| 17 | + expect(result).toBeDefined(); |
| 18 | + expect(result.titleSlug).toBe(titleSlug); |
| 19 | + expect(result.title).toBe("Two Sum"); |
| 20 | + expect(result.questionId).toBeDefined(); |
| 21 | + expect(result.questionFrontendId).toBeDefined(); |
| 22 | + expect(result.content).toBeDefined(); |
| 23 | + expect(result.difficulty).toBeDefined(); |
| 24 | + expect(Array.isArray(result.topicTags)).toBe(true); |
| 25 | + expect(Array.isArray(result.codeSnippets)).toBe(true); |
| 26 | + expect(result.hints).toBeDefined(); |
| 27 | + |
| 28 | + if (result.similarQuestions) { |
| 29 | + expect(Array.isArray(result.similarQuestions)).toBe(true); |
| 30 | + } |
| 31 | + }, 30000); |
| 32 | + |
| 33 | + it("should handle invalid problems correctly", async () => { |
| 34 | + const invalidSlug = `invalid-problem-${Date.now()}`; |
| 35 | + |
| 36 | + try { |
| 37 | + await service.fetchProblemSimplified(invalidSlug); |
| 38 | + expect(true).toBe(false); |
| 39 | + } catch (error) { |
| 40 | + expect(error).toBeDefined(); |
| 41 | + } |
| 42 | + }, 30000); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe("LeetCodeCNService", () => { |
| 47 | + const credential = new Credential(); |
| 48 | + const leetCodeApi = new LeetCodeCN(credential); |
| 49 | + const service = new LeetCodeCNService(leetCodeApi, credential); |
| 50 | + |
| 51 | + describe("fetchProblemSimplified", () => { |
| 52 | + it("should return simplified problem data", async () => { |
| 53 | + const titleSlug = "two-sum"; |
| 54 | + const result = await service.fetchProblemSimplified(titleSlug); |
| 55 | + |
| 56 | + expect(result).toBeDefined(); |
| 57 | + expect(result.titleSlug).toBe(titleSlug); |
| 58 | + expect(result.questionId).toBeDefined(); |
| 59 | + expect(result.questionFrontendId).toBeDefined(); |
| 60 | + expect(result.title).toBeDefined(); |
| 61 | + expect(result.content).toBeDefined(); |
| 62 | + expect(result.difficulty).toBeDefined(); |
| 63 | + expect(Array.isArray(result.topicTags)).toBe(true); |
| 64 | + expect(Array.isArray(result.codeSnippets)).toBe(true); |
| 65 | + expect(result.hints).toBeDefined(); |
| 66 | + |
| 67 | + if (result.similarQuestions) { |
| 68 | + expect(Array.isArray(result.similarQuestions)).toBe(true); |
| 69 | + } |
| 70 | + |
| 71 | + console.log( |
| 72 | + `Successfully fetched simplified data for ${titleSlug}` |
| 73 | + ); |
| 74 | + }, 30000); |
| 75 | + |
| 76 | + it("should handle invalid problems correctly", async () => { |
| 77 | + const invalidSlug = `invalid-problem-${Date.now()}`; |
| 78 | + |
| 79 | + try { |
| 80 | + await service.fetchProblemSimplified(invalidSlug); |
| 81 | + expect(true).toBe(false); |
| 82 | + } catch (error) { |
| 83 | + expect(error).toBeDefined(); |
| 84 | + } |
| 85 | + }, 30000); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments