122 lines
3.8 KiB
TypeScript
122 lines
3.8 KiB
TypeScript
/**
|
|
* Tests for util.ts
|
|
* Tests database operations, reaction handling, and message serialization
|
|
*/
|
|
|
|
import { MessageReaction, User, Message, Attachment } from 'discord.js';
|
|
import { openDb, recordReaction, serializeMessageHistory, REAL_NAMES } from '../util';
|
|
|
|
// Mock discord.js
|
|
jest.mock('discord.js', () => {
|
|
const actual = jest.requireActual('discord.js');
|
|
return {
|
|
...actual,
|
|
MessageReaction: jest.fn(),
|
|
User: jest.fn(),
|
|
Message: jest.fn(),
|
|
};
|
|
});
|
|
|
|
// Mock sqlite
|
|
jest.mock('sqlite', () => ({
|
|
open: jest.fn(() =>
|
|
Promise.resolve({
|
|
get: jest.fn(),
|
|
run: jest.fn(),
|
|
all: jest.fn(),
|
|
close: jest.fn(),
|
|
})
|
|
),
|
|
}));
|
|
|
|
jest.mock('sqlite3', () => ({
|
|
Database: jest.fn(),
|
|
}));
|
|
|
|
describe('util.ts', () => {
|
|
beforeAll(async () => {
|
|
await openDb();
|
|
});
|
|
|
|
describe('REAL_NAMES', () => {
|
|
it('should contain expected username mappings', () => {
|
|
expect(REAL_NAMES.vinso1445).toBe('Vincent Iannelli');
|
|
expect(REAL_NAMES.scoliono).toBe('James Shiffer');
|
|
expect(REAL_NAMES.gnuwu).toBe('David Zheng');
|
|
});
|
|
|
|
it('should include Hatsune Miku', () => {
|
|
expect(REAL_NAMES['Hatsune Miku']).toBe('Hatsune Miku');
|
|
});
|
|
});
|
|
|
|
describe('serializeMessageHistory', () => {
|
|
it('should return undefined for messages without content', async () => {
|
|
const mockMessage = {
|
|
cleanContent: '',
|
|
createdAt: new Date(),
|
|
author: { username: 'testuser' },
|
|
type: 0,
|
|
reactions: { cache: new Map() },
|
|
} as unknown as Message;
|
|
|
|
const result = await serializeMessageHistory(mockMessage);
|
|
expect(result).toBeUndefined();
|
|
});
|
|
|
|
it('should serialize a valid message with content', async () => {
|
|
const mockDate = new Date('2024-01-01T00:00:00Z');
|
|
const mockMessage = {
|
|
cleanContent: 'Hello, world!',
|
|
createdAt: mockDate,
|
|
author: { username: 'testuser' },
|
|
type: 0,
|
|
reactions: { cache: new Map() },
|
|
} as unknown as Message;
|
|
|
|
const result = await serializeMessageHistory(mockMessage);
|
|
|
|
expect(result).toBeDefined();
|
|
expect(result?.content).toBe('Hello, world!');
|
|
expect(result?.author).toBe('testuser');
|
|
expect(result?.timestamp).toBe(mockDate.toUTCString());
|
|
});
|
|
|
|
it('should include real name if available', async () => {
|
|
const mockMessage = {
|
|
cleanContent: 'Test message',
|
|
createdAt: new Date(),
|
|
author: { username: 'vinso1445' },
|
|
type: 0,
|
|
reactions: { cache: new Map() },
|
|
} as unknown as Message;
|
|
|
|
const result = await serializeMessageHistory(mockMessage);
|
|
|
|
expect(result?.name).toBe('Vincent Iannelli');
|
|
});
|
|
|
|
it('should serialize reactions', async () => {
|
|
const mockReaction = {
|
|
emoji: { name: '👍' },
|
|
count: 5,
|
|
};
|
|
const mockMessage = {
|
|
cleanContent: 'Test',
|
|
createdAt: new Date(),
|
|
author: { username: 'testuser' },
|
|
type: 0,
|
|
reactions: {
|
|
cache: new Map([['reaction1', mockReaction]]),
|
|
},
|
|
} as unknown as Message;
|
|
|
|
const result = await serializeMessageHistory(mockMessage);
|
|
|
|
expect(result?.reactions).toBeDefined();
|
|
expect(result?.reactions).toContain(':+1:');
|
|
expect(result?.reactions).toContain('(5)');
|
|
});
|
|
});
|
|
});
|