147 lines
4.7 KiB
TypeScript
147 lines
4.7 KiB
TypeScript
import { initDB, createContact, createTask, getTaskById, getTasksByContactId, getPendingTasksByContactId, getOverdueTasks, updateTask, deleteTask, closeDB } from '@/lib/db'
|
|
|
|
beforeAll(async () => {
|
|
await initDB()
|
|
})
|
|
|
|
afterAll(() => {
|
|
closeDB()
|
|
})
|
|
|
|
beforeEach(() => {
|
|
const db = require('@/lib/db').getDB()
|
|
db.run('DELETE FROM tasks')
|
|
db.run('DELETE FROM messages')
|
|
db.run('DELETE FROM contacts')
|
|
})
|
|
|
|
describe('Task Management', () => {
|
|
describe('5.1 Task Creation Tests', () => {
|
|
it('test_create_task_with_required_fields', async () => {
|
|
const contact = createContact({ name: 'Test Contact', phone: '+5511999999999' })
|
|
|
|
const task = createTask({
|
|
contact_id: contact.id,
|
|
title: 'Follow up call',
|
|
due_date: '2026-04-10',
|
|
})
|
|
|
|
expect(task).toBeDefined()
|
|
expect(task.id).toBeGreaterThan(0)
|
|
expect(task.contact_id).toBe(contact.id)
|
|
expect(task.title).toBe('Follow up call')
|
|
expect(task.due_date).toBe('2026-04-10')
|
|
expect(task.status).toBe('pending')
|
|
expect(task.completed_at).toBeNull()
|
|
})
|
|
|
|
it('test_create_task_missing_due_date', async () => {
|
|
const contact = createContact({ name: 'Test Contact', phone: '+5511999999998' })
|
|
|
|
expect(() => {
|
|
createTask({
|
|
contact_id: contact.id,
|
|
title: 'Follow up call',
|
|
due_date: '',
|
|
})
|
|
}).toThrow('Title and due date are required')
|
|
})
|
|
|
|
it('test_create_task_missing_title', async () => {
|
|
const contact = createContact({ name: 'Test Contact', phone: '+5511999999997' })
|
|
|
|
expect(() => {
|
|
createTask({
|
|
contact_id: contact.id,
|
|
title: '',
|
|
due_date: '2026-04-10',
|
|
})
|
|
}).toThrow('Title and due date are required')
|
|
})
|
|
})
|
|
|
|
describe('5.2 Task Updates Tests', () => {
|
|
it('test_mark_task_completed', async () => {
|
|
const contact = createContact({ name: 'Test Contact', phone: '+5511999999996' })
|
|
const task = createTask({
|
|
contact_id: contact.id,
|
|
title: 'Follow up call',
|
|
due_date: '2026-04-10',
|
|
})
|
|
|
|
const updated = updateTask(task.id, { status: 'completed' })
|
|
|
|
expect(updated).toBeDefined()
|
|
expect(updated?.status).toBe('completed')
|
|
expect(updated?.completed_at).not.toBeNull()
|
|
})
|
|
|
|
it('test_update_task_due_date', async () => {
|
|
const contact = createContact({ name: 'Test Contact', phone: '+5511999999995' })
|
|
const task = createTask({
|
|
contact_id: contact.id,
|
|
title: 'Follow up call',
|
|
due_date: '2026-04-10',
|
|
})
|
|
|
|
const updated = updateTask(task.id, { due_date: '2026-04-15' })
|
|
|
|
expect(updated).toBeDefined()
|
|
expect(updated?.due_date).toBe('2026-04-15')
|
|
})
|
|
})
|
|
|
|
describe('5.3 Task Queries Tests', () => {
|
|
it('test_list_pending_tasks_for_contact', async () => {
|
|
const contact = createContact({ name: 'Test Contact', phone: '+5511999999994' })
|
|
|
|
createTask({ contact_id: contact.id, title: 'Task 1', due_date: '2026-04-10' })
|
|
createTask({ contact_id: contact.id, title: 'Task 2', due_date: '2026-04-12' })
|
|
|
|
const completedTask = createTask({ contact_id: contact.id, title: 'Task 3', due_date: '2026-04-08' })
|
|
updateTask(completedTask.id, { status: 'completed' })
|
|
|
|
const pendingTasks = getPendingTasksByContactId(contact.id)
|
|
|
|
expect(pendingTasks).toHaveLength(2)
|
|
expect(pendingTasks.every(t => t.status === 'pending')).toBe(true)
|
|
})
|
|
|
|
it('test_list_overdue_tasks', async () => {
|
|
const contact = createContact({ name: 'Test Contact', phone: '+5511999999993' })
|
|
|
|
createTask({ contact_id: contact.id, title: 'Overdue Task', due_date: '2020-01-01' })
|
|
createTask({ contact_id: contact.id, title: 'Future Task', due_date: '2030-01-01' })
|
|
|
|
const overdueTasks = getOverdueTasks()
|
|
|
|
expect(overdueTasks.length).toBeGreaterThan(0)
|
|
expect(overdueTasks.every(t => t.status === 'pending' && t.due_date < new Date().toISOString().split('T')[0])).toBe(true)
|
|
})
|
|
|
|
it('test_create_task_without_contact', async () => {
|
|
expect(() => {
|
|
createTask({
|
|
contact_id: 99999,
|
|
title: 'Follow up call',
|
|
due_date: '2026-04-10',
|
|
})
|
|
}).toThrow('Contact not found')
|
|
})
|
|
|
|
it('test_get_task_by_id', async () => {
|
|
const contact = createContact({ name: 'Test Contact', phone: '+5511999999992' })
|
|
const task = createTask({
|
|
contact_id: contact.id,
|
|
title: 'Test Task',
|
|
due_date: '2026-04-15',
|
|
})
|
|
|
|
const found = getTaskById(task.id)
|
|
|
|
expect(found).toBeDefined()
|
|
expect(found?.id).toBe(task.id)
|
|
expect(found?.title).toBe('Test Task')
|
|
})
|
|
})
|
|
}) |