Files
ai_web/server/stats.test.ts
2026-01-27 13:41:31 +08:00

94 lines
2.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
getProductCount,
getWebsiteCount,
getCategoryCount,
getProductsByDateRange,
getWebsitesByDateRange,
getCategoryDistribution,
getRecentProducts,
getRecentWebsites,
} from './db';
describe('Admin Statistics Functions', () => {
describe('Count Functions', () => {
it('should get product count', async () => {
const count = await getProductCount();
expect(typeof count).toBe('number');
expect(count).toBeGreaterThanOrEqual(0);
});
it('should get website count', async () => {
const count = await getWebsiteCount();
expect(typeof count).toBe('number');
expect(count).toBeGreaterThanOrEqual(0);
});
it('should get category count', async () => {
const count = await getCategoryCount();
expect(typeof count).toBe('number');
expect(count).toBeGreaterThanOrEqual(0);
});
});
describe('Trend Functions', () => {
it('should get products by date range', async () => {
const trend = await getProductsByDateRange(30);
expect(Array.isArray(trend)).toBe(true);
if (trend.length > 0) {
expect(trend[0]).toHaveProperty('date');
expect(trend[0]).toHaveProperty('count');
}
});
it('should get websites by date range', async () => {
const trend = await getWebsitesByDateRange(30);
expect(Array.isArray(trend)).toBe(true);
if (trend.length > 0) {
expect(trend[0]).toHaveProperty('date');
expect(trend[0]).toHaveProperty('count');
}
});
});
describe('Distribution Functions', () => {
it('should get category distribution', async () => {
const dist = await getCategoryDistribution();
expect(Array.isArray(dist)).toBe(true);
if (dist.length > 0) {
expect(dist[0]).toHaveProperty('categoryName');
expect(dist[0]).toHaveProperty('productCount');
}
});
});
describe('Recent Items Functions', () => {
it('should get recent products', async () => {
const products = await getRecentProducts(5);
expect(Array.isArray(products)).toBe(true);
expect(products.length).toBeLessThanOrEqual(5);
});
it('should get recent websites', async () => {
const websites = await getRecentWebsites(5);
expect(Array.isArray(websites)).toBe(true);
expect(websites.length).toBeLessThanOrEqual(5);
});
});
describe('Date Range Variations', () => {
it('should handle different date ranges', async () => {
const trend7 = await getProductsByDateRange(7);
const trend30 = await getProductsByDateRange(30);
const trend90 = await getProductsByDateRange(90);
expect(Array.isArray(trend7)).toBe(true);
expect(Array.isArray(trend30)).toBe(true);
expect(Array.isArray(trend90)).toBe(true);
});
});
});