Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/get-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const { data } = z.object({ file: z.string() }).safeParse(argv);
if (data) {
const path = data.file;
const state = drive.getPlaceholderState(path);
const pendingStates = drive.getPlaceholderWithStatePending();
logger.info({ state, pendingStates });
logger.info({ state });
} else {
logger.error("Por favor especifica un archivo con --file <path>");
}
115 changes: 114 additions & 1 deletion src/watcher/watcher.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import { beforeEach } from "vitest";
import { mockDeep } from "vitest-mock-extended";
import { Logger } from "winston";

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

import { OnAddDirService } from "./events/on-add-dir.service";
import { OnAddService } from "./events/on-add.service";
import { OnAllService } from "./events/on-all.service";
import { OnRawService } from "./events/on-raw.service";
import { Watcher } from "./watcher";
import { Addon } from "@/addon-wrapper";

describe("Watcher", () => {
const addon = mockDeep<Addon>();
Expand Down Expand Up @@ -237,4 +238,116 @@ describe("Watcher", () => {
expect(getEvents()).toEqual(["addDir", "addDir", "unlinkDir"]);
});
});

describe("When pin items", () => {
it("When pin a file, then emit one change event", async () => {
// Arrange
const syncRootPath = join(TEST_FILES, v4());
const file = join(syncRootPath, `${v4()}.txt`);
await setupWatcher(syncRootPath);
await writeFile(file, Buffer.alloc(1000));

// Act
await sleep(50);
execSync(`attrib +P ${file}`);
await sleep(50);

// Assert
expect(getEvents()).toEqual(["addDir", "add", "change"]);
// expect(addon.getPlaceholderState({ path: file })).toBe({ pinState: PinState.AlwaysLocal, syncState: SyncState.InSync });
});

it("When pin a folder, then do not emit any event", async () => {
// Arrange
const syncRootPath = join(TEST_FILES, v4());
const folder = join(syncRootPath, v4());
await setupWatcher(syncRootPath);
await mkdir(folder);

// Act
await sleep(50);
execSync(`attrib +P ${folder}`);
await sleep(50);

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

describe("When unpin items", () => {
it("When unpin a file, then emit one change event", async () => {
// Arrange
const syncRootPath = join(TEST_FILES, v4());
const file = join(syncRootPath, `${v4()}.txt`);
await setupWatcher(syncRootPath);
await writeFile(file, Buffer.alloc(1000));

// Act
await sleep(50);
execSync(`attrib +P ${file}`);
await sleep(50);
execSync(`attrib -P ${file}`);
await sleep(50);

// Assert
expect(getEvents()).toEqual(["addDir", "add", "change", "change"]);
// expect(addon.getPlaceholderState({ path: file })).toBe({ pinState: PinState.Unspecified, syncState: SyncState.InSync });
});

it("When unpin a folder, then do not emit any event", async () => {
// Arrange
const syncRootPath = join(TEST_FILES, v4());
const folder = join(syncRootPath, v4());
await setupWatcher(syncRootPath);
await mkdir(folder);

// Act
await sleep(50);
execSync(`attrib +P ${folder}`);
await sleep(50);
execSync(`attrib -P ${folder}`);
await sleep(50);

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

describe("When set items to online only", () => {
it("When set a file to online only, then emit one change event", async () => {
// Arrange
const syncRootPath = join(TEST_FILES, v4());
const file = join(syncRootPath, `${v4()}.txt`);
await setupWatcher(syncRootPath);
await writeFile(file, Buffer.alloc(1000));

// Act
await sleep(50);
execSync(`attrib -P +U ${file}`);
await sleep(50);

// Assert
expect(getEvents()).toEqual(["addDir", "add", "change"]);
// expect(addon.getPlaceholderState({ path: file })).toBe({ pinState: PinState.Unspecified, syncState: SyncState.InSync });
});

it("When set a folder to online only, then do not emit any event", async () => {
// Arrange
const syncRootPath = join(TEST_FILES, v4());
const folder = join(syncRootPath, v4());
await setupWatcher(syncRootPath);
await mkdir(folder);

// Act
await sleep(50);
execSync(`attrib -P +U ${folder}`);
await sleep(50);

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