|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { apiServices } from "../../../infrastructure/api/networkServices"; |
| 3 | +import { |
| 4 | + AdvisorMessage, |
| 5 | + getConversationAPI, |
| 6 | + saveConversationAPI, |
| 7 | +} from "../advisor.repository"; |
| 8 | + |
| 9 | +vi.mock("../../../infrastructure/api/networkServices", () => { |
| 10 | + return { |
| 11 | + apiServices: { |
| 12 | + get: vi.fn(), |
| 13 | + post: vi.fn(), |
| 14 | + patch: vi.fn(), |
| 15 | + put: vi.fn(), |
| 16 | + delete: vi.fn(), |
| 17 | + }, |
| 18 | + }; |
| 19 | +}); |
| 20 | + |
| 21 | +describe("Test Advisor Repository", () => { |
| 22 | + describe("getConversationAPI", () => { |
| 23 | + beforeEach(vi.clearAllMocks); |
| 24 | + |
| 25 | + afterEach(vi.clearAllMocks); |
| 26 | + it("should add the domain to the url and make a GET request", async () => { |
| 27 | + const mockResponse = { |
| 28 | + data: { |
| 29 | + domain: "test-domain", |
| 30 | + messages: [], |
| 31 | + }, |
| 32 | + status: 200, |
| 33 | + statusText: "OK", |
| 34 | + }; |
| 35 | + |
| 36 | + vi.mocked(apiServices.get).mockResolvedValue(mockResponse); |
| 37 | + |
| 38 | + await getConversationAPI("test-domain"); |
| 39 | + |
| 40 | + expect(apiServices.get).toHaveBeenCalledTimes(1); |
| 41 | + expect(apiServices.get).toHaveBeenCalledWith( |
| 42 | + `/advisor/conversations/test-domain`, |
| 43 | + ); |
| 44 | + }); |
| 45 | + it("should throw an error with status and data if the API call fails", async () => { |
| 46 | + const mockError = { |
| 47 | + response: { |
| 48 | + status: 404, |
| 49 | + data: { message: "Not Found" }, |
| 50 | + }, |
| 51 | + }; |
| 52 | + |
| 53 | + vi.mocked(apiServices.get).mockRejectedValue(mockError); |
| 54 | + |
| 55 | + await expect(getConversationAPI("test-domain")).rejects.toThrow(); |
| 56 | + |
| 57 | + try { |
| 58 | + await getConversationAPI("test-domain"); |
| 59 | + } catch (error) { |
| 60 | + expect(error).toEqual({ |
| 61 | + ...mockError, |
| 62 | + status: 404, |
| 63 | + data: { message: "Not Found" }, |
| 64 | + }); |
| 65 | + } |
| 66 | + }); |
| 67 | + it("should return the response data on successful API call", async () => { |
| 68 | + const mockResponse = { |
| 69 | + data: { |
| 70 | + domain: "test-domain", |
| 71 | + messages: [], |
| 72 | + }, |
| 73 | + status: 200, |
| 74 | + statusText: "OK", |
| 75 | + }; |
| 76 | + vi.mocked(apiServices.get).mockResolvedValue(mockResponse); |
| 77 | + |
| 78 | + const result = await getConversationAPI("test-domain"); |
| 79 | + expect(result).toEqual(mockResponse); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should throw error without response property for network errors", async () => { |
| 83 | + const networkError = new Error("Network timeout"); |
| 84 | + |
| 85 | + vi.mocked(apiServices.get).mockRejectedValue(networkError); |
| 86 | + |
| 87 | + await expect(getConversationAPI("test-domain")).rejects.toThrow( |
| 88 | + "Network timeout", |
| 89 | + ); |
| 90 | + }); |
| 91 | + }); |
| 92 | + describe("saveConversationAPI", () => { |
| 93 | + beforeEach(vi.clearAllMocks); |
| 94 | + |
| 95 | + afterEach(vi.clearAllMocks); |
| 96 | + const messages = [ |
| 97 | + { |
| 98 | + id: "1", |
| 99 | + role: "user", |
| 100 | + content: "Test message", |
| 101 | + createdAt: "2024-01-01T00:00:00Z", |
| 102 | + chartData: undefined, |
| 103 | + }, |
| 104 | + ]; |
| 105 | + |
| 106 | + it("should add the domain to the url and make a POST request with messages", async () => { |
| 107 | + const mockResponse = { |
| 108 | + data: { |
| 109 | + domain: "test-domain", |
| 110 | + messages: [], |
| 111 | + }, |
| 112 | + status: 200, |
| 113 | + statusText: "OK", |
| 114 | + }; |
| 115 | + |
| 116 | + vi.mocked(apiServices.post).mockResolvedValue(mockResponse); |
| 117 | + |
| 118 | + await saveConversationAPI("test-domain", messages as AdvisorMessage[]); |
| 119 | + |
| 120 | + expect(apiServices.post).toHaveBeenCalledTimes(1); |
| 121 | + expect(apiServices.post).toHaveBeenCalledWith( |
| 122 | + `/advisor/conversations/test-domain`, |
| 123 | + { messages }, |
| 124 | + ); |
| 125 | + }); |
| 126 | + it("should throw an error with status and data if the API call fails", async () => { |
| 127 | + const mockError = { |
| 128 | + response: { |
| 129 | + status: 500, |
| 130 | + data: { message: "Internal Server Error" }, |
| 131 | + }, |
| 132 | + }; |
| 133 | + |
| 134 | + vi.mocked(apiServices.post).mockRejectedValue(mockError); |
| 135 | + |
| 136 | + await expect( |
| 137 | + saveConversationAPI("test-domain", messages as AdvisorMessage[]), |
| 138 | + ).rejects.toThrow(); |
| 139 | + |
| 140 | + try { |
| 141 | + await saveConversationAPI("test-domain", messages as AdvisorMessage[]); |
| 142 | + } catch (error) { |
| 143 | + expect(error).toEqual({ |
| 144 | + ...mockError, |
| 145 | + status: 500, |
| 146 | + data: { message: "Internal Server Error" }, |
| 147 | + }); |
| 148 | + } |
| 149 | + }); |
| 150 | + it("should return the response data on successful API call", async () => { |
| 151 | + const mockResponse = { |
| 152 | + data: { |
| 153 | + domain: "test-domain", |
| 154 | + messages: [], |
| 155 | + }, |
| 156 | + status: 200, |
| 157 | + statusText: "OK", |
| 158 | + }; |
| 159 | + vi.mocked(apiServices.post).mockResolvedValue(mockResponse); |
| 160 | + |
| 161 | + const result = await saveConversationAPI( |
| 162 | + "test-domain", |
| 163 | + messages as AdvisorMessage[], |
| 164 | + ); |
| 165 | + expect(result).toEqual(mockResponse); |
| 166 | + }); |
| 167 | + |
| 168 | + it("should throw error without response property for network errors", async () => { |
| 169 | + const networkError = new Error("Connection refused"); |
| 170 | + |
| 171 | + vi.mocked(apiServices.post).mockRejectedValue(networkError); |
| 172 | + |
| 173 | + await expect( |
| 174 | + saveConversationAPI("test-domain", messages as AdvisorMessage[]), |
| 175 | + ).rejects.toThrow("Connection refused"); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments