From 557ed46c3d806bb8a446fa51216b044ae59bf6dd Mon Sep 17 00:00:00 2001 From: ernestjx <73307945+ernestjx@users.noreply.github.com> Date: Sun, 27 Oct 2024 20:25:38 +0800 Subject: [PATCH] clean up --- test/service/GenaiService.test.tsx | 97 ------------------- test/service/GoogleSignInService.test.tsx | 110 ---------------------- 2 files changed, 207 deletions(-) delete mode 100644 test/service/GenaiService.test.tsx delete mode 100644 test/service/GoogleSignInService.test.tsx diff --git a/test/service/GenaiService.test.tsx b/test/service/GenaiService.test.tsx deleted file mode 100644 index 3b7aebb..0000000 --- a/test/service/GenaiService.test.tsx +++ /dev/null @@ -1,97 +0,0 @@ -import { describe, it, expect, vi, beforeEach, Mock } from 'vitest'; -import { GenaiService } from '../../service/GenaiService'; // Adjust the import according to your project structure -import { Questions, Genai } from '@/types'; // Adjust the import according to your project structure - -// Mock the global fetch function -global.fetch = vi.fn(); - -const mockTopic: Questions.Topic = { - name: 'Math', - status: 'active', - edited: false, - skills: [{ - name: 'Algebra', - topicId: 1, - status: 'active', - edited: false } - ] -}; - -describe('GenaiService', () => { - beforeEach(() => { - vi.clearAllMocks(); - }); - - describe('generateMCQByTopicStream', () => { - it('should call fetch with correct parameters and handle stream response', async () => { - const mockCallback = vi.fn(); - const mockResponse = new Response( - new ReadableStream({ - start(controller) { - controller.enqueue(new TextEncoder().encode('event: data\n\ndata: {"question": "What is 2 + 2?"}\n')); - controller.enqueue(new TextEncoder().encode('event: end\n')); - controller.close(); - } - }) - ); - - (fetch as unknown as Mock).mockResolvedValueOnce(mockResponse); - - await GenaiService.generateMCQByTopicStream(5, mockTopic, mockCallback); - - expect(fetch).toHaveBeenCalledWith(expect.any(String), { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - credentials: 'include', - body: JSON.stringify({ - input: { - number: 5, - topic: 'Math', - skill: 'Algebra', - } - }) - }); - expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({ - question: 'What is 2 + 2?' - })); - }); - }); - - describe('generateMCQByTopic', () => { - it('should call fetch and return MCQ data', async () => { - const mockResponseData: Genai.InvokeResult = { - output: { question: 'What is 2 + 2?' } - }; - - (fetch as unknown as Mock).mockResolvedValueOnce({ - status: 200, - json: vi.fn().mockResolvedValueOnce(mockResponseData) - }); - - const result = await GenaiService.generateMCQByTopic(5, mockTopic); - - expect(fetch).toHaveBeenCalledWith(expect.any(String), { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - credentials: 'include', - body: JSON.stringify({ - input: { - number: 5, - topic: 'Math', - skill: 'Algebra', - } - }) - }); - expect(result).toEqual(mockResponseData.output); - }); - - it('should throw an error when the response status is not 200', async () => { - (fetch as unknown as Mock).mockResolvedValueOnce({ - status: 400, - json: vi.fn().mockResolvedValueOnce({}), - }); - - await expect(GenaiService.generateMCQByTopic(5, mockTopic)).rejects.toThrow('400 while saving MCQ'); - }); - }); -}); diff --git a/test/service/GoogleSignInService.test.tsx b/test/service/GoogleSignInService.test.tsx deleted file mode 100644 index 1503b80..0000000 --- a/test/service/GoogleSignInService.test.tsx +++ /dev/null @@ -1,110 +0,0 @@ -import { describe, it, expect, beforeEach, vi } from 'vitest'; -import { GoogleSigninService } from '../../service/GoogleSignInService'; -import { PKCECodeChallenge } from '@/types'; - -const mockFetch = vi.fn(); - -global.fetch = mockFetch; // Mocking the global fetch API - -describe('GoogleSigninService', () => { - const state = 'test-state'; - const codeChallenge = 'test-code-challenge'; - - beforeEach(() => { - vi.clearAllMocks(); - // Reset sessionStorage before each test - sessionStorage.clear(); - }); - - it('should generate random bytes', () => { - const randomBytes = GoogleSigninService.generateRandomBytes(); - expect(randomBytes).toHaveLength(16); // 8 bytes in hex - }); - - // it('should sign in and set window location.href', () => { - // const idpAuthorizeEndpoint = 'https://example.com/auth'; - // process.env.NEXT_PUBLIC_IDP_AuthorizeEndpoint = idpAuthorizeEndpoint; - // const clientId = 'test-client-id'; - // process.env.NEXT_PUBLIC_COGNITO_CLIENT_ID = clientId; - // process.env.NEXT_PUBLIC_RedirectUrl = 'https://example.com/callback'; - - // const hrefSpy = vi.spyOn(window.location, 'href', 'set'); - - // GoogleSigninService.signIn(state, codeChallenge); - - // expect(hrefSpy).toHaveBeenCalledWith( - // `${idpAuthorizeEndpoint}?response_type=code&client_id=${clientId}&state=${state}&code_challenge_method=S256&code_challenge=${codeChallenge}&scope=openid+email&identity_provider=Google&redirect_uri=https://example.com/callback` - // ); - // }); - - it('should generate code challenge', async () => { - const codeChallengeResponse: PKCECodeChallenge = await GoogleSigninService.generateCodeChallenge(); - - expect(codeChallengeResponse).toHaveProperty('codeVerifier'); - expect(codeChallengeResponse).toHaveProperty('codeChallenge'); - expect(codeChallengeResponse.codeVerifier).toHaveLength(56); // Adjust based on generateRandomString length - }); - - it('should get access token', async () => { - const mockResponse = { access_token: 'test-access-token' }; - mockFetch.mockResolvedValueOnce({ - ok: true, - json: async () => mockResponse, - }); - - const response = await GoogleSigninService.getAccessToken('verifier', 'authCode'); - expect(mockFetch).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ method: 'POST' })); - expect(response).toEqual(mockResponse); - }); - - it('should throw an error when get access token fails', async () => { - mockFetch.mockResolvedValueOnce({ ok: false, status: 400 }); - - await expect(GoogleSigninService.getAccessToken('verifier', 'authCode')).rejects.toThrow( - '400 at signing with google account.' - ); - }); - - it('should get authenticated and store user in sessionStorage', async () => { - const mockUserProfile = { id: 1, name: 'John Doe' }; - - // Mock the fetch response to return a successful response with the user profile - mockFetch.mockResolvedValueOnce({ - ok: true, - json: async () => mockUserProfile, - }); - - const response = await GoogleSigninService.getAuthenticated('verifier', 'authCode'); - - expect(mockFetch).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({ method: 'POST' })); - - // Check that the user is stored correctly in sessionStorage - expect(sessionStorage.getItem('USER')).toEqual(JSON.stringify(mockUserProfile)); - // expect(response).toEqual(mockUserProfile); // Expect the response to equal the mock user profile - }); - - it('should throw an error when get authenticated fails', async () => { - mockFetch.mockResolvedValueOnce({ ok: false, status: 401 }); - - await expect(GoogleSigninService.getAuthenticated('verifier', 'authCode')).rejects.toThrow( - '401 at signing with google account.' - ); - }); - - it('should sign out and clear sessionStorage', async () => { - mockFetch.mockResolvedValueOnce({ ok: true }); - - await GoogleSigninService.signOut(); - - expect(sessionStorage.getItem('IS_LOGIN')).toBe('false'); - expect(sessionStorage.getItem('USER')).toBeNull(); - }); - - it('should throw an error when sign out fails', async () => { - mockFetch.mockResolvedValueOnce({ ok: false, status: 500 }); - - await expect(GoogleSigninService.signOut()).rejects.toThrow('500 at signing out.'); - expect(sessionStorage.getItem('IS_LOGIN')).toBe('false'); - expect(sessionStorage.getItem('USER')).toBeNull(); - }); -});