|
| 1 | +import { |
| 2 | + calculateQualScore, |
| 3 | + estimateOptimalModel, |
| 4 | + countTokensOfConversation, |
| 5 | + gptRelevance, |
| 6 | +} from "./calculate-quality-score"; |
| 7 | +import { Comment, Issue, User, UserType } from "../../../../types/payload"; |
| 8 | + |
| 9 | +// jest.mock("openai", () => { |
| 10 | +// return jest.fn().mockImplementation(() => { |
| 11 | +// return { |
| 12 | +// chat: { |
| 13 | +// completions: { |
| 14 | +// create: jest.fn().mockResolvedValue({ |
| 15 | +// choices: [ |
| 16 | +// { |
| 17 | +// message: { |
| 18 | +// content: "[1, 1, 0]", |
| 19 | +// }, |
| 20 | +// }, |
| 21 | +// ], |
| 22 | +// }), |
| 23 | +// }, |
| 24 | +// }, |
| 25 | +// }; |
| 26 | +// }); |
| 27 | +// }); |
| 28 | + |
| 29 | +describe("(**Real OpenAI API Call**) calculateQualScore", () => { |
| 30 | + it("should calculate quality score", async () => { |
| 31 | + const issue = { body: "my topic is about apples" } as Issue; |
| 32 | + const comments: Comment[] = [ |
| 33 | + { body: "the apple is red", user: { type: UserType.User } as User } as Comment, |
| 34 | + { body: "it is juicy", user: { type: UserType.User } as User } as Comment, |
| 35 | + { body: "bananas are great", user: { type: UserType.User } as User } as Comment, |
| 36 | + ]; |
| 37 | + const result = await calculateQualScore(issue, comments); |
| 38 | + expect(result).toBeDefined(); |
| 39 | + expect(result.tokens).toBeGreaterThan(0); |
| 40 | + expect(result.estimatedOptimalModel).toBeDefined(); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe("(**Real OpenAI API Call**) gptRelevance", () => { |
| 45 | + it("should calculate gpt relevance", async () => { |
| 46 | + const result = await gptRelevance("gpt-3.5-turbo", "my topic is about apples", [ |
| 47 | + "the apple is red", |
| 48 | + "it is juicy", |
| 49 | + "bananas are great", |
| 50 | + ]); |
| 51 | + expect(result[0]).toBeGreaterThan(0); |
| 52 | + expect(result[1]).toBeGreaterThan(0); |
| 53 | + expect(result[result.length - 1]).toBe(0); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe("countTokensOfConversation", () => { |
| 58 | + it("should count tokens of conversation", () => { |
| 59 | + const issue = { body: "my topic is about apples" } as Issue; |
| 60 | + const comments: Comment[] = [ |
| 61 | + { body: "the apple is red", user: { type: UserType.User } as User } as Comment, |
| 62 | + { body: "it is juicy", user: { type: UserType.User } as User } as Comment, |
| 63 | + { body: "bananas are great", user: { type: UserType.User } as User } as Comment, |
| 64 | + ]; |
| 65 | + const result = countTokensOfConversation(issue, comments); |
| 66 | + expect(result).toBeGreaterThan(0); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("estimateOptimalModel", () => { |
| 71 | + it("should estimate optimal model", () => { |
| 72 | + const result = estimateOptimalModel(5000); |
| 73 | + expect(result).toBe("gpt-3.5-turbo-16k"); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +jest.mock("openai", () => { |
| 78 | + // mock OPEN AI API |
| 79 | + // the purpose of this is to test without real API calls in order to isolate issues |
| 80 | + return jest.fn().mockImplementation(() => { |
| 81 | + return { |
| 82 | + chat: { |
| 83 | + completions: { |
| 84 | + create: jest.fn().mockResolvedValue({ |
| 85 | + choices: [ |
| 86 | + { |
| 87 | + message: { |
| 88 | + content: "[1, 1, 0]", |
| 89 | + }, |
| 90 | + }, |
| 91 | + ], |
| 92 | + }), |
| 93 | + }, |
| 94 | + }, |
| 95 | + }; |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe("calculateQualScore", () => { |
| 100 | + it("should calculate quality score", async () => { |
| 101 | + const issue = { body: "issue body" } as Issue; |
| 102 | + const comment = { body: "comment body", user: { type: "User" } } as Comment; |
| 103 | + const comments = [comment, comment, comment] as Comment[]; |
| 104 | + const result = await calculateQualScore(issue, comments); |
| 105 | + expect(result).toBeDefined(); |
| 106 | + expect(result.tokens).toBeGreaterThan(0); |
| 107 | + expect(result.estimatedOptimalModel).toBeDefined(); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +// describe("countTokensOfConversation", () => { |
| 112 | +// it("should count tokens of conversation", () => { |
| 113 | +// const issue = { body: "issue body" } as Issue; |
| 114 | +// const comments = [{ body: "comment body", user: { type: "User" } }] as Comment[]; |
| 115 | +// const result = countTokensOfConversation(issue, comments); |
| 116 | +// expect(result).toBeGreaterThan(0); |
| 117 | +// }); |
| 118 | +// }); |
| 119 | + |
| 120 | +describe("gptRelevance", () => { |
| 121 | + it("should calculate gpt relevance", async () => { |
| 122 | + const result = await gptRelevance("gpt-3.5-turbo", "issue body", ["comment body"]); |
| 123 | + expect(result).toEqual([1, 1, 0]); |
| 124 | + }); |
| 125 | +}); |
0 commit comments