feat: initial setup with WhatsApp CRM apps and sync services
This commit is contained in:
151
apps/whatsapp-sync/tests/database.test.js
Normal file
151
apps/whatsapp-sync/tests/database.test.js
Normal file
@@ -0,0 +1,151 @@
|
||||
var MockDatabase = function() {
|
||||
this.contacts = [];
|
||||
this.messages = [];
|
||||
this.tasks = [];
|
||||
this._prepared = {};
|
||||
};
|
||||
|
||||
MockDatabase.prototype.exec = function(sql) {
|
||||
return [];
|
||||
};
|
||||
|
||||
MockDatabase.prototype.prepare = function(sql) {
|
||||
var self = this;
|
||||
var stmt = {
|
||||
_sql: sql,
|
||||
run: function() { return { lastInsertRowid: 1, changes: 1 }; },
|
||||
get: function() { return undefined; },
|
||||
all: function() { return []; },
|
||||
};
|
||||
|
||||
if (sql.indexOf('INSERT INTO contacts') !== -1) {
|
||||
stmt.run = function(params) {
|
||||
self.contacts.push(params);
|
||||
return { lastInsertRowid: self.contacts.length, changes: 1 };
|
||||
};
|
||||
} else if (sql.indexOf('SELECT * FROM contacts WHERE phone') !== -1) {
|
||||
stmt.get = function(phone) {
|
||||
return self.contacts.find(function(c) { return c.phone === phone; });
|
||||
};
|
||||
} else if (sql.indexOf('SELECT * FROM contacts WHERE stage =') !== -1) {
|
||||
stmt.all = function(stage) {
|
||||
return self.contacts.filter(function(c) { return c.stage === stage; });
|
||||
};
|
||||
} else if (sql.indexOf('SELECT * FROM contacts WHERE payment_status') !== -1) {
|
||||
stmt.all = function(status) {
|
||||
return self.contacts.filter(function(c) { return c.payment_status === status; });
|
||||
};
|
||||
} else if (sql.indexOf('SELECT * FROM messages WHERE contact_phone') !== -1) {
|
||||
stmt.all = function(phone) {
|
||||
return self.messages.filter(function(m) { return m.contact_phone === phone; });
|
||||
};
|
||||
}
|
||||
|
||||
return stmt;
|
||||
};
|
||||
|
||||
MockDatabase.prototype.close = function() {};
|
||||
|
||||
jest.mock('better-sqlite3', function() {
|
||||
return function() {
|
||||
return new MockDatabase();
|
||||
};
|
||||
});
|
||||
|
||||
describe('4. SQLite Database Layer', function() {
|
||||
var db;
|
||||
var database;
|
||||
|
||||
beforeEach(function() {
|
||||
jest.clearAllMocks();
|
||||
database = require('../src/database');
|
||||
db = database.initialize(':memory:');
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
if (db && db.close) {
|
||||
db.close();
|
||||
}
|
||||
});
|
||||
|
||||
describe('Database Initialization', function() {
|
||||
test('test_database_schema_created_on_init - creates contacts table', function() {
|
||||
expect(db).toBeDefined();
|
||||
});
|
||||
|
||||
test('test_database_creates_messages_table - messages table exists', function() {
|
||||
expect(db).toBeDefined();
|
||||
});
|
||||
|
||||
test('test_database_creates_tasks_table - tasks table exists', function() {
|
||||
expect(db).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Contact Operations', function() {
|
||||
test('test_insert_contact - creates new contact', function() {
|
||||
var contact = {
|
||||
phone: '5511999999999',
|
||||
name: 'John Doe',
|
||||
stage: 'LEADS DE ENTRADA',
|
||||
};
|
||||
|
||||
var result = database.insertContact(db, contact);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
test('test_find_contact_by_phone - retrieves contact by phone', function() {
|
||||
var contact = {
|
||||
phone: '5511999999999',
|
||||
name: 'John Doe',
|
||||
stage: 'LEADS DE ENTRADA',
|
||||
};
|
||||
|
||||
database.insertContact(db, contact);
|
||||
var found = database.findContactByPhone(db, '5511999999999');
|
||||
|
||||
expect(found).toBeDefined();
|
||||
});
|
||||
|
||||
test('test_update_contact_stage - updates contact stage', function() {
|
||||
var contact = {
|
||||
phone: '5511999999999',
|
||||
name: 'John Doe',
|
||||
stage: 'LEADS DE ENTRADA',
|
||||
};
|
||||
|
||||
database.insertContact(db, contact);
|
||||
var result = database.updateContactStage(db, '5511999999999', 'DECIDINDO');
|
||||
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
test('test_list_contacts_by_stage - filters contacts by stage', function() {
|
||||
var contacts = database.listContactsByStage(db, 'LEADS DE ENTRADA');
|
||||
|
||||
expect(Array.isArray(contacts)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Message Operations', function() {
|
||||
test('test_insert_message - creates message record', function() {
|
||||
var message = {
|
||||
id: 'msg_123',
|
||||
contactPhone: '5511999999999',
|
||||
body: 'Hello world',
|
||||
timestamp: 1234567890,
|
||||
};
|
||||
|
||||
var result = database.insertMessage(db, message);
|
||||
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
test('test_find_messages_by_contact - retrieves messages for contact', function() {
|
||||
var messages = database.findMessagesByContact(db, '5511999999999');
|
||||
|
||||
expect(Array.isArray(messages)).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user