youtube_crawler.test.js 2.68 KB
/**
 * YouTube Trending Crawler Unit Tests
 */

const { YouTubeTrendingCrawler, getMockTrendingVideos, validateRegionCode } = require('./youtube_crawler');

describe('YouTubeTrendingCrawler', () => {
  test('should create crawler instance', () => {
    const crawler = new YouTubeTrendingCrawler({ useMock: true });
    expect(crawler).toBeDefined();
    expect(crawler.getRemainingQuota()).toBe(10000);
  });

  test('validateRegionCode should accept valid regions', () => {
    expect(validateRegionCode('US')).toBe(true);
    expect(validateRegionCode('JP')).toBe(true);
    expect(validateRegionCode('KR')).toBe(true);
    expect(validateRegionCode('XX')).toBe(false);
  });

  test('getMockTrendingVideos should return mock data', () => {
    const videos = getMockTrendingVideos({ regionCode: 'US', maxResults: 3 });
    expect(videos.length).toBe(3);
    expect(videos[0].video_id).toBeDefined();
    expect(videos[0].view_count).toBeDefined();
  });

  test('get_trending mock should return videos with required fields', async () => {
    const crawler = new YouTubeTrendingCrawler({ useMock: true });
    const videos = await crawler.get_trending('US', 2);
    expect(videos.length).toBe(2);
    const v = videos[0];
    expect(v.video_id).toBeDefined();
    expect(v.title).toBeDefined();
    expect(v.channel_id).toBeDefined();
    expect(v.channel_title).toBeDefined();
    expect(v.published_at).toBeDefined();
    expect(v.view_count).toBeDefined();
    expect(v.category_id).toBeDefined();
    expect(v.source_url).toBeDefined();
    expect(v.crawl_time).toBeDefined();
  });

  test('schema validation should pass for valid video', () => {
    const crawler = new YouTubeTrendingCrawler({ useMock: true });
    const result = crawler.validateSchema({
      video_id: 'AbCdEfGhIjK',
      title: 'Test',
      channel_id: 'UC_123',
      channel_title: 'Channel',
      published_at: '2024-01-01T00:00:00Z',
      view_count: 1000,
      category_id: '10',
      source_url: 'https://www.youtube.com/watch?v=AbCdEfGhIjK',
      crawl_time: '2024-01-01T00:00:00Z'
    });
    expect(result.valid).toBe(true);
  });

  test('save_to_json and save_to_csv should create files', () => {
    const fs = require('fs');
    const path = require('path');
    const crawler = new YouTubeTrendingCrawler({ useMock: true });
    const videos = crawler._getMockData('US', 1);

    const jsonPath = path.join('/tmp', 'yt_test.json');
    const csvPath = path.join('/tmp', 'yt_test.csv');

    crawler.save_to_json(videos, jsonPath);
    expect(fs.existsSync(jsonPath)).toBe(true);

    crawler.save_to_csv(videos, csvPath);
    expect(fs.existsSync(csvPath)).toBe(true);

    fs.unlinkSync(jsonPath);
    fs.unlinkSync(csvPath);
  });
});