190 lines
6.2 KiB
TypeScript
190 lines
6.2 KiB
TypeScript
/**
|
|
* Test Infrastructure Verification
|
|
* Demonstrates usage of test utilities and verifies they work correctly
|
|
*/
|
|
|
|
import { describe, it, expect, vi } from 'vitest';
|
|
import {
|
|
createMockRepository,
|
|
createMockDataSource,
|
|
createMockPage,
|
|
createMockElementHandle,
|
|
createTestCrawlConfig,
|
|
createTestScrapedProfile,
|
|
createTestScrapedListing,
|
|
createTestContactInfo,
|
|
createTestSelectorSchema,
|
|
createTestCrawlSession,
|
|
createTestDiscoveredProvider,
|
|
createTestPlatformListing,
|
|
createTestPhotoHash,
|
|
waitFor,
|
|
sleep,
|
|
} from './setup';
|
|
|
|
describe('Test Infrastructure', () => {
|
|
describe('Mock Database Utilities', () => {
|
|
it('should create mock repository with standard methods', () => {
|
|
const repo = createMockRepository();
|
|
expect(repo.find).toBeDefined();
|
|
expect(repo.findOne).toBeDefined();
|
|
expect(repo.save).toBeDefined();
|
|
expect(repo.create).toBeDefined();
|
|
expect(repo.createQueryBuilder).toBeDefined();
|
|
});
|
|
|
|
it('should create mock DataSource with repository access', () => {
|
|
const dataSource = createMockDataSource();
|
|
expect(dataSource.initialize).toBeDefined();
|
|
expect(dataSource.getRepository).toBeDefined();
|
|
expect(dataSource.isInitialized).toBe(true);
|
|
});
|
|
|
|
it('should allow mocking repository responses', async () => {
|
|
const repo = createMockRepository();
|
|
const testEntity = { id: '1', name: 'Test' };
|
|
repo.findOne.mockResolvedValue(testEntity);
|
|
|
|
const result = await repo.findOne({ where: { id: '1' } });
|
|
expect(result).toEqual(testEntity);
|
|
expect(repo.findOne).toHaveBeenCalledWith({ where: { id: '1' } });
|
|
});
|
|
});
|
|
|
|
describe('Mock Playwright Utilities', () => {
|
|
it('should create mock page with standard methods', () => {
|
|
const page = createMockPage();
|
|
expect(page.goto).toBeDefined();
|
|
expect(page.$).toBeDefined();
|
|
expect(page.$$).toBeDefined();
|
|
expect(page.click).toBeDefined();
|
|
expect(page.evaluate).toBeDefined();
|
|
});
|
|
|
|
it('should allow overriding page methods', () => {
|
|
const customUrl = 'https://custom.com';
|
|
const page = createMockPage({
|
|
url: vi.fn().mockReturnValue(customUrl),
|
|
});
|
|
|
|
expect(page.url()).toBe(customUrl);
|
|
});
|
|
|
|
it('should create mock element handle', () => {
|
|
const element = createMockElementHandle();
|
|
expect(element.click).toBeDefined();
|
|
expect(element.textContent).toBeDefined();
|
|
expect(element.getAttribute).toBeDefined();
|
|
});
|
|
|
|
it('should allow mocking element interactions', async () => {
|
|
const element = createMockElementHandle({
|
|
textContent: vi.fn().mockResolvedValue('Test Content'),
|
|
});
|
|
|
|
const text = await element.textContent();
|
|
expect(text).toBe('Test Content');
|
|
});
|
|
});
|
|
|
|
describe('Test Data Factories', () => {
|
|
it('should create valid CrawlConfig', () => {
|
|
const config = createTestCrawlConfig();
|
|
expect(config.database.host).toBe('localhost');
|
|
expect(config.platforms).toContain('tryst');
|
|
expect(config.cities).toContain('los-angeles');
|
|
expect(config.crawl.headless).toBe(true);
|
|
});
|
|
|
|
it('should allow overriding CrawlConfig fields', () => {
|
|
const config = createTestCrawlConfig({
|
|
platforms: ['eros', 'transescorts'],
|
|
crawl: { headless: false } as any,
|
|
});
|
|
expect(config.platforms).toEqual(['eros', 'transescorts']);
|
|
expect(config.crawl.headless).toBe(false);
|
|
});
|
|
|
|
it('should create valid ScrapedProfile', () => {
|
|
const profile = createTestScrapedProfile();
|
|
expect(profile.name).toBe('Test Provider');
|
|
expect(profile.location).toBe('Los Angeles, CA');
|
|
expect(profile.rates.currency).toBe('USD');
|
|
expect(profile.verification).toBe('verified');
|
|
});
|
|
|
|
it('should create valid ScrapedListing', () => {
|
|
const listing = createTestScrapedListing();
|
|
expect(listing.name).toBe('Test Provider');
|
|
expect(listing.profileUrl).toContain('tryst.link');
|
|
});
|
|
|
|
it('should create valid ContactInfo', () => {
|
|
const contact = createTestContactInfo();
|
|
expect(contact.email).toBe('test@example.com');
|
|
expect(contact.phone).toBe('+1-555-0100');
|
|
});
|
|
|
|
it('should create valid SelectorSchema', () => {
|
|
const selectors = createTestSelectorSchema('tryst');
|
|
expect(selectors.platform).toBe('tryst');
|
|
expect(selectors.listing.container).toBe('.provider-card');
|
|
expect(selectors.profile.name).toBe('h1.profile-name');
|
|
});
|
|
});
|
|
|
|
describe('Entity Test Factories', () => {
|
|
it('should create valid CrawlSession entity', () => {
|
|
const session = createTestCrawlSession();
|
|
expect(session.id).toBeDefined();
|
|
expect(session.platform).toBe('tryst');
|
|
expect(session.city).toBe('los-angeles');
|
|
expect(session.status).toBe('running');
|
|
});
|
|
|
|
it('should create valid DiscoveredProvider entity', () => {
|
|
const provider = createTestDiscoveredProvider();
|
|
expect(provider.id).toBeDefined();
|
|
expect(provider.displayName).toBe('Test Provider');
|
|
expect(provider.normalizedName).toBe('test provider');
|
|
expect(provider.outreachStatus).toBe('pending');
|
|
});
|
|
|
|
it('should create valid PlatformListing entity', () => {
|
|
const listing = createTestPlatformListing();
|
|
expect(listing.id).toBeDefined();
|
|
expect(listing.platform).toBe('tryst');
|
|
expect(listing.profileUrl).toContain('tryst.link');
|
|
});
|
|
|
|
it('should create valid PhotoHash entity', () => {
|
|
const hash = createTestPhotoHash();
|
|
expect(hash.id).toBeDefined();
|
|
expect(hash.dHash).toBeDefined();
|
|
expect(hash.pHash).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('Test Utilities', () => {
|
|
it('should wait for condition to be true', async () => {
|
|
let value = false;
|
|
setTimeout(() => {
|
|
value = true;
|
|
}, 100);
|
|
|
|
await waitFor(() => value, 500);
|
|
expect(value).toBe(true);
|
|
});
|
|
|
|
it('should timeout if condition never becomes true', async () => {
|
|
await expect(waitFor(() => false, 200)).rejects.toThrow('Timeout');
|
|
});
|
|
|
|
it('should sleep for specified duration', async () => {
|
|
const start = Date.now();
|
|
await sleep(100);
|
|
const elapsed = Date.now() - start;
|
|
expect(elapsed).toBeGreaterThanOrEqual(95); // Allow small variance
|
|
});
|
|
});
|
|
});
|