#!/usr/bin/env node /** * Local editor tests for the Neovim plugin, not part of `pnpm test` or CI. Each scenario * starts headless Neovim in a temporary project with this repository's plugin and a * packed build of this repository's `@tsrx/language-server`, installed outside the * repository with npm as an editor installs it (so no `typescript` next to it). It opens * a `.tsrx` file in the project's `app/` folder and checks which TypeScript the server * runs, the warning Neovim shows when it finds none it can run (#1008), and, on Neovim * 0.12 or newer, that typing `
` in Insert mode closes the tag with the cursor between
* the tags (#1004).
*
* pnpm --filter @tsrx/nvim-plugin test:editor [-- --keep]
*
* Neovim: TSRX_NVIM, else `nvim` on PATH, else ~/.local/opt/nvim-*\/bin/nvim. Packing the
* server and installing its dependencies needs network access.
*/
import { spawn, spawnSync } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import {
create_test_project,
install_packed_language_server,
} from '../../../scripts/editor-test-language-server.js';
const here = path.dirname(fileURLToPath(import.meta.url));
const package_dir = path.dirname(here);
const keep = process.argv.includes('--keep');
/** @returns {string | undefined} */
function find_nvim() {
if (process.env.TSRX_NVIM) return process.env.TSRX_NVIM;
const which = spawnSync('which', ['nvim'], { encoding: 'utf8' });
if (which.status === 0 && which.stdout.trim()) return which.stdout.trim();
const opt = path.join(os.homedir(), '.local', 'opt');
if (!fs.existsSync(opt)) return undefined;
for (const entry of fs.readdirSync(opt)) {
const candidate = path.join(opt, entry, 'bin', 'nvim');
if (entry.startsWith('nvim') && fs.existsSync(candidate)) return candidate;
}
return undefined;
}
const nvim = find_nvim();
if (!nvim) {
console.log('Neovim not found: set TSRX_NVIM or put nvim on PATH. Skipping.');
process.exit(0);
}
const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'tsrx-nvim-')));
console.log(`Neovim: ${nvim}`);
console.log(`Temporary directory: ${root}\n`);
// This repository's server, packed and installed as an editor installs it.
const { bin: server_bin, typescript_next_to_server } = install_packed_language_server(root);
/**
* @typedef {{
* name: string,
* description: string,
* typescript?: 'repository' | { version: string },
* no_root_marker?: boolean,
* expect: { typescript_features: boolean, warning?: RegExp },
* }} Scenario
*/
/** @type {Scenario[]} */
const SCENARIOS = [
{
name: 'no-typescript',
description: 'no typescript in the project or next to the server',
expect: {
typescript_features: false,
warning:
/The TSRX language server did not find TypeScript in .*, in the server installation, or in their parent folders\./,
},
},
{
name: 'typescript-7',
description: 'TypeScript 7 in the parent folder of the open folder',
typescript: { version: '7.1.0-dev.20261002.1' },
expect: {
typescript_features: false,
warning:
/The TSRX language server found TypeScript 7\.1\.0-dev\.20261002\.1 in .*node_modules\/typescript\./,
},
},
{
name: 'typescript-5.9',
description: 'TypeScript 5.9 in the parent folder of the open folder',
typescript: 'repository',
expect: { typescript_features: true },
},
{
name: 'typescript-5.9-no-root-marker',
description:
'TypeScript 5.9, no package.json or .git: Neovim names no folder, the server searches from where it started',
typescript: 'repository',
no_root_marker: true,
expect: { typescript_features: true },
},
];
/** @param {Scenario} scenario */
function create_project(scenario) {
return create_test_project({
dir: path.join(root, scenario.name),
server_bin,
typescript: scenario.typescript,
// The plugin's root marker, so Neovim sends the project as the workspace folder.
package_json: scenario.no_root_marker
? undefined
: { name: `tsrx-nvim-${scenario.name}`, private: true },
}).app;
}
/**
* @param {Scenario} scenario
* @returns {Promise