|
| 1 | +/* eslint-env jest */ |
| 2 | +import * as cp from 'child_process'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +import { NeovimClient, attach, findNvim } from 'neovim'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Runs a program and returns its output. |
| 10 | + */ |
| 11 | +async function run(cmd: string, args: string[]) { |
| 12 | + return new Promise<{ proc: ReturnType<typeof cp.spawn>, stdout: string, stderr: string}>((resolve, reject) => { |
| 13 | + const proc = cp.spawn(cmd, args, { shell: false }); |
| 14 | + const rv = { |
| 15 | + proc: proc, |
| 16 | + stdout: '', |
| 17 | + stderr: '', |
| 18 | + } |
| 19 | + |
| 20 | + proc.stdout.on('data', (data) => { |
| 21 | + rv.stdout += data.toString(); |
| 22 | + }); |
| 23 | + |
| 24 | + proc.stderr.on('data', (data) => { |
| 25 | + rv.stderr += data.toString(); |
| 26 | + }); |
| 27 | + |
| 28 | + proc.on('exit', (code_) => { |
| 29 | + resolve(rv); |
| 30 | + }); |
| 31 | + |
| 32 | + proc.on('error', (e) => { |
| 33 | + reject(e); |
| 34 | + }); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +describe('Node host2', () => { |
| 39 | + const thisDir = path.resolve(__dirname); |
| 40 | + const pluginDir = path.resolve(thisDir, '../../example-plugin2/'); |
| 41 | + const pluginMain = path.resolve(pluginDir, 'index.js'); |
| 42 | + |
| 43 | + const testdir = process.cwd(); |
| 44 | + let nvimProc: ReturnType<typeof cp.spawn>; |
| 45 | + let nvim: NeovimClient; |
| 46 | + |
| 47 | + beforeAll(async () => { |
| 48 | + const minVersion = '0.9.5' |
| 49 | + const nvimInfo = findNvim({ minVersion: minVersion }); |
| 50 | + const nvimPath = nvimInfo.matches[0]?.path; |
| 51 | + if (!nvimPath) { |
| 52 | + throw new Error(`nvim ${minVersion} not found`) |
| 53 | + } |
| 54 | + |
| 55 | + nvimProc = cp.spawn(nvimPath, ['--clean', '-n', '--headless', '--embed'], {}); |
| 56 | + nvim = attach({ proc: nvimProc }); |
| 57 | + }); |
| 58 | + |
| 59 | + afterAll(() => { |
| 60 | + process.chdir(testdir); |
| 61 | + nvim.quit(); |
| 62 | + if (nvimProc && nvimProc.connected) { |
| 63 | + nvimProc.disconnect(); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + beforeEach(() => {}); |
| 68 | + |
| 69 | + afterEach(() => {}); |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * From the Nvim process, starts a new "node …/plugin/index.js" RPC job (that |
| 74 | + * is, a node "plugin host", aka an Nvim node client). |
| 75 | + */ |
| 76 | + async function newPluginChan() { |
| 77 | + const luacode = ` |
| 78 | + -- "node …/plugin/index.js" |
| 79 | + local argv = {'${process.argv0}', '${pluginMain}'} |
| 80 | + local chan = vim.fn.jobstart(argv, { rpc = true, stderr_buffered = true }) |
| 81 | + return chan |
| 82 | + ` |
| 83 | + return await nvim.lua(luacode); |
| 84 | + } |
| 85 | + |
| 86 | + it('`node plugin.js --version` prints node-client version', async () => { |
| 87 | + //process.chdir(thisDir); |
| 88 | + const proc = await run(process.argv0, [pluginMain, '--version']); |
| 89 | + // "5.1.1-dev.0\n" |
| 90 | + expect(proc.stdout).toMatch(/\d+\.\d+\.\d+/); |
| 91 | + |
| 92 | + proc.proc.kill('SIGKILL'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('responds to "poll" with "ok"', async () => { |
| 96 | + // See also the old provider#Poll() function. |
| 97 | + |
| 98 | + // From Nvim, start an "node …/plugin/index.js" RPC job. |
| 99 | + // Then use that channel to call methods on the remote plugin. |
| 100 | + const chan = await newPluginChan(); |
| 101 | + const rv = await nvim.lua(`return vim.rpcrequest(..., 'poll')`, [ chan ]); |
| 102 | + |
| 103 | + expect(rv).toEqual('ok'); |
| 104 | + }); |
| 105 | + |
| 106 | + //it('responds to "nvim_xx" methods', async () => { |
| 107 | + // // This is just a happy accident of the fact that Nvim plugin host === client. |
| 108 | + // const chan = await newPluginChan(); |
| 109 | + // const rv = await nvim.lua(`return vim.rpcrequest(..., 'nvim_eval', '1 + 3')`, [ chan ]); |
| 110 | + // expect(rv).toEqual(3); |
| 111 | + //}); |
| 112 | + |
| 113 | + it('responds to custom, plugin-defined methods', async () => { |
| 114 | + const chan = await newPluginChan(); |
| 115 | + // The "testMethod1" function is defined in …/example-plugin2/index.js. |
| 116 | + const rv = await nvim.lua(`return vim.rpcrequest(..., 'testMethod1', {})`, [ chan ]); |
| 117 | + |
| 118 | + expect(rv).toEqual('called hostTest'); |
| 119 | + }); |
| 120 | + |
| 121 | + // TODO |
| 122 | + //it('Lua plugin can define autocmds/functions that call the remote plugin', async () => { |
| 123 | + // // JSHostTestCmd |
| 124 | + // // BufEnter |
| 125 | + //}); |
| 126 | +}); |
| 127 | + |
0 commit comments