|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { withDriver, TMP_DIR } from "./test/preamble"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | +import { promisify } from "util"; |
| 6 | +import { getCurrentBuffer } from "./nvim/nvim"; |
| 7 | +import type { AbsFilePath } from "./utils/files"; |
| 8 | +import type { Line } from "./nvim/buffer"; |
| 9 | + |
| 10 | +const writeFile = promisify(fs.writeFile); |
| 11 | + |
| 12 | +describe("node/buffer-tracker.spec.ts", () => { |
| 13 | + it("should track buffer as not modified after initial read", async () => { |
| 14 | + await withDriver({}, async (driver) => { |
| 15 | + // Create a temporary file for testing |
| 16 | + const filePath = path.join(TMP_DIR, "test-file.txt") as AbsFilePath; |
| 17 | + await writeFile(filePath, "initial content"); |
| 18 | + |
| 19 | + await driver.editFile(filePath); |
| 20 | + const buffer = await getCurrentBuffer(driver.nvim); |
| 21 | + |
| 22 | + // Check buffer name matches the file path |
| 23 | + const bufferName = await buffer.getName(); |
| 24 | + expect(bufferName).toContain(filePath); |
| 25 | + |
| 26 | + // Get tracker and verify it's tracking the buffer |
| 27 | + const bufferTracker = driver.magenta.bufferTracker; |
| 28 | + await bufferTracker.trackBufferSync(filePath, buffer.id); |
| 29 | + |
| 30 | + // Check buffer is not modified |
| 31 | + const isModified = await bufferTracker.isBufferModifiedSinceSync( |
| 32 | + filePath, |
| 33 | + buffer.id, |
| 34 | + ); |
| 35 | + expect(isModified).toBe(false); |
| 36 | + |
| 37 | + // Verify sync info exists |
| 38 | + const syncInfo = bufferTracker.getSyncInfo(filePath); |
| 39 | + expect(syncInfo).toBeDefined(); |
| 40 | + expect(syncInfo?.bufnr).toBe(buffer.id); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should detect buffer as modified after edits without saving", async () => { |
| 45 | + await withDriver({}, async (driver) => { |
| 46 | + // Create a temporary file for testing |
| 47 | + const filePath = path.join( |
| 48 | + TMP_DIR, |
| 49 | + "test-file-modified.txt", |
| 50 | + ) as AbsFilePath; |
| 51 | + await writeFile(filePath, "initial content"); |
| 52 | + |
| 53 | + // Edit the file |
| 54 | + await driver.editFile(filePath); |
| 55 | + const buffer = await getCurrentBuffer(driver.nvim); |
| 56 | + |
| 57 | + // Check buffer name matches the file path |
| 58 | + const bufferName = await buffer.getName(); |
| 59 | + expect(bufferName).toContain(filePath); |
| 60 | + |
| 61 | + // Get tracker and initial state |
| 62 | + const bufferTracker = driver.magenta.bufferTracker; |
| 63 | + await bufferTracker.trackBufferSync(filePath, buffer.id); |
| 64 | + const initialSyncInfo = bufferTracker.getSyncInfo(filePath); |
| 65 | + |
| 66 | + // Modify the buffer without saving |
| 67 | + await buffer.setLines({ |
| 68 | + start: 0, |
| 69 | + end: -1, |
| 70 | + lines: ["modified content" as Line], |
| 71 | + }); |
| 72 | + |
| 73 | + // Check buffer is detected as modified |
| 74 | + const isModified = await bufferTracker.isBufferModifiedSinceSync( |
| 75 | + filePath, |
| 76 | + buffer.id, |
| 77 | + ); |
| 78 | + expect(isModified).toBe(true); |
| 79 | + |
| 80 | + // Verify mtime hasn't changed |
| 81 | + const currentSyncInfo = bufferTracker.getSyncInfo(filePath); |
| 82 | + expect(currentSyncInfo?.mtime).toBe(initialSyncInfo?.mtime); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should update sync info after writing changes to disk", async () => { |
| 87 | + await withDriver({}, async (driver) => { |
| 88 | + // Create a temporary file for testing |
| 89 | + const filePath = path.join(TMP_DIR, "test-file-write.txt") as AbsFilePath; |
| 90 | + await writeFile(filePath, "initial content"); |
| 91 | + |
| 92 | + // Edit the file |
| 93 | + await driver.editFile(filePath); |
| 94 | + const buffer = await getCurrentBuffer(driver.nvim); |
| 95 | + |
| 96 | + // Check buffer name matches the file path |
| 97 | + const bufferName = await buffer.getName(); |
| 98 | + expect(bufferName).toContain(filePath); |
| 99 | + |
| 100 | + // Get tracker and initial state |
| 101 | + const bufferTracker = driver.magenta.bufferTracker; |
| 102 | + await bufferTracker.trackBufferSync(filePath, buffer.id); |
| 103 | + const initialSyncInfo = bufferTracker.getSyncInfo(filePath); |
| 104 | + |
| 105 | + // Modify the buffer |
| 106 | + await buffer.setLines({ |
| 107 | + start: 0, |
| 108 | + end: -1, |
| 109 | + lines: ["modified content" as Line], |
| 110 | + }); |
| 111 | + |
| 112 | + // Save the buffer |
| 113 | + await driver.command("write"); |
| 114 | + |
| 115 | + // Track the sync again |
| 116 | + await bufferTracker.trackBufferSync(filePath, buffer.id); |
| 117 | + |
| 118 | + // Check buffer is no longer modified |
| 119 | + const isModified = await bufferTracker.isBufferModifiedSinceSync( |
| 120 | + filePath, |
| 121 | + buffer.id, |
| 122 | + ); |
| 123 | + expect(isModified).toBe(false); |
| 124 | + |
| 125 | + // Verify mtime has changed |
| 126 | + const updatedSyncInfo = bufferTracker.getSyncInfo(filePath); |
| 127 | + expect(updatedSyncInfo?.mtime).toBeGreaterThan( |
| 128 | + initialSyncInfo?.mtime as number, |
| 129 | + ); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should clear tracking info when requested", async () => { |
| 134 | + await withDriver({}, async (driver) => { |
| 135 | + // Create a temporary file for testing |
| 136 | + const filePath = path.join(TMP_DIR, "test-file-clear.txt") as AbsFilePath; |
| 137 | + await writeFile(filePath, "initial content"); |
| 138 | + |
| 139 | + // Edit the file |
| 140 | + await driver.editFile(filePath); |
| 141 | + const buffer = await getCurrentBuffer(driver.nvim); |
| 142 | + |
| 143 | + // Check buffer name matches the file path |
| 144 | + const bufferName = await buffer.getName(); |
| 145 | + expect(bufferName).toContain(filePath); |
| 146 | + |
| 147 | + // Get tracker and initial state |
| 148 | + const bufferTracker = driver.magenta.bufferTracker; |
| 149 | + await bufferTracker.trackBufferSync(filePath, buffer.id); |
| 150 | + |
| 151 | + // Verify sync info exists |
| 152 | + expect(bufferTracker.getSyncInfo(filePath)).toBeDefined(); |
| 153 | + |
| 154 | + // Clear tracking info |
| 155 | + bufferTracker.clearFileTracking(filePath); |
| 156 | + |
| 157 | + // Verify sync info is gone |
| 158 | + expect(bufferTracker.getSyncInfo(filePath)).toBeUndefined(); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments