diff --git a/src/sync/sync-service.ts b/src/sync/sync-service.ts index 5d24363..fbf732f 100644 --- a/src/sync/sync-service.ts +++ b/src/sync/sync-service.ts @@ -246,20 +246,24 @@ 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); @@ -267,6 +271,15 @@ export class SyncService { 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) && @@ -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; @@ -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 } : {}), }; diff --git a/tests/sync-service.test.ts b/tests/sync-service.test.ts index e897409..e06992f 100644 --- a/tests/sync-service.test.ts +++ b/tests/sync-service.test.ts @@ -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);