-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathScope.test.js
More file actions
280 lines (203 loc) · 10.7 KB
/
Scope.test.js
File metadata and controls
280 lines (203 loc) · 10.7 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
const { Scope, MissingDefaultFilesOptionError } = require('#src/components/Scope');
const { EventEmitter } = require('node:events');
const assert = require('node:assert/strict');
const { join, basename } = require('node:path');
const { tmpdir } = require('node:os');
const { mkdtempSync, writeFileSync, rmSync } = require('node:fs');
const { stringify } = require('yaml');
const { spy } = require('sinon');
const { OptionsWatcher } = require('#src/components/OptionsWatcher');
const { Resources } = require('#src/resources/Resources');
const { EntryHandler } = require('#src/components/EntryHandler');
const { restartNeeded, resetRestartNeeded } = require('#src/components/requestRestart');
const { writeFile } = require('node:fs/promises');
const { waitFor } = require('./waitFor.js');
const { ApplicationScope } = require('#js/components/ApplicationScope');
describe('Scope', () => {
beforeEach(() => {
this.resources = new Resources();
this.server = {};
this.directory = mkdtempSync(join(tmpdir(), 'harper.unit-test.scope-'));
this.name = basename(this.directory);
this.configFilePath = join(this.directory, 'config.yaml');
this.testFilePath = join(this.directory, 'test.js');
writeFileSync(this.testFilePath, '"foo";');
resetRestartNeeded();
});
afterEach(() => {
resetRestartNeeded();
try {
rmSync(this.directory, { recursive: true, force: true });
// eslint-disable-next-line sonarjs/no-ignored-exceptions
} catch {
// best effort to clean up - but doesn't matter too much since this is a temp directory
}
});
it('should create a default entry handler', async () => {
writeFileSync(this.configFilePath, stringify({ [this.name]: { files: 'test.js' } }));
const scope = new Scope(
this.name,
this.directory,
this.configFilePath,
new ApplicationScope('test', this.resources, this.server)
);
const readySpy = spy();
scope.on('ready', readySpy);
await scope.ready;
assert.ok(readySpy.calledOnce, 'ready event should be emitted once');
assert.ok(scope instanceof EventEmitter, 'Scope should be an instance of EventEmitter');
assert.ok(scope.options instanceof OptionsWatcher, 'Scope should have an OptionsWatcher instance');
assert.ok(scope.resources instanceof Resources, 'Scope should have a resources property of type Map');
assert.ok(scope.server !== undefined, 'Scope should have a server property');
// Even though scope is ready, we haven't provided an entry handler yet so modifying a file matched by files option should not request a restart
await writeFile(this.testFilePath, '"bar";');
assert.equal(restartNeeded(), false, 'requestRestart should not be called');
const entryHandlerNoArgs = scope.handleEntry();
assert.ok(entryHandlerNoArgs instanceof EntryHandler, 'Entry handler should be created');
// Now, since there is not entry handler function, modifying the file should request a restart
await writeFile(this.testFilePath, '"baz";');
await waitFor(() => restartNeeded());
assert.equal(restartNeeded(), true, 'requestRestart should be called');
// even though it doesn't do anything this counts as an all handler
const entryHandlerFunctionArg = scope.handleEntry(() => {});
assert.ok(entryHandlerFunctionArg instanceof EntryHandler, 'Entry handler should be created');
assert.deepEqual(entryHandlerNoArgs, entryHandlerFunctionArg, 'Entry handlers should be the same');
const scopeCloseSpy = spy();
scope.on('close', scopeCloseSpy);
const scopeOptionsCloseSpy = spy();
scope.options.on('close', scopeOptionsCloseSpy);
const entryHandlerCloseSpy = spy();
entryHandlerNoArgs.on('close', entryHandlerCloseSpy);
scope.close();
assert.equal(scopeCloseSpy.callCount, 1, 'close event should be emitted once');
assert.equal(scopeOptionsCloseSpy.callCount, 1, 'close event for options should be emitted once');
assert.equal(entryHandlerCloseSpy.callCount, 1, 'close event for entry handler should be emitted once');
});
it('should create a default entry handler with urlPath', async () => {
writeFileSync(this.configFilePath, stringify({ [this.name]: { files: 'test.js', urlPath: 'abc' } }));
const scope = new Scope(
this.name,
this.directory,
this.configFilePath,
new ApplicationScope('test', this.resources, this.server)
);
const readySpy = spy();
scope.on('ready', readySpy);
await scope.ready;
assert.ok(readySpy.calledOnce, 'ready event should be emitted once');
assert.ok(scope instanceof EventEmitter, 'Scope should be an instance of EventEmitter');
assert.ok(scope.options instanceof OptionsWatcher, 'Scope should have an OptionsWatcher instance');
assert.ok(scope.resources instanceof Resources, 'Scope should have a resources property of type Map');
assert.ok(scope.server !== undefined, 'Scope should have a server property');
const handleEntrySpy = spy();
const entryHandler = scope.handleEntry(handleEntrySpy);
assert.ok(entryHandler instanceof EntryHandler, 'Entry handler should be created');
await writeFile(this.testFilePath, '"foo";');
await waitFor(() => handleEntrySpy.callCount > 0);
const callArgs = handleEntrySpy.getCall(0).args[0];
assert.equal(callArgs.eventType, 'add', 'handleEntry argument `eventType` should be `add`');
assert.equal(callArgs.entryType, 'file', 'handleEntry argument `entryType` should be `file`');
assert.equal(
callArgs.absolutePath,
this.testFilePath,
'handleEntry argument `absolutePath` should be the test file path'
);
assert.equal(callArgs.urlPath, '/abc/test.js', 'handleEntry argument `urlPath` should be `abc/test.js`');
assert.ok(callArgs.stats !== undefined, 'add event argument `stats` should be defined');
assert.ok(callArgs.stats.isFile(), 'add event argument `stats` should be a file');
const scopeCloseSpy = spy();
scope.on('close', scopeCloseSpy);
const scopeOptionsCloseSpy = spy();
scope.options.on('close', scopeOptionsCloseSpy);
const entryHandlerCloseSpy = spy();
entryHandler.on('close', entryHandlerCloseSpy);
scope.close();
assert.equal(scopeCloseSpy.callCount, 1, 'close event should be emitted once');
assert.equal(scopeOptionsCloseSpy.callCount, 1, 'close event for options should be emitted once');
assert.equal(entryHandlerCloseSpy.callCount, 1, 'close event for entry handler should be emitted once');
});
it('should call requestRestart if no entry handler is provided', async () => {
writeFileSync(this.configFilePath, stringify({ [this.name]: { files: '.' } }));
const scope = new Scope(this.name, this.directory, this.configFilePath, this.resources, this.server);
await scope.ready;
const entryHandler = scope.handleEntry();
// Wait for initial load to complete - the default behavior will trigger restart
await entryHandler.ready;
assert.equal(restartNeeded(), true, 'requestRestart was called');
scope.close();
});
it('should call requestRestart if no options handler is provided', async () => {
writeFileSync(this.configFilePath, stringify({ [this.name]: { files: '.' } }));
const scope = new Scope(this.name, this.directory, this.configFilePath, this.resources, this.server);
await scope.ready;
scope.handleEntry(() => {});
assert.equal(restartNeeded(), false, 'requestRestart was not called');
await writeFile(this.configFilePath, stringify({ [this.name]: { files: '.', foo: 'bar' } }));
await waitFor(() => restartNeeded());
assert.equal(restartNeeded(), true, 'requestRestart was called');
scope.close();
});
it('should emit error for missing default entry handler', async () => {
writeFileSync(this.configFilePath, stringify({ [this.name]: { foo: 'bar' } }));
const scope = new Scope(this.name, this.directory, this.configFilePath, this.resources, this.server);
await scope.ready;
const errorSpy = spy();
scope.on('error', errorSpy);
const entryHandler = scope.handleEntry();
assert.equal(entryHandler, undefined, 'Entry handler should be undefined');
assert.equal(errorSpy.callCount, 1, 'error event should be emitted once');
assert.deepEqual(
errorSpy.getCall(0).args,
[new MissingDefaultFilesOptionError()],
'error event should be a missing default files option error'
);
scope.handleEntry(() => {});
assert.equal(errorSpy.callCount, 2, 'error event should be emitted once');
assert.deepEqual(
errorSpy.getCall(1).args,
[new MissingDefaultFilesOptionError()],
'error event should be a missing default files option error'
);
assert.equal(restartNeeded(), false, 'requestRestart should not be called');
scope.close();
});
it('should support custom entry handlers', async () => {
writeFileSync(this.configFilePath, stringify({ [this.name]: { foo: 'bar' } }));
const scope = new Scope(this.name, this.directory, this.configFilePath, this.resources, this.server);
await scope.ready;
const customEntryHandlerPathOnlyArg = scope.handleEntry('.');
assert.ok(customEntryHandlerPathOnlyArg instanceof EntryHandler, 'Custom entry handler should be created');
// Reset restart flag - the first handler without a function triggers restart when it encounters files
resetRestartNeeded();
const customEntryHandlerPathAndFunctionArgs = scope.handleEntry('.', () => {});
assert.ok(customEntryHandlerPathAndFunctionArgs instanceof EntryHandler, 'Custom entry handler should be created');
assert.equal(restartNeeded(), false, 'requestRestart should not be called');
const entryHandleCloseSpy1 = spy();
const entryHandleCloseSpy2 = spy();
customEntryHandlerPathOnlyArg.on('close', entryHandleCloseSpy1);
customEntryHandlerPathAndFunctionArgs.on('close', entryHandleCloseSpy2);
scope.close();
assert.equal(entryHandleCloseSpy1.callCount, 1, 'close event for custom entry handler should be emitted once');
assert.equal(entryHandleCloseSpy2.callCount, 1, 'close event for custom entry handler should be emitted once');
});
it('should support synchronous handleEntry with event-based initial load tracking', async () => {
writeFileSync(this.configFilePath, stringify({ [this.name]: { files: 'test.js' } }));
const scope = new Scope(this.name, this.directory, this.configFilePath, this.resources, this.server);
await scope.ready;
const handleEntrySpy = spy();
// Call handleEntry - returns EntryHandler immediately
const entryHandler = scope.handleEntry(handleEntrySpy);
// Should return an EntryHandler immediately (not a Promise)
assert.ok(entryHandler instanceof EntryHandler, 'handleEntry should return EntryHandler synchronously');
// Can listen for the ready event if needed
const readySpy = spy();
entryHandler.on('ready', readySpy);
// Wait for initial load to complete
await entryHandler.ready;
assert.ok(readySpy.calledOnce, 'ready event should be emitted once');
// Handler should be called for initial files
await waitFor(() => handleEntrySpy.callCount > 0);
assert.ok(handleEntrySpy.callCount > 0, 'Entry handler should be called');
scope.close();
});
});