// Root Unit Tests — pickServer(), findRoot(), pathToUri(), uriToPath(). import { describe, it } from "node:test"; import * as assert from "node:assert/strict"; import * as path from "node:path"; import { fileURLToPath } from "node:url"; import { pickServer, findRoot, pathToUri, uriToPath } from "../../src/root.ts"; import { UnsupportedExtensionError } from "../../src/types.ts"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const projectRoot = path.resolve(__dirname, "..", ".."); describe("pathToUri / uriToPath", () => { it("converts a relative path to a file URI", () => { const uri = pathToUri("cli.ts"); assert.ok(uri.startsWith("file://")); assert.ok(uri.endsWith("cli.ts")); }); it("converts an absolute path to a file URI", () => { const absPath = path.resolve(projectRoot, "cli.ts"); const uri = pathToUri(absPath); assert.strictEqual(uri, `file://${absPath}`); }); it("round-trips path -> uri -> path", () => { const absPath = path.resolve(projectRoot, "src", "client.ts"); const uri = pathToUri(absPath); const back = uriToPath(uri); assert.strictEqual(back, absPath); }); }); describe("pickServer", () => { it("picks gopls for .go files", () => { const server = pickServer("/some/path/file.go"); assert.strictEqual(server.id, "gopls"); }); it("picks typescript-language-server for .ts files", () => { const server = pickServer("/some/path/file.ts"); assert.strictEqual(server.id, "typescript-language-server"); }); it("picks typescript-language-server for .tsx files", () => { const server = pickServer("/some/path/file.tsx"); assert.strictEqual(server.id, "typescript-language-server"); }); it("picks pyright for .py files", () => { const server = pickServer("/some/path/file.py"); assert.strictEqual(server.id, "pyright"); }); it("throws UnsupportedExtensionError for unknown extensions", () => { assert.throws( () => pickServer("/some/path/file.xyz"), UnsupportedExtensionError, ); }); }); describe("findRoot", () => { it("finds project root using go.mod marker", () => { // The project root has package.json but not go.mod, so use a file in the // project and look for tsconfig.json. const root = findRoot( path.join(projectRoot, "cli.ts"), ["tsconfig.json"], ); assert.strictEqual(root, projectRoot); }); it("finds nearest directory containing marker", () => { // The test/fixtures/ dir doesn't have tsconfig.json, but the project root does. const fixtureFile = path.join(projectRoot, "test", "fixtures", "sample.ts"); const root = findRoot(fixtureFile, ["tsconfig.json"]); assert.strictEqual(root, projectRoot); }); it("falls back to file's directory when no marker found", () => { const root = findRoot( path.join(projectRoot, "cli.ts"), ["nonexistent-marker-xyz"], ); assert.strictEqual(root, projectRoot); }); });