Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #1

Merged
merged 2 commits into from
Dec 31, 2024
Merged
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
5 changes: 5 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: 20.x
- name: Setup Neovim
uses: rhysd/action-setup-vim@v1
with:
neovim: true
version: stable
- name: Install bun
uses: oven-sh/setup-bun@v2
- name: Install dependencies
Expand Down
Binary file modified bun.lockb
Binary file not shown.
16 changes: 16 additions & 0 deletions src/attach.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from "bun:test";
import { withNvimClient } from "../tests/preamble";

describe("src/attach.test.ts", () => {
it("nvim_buf_set_lines with a large file", async () => {
await withNvimClient(async (nvim) => {
const lines: string[] = [];
for (let line = 0; line < 500; line += 1) {
lines.push("x".repeat(100));
}
await nvim.call("nvim_buf_set_lines", [0, 0, -1, false, lines]);
const roundtripLines = await nvim.call("nvim_buf_get_lines", [0, 0, -1, false]);
expect(roundtripLines).toEqual(lines);
});
});
});
43 changes: 23 additions & 20 deletions src/attach.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Packr, UnpackrStream, addExtension, unpack } from "msgpackr";
import { EventEmitter } from "node:events";
import net from "node:net";
import { createLogger, prettyRPCMessage } from "./logger.ts";
import {
MessageType,
Expand Down Expand Up @@ -41,26 +42,28 @@ export async function attach<ApiInfo extends BaseEvents = BaseEvents>({
let lastReqId = 0;
let handlerId = 0;

const nvimSocket = await Bun.connect({
unix: socket,
socket: {
binaryType: "uint8array",
data(_, data) {
// Sometimes RPC messages are split into multiple socket messages.
// `unpackrStream` handles collecting all socket messages if the RPC message
// is split and decoding it.
unpackrStream.write(data);
},
error(_, error) {
logger?.error("socket error", error);
},
end() {
logger?.debug("connection closed by neovim");
},
close() {
logger?.debug("connection closed by bunvim");
},
},
const nvimSocket = await new Promise<net.Socket>((resolve, reject) => {
const client = new net.Socket();
client.once("error", reject);
client.once("connect", () => {
client
.removeListener("error", reject)
.on("data", (data: Buffer) => {
unpackrStream.write(data);
})
.on("error", (error) => {
logger?.error("socket error", error);
})
.on("end", () => {
logger?.debug("connection closed by neovim");
})
.on("close", () => {
logger?.debug("connection closed by bunvim");
});
resolve(client);
});

client.connect(socket);
});

function processMessageOutQueue() {
Expand Down
5 changes: 0 additions & 5 deletions src/sample.test.ts

This file was deleted.

72 changes: 72 additions & 0 deletions tests/preamble.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { attach, type Nvim } from "../src/index.ts";
import { unlink } from "node:fs/promises";
import { spawn } from "child_process";
import path from "path";

const SOCK = `/tmp/bunvim-test.sock`;
export async function withNvimProcess(fn: (sock: string) => Promise<void>) {
try {
await unlink(SOCK);
} catch (e) {
if ((e as { code: string }).code !== "ENOENT") {
console.error(e);
}
}

const nvimProcess = spawn(
"nvim",
["--headless", "-n", "--clean", "--listen", SOCK],
{
// root dir relative to this file
cwd: path.resolve(path.dirname(__filename), "../"),
},
);

if (!nvimProcess.pid) {
throw new Error("Failed to start nvim process");
}

try {
nvimProcess.on("error", (err) => {
throw err;
});

nvimProcess.on("exit", (code, signal) => {
if (code !== 1) {
throw new Error(
`Nvim process exited with code ${code} and signal ${signal}`,
);
}
});

// give enough time for socket to be created
// TODO: poll for socket instead
await new Promise((resolve) => setTimeout(resolve, 500));

await fn(SOCK);
} finally {
const res = nvimProcess.kill();
console.log(`Killed process ${nvimProcess.pid} with result ${res}`);
}
}

export async function withNvimClient(fn: (nvim: Nvim) => Promise<void>) {
return await withNvimProcess(async (sock) => {
const nvim = await attach({
socket: sock,
client: { name: "test" },
logging: { level: "debug" },
});

try {
await fn(nvim);
} finally {
nvim.detach();
}
});
}

process.on("uncaughtException", (err) => {
console.error(err);
process.exit(1);
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
"checkJs": true,
"esModuleInterop": true
},
"include": ["src", "./package.json"]
"include": ["src", "test", "./package.json"]
}
Loading