Forward awareness/cursor to vim from bridge

This commit is contained in:
Jared Miller 2026-01-27 20:41:12 -05:00
parent 56aa8dc9bd
commit e467881a8c
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 81 additions and 0 deletions

View file

@ -192,3 +192,69 @@ describe("Bridge lifecycle", () => {
expect(exitCode).toBe(0);
});
});
describe("awareness", () => {
test("forwards awareness from daemon to vim", async () => {
// Start daemon
const server = Bun.serve({
port: 4043,
fetch(req, server) {
if (new URL(req.url).pathname === "/ws") {
server.upgrade(req);
return;
}
return new Response("not found", { status: 404 });
},
websocket: {
open(ws) {
// Simulate sending awareness after join
setTimeout(() => {
ws.send(JSON.stringify({
type: "awareness",
data: { clientId: 99, cursor: { line: 3, col: 7 }, name: "peer" }
}));
}, 100);
},
message() {},
close() {},
},
});
const output: string[] = [];
const bridge = spawn({
cmd: ["bun", "adapters/vim/bridge.ts"],
env: { ...process.env, COLLABD_URL: "ws://localhost:4043/ws" },
stdin: "pipe",
stdout: "pipe",
});
const reader = bridge.stdout.getReader();
const decoder = new TextDecoder();
// Collect output
(async () => {
while (true) {
const { done, value } = await reader.read();
if (done) break;
output.push(decoder.decode(value));
}
})();
await new Promise(r => setTimeout(r, 50));
bridge.stdin.write(JSON.stringify({ type: "connect", room: "test" }) + "\n");
await new Promise(r => setTimeout(r, 200));
const awarenessMsg = output.join("").split("\n")
.filter(Boolean)
.map(l => JSON.parse(l))
.find(m => m.type === "cursor");
expect(awarenessMsg).toBeDefined();
expect(awarenessMsg.data.line).toBe(3);
expect(awarenessMsg.data.col).toBe(7);
expect(awarenessMsg.data.name).toBe("peer");
bridge.kill();
server.stop();
});
});

View file

@ -58,6 +58,21 @@ function connect(roomName: string) {
send({ type: "peers", count: msg.count });
break;
}
case "awareness": {
// Forward cursor info to vim
if (msg.data?.cursor) {
send({
type: "cursor",
data: {
clientId: msg.data.clientId,
line: msg.data.cursor.line,
col: msg.data.cursor.col,
name: msg.data.name || `peer-${msg.data.clientId}`,
},
});
}
break;
}
}
};