Skip to content

Commit a2bdde9

Browse files
Add modify file watcher test (#126)
1 parent a41d3d9 commit a2bdde9

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

src/virtual-drive.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@ class VirtualDrive {
168168
return result;
169169
}
170170

171-
private test(): void {
172-
console.log("Test");
173-
}
174-
175171
watchAndWait(
176172
path: string,
177173
queueManager: IQueueManager,

src/watcher/watcher.unit.test.ts

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Logger } from "winston";
1010

1111
import { Addon } from "@/addon-wrapper";
1212
import { QueueManager } from "@/queue/queue-manager";
13-
import { PinState, SyncState } from "@/types/placeholder.type";
1413
import { sleep } from "@/utils";
1514

1615
import { OnAddDirService } from "./events/on-add-dir.service";
@@ -48,7 +47,7 @@ describe("Watcher", () => {
4847
vi.clearAllMocks();
4948
});
5049

51-
describe("When call watchAndWait", () => {
50+
describe("[Watcher] When call watchAndWait", () => {
5251
it("When folder is empty, then emit one addDir event", async () => {
5352
// Arrange
5453
const syncRootPath = join(TEST_FILES, v4());
@@ -81,7 +80,7 @@ describe("Watcher", () => {
8180
});
8281
});
8382

84-
describe("When add items", () => {
83+
describe("[Watcher] When add items", () => {
8584
it("When add an empty folder, then emit one addDir event", async () => {
8685
// Arrange
8786
const syncRootPath = join(TEST_FILES, v4());
@@ -131,7 +130,36 @@ describe("Watcher", () => {
131130
});
132131
});
133132

134-
describe("When rename items", () => {
133+
describe("[Watcher] When modify items", () => {
134+
it("When modify a file, then emit one change event", async () => {
135+
// Arrange
136+
const syncRootPath = join(TEST_FILES, v4());
137+
const fileName = `${v4()}.txt`;
138+
const file = join(syncRootPath, fileName);
139+
await setupWatcher(syncRootPath);
140+
execSync(`echo "Content" > ${file}`);
141+
142+
// Act
143+
await sleep(50);
144+
execSync(`echo "More content" >> ${file}`);
145+
await sleep(50);
146+
147+
// Assert
148+
expect(getEvents()).toEqual(["addDir", "add", "change"]);
149+
expect(onRaw.execute).toHaveBeenCalledWith(
150+
expect.objectContaining({
151+
event: "change",
152+
path: fileName,
153+
details: {
154+
watchedPath: file,
155+
// TODO: why does not include prev and curr stats
156+
},
157+
}),
158+
);
159+
});
160+
});
161+
162+
describe("[Addon] When rename items", () => {
135163
it("When rename a file, then do not emit any event", async () => {
136164
// Arrange
137165
const syncRootPath = join(TEST_FILES, v4());
@@ -167,7 +195,7 @@ describe("Watcher", () => {
167195
});
168196
});
169197

170-
describe("When move items", () => {
198+
describe("[Addon] When move items", () => {
171199
it("When move a file to a folder, then do not emit any event", async () => {
172200
// Arrange
173201
const syncRootPath = join(TEST_FILES, v4());
@@ -205,7 +233,7 @@ describe("Watcher", () => {
205233
});
206234
});
207235

208-
describe("When delete items", () => {
236+
describe("[Addon] When delete items", () => {
209237
it("When delete a file, then emit one unlink event", async () => {
210238
// Arrange
211239
const syncRootPath = join(TEST_FILES, v4());
@@ -239,11 +267,12 @@ describe("Watcher", () => {
239267
});
240268
});
241269

242-
describe("When pin items", () => {
270+
describe("[Watcher] When pin items", () => {
243271
it("When pin a file, then emit one change event", async () => {
244272
// Arrange
245273
const syncRootPath = join(TEST_FILES, v4());
246-
const file = join(syncRootPath, `${v4()}.txt`);
274+
const fileName = `${v4()}.txt`;
275+
const file = join(syncRootPath, fileName);
247276
await setupWatcher(syncRootPath);
248277
await writeFile(file, Buffer.alloc(1000));
249278

@@ -254,7 +283,16 @@ describe("Watcher", () => {
254283

255284
// Assert
256285
expect(getEvents()).toEqual(["addDir", "add", "change"]);
257-
// expect(addon.getPlaceholderState({ path: file })).toBe({ pinState: PinState.AlwaysLocal, syncState: SyncState.InSync });
286+
expect(onRaw.execute).toHaveBeenCalledWith(
287+
expect.objectContaining({
288+
event: "change",
289+
path: fileName,
290+
details: {
291+
watchedPath: file,
292+
// TODO: why does not include prev and curr stats
293+
},
294+
}),
295+
);
258296
});
259297

260298
it("When pin a folder, then do not emit any event", async () => {
@@ -271,15 +309,15 @@ describe("Watcher", () => {
271309

272310
// Assert
273311
expect(getEvents()).toEqual(["addDir", "addDir"]);
274-
// expect(addon.getPlaceholderState({ path: folder })).toBe({ pinState: PinState.AlwaysLocal, syncState: SyncState.InSync });
275312
});
276313
});
277314

278-
describe("When unpin items", () => {
315+
describe("[Watcher] When unpin items", () => {
279316
it("When unpin a file, then emit one change event", async () => {
280317
// Arrange
281318
const syncRootPath = join(TEST_FILES, v4());
282-
const file = join(syncRootPath, `${v4()}.txt`);
319+
const fileName = `${v4()}.txt`;
320+
const file = join(syncRootPath, fileName);
283321
await setupWatcher(syncRootPath);
284322
await writeFile(file, Buffer.alloc(1000));
285323

@@ -292,7 +330,16 @@ describe("Watcher", () => {
292330

293331
// Assert
294332
expect(getEvents()).toEqual(["addDir", "add", "change", "change"]);
295-
// expect(addon.getPlaceholderState({ path: file })).toBe({ pinState: PinState.Unspecified, syncState: SyncState.InSync });
333+
expect(onRaw.execute).toHaveBeenCalledWith(
334+
expect.objectContaining({
335+
event: "change",
336+
path: fileName,
337+
details: {
338+
watchedPath: file,
339+
// TODO: why does not include prev and curr stats
340+
},
341+
}),
342+
);
296343
});
297344

298345
it("When unpin a folder, then do not emit any event", async () => {
@@ -311,15 +358,15 @@ describe("Watcher", () => {
311358

312359
// Assert
313360
expect(getEvents()).toEqual(["addDir", "addDir"]);
314-
// expect(addon.getPlaceholderState({ path: folder })).toBe({ pinState: PinState.Unspecified, syncState: SyncState.InSync });
315361
});
316362
});
317363

318-
describe("When set items to online only", () => {
364+
describe("[Watcher] When set items to online only", () => {
319365
it("When set a file to online only, then emit one change event", async () => {
320366
// Arrange
321367
const syncRootPath = join(TEST_FILES, v4());
322-
const file = join(syncRootPath, `${v4()}.txt`);
368+
const fileName = `${v4()}.txt`;
369+
const file = join(syncRootPath, fileName);
323370
await setupWatcher(syncRootPath);
324371
await writeFile(file, Buffer.alloc(1000));
325372

@@ -330,7 +377,16 @@ describe("Watcher", () => {
330377

331378
// Assert
332379
expect(getEvents()).toEqual(["addDir", "add", "change"]);
333-
// expect(addon.getPlaceholderState({ path: file })).toBe({ pinState: PinState.Unspecified, syncState: SyncState.InSync });
380+
expect(onRaw.execute).toHaveBeenCalledWith(
381+
expect.objectContaining({
382+
event: "change",
383+
path: fileName,
384+
details: {
385+
watchedPath: file,
386+
// TODO: why does not include prev and curr stats
387+
},
388+
}),
389+
);
334390
});
335391

336392
it("When set a folder to online only, then do not emit any event", async () => {
@@ -347,7 +403,6 @@ describe("Watcher", () => {
347403

348404
// Assert
349405
expect(getEvents()).toEqual(["addDir", "addDir"]);
350-
// expect(addon.getPlaceholderState({ path: folder })).toBe({ pinState: PinState.Unspecified, syncState: SyncState.InSync });
351406
});
352407
});
353408
});

0 commit comments

Comments
 (0)