hadith-collections
Version:
A comprehensive npm package for searching and browsing hadith collections with Arabic and English support
286 lines (242 loc) • 9.74 kB
JavaScript
const HadithDB = require('../index.js');
const path = require('path');
describe('Hadith Collections', () => {
const dbPath = path.join(__dirname, '..', 'data', 'hadith.db');
let hadithDb;
beforeAll(async () => {
hadithDb = new HadithDB(dbPath);
await hadithDb.connect();
});
afterAll(async () => {
if (hadithDb) {
await hadithDb.close();
}
});
describe('Database initialization', () => {
it('should initialize database successfully', () => {
expect(hadithDb).not.toBeNull();
});
});
describe('Collections', () => {
it('should get collections as an array with plain text data', async () => {
const collections = await hadithDb.getCollections();
expect(Array.isArray(collections)).toBe(true);
expect(collections.length).toBeGreaterThan(0);
const firstCollection = collections[0];
expect(firstCollection).toStrictEqual(expect.objectContaining({
id: expect.any(Number),
title: expect.any(String),
title_en: expect.any(String)
}));
// Verify it contains actual plain text
expect(typeof firstCollection.title).toBe('string');
expect(firstCollection.title.length).toBeGreaterThan(0);
expect(typeof firstCollection.title_en).toBe('string');
expect(firstCollection.title_en.length).toBeGreaterThan(0);
});
it('should get specific collection with plain text content', async () => {
const collection = await hadithDb.getCollection(1);
expect(collection).not.toBeNull();
expect(collection).toStrictEqual(expect.objectContaining({
id: 1,
title: expect.any(String),
title_en: expect.any(String)
}));
// Verify plain text content
expect(collection.title).toBeTruthy();
expect(collection.title_en).toBeTruthy();
});
});
describe('Books', () => {
it('should get books with plain text titles', async () => {
const books = await hadithDb.getBooks(1);
expect(Array.isArray(books)).toBe(true);
expect(books.length).toBeGreaterThan(0);
const firstBook = books[0];
expect(firstBook).toStrictEqual(expect.objectContaining({
id: expect.any(Number),
collection_id: 1,
title: expect.any(String),
title_en: expect.any(String)
}));
// Verify plain text content
expect(firstBook.title.length).toBeGreaterThan(0);
expect(firstBook.title_en.length).toBeGreaterThan(0);
});
it('should get specific book with plain text data', async () => {
const book = await hadithDb.getBook(1, 1);
expect(book).not.toBeNull();
expect(book).toStrictEqual(expect.objectContaining({
id: 1,
collection_id: 1,
title: expect.any(String),
title_en: expect.any(String)
}));
// Verify content is plain text
expect(book.title).toBeTruthy();
expect(book.title_en).toBeTruthy();
});
});
describe('Chapters', () => {
it('should get chapters with plain text titles', async () => {
const chapters = await hadithDb.getChapters(1, 1);
expect(Array.isArray(chapters)).toBe(true);
if (chapters.length > 0) {
const firstChapter = chapters[0];
expect(firstChapter).toStrictEqual(expect.objectContaining({
id: expect.any(Number),
title: expect.any(String)
}));
// Verify plain text content
expect(firstChapter.title.length).toBeGreaterThan(0);
}
});
});
describe('Hadiths', () => {
it('should get hadiths with plain text content', async () => {
const hadiths = await hadithDb.getHadithsByCollection(1, { limit: 5 });
expect(Array.isArray(hadiths)).toBe(true);
expect(hadiths.length).toBeGreaterThan(0);
expect(hadiths.length).toBeLessThanOrEqual(5);
const firstHadith = hadiths[0];
expect(firstHadith).toStrictEqual(expect.objectContaining({
urn: expect.any(String),
content: expect.any(String),
collection_id: 1
}));
// Verify plain text content
expect(firstHadith.content).toBeTruthy();
expect(firstHadith.content.length).toBeGreaterThan(0);
expect(firstHadith.urn).toBeTruthy();
});
it('should get English hadiths with plain text content', async () => {
const englishHadiths = await hadithDb.getEnglishHadithsByCollection(1, { limit: 5 });
expect(Array.isArray(englishHadiths)).toBe(true);
if (englishHadiths.length > 0) {
const firstHadith = englishHadiths[0];
expect(firstHadith).toStrictEqual(expect.objectContaining({
urn: expect.any(String),
content: expect.any(String),
collection_id: 1
}));
// Verify plain text content
expect(firstHadith.content).toBeTruthy();
expect(firstHadith.content.length).toBeGreaterThan(0);
}
});
it('should get random hadith with plain text content', async () => {
const randomHadith = await hadithDb.getRandomHadith();
if (randomHadith) {
expect(randomHadith).toStrictEqual(expect.objectContaining({
urn: expect.any(String),
content: expect.any(String)
}));
// Verify plain text content
expect(randomHadith.content).toBeTruthy();
expect(randomHadith.content.length).toBeGreaterThan(0);
}
});
});
describe('Search functionality', () => {
it('should search Arabic hadiths and return plain text results', async () => {
const results = await hadithDb.searchArabic('الصلاة', { limit: 3 });
expect(Array.isArray(results)).toBe(true);
if (results.length > 0) {
const firstResult = results[0];
expect(firstResult).toStrictEqual(expect.objectContaining({
content: expect.any(String)
}));
// Verify plain text content
expect(firstResult.content).toBeTruthy();
expect(firstResult.content.length).toBeGreaterThan(0);
}
});
it('should search English hadiths and return plain text results', async () => {
const results = await hadithDb.searchEnglish('prayer', { limit: 3 });
expect(Array.isArray(results)).toBe(true);
if (results.length > 0) {
const firstResult = results[0];
expect(firstResult).toStrictEqual(expect.objectContaining({
content: expect.any(String)
}));
// Verify plain text content
expect(firstResult.content).toBeTruthy();
expect(firstResult.content.length).toBeGreaterThan(0);
}
});
it('should perform combined search with plain text results', async () => {
const results = await hadithDb.search('prayer', { limit: 3 });
expect(results).toStrictEqual(expect.objectContaining({
arabic: expect.any(Array),
english: expect.any(Array),
total: expect.any(Number)
}));
expect(results.total).toBe(results.arabic.length + results.english.length);
// Verify plain text content in results
if (results.arabic.length > 0) {
expect(results.arabic[0].content).toBeTruthy();
}
if (results.english.length > 0) {
expect(results.english[0].content).toBeTruthy();
}
});
});
describe('Statistics', () => {
it('should get collection statistics with numeric data', async () => {
const stats = await hadithDb.getCollectionStats();
expect(stats).toStrictEqual(expect.objectContaining({
collection_count: expect.any(Number),
total_books: expect.any(Number),
total_chapters: expect.any(Number)
}));
// Verify actual numeric values
expect(stats.collection_count).toBeGreaterThan(0);
expect(stats.total_books).toBeGreaterThan(0);
});
it('should get specific collection statistics with numeric data', async () => {
const collectionStats = await hadithDb.getCollectionStats(1);
expect(collectionStats).toStrictEqual(expect.objectContaining({
book_count: expect.any(Number)
}));
// Verify actual numeric values
expect(collectionStats.book_count).toBeGreaterThan(0);
});
});
describe('Scholars', () => {
it('should get scholars with plain text biographical data', async () => {
const scholars = await hadithDb.getScholars();
expect(Array.isArray(scholars)).toBe(true);
if (scholars.length > 0) {
const firstScholar = scholars[0];
expect(firstScholar).toStrictEqual(expect.objectContaining({
id: expect.any(Number)
}));
// Verify plain text content if available
if (firstScholar.name) {
expect(firstScholar.name.length).toBeGreaterThan(0);
}
}
});
});
describe('Database info', () => {
it('should get database info with plain text metadata', async () => {
const info = await hadithDb.getInfo();
expect(info).toStrictEqual(expect.objectContaining({
version: expect.any(String),
database_path: expect.any(String),
collections: expect.any(Array),
statistics: expect.any(Object)
}));
// Verify plain text content
expect(info.version).toBeTruthy();
expect(info.database_path).toBeTruthy();
expect(info.collections.length).toBeGreaterThan(0);
// Verify collections contain plain text
if (info.collections.length > 0) {
const firstCollection = info.collections[0];
expect(firstCollection.title).toBeTruthy();
expect(firstCollection.title_en).toBeTruthy();
}
});
});
});