naturalify
Version:
A CLI tool that adjusts a sentence to more natural English
73 lines (72 loc) • 3.74 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { describe, it, expect, vi } from 'vitest';
import HistoryRepository from '../historyRepository.js';
const historyData = {
communication: 'neutral',
original_sentence: 'looks good to me.',
transformed_sentence: 'I think it appears to be quite good.',
};
const mockRun = vi.hoisted(() => vi.fn().mockImplementation((_, __, callback) => {
callback(null);
}));
const mockAll = vi.hoisted(() => vi.fn().mockImplementation((_, callback) => {
callback(null);
}));
vi.mock('../../database/index.ts', () => ({
default: {
getInstance: vi.fn().mockReturnValue({
run: mockRun,
all: mockAll,
}),
},
}));
describe('repositories/historyRepository.ts', () => {
let historyRepository;
beforeEach(() => {
vi.clearAllMocks();
historyRepository = new HistoryRepository();
});
it('should save history correctly', () => __awaiter(void 0, void 0, void 0, function* () {
yield expect(historyRepository.saveHistory(historyData)).resolves.not.toThrow();
expect(mockRun).toHaveBeenCalledWith('INSERT INTO history (communication, original_sentence, transformed_sentence) VALUES (?, ?, ?)', [
historyData.communication,
historyData.original_sentence,
historyData.transformed_sentence,
], expect.any(Function));
}));
it('should get history correctly', () => __awaiter(void 0, void 0, void 0, function* () {
const rows = [historyData];
mockAll.mockImplementation((_, callback) => {
callback(null, rows);
});
yield expect(historyRepository.getHistory()).resolves.toEqual(rows);
expect(mockAll).toHaveBeenCalledWith('SELECT * FROM history ORDER BY timestamp DESC', expect.any(Function));
}));
it('should clear history correctly', () => __awaiter(void 0, void 0, void 0, function* () {
mockRun.mockImplementation((_, callback) => {
callback(null);
});
yield expect(historyRepository.clearHistory()).resolves.not.toThrow();
expect(mockRun).toHaveBeenCalledWith('DELETE FROM history', expect.any(Function));
}));
it('should handle error in saveHistory', () => __awaiter(void 0, void 0, void 0, function* () {
mockRun.mockImplementation((_, __, callback) => callback(new Error('Database error')));
yield expect(historyRepository.saveHistory(historyData)).rejects.toThrow('Database error');
}));
it('should handle error in getHistory', () => __awaiter(void 0, void 0, void 0, function* () {
mockAll.mockImplementation((_, callback) => callback(new Error('Database error'), null));
yield expect(historyRepository.getHistory()).rejects.toThrow('Database error');
}));
it('should handle error in clearHistory', () => __awaiter(void 0, void 0, void 0, function* () {
mockRun.mockImplementation((_, callback) => callback(new Error('Database error')));
yield expect(historyRepository.clearHistory()).rejects.toThrow('Database error');
}));
});