@chemzqm/neovim
Version:
NodeJS client API for vim9 and neovim
138 lines (137 loc) • 5.59 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const cp = __importStar(require("child_process"));
const attach_1 = require("../attach/attach");
describe('Nvim Promise API', () => {
let proc;
let nvim;
let requests;
let notifications;
beforeAll(async () => {
var _a;
let cmd = (_a = process.env.NVIM_COMMAND) !== null && _a !== void 0 ? _a : 'nvim';
try {
proc = cp.spawn(cmd, ['-u', 'NONE', '-N', '--embed', '-c', 'set noswapfile'], {
cwd: __dirname,
});
nvim = (0, attach_1.attach)({ proc });
nvim.on('request', (method, args, resp) => {
requests.push({ method, args });
resp.send(`received ${method}(${args})`);
});
nvim.on('notification', (method, args) => {
notifications.push({ method, args });
});
}
catch (err) {
// eslint-disable-next-line no-console
console.log(err);
}
});
afterAll(() => {
nvim.quit();
if (proc && typeof proc.disconnect === 'function') {
proc.disconnect();
}
});
beforeEach(() => {
requests = [];
notifications = [];
});
it('can send requests and receive response', async () => {
const result = await nvim.eval('{"k1": "v1", "k2": 2}');
expect(result).toEqual({ k1: 'v1', k2: 2 });
});
it('can receive requests and send responses', async () => {
const res = await nvim.eval('rpcrequest(1, "request", 1, 2, 3)');
expect(res).toEqual('received request(1,2,3)');
expect(requests).toEqual([{ method: 'request', args: [1, 2, 3] }]);
expect(notifications).toEqual([]);
});
it('can receive notifications', async () => {
const res = await nvim.eval('rpcnotify(1, "notify", 1, 2, 3)');
expect(res).toEqual(1);
expect(requests).toEqual([]);
return new Promise(resolve => setImmediate(() => {
expect(notifications).toEqual([{ method: 'notify', args: [1, 2, 3] }]);
resolve(undefined);
}));
});
it('can deal with custom types', async () => {
await nvim.command('vsp');
await nvim.command('vsp');
await nvim.command('vsp');
const windows = await nvim.windows;
expect(windows.length).toEqual(4);
await nvim.setWindow(windows[2]);
const win = await nvim.window;
expect(win.equals(windows[0])).toBe(false);
expect(win.equals(windows[2])).toBe(true);
const buf = await nvim.buffer;
const lines = await buf.getLines({ start: 0, end: -1, strictIndexing: false });
expect(lines).toEqual([]);
buf.setLines(['line1', 'line2'], { start: 0, end: 1 });
const newLines = await buf.getLines({ start: 0, end: -1, strictIndexing: false });
expect(newLines).toEqual(['line1', 'line2']);
});
it('should create and delete autocmd', async () => {
let group = await nvim.createAugroup('MyGroup', { clear: false });
expect(typeof group).toBe('number');
let res = await nvim.createAutocmd('BufEnter', { group: 'MyGroup', pattern: '*', command: 'let g:f = "bar"' });
expect(typeof res).toBe('number');
let output = await nvim.call('execute', 'autocmd BufEnter');
expect(output).toMatch('bar');
await nvim.command(`doautocmd <nomodeline> BufEnter`);
let val = await nvim.getVar('f');
expect(val).toBe('bar');
nvim.deleteAutocmd(res);
output = await nvim.call('execute', 'autocmd BufEnter');
expect(output.includes('bar')).toBe(false);
});
it('emits "disconnect" after quit', done => {
const disconnectMock = jest.fn();
nvim.on('disconnect', disconnectMock);
nvim.quit();
proc.on('close', () => {
expect(disconnectMock.mock.calls.length).toBe(1);
done();
});
// Event doesn't actually emit when we quit nvim, but when the child process is killed
if (typeof proc.disconnect === 'function') {
proc.disconnect();
}
});
});