import { existsSync } from "node:fs"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { diagnosticHelp } from "../diagnostic-help.ts"; import { findNu } from "../runtime.ts"; const binary = findNu(); describe.runIf(Boolean(binary))("parser-backed Nu help", () => { let cwd: string; beforeEach(async () => { cwd = await mkdtemp(join(tmpdir(), "pi-nu-diagnostic-test-")); }); afterEach(async () => { await rm(cwd, { recursive: true, force: true }); }); it.each([ ["version --bogus", "version"], ['print o> "unused.txt" --bogus "ok"', "print"], ["pwd --bogus", "pwd"], ["source --bogus", "source"], [" # 日本 comment\nversion --bogus\n", "version"], [ 'print "never executed"; const n = 2; "abc" | str substring --bogus', "str substring", ], [ 'def private_test [] { "body" }; "abc" | str substring --bogus', "str substring", ], ['alias part = str substring; "abc" | part --bogus', "str substring"], ["str substring (1 | into int) --bogus", "str substring"], ['("abc" | str upcase) | str substring --bogus', "str substring"], ['["a" "b"] | str substring --bogus', "str substring"], ['"abcdef" | str substring --start 1 --length 3', "str substring"], ['"日本🙂" | str substring --wrong', "str substring"], [ '"source ./fifo; const x = (open file)" | str substring --bogus', "str substring", ], ['"abc" | str substring --bogus', "str substring"], ['"abc" | str\tsubstring --bogus', "str substring"], ['str expand "img{007..009}.{png,webp}"', "str expand"], ["version surplus", "version"], ['version (str expand "x")', "version"], ["str nonexistent text", "str"], ["ls -Z", "ls"], // Historical targets: 01a16d55-f247-770d-8743-e8febd43c626 // (8fcb1c8c, c60416a2, 90bb8c16, adedbcd4, 1bd46686), // plus 01a12c1b-c5ea-7607-a5af-13a4167a14f7 / 70f3b75b. ["echo --bogus", "echo"], ['"dGVzdA==" | decode base64 --bogus', "decode base64"], ['http get "https://example.invalid" --bogus', "http get"], ["mkdir --bogus", "mkdir"], ["into string --bogus", "into string"], ["ps --bogus", "ps"], ])("selects installed help for %s", async (source, command) => { const result = await diagnosticHelp(binary!, source, cwd); expect(result?.command).toBe(command); expect(result?.text).toContain(`> ${command}`); expect(Buffer.byteLength(result?.text ?? "")).toBeLessThanOrEqual(8192); expect(result?.text).not.toContain("\x1b"); }); it.each([ '"abcdef" | str substring 1..3', '"abcdef" | str substring 1..4', '"bad integer" | into int', '"abc" | math sum', "source absent.nu", "print (str substring --bogus)", 'print (str expand "x")', 'print o> (version surplus) "ok"', 'print o> (do { def print [] { "custom" }; print --bogus }) "ok"', 'def version [] { "custom" }; version --bogus', 'def local [] { "custom" }; alias version = local; version --bogus', "extern version [--known]; version --bogus", ])( "omits help without an eligible, resolvable parser error: %s", async (source) => { await expect( diagnosticHelp(binary!, source, cwd), ).resolves.toBeUndefined(); }, ); it("resolves relative source imports in the original cwd without running their statements", async () => { await writeFile( join(cwd, "defs.nu"), 'alias part = str substring\n"should not execute" | save marker.txt\n', ); const result = await diagnosticHelp( binary!, 'source ./defs.nu; "abc" | part --bogus', cwd, ); expect(result?.command).toBe("str substring"); expect(existsSync(join(cwd, "marker.txt"))).toBe(false); }); it("resolves relative module imports using Nu's declaration identity", async () => { await writeFile( join(cwd, "defs.nu"), "export alias part = str substring\n", ); const result = await diagnosticHelp( binary!, 'use ./defs.nu part; "abc" | part --bogus', cwd, ); expect(result?.command).toBe("str substring"); }); it("does not start work when cancelled, out of budget, oversized or missing Nu", async () => { await expect( diagnosticHelp(binary!, "version --bogus", cwd, AbortSignal.abort()), ).resolves.toBeUndefined(); await expect( diagnosticHelp(binary!, "version --bogus", cwd, undefined, 0), ).resolves.toBeUndefined(); await expect( diagnosticHelp(binary!, "version --bogus".repeat(2000), cwd), ).resolves.toBeUndefined(); await expect( diagnosticHelp(join(cwd, "absent-nu"), "version --bogus", cwd), ).resolves.toBeUndefined(); }); it("cancels an in-progress lookup without replacing the original error", async () => { const controller = new AbortController(); const pending = diagnosticHelp( binary!, "version --bogus", cwd, controller.signal, ); controller.abort(); await expect(pending).resolves.toBeUndefined(); }); });