Skip to content
Open
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
41 changes: 30 additions & 11 deletions src/sync/sync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,27 +246,40 @@ export class SyncService {
existing.vaultPath = vaultPath;
}
}
const localMd5 = localBytes === null ? null : md5(localBytes);

// A tracked file whose local copy diverges from its baseline is a local
// edit — protect it without a cloud round-trip.
if (
localBytes !== null &&
(!existing ||
existing.vaultPath !== vaultPath ||
!sameMd5(md5(localBytes), existing.md5))
localMd5 !== null &&
existing &&
(existing.vaultPath !== vaultPath || !sameMd5(localMd5, existing.md5))
) {
return {
status: "protected",
vaultPath,
...(existing?.pageCount !== undefined
...(existing.pageCount !== undefined
? { pageCount: existing.pageCount }
: {}),
};
}

let descriptor;
try {
descriptor = await this.cloud.getDownloadDescriptor(input.file.id);
} catch (error) {
throw this.downloadError(input.file, error);
}

// An untracked local file (e.g. a folder removed from mirroring and added
// back, leaving files in place without a baseline) is adopted when it
// already matches the Cloud file, and protected otherwise.
const adopting =
localMd5 !== null && !existing && sameMd5(localMd5, descriptor.md5);
if (localMd5 !== null && !existing && !adopting) {
return { status: "protected", vaultPath };
}

if (
existing &&
sameMd5(existing.md5, descriptor.md5) &&
Expand All @@ -283,10 +296,14 @@ export class SyncService {
}

let bytes: Uint8Array;
try {
bytes = await this.cloud.download(descriptor.url);
} catch (error) {
throw this.downloadError(input.file, error);
if (adopting && localBytes !== null) {
bytes = localBytes;
} else {
try {
bytes = await this.cloud.download(descriptor.url);
} catch (error) {
throw this.downloadError(input.file, error);
}
}
const isNote = input.file.fileName.toLocaleLowerCase().endsWith(".note");
let pageCount: number | undefined;
Expand Down Expand Up @@ -316,13 +333,15 @@ export class SyncService {
...(existing?.watchHooks ? { watchHooks: existing.watchHooks } : {}),
};

await this.vault.writeBinary(vaultPath, bytes);
if (!adopting) {
await this.vault.writeBinary(vaultPath, bytes);
}
manifest.files[input.file.id] = entry;
if (!callerManifest) {
await saveManifest(this.vault, this.manifestPath, manifest);
}
return {
status: "mirrored",
status: adopting ? "skipped" : "mirrored",
vaultPath,
...(pageCount !== undefined ? { pageCount } : {}),
};
Expand Down
40 changes: 40 additions & 0 deletions tests/sync-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,46 @@ describe("SyncService mirror", () => {
expect(cloud.download).not.toHaveBeenCalled();
});

it("adopts an untracked local file that matches the Cloud file", async () => {
const vault = new MemoryVault();
const { cloud, service } = createService(vault);
const manifest = emptyManifest();
const input = {
file,
remotePath: "/Note/Journal/2026/7 July 2026.note",
};
// A leftover local file with no manifest baseline (e.g. a folder removed
// from mirroring and added back), matching the Cloud bytes.
const vaultPath = "supernote/Note/Journal/2026/7 July 2026.note";
await vault.writeBinary(vaultPath, new Uint8Array([9]));
cloud.download.mockClear();

await expect(service.mirrorFile(input, manifest)).resolves.toMatchObject({
status: "skipped",
vaultPath,
});
expect(cloud.download).not.toHaveBeenCalled();
expect(manifest.files[file.id]?.md5).toBe(descriptor.md5);
});

it("still protects an untracked local file that differs from the Cloud", async () => {
const vault = new MemoryVault();
const { service } = createService(vault);
const manifest = emptyManifest();
const input = {
file,
remotePath: "/Note/Journal/2026/7 July 2026.note",
};
const vaultPath = "supernote/Note/Journal/2026/7 July 2026.note";
await vault.writeBinary(vaultPath, new Uint8Array([1, 2, 3]));

await expect(service.mirrorFile(input, manifest)).resolves.toMatchObject({
status: "protected",
vaultPath,
});
expect(manifest.files[file.id]).toBeUndefined();
});

it("removes a cloud-deleted file from the Mirror and its manifest", async () => {
const vault = new MemoryVault();
const { service } = createService(vault);
Expand Down