-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvitest.setup.js
165 lines (141 loc) · 3.5 KB
/
vitest.setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Este archivo se ejecutará antes de cada test
import { vi } from 'vitest';
// Configuración global para los tests
// Mock fs module
const mockFs = {
existsSync: vi.fn(),
mkdirSync: vi.fn(),
appendFileSync: vi.fn(),
unlinkSync: vi.fn(),
rmdirSync: vi.fn(),
writeFileSync: vi.fn(),
readFileSync: vi.fn(),
promises: {
writeFile: vi.fn(),
readFile: vi.fn(),
mkdir: vi.fn(),
},
};
// Mock better-sqlite3
const mockDb = {
exec: vi.fn(),
prepare: vi.fn(() => ({
run: vi.fn(),
get: vi.fn(),
all: vi.fn(),
})),
close: vi.fn(),
};
// Mock path
const mockPath = {
dirname: vi.fn((p) => p.split('/').slice(0, -1).join('/')),
join: vi.fn((...args) => args.join('/')),
};
// Mock Logger
class MockLogger {
constructor(logDir = './logs') {
this.logDir = mockPath.dirname(logDir);
this.createLogDirectory();
}
createLogDirectory() {
if (!mockFs.existsSync('./logs')) {
mockFs.mkdirSync('./logs', { recursive: true });
}
}
getLogPath() {
const date = new Date().toISOString().split('T')[0];
return mockPath.join(this.logDir, `${date}.log`);
}
info(message) {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] INFO: ${message}\n`;
mockFs.appendFileSync(this.getLogPath(), logMessage);
}
error(message) {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] ERROR: ${message}\n`;
mockFs.appendFileSync(this.getLogPath(), logMessage);
}
warn(message) {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] WARN: ${message}\n`;
mockFs.appendFileSync(this.getLogPath(), logMessage);
}
debug(message) {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] DEBUG: ${message}\n`;
mockFs.appendFileSync(this.getLogPath(), logMessage);
}
}
const mockLogger = new MockLogger();
// Mock LanguageDetector
const mockLanguageDetector = {
detect: vi.fn((text) => (text ? 'en' : 'und')),
};
// Mock Cache implementation
class MockCache {
constructor(ttl = 100) {
this.cache = new Map();
this.ttl = ttl;
}
set(key, value) {
const timestamp = Date.now();
this.cache.set(key, { value, timestamp });
}
get(key) {
const item = this.cache.get(key);
if (!item) return undefined;
const age = Date.now() - item.timestamp;
if (age > this.ttl) {
this.cache.delete(key);
return undefined;
}
return item.value;
}
}
// Mock MorphologyProcessor
class MockMorphologyProcessor {
process(text) {
if (!text) return '';
// Simple mock implementation that removes 'ing' suffix
return text.replace(/ing$/, '');
}
}
// Mock fs module
vi.mock('fs', () => ({
...mockFs,
default: mockFs,
}));
// Mock better-sqlite3
vi.mock('better-sqlite3', () => ({
default: vi.fn(() => mockDb),
}));
// Mock path module
vi.mock('path', () => ({
__esModule: true,
default: mockPath,
dirname: mockPath.dirname,
join: mockPath.join,
}));
// Mock utils.js
vi.mock('src/utils.js', () => ({
Logger: MockLogger,
LanguageDetector: {
detect: mockLanguageDetector.detect,
},
Cache: MockCache,
MorphologyProcessor: MockMorphologyProcessor,
}));
// Clean up mocks after each test
vi.mock('vitest', async (importOriginal) => {
const vitest = await importOriginal();
return {
...vitest,
afterEach: (fn) => {
vi.clearAllMocks();
fn && fn();
},
};
});
// Exportar los mocks para uso en tests
export { mockFs, mockDb, mockLogger, mockPath };