|
| 1 | +/** |
| 2 | + * INV-EDIT-02 — heal() contamination after recovery integration test. |
| 3 | + */ |
| 4 | + |
| 5 | +import * as Y from "yjs"; |
| 6 | +import { ReconciliationController } from "../src/runtime/reconciliationController"; |
| 7 | +import { EditorBindingManager } from "../src/sync/editorBinding"; |
| 8 | +import { TFile, MarkdownView } from "obsidian"; |
| 9 | + |
| 10 | +let passed = 0; |
| 11 | +let failed = 0; |
| 12 | + |
| 13 | +function assert(condition: boolean, msg: string) { |
| 14 | + if (condition) { |
| 15 | + console.log(` PASS ${msg}`); |
| 16 | + passed++; |
| 17 | + } else { |
| 18 | + console.error(` FAIL ${msg}`); |
| 19 | + failed++; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function makeTFile(path: string): TFile { |
| 24 | + const file = new TFile() as TFile & { path: string }; |
| 25 | + file.path = path; |
| 26 | + return file; |
| 27 | +} |
| 28 | + |
| 29 | +// ── Test 1: heal() contamination (S-B) — FIXED ──────────────────────────────── |
| 30 | +console.log("\n--- Test 1: heal() contamination (S-B) — FIXED ---"); |
| 31 | +(async () => { |
| 32 | + const path = "test.md"; |
| 33 | + const diskContent = "v2 (disk)"; |
| 34 | + const staleCrdt = "v1 (stale)"; |
| 35 | + const staleEditor = "v1 (stale)"; |
| 36 | + |
| 37 | + const doc = new Y.Doc(); |
| 38 | + const ytext = doc.getText("content"); |
| 39 | + ytext.insert(0, staleCrdt); |
| 40 | + |
| 41 | + const file = makeTFile(path); |
| 42 | + const fakeCm = { |
| 43 | + state: { |
| 44 | + field: () => ({}), |
| 45 | + doc: { length: staleEditor.length }, |
| 46 | + }, |
| 47 | + dispatch: () => {}, |
| 48 | + }; |
| 49 | + const view = { |
| 50 | + file, |
| 51 | + editor: { |
| 52 | + getValue: () => staleEditor, |
| 53 | + cm: fakeCm, |
| 54 | + }, |
| 55 | + leaf: { id: "leaf-1" }, |
| 56 | + } as any; |
| 57 | + Object.setPrototypeOf(view, MarkdownView.prototype); |
| 58 | + |
| 59 | + const fakeVaultSync = { |
| 60 | + provider: { |
| 61 | + __kind: "fake-provider", |
| 62 | + awareness: { |
| 63 | + setLocalStateField: () => {}, |
| 64 | + getLocalState: () => ({}), |
| 65 | + on: () => {}, |
| 66 | + off: () => {}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + ydoc: doc, |
| 70 | + getTextForPath: () => ytext, |
| 71 | + getFileIdForText: () => "file-1", |
| 72 | + getFileId: () => "file-1", |
| 73 | + serverAckTracker: { withActiveOpId: (_id: any, cb: any) => cb() }, |
| 74 | + ensureFile: (path: string, content: string) => { |
| 75 | + ytext.delete(0, ytext.length); |
| 76 | + ytext.insert(0, content); |
| 77 | + return ytext; |
| 78 | + }, |
| 79 | + }; |
| 80 | + |
| 81 | + const editorBindings = new EditorBindingManager( |
| 82 | + fakeVaultSync as any, |
| 83 | + false, |
| 84 | + ); |
| 85 | + (editorBindings as any).getCmView = () => fakeCm; |
| 86 | + editorBindings.bind(view as any, "device"); |
| 87 | + const b = (editorBindings as any).bindings.get("leaf-1"); |
| 88 | + if (b) b.lastEditorChangeAtMs = 0; |
| 89 | + |
| 90 | + const app = { |
| 91 | + vault: { |
| 92 | + read: async () => diskContent, |
| 93 | + adapter: { |
| 94 | + stat: async () => ({ mtime: 10, size: diskContent.length }), |
| 95 | + }, |
| 96 | + getAbstractFileByPath: (p: string) => (p === path ? file : null), |
| 97 | + }, |
| 98 | + workspace: { |
| 99 | + iterateAllLeaves: (cb: any) => { |
| 100 | + cb({ view }); |
| 101 | + }, |
| 102 | + }, |
| 103 | + }; |
| 104 | + |
| 105 | + const controller = new ReconciliationController({ |
| 106 | + app: app as any, |
| 107 | + getSettings: () => ({ deviceName: "device" }) as any, |
| 108 | + getRuntimeConfig: () => |
| 109 | + ({ |
| 110 | + maxFileSizeBytes: 0, |
| 111 | + maxFileSizeKB: 0, |
| 112 | + excludePatterns: [], |
| 113 | + externalEditPolicy: "always", |
| 114 | + }) as any, |
| 115 | + getVaultSync: () => fakeVaultSync as any, |
| 116 | + getDiskMirror: () => |
| 117 | + ({ |
| 118 | + updateDiskIndexForPath: async () => {}, |
| 119 | + isPreservedUnresolved: () => false, |
| 120 | + recordRepairEcho: async () => {}, |
| 121 | + }) as any, |
| 122 | + getBlobSync: () => null, |
| 123 | + getEditorBindings: () => editorBindings as any, |
| 124 | + getDiskIndex: () => ({}), |
| 125 | + setDiskIndex: () => {}, |
| 126 | + isMarkdownPathSyncable: () => true, |
| 127 | + shouldBlockFrontmatterIngest: () => false, |
| 128 | + refreshServerCapabilities: async () => {}, |
| 129 | + validateOpenEditorBindings: () => {}, |
| 130 | + onReconciled: () => {}, |
| 131 | + getAwaitingFirstProviderSyncAfterStartup: () => false, |
| 132 | + setAwaitingFirstProviderSyncAfterStartup: () => {}, |
| 133 | + saveDiskIndex: async () => {}, |
| 134 | + refreshStatusBar: () => {}, |
| 135 | + trace: () => {}, |
| 136 | + scheduleTraceStateSnapshot: () => {}, |
| 137 | + log: () => {}, |
| 138 | + }); |
| 139 | + |
| 140 | + // Trigger disk-authority recovery (IDLE branch) |
| 141 | + // We use "v2 (disk)" on disk, "v1 (stale)" in CRDT and Editor. |
| 142 | + await (controller as any).syncFileFromDisk(file, "modify"); |
| 143 | + |
| 144 | + assert( |
| 145 | + ytext.toString() === diskContent, |
| 146 | + `after recovery: CRDT equals disk content (content="${ytext.toString()}")`, |
| 147 | + ); |
| 148 | + |
| 149 | + const isActive = controller.isDiskAuthorityRecoveryActive(path); |
| 150 | + assert(isActive, "recovery lock is active"); |
| 151 | + |
| 152 | + // Simulate concurrent heal() call in the same tick. |
| 153 | + editorBindings.heal(view as any, "device", "concurrent-check", (p) => |
| 154 | + controller.isDiskAuthorityRecoveryActive(p), |
| 155 | + ); |
| 156 | + |
| 157 | + assert( |
| 158 | + ytext.toString() === diskContent, |
| 159 | + `SUCCESS: heal() skipped overwrite (content is still "${ytext.toString()}")`, |
| 160 | + ); |
| 161 | + |
| 162 | + doc.destroy(); |
| 163 | + process.exit(failed > 0 ? 1 : 0); |
| 164 | +})(); |
0 commit comments