|
| 1 | +import { withNvimClient } from "../test/preamble.ts"; |
| 2 | +import { describe, expect, it } from "bun:test"; |
| 3 | +import { BufferAndFileManager } from "./file-and-buffer-manager.ts"; |
| 4 | +import path from "path"; |
| 5 | +import fs from "fs"; |
| 6 | +import { getCurrentBuffer, getcwd } from "../nvim/nvim.ts"; |
| 7 | +import type { MessageId } from "../chat/message.ts"; |
| 8 | +import type { Line } from "../nvim/buffer.ts"; |
| 9 | + |
| 10 | +describe("Neovim Plugin Tests", () => { |
| 11 | + it("basic rendering & update", async () => { |
| 12 | + await withNvimClient(async (nvim) => { |
| 13 | + const bufferAndFileManager = new BufferAndFileManager(nvim); |
| 14 | + const cwd = await getcwd(nvim); |
| 15 | + |
| 16 | + const absFilePath = path.resolve(cwd, "bun/test/fixtures/poem.txt"); |
| 17 | + { |
| 18 | + const res = await bufferAndFileManager.getFileContents( |
| 19 | + absFilePath, |
| 20 | + 1 as MessageId, |
| 21 | + ); |
| 22 | + |
| 23 | + expect(res).toEqual({ |
| 24 | + status: "ok", |
| 25 | + value: { |
| 26 | + messageId: 1 as MessageId, |
| 27 | + relFilePath: "bun/test/fixtures/poem.txt", |
| 28 | + content: `\ |
| 29 | +Moonlight whispers through the trees, |
| 30 | +Silver shadows dance with ease. |
| 31 | +Stars above like diamonds bright, |
| 32 | +Paint their stories in the night. |
| 33 | +`, |
| 34 | + }, |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + { |
| 39 | + const res = await bufferAndFileManager.getFileContents( |
| 40 | + absFilePath, |
| 41 | + 3 as MessageId, |
| 42 | + ); |
| 43 | + |
| 44 | + expect( |
| 45 | + res, |
| 46 | + "when the content hasn't changed, we should return the initial message id", |
| 47 | + ).toEqual({ |
| 48 | + status: "ok", |
| 49 | + value: { |
| 50 | + messageId: 1 as MessageId, |
| 51 | + relFilePath: "bun/test/fixtures/poem.txt", |
| 52 | + content: `\ |
| 53 | +Moonlight whispers through the trees, |
| 54 | +Silver shadows dance with ease. |
| 55 | +Stars above like diamonds bright, |
| 56 | +Paint their stories in the night. |
| 57 | +`, |
| 58 | + }, |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + await touchFile(absFilePath); |
| 63 | + { |
| 64 | + const res = await bufferAndFileManager.getFileContents( |
| 65 | + absFilePath, |
| 66 | + 5 as MessageId, |
| 67 | + ); |
| 68 | + |
| 69 | + expect( |
| 70 | + res, |
| 71 | + "after the file is touched, we update the returned messageId", |
| 72 | + ).toEqual({ |
| 73 | + status: "ok", |
| 74 | + value: { |
| 75 | + messageId: 5 as MessageId, |
| 76 | + relFilePath: "bun/test/fixtures/poem.txt", |
| 77 | + content: `\ |
| 78 | +Moonlight whispers through the trees, |
| 79 | +Silver shadows dance with ease. |
| 80 | +Stars above like diamonds bright, |
| 81 | +Paint their stories in the night. |
| 82 | +`, |
| 83 | + }, |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + await nvim.call("nvim_exec2", [`edit ${absFilePath}`, {}]); |
| 88 | + const buf = await getCurrentBuffer(nvim); |
| 89 | + await buf.setLines({ |
| 90 | + start: 0, |
| 91 | + end: -1, |
| 92 | + lines: ["new content"] as Line[], |
| 93 | + }); |
| 94 | + |
| 95 | + { |
| 96 | + const res = await bufferAndFileManager.getFileContents( |
| 97 | + absFilePath, |
| 98 | + 7 as MessageId, |
| 99 | + ); |
| 100 | + |
| 101 | + expect( |
| 102 | + res, |
| 103 | + "after the file is loaded into a buffer, we load the buffer content", |
| 104 | + ).toEqual({ |
| 105 | + status: "ok", |
| 106 | + value: { |
| 107 | + messageId: 7 as MessageId, |
| 108 | + relFilePath: "bun/test/fixtures/poem.txt", |
| 109 | + content: `new content`, |
| 110 | + }, |
| 111 | + }); |
| 112 | + } |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +async function touchFile(filePath: string): Promise<void> { |
| 118 | + const time = new Date(); |
| 119 | + await fs.promises.utimes(filePath, time, time); |
| 120 | +} |
0 commit comments