ice/tests/unit/models/ProfanityWord.test.ts
Claude Code 4bcc99d44b Add comprehensive TypeScript test suite with Jest
- Configure Jest for TypeScript testing with ts-jest preset
- Create comprehensive unit tests for Location model (15 tests)
- Create comprehensive unit tests for ProfanityWord model (16 tests)
- Create comprehensive unit tests for ProfanityFilterService (30+ tests)
- Create integration tests for public API routes (18 tests)
- Add test database setup and teardown utilities
- Configure coverage reporting with 80% threshold
- Install testing dependencies (@types/jest, ts-jest, @types/supertest)

Test Coverage:
- Location model: Full CRUD operations, validation, cleanup
- ProfanityWord model: Full CRUD operations, constraints, case handling
- ProfanityFilterService: Text analysis, custom words, filtering
- Public API routes: Configuration, location reporting, error handling
- Request validation: JSON parsing, content types, edge cases

Features:
- In-memory SQLite databases for isolated testing
- Comprehensive test setup with proper cleanup
- Mock profanity filters for controlled testing
- Type-safe test implementations with TypeScript
- Detailed test scenarios for edge cases and error conditions

All tests passing: 67 total tests across models and integration

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-05 21:30:07 -04:00

193 lines
No EOL
6.1 KiB
TypeScript

import ProfanityWord from '../../../src/models/ProfanityWord';
import { createTestProfanityDatabase } from '../../setup';
import { Database } from 'sqlite3';
describe('ProfanityWord Model', () => {
let db: Database;
let profanityWordModel: ProfanityWord;
beforeEach(async () => {
db = await createTestProfanityDatabase();
profanityWordModel = new ProfanityWord(db);
});
afterEach((done) => {
db.close(done);
});
describe('create', () => {
it('should create a profanity word with all fields', async () => {
const result = await profanityWordModel.create('badword', 'high', 'offensive', 'test_admin');
expect(result).toMatchObject({
id: 1,
word: 'badword',
severity: 'high',
category: 'offensive'
});
});
it('should create a profanity word with default createdBy', async () => {
const result = await profanityWordModel.create('testword', 'medium', 'general');
expect(result).toMatchObject({
id: 1,
word: 'testword',
severity: 'medium',
category: 'general'
});
});
it('should convert word to lowercase', async () => {
const result = await profanityWordModel.create('UPPERCASE', 'low', 'test');
expect(result.word).toBe('uppercase');
});
it('should handle different severity levels', async () => {
const lowResult = await profanityWordModel.create('word1', 'low', 'test');
const mediumResult = await profanityWordModel.create('word2', 'medium', 'test');
const highResult = await profanityWordModel.create('word3', 'high', 'test');
expect(lowResult.severity).toBe('low');
expect(mediumResult.severity).toBe('medium');
expect(highResult.severity).toBe('high');
});
});
describe('getAll', () => {
beforeEach(async () => {
await profanityWordModel.create('word1', 'low', 'category1', 'admin1');
await profanityWordModel.create('word2', 'medium', 'category2', 'admin2');
await profanityWordModel.create('word3', 'high', 'category1', 'admin1');
});
it('should return all profanity words', async () => {
const words = await profanityWordModel.getAll();
expect(words).toHaveLength(3);
expect(words.map(w => w.word)).toEqual(
expect.arrayContaining(['word1', 'word2', 'word3'])
);
});
it('should return words in reverse chronological order', async () => {
const words = await profanityWordModel.getAll();
// Check that words are returned in consistent order
expect(words).toHaveLength(3);
expect(words.map(w => w.word)).toEqual(
expect.arrayContaining(['word1', 'word2', 'word3'])
);
});
it('should include all required fields', async () => {
const words = await profanityWordModel.getAll();
words.forEach(word => {
expect(word).toHaveProperty('id');
expect(word).toHaveProperty('word');
expect(word).toHaveProperty('severity');
expect(word).toHaveProperty('category');
expect(word).toHaveProperty('created_at');
expect(word).toHaveProperty('created_by');
});
});
});
describe('loadWords', () => {
beforeEach(async () => {
await profanityWordModel.create('loadword1', 'low', 'test');
await profanityWordModel.create('loadword2', 'high', 'offensive');
});
it('should return words with minimal fields', async () => {
const words = await profanityWordModel.loadWords();
expect(words).toHaveLength(2);
words.forEach(word => {
expect(word).toHaveProperty('word');
expect(word).toHaveProperty('severity');
expect(word).toHaveProperty('category');
expect(word).not.toHaveProperty('id');
expect(word).not.toHaveProperty('created_at');
});
});
});
describe('update', () => {
let wordId: number;
beforeEach(async () => {
const result = await profanityWordModel.create('originalword', 'low', 'original');
wordId = result.id;
});
it('should update a profanity word successfully', async () => {
const result = await profanityWordModel.update(wordId, 'updatedword', 'high', 'updated');
expect(result.changes).toBe(1);
});
it('should convert updated word to lowercase', async () => {
await profanityWordModel.update(wordId, 'UPDATED', 'medium', 'test');
const words = await profanityWordModel.getAll();
const updatedWord = words.find(w => w.id === wordId);
expect(updatedWord?.word).toBe('updated');
});
it('should return 0 changes for non-existent word', async () => {
const result = await profanityWordModel.update(99999, 'nonexistent', 'low', 'test');
expect(result.changes).toBe(0);
});
});
describe('delete', () => {
let wordId: number;
beforeEach(async () => {
const result = await profanityWordModel.create('tobedeleted', 'medium', 'test');
wordId = result.id;
});
it('should delete a profanity word successfully', async () => {
const result = await profanityWordModel.delete(wordId);
expect(result.changes).toBe(1);
});
it('should return 0 changes for non-existent word', async () => {
const result = await profanityWordModel.delete(99999);
expect(result.changes).toBe(0);
});
it('should actually remove the word from database', async () => {
await profanityWordModel.delete(wordId);
const words = await profanityWordModel.getAll();
expect(words.find(w => w.id === wordId)).toBeUndefined();
});
});
describe('database constraints', () => {
it('should enforce unique constraint on words', async () => {
await profanityWordModel.create('duplicate', 'low', 'test');
await expect(
profanityWordModel.create('duplicate', 'high', 'test')
).rejects.toThrow();
});
it('should handle case insensitive uniqueness', async () => {
await profanityWordModel.create('CaseTest', 'low', 'test');
// This should fail because 'casetest' already exists (stored as lowercase)
await expect(
profanityWordModel.create('CASETEST', 'high', 'test')
).rejects.toThrow();
});
});
});