mirror of
https://github.com/ryanwtf7/hianime-api.git
synced 2026-04-17 21:41:44 +00:00
initial commit
This commit is contained in:
137
scripts/tests/vitest/controllers/comprehensive.test.ts
Normal file
137
scripts/tests/vitest/controllers/comprehensive.test.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import { describe, it, expect, vi, beforeEach, Mock } from 'vitest';
|
||||
import { Context } from 'hono';
|
||||
import homepageController from '../../../../src/controllers/homepage.controller';
|
||||
import detailpageController from '../../../../src/controllers/detailpage.controller';
|
||||
import searchController from '../../../../src/controllers/search.controller';
|
||||
import episodesController from '../../../../src/controllers/episodes.controller';
|
||||
import charactersController from '../../../../src/controllers/characters.controller';
|
||||
import characterDetailController from '../../../../src/controllers/characterDetail.controller';
|
||||
import listpageController from '../../../../src/controllers/listpage.controller';
|
||||
import topSearchController from '../../../../src/controllers/topSearch.controller';
|
||||
import schedulesController from '../../../../src/controllers/schedules.controller';
|
||||
import newsController from '../../../../src/controllers/news.controller';
|
||||
import suggestionController from '../../../../src/controllers/suggestion.controller';
|
||||
import nextEpisodeScheduleController from '../../../../src/controllers/nextEpisodeSchedule.controller';
|
||||
import randomController from '../../../../src/controllers/random.controller';
|
||||
import filterController from '../../../../src/controllers/filter.controller';
|
||||
import allGenresController from '../../../../src/controllers/allGenres.controller';
|
||||
import { mockHtmlData } from '../../data/mocks';
|
||||
|
||||
// Mock axiosInstance globally
|
||||
vi.mock('../../../../src/services/axiosInstance', () => ({
|
||||
axiosInstance: vi.fn(),
|
||||
}));
|
||||
|
||||
import { axiosInstance } from '../../../../src/services/axiosInstance';
|
||||
|
||||
const createMockContext = (
|
||||
params: Record<string, string> = {},
|
||||
query: Record<string, string> = {}
|
||||
) => {
|
||||
return {
|
||||
req: {
|
||||
param: (name?: string) => (name ? params[name] : params),
|
||||
query: (name?: string) => (name ? query[name] : query),
|
||||
},
|
||||
json: vi.fn(data => data),
|
||||
} as unknown as Context;
|
||||
};
|
||||
|
||||
describe('Controllers Comprehensive Suite', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
const mockSuccess = (data: string) =>
|
||||
(axiosInstance as Mock).mockResolvedValue({ success: true, data });
|
||||
|
||||
it('homepageController should return homepage data', async () => {
|
||||
mockSuccess(mockHtmlData.homepage);
|
||||
const result = (await homepageController()) as unknown as Record<string, unknown>;
|
||||
expect(result.spotlight).toBeDefined();
|
||||
});
|
||||
|
||||
it('detailpageController should return anime details', async () => {
|
||||
mockSuccess(mockHtmlData.detail);
|
||||
const result = await detailpageController(createMockContext({ id: '123' }));
|
||||
expect(result.title).toBe('Detail Anime');
|
||||
});
|
||||
|
||||
it('searchController should return search results', async () => {
|
||||
mockSuccess(mockHtmlData.search);
|
||||
const result = await searchController(createMockContext({}, { keyword: 'one' }));
|
||||
expect(result.response).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('episodesController should return episodes', async () => {
|
||||
mockSuccess(mockHtmlData.episodes);
|
||||
const result = await episodesController(createMockContext({ id: '123' }));
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('charactersController should return characters', async () => {
|
||||
mockSuccess(mockHtmlData.characters);
|
||||
const result = await charactersController(createMockContext({ id: '123' }));
|
||||
expect(result.response).toBeDefined();
|
||||
});
|
||||
|
||||
it('characterDetailController should return character details', async () => {
|
||||
mockSuccess(mockHtmlData.characterDetail);
|
||||
const result = await characterDetailController(createMockContext({ id: '123' }));
|
||||
expect(result.name).toBe('Character Full Name');
|
||||
});
|
||||
|
||||
it('listpageController should return anime list', async () => {
|
||||
mockSuccess(mockHtmlData.search);
|
||||
const result = await listpageController(createMockContext({ query: 'most-popular' }));
|
||||
expect(result.response).toBeDefined();
|
||||
});
|
||||
|
||||
it('topSearchController should return top search items', async () => {
|
||||
mockSuccess(mockHtmlData.topSearch);
|
||||
const result = await topSearchController(createMockContext());
|
||||
expect(result).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('schedulesController should return schedules', async () => {
|
||||
mockSuccess(JSON.stringify({ html: mockHtmlData.schedule }));
|
||||
const result = await schedulesController(createMockContext());
|
||||
expect(result.data).toBeDefined();
|
||||
});
|
||||
|
||||
it('newsController should return news items', async () => {
|
||||
mockSuccess(mockHtmlData.news);
|
||||
const result = await newsController(createMockContext());
|
||||
expect(result.news).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('suggestionController should return suggestions', async () => {
|
||||
mockSuccess(JSON.stringify({ html: mockHtmlData.suggestions }));
|
||||
const result = await suggestionController(createMockContext({}, { keyword: 'suggest' }));
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('nextEpisodeScheduleController should return next episode time', async () => {
|
||||
mockSuccess(mockHtmlData.scheduleNext);
|
||||
const result = await nextEpisodeScheduleController(createMockContext({ id: '123' }));
|
||||
expect(result).toBe('10:00');
|
||||
});
|
||||
|
||||
it('filterController should handle complex queries', async () => {
|
||||
mockSuccess(mockHtmlData.search);
|
||||
const result = await filterController(createMockContext({}, { keyword: 'one' }));
|
||||
expect(result.response).toBeDefined();
|
||||
});
|
||||
|
||||
it('allGenresController should return all genres', async () => {
|
||||
mockSuccess(mockHtmlData.homepage);
|
||||
const result = await allGenresController();
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
it('randomController should return random anime', async () => {
|
||||
mockSuccess(mockHtmlData.search);
|
||||
const result = await randomController(createMockContext());
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
});
|
||||
101
scripts/tests/vitest/extractors/comprehensive.test.ts
Normal file
101
scripts/tests/vitest/extractors/comprehensive.test.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { extractHomepage } from '../../../../src/extractor/extractHomepage';
|
||||
import { extractDetailpage } from '../../../../src/extractor/extractDetailpage';
|
||||
import { extractListPage } from '../../../../src/extractor/extractListpage';
|
||||
import { extractCharacters } from '../../../../src/extractor/extractCharacters';
|
||||
import { extractNews } from '../../../../src/extractor/extractNews';
|
||||
import { extractSchedule } from '../../../../src/extractor/extractSchedule';
|
||||
import { extractEpisodes } from '../../../../src/extractor/extractEpisodes';
|
||||
import { extractCharacterDetail } from '../../../../src/extractor/extractCharacterDetail';
|
||||
import { extractSuggestions } from '../../../../src/extractor/extractSuggestions';
|
||||
import { extractTopSearch } from '../../../../src/extractor/extractTopSearch';
|
||||
import { extractNextEpisodeSchedule } from '../../../../src/extractor/extractNextEpisodeSchedule';
|
||||
import { mockHtmlData } from '../../data/mocks';
|
||||
|
||||
describe('Extractors Comprehensive Suite', () => {
|
||||
describe('extractHomepage', () => {
|
||||
it('should extract spotlight items', () => {
|
||||
const result = extractHomepage(mockHtmlData.homepage);
|
||||
expect(result.spotlight).toHaveLength(1);
|
||||
expect(result.spotlight[0].title).toBe('Spotlight Anime');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractDetailpage', () => {
|
||||
it('should extract detail info', () => {
|
||||
const result = extractDetailpage(mockHtmlData.detail);
|
||||
expect(result.title).toBe('Detail Anime');
|
||||
expect(result.is18Plus).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractListPage', () => {
|
||||
it('should extract results from list page', () => {
|
||||
const result = extractListPage(mockHtmlData.search);
|
||||
expect(result.response).toHaveLength(1);
|
||||
expect(result.response[0].title).toBe('Search Result');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractCharacters', () => {
|
||||
it('should extract characters', () => {
|
||||
const result = extractCharacters(mockHtmlData.characters);
|
||||
expect(result.response).toHaveLength(1);
|
||||
expect(result.response[0].name).toBe('Character Name');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractNews', () => {
|
||||
it('should extract news items', () => {
|
||||
const result = extractNews(mockHtmlData.news);
|
||||
expect(result.news).toHaveLength(1);
|
||||
expect(result.news[0].title).toBe('News Title');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractSchedule', () => {
|
||||
it('should extract schedule', () => {
|
||||
const result = extractSchedule(mockHtmlData.schedule);
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].title).toBe('Scheduled Anime');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractEpisodes', () => {
|
||||
it('should extract episodes', () => {
|
||||
const result = extractEpisodes(mockHtmlData.episodes);
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].title).toBe('Episode 1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractCharacterDetail', () => {
|
||||
it('should extract character detail', () => {
|
||||
const result = extractCharacterDetail(mockHtmlData.characterDetail);
|
||||
expect(result.name).toBe('Character Full Name');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractSuggestions', () => {
|
||||
it('should extract suggestions', () => {
|
||||
const result = extractSuggestions(mockHtmlData.suggestions);
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].title).toBe('S1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractTopSearch', () => {
|
||||
it('should extract top search', () => {
|
||||
const result = extractTopSearch(mockHtmlData.topSearch);
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result[2].title).toBe('Top Title');
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractNextEpisodeSchedule', () => {
|
||||
it('should extract next episode schedule', () => {
|
||||
const result = extractNextEpisodeSchedule(mockHtmlData.scheduleNext);
|
||||
expect(result).toBe('10:00');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user