/** * Tests of hover documentation and signature help (M3.4, * `assist/hover.ts`, `assist/signature.ts`): the queries they send, the * tooltip range, safe rendering, the highlighted parameter, the counter, * typing `(` and `,`, following the cursor and closing with Escape. */ import { EditorState, type Extension } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; import { afterEach, describe, expect, it, vi } from "vitest"; import { assistExtensions } from "../../../src/components/editor/assist"; import { assistEnabled, quickDocumentation } from "../../../src/components/editor/assist/commands"; import { hoverAt } from "../../../src/components/editor/assist/hover"; import { lspSignatureHelp, renderSignature, setSignature, signatureField, } from "../../../src/components/editor/assist/signature"; import { toHover, toSignatureHelp, type SignatureHelpResult } from "../../../src/components/editor/assist/types"; import { fakeBuffer, fakeTransport } from "./fakes"; /** Views created by a test, destroyed after it. */ let views: EditorView[] = []; afterEach(() => { for (const view of views) { view.destroy(); } views = []; }); /** A mounted view with `doc` and `extensions`. */ function mount(doc: string, extensions: Extension = []): EditorView { const parent = document.createElement("div"); document.body.append(parent); const view = new EditorView({ state: EditorState.create({ doc, extensions }), parent }); views.push(view); return view; } /** A signature help result for `pair(int $a, string $b)`. */ const pair: SignatureHelpResult = { signatures: [ { label: "pair(int $a, string $b): void", documentation: "Makes a *pair*.", parameters: [{ label: [5, 11], documentation: "the `first`" }, { label: [13, 22] }], }, ], activeSignature: 0, activeParameter: 1, }; describe("hover", () => { it("asks for the hovered position and spans the returned range", async () => { const view = mount("", range: { start: { line: 1, column: 9 }, end: { line: 1, column: 12 } }, }); const tooltip = await hoverAt(view, 16, fakeBuffer(3), transport); expect(transport.calls).toEqual([{ command: "lsp.hover", params: { id: 3, position: { line: 1, column: 10 } } }]); expect(tooltip?.pos).toBe(15); expect(tooltip?.end).toBe(18); expect(tooltip?.above).toBe(true); const dom = tooltip?.create(view).dom as HTMLElement; expect(dom.classList.contains("lt-assist-hover")).toBe(true); expect(dom.querySelector("pre code")?.textContent).toBe("class Foo"); expect(dom.querySelector("img")).toBeNull(); expect(dom.textContent).toContain(""); }); it("shows nothing for empty or failed answers", async () => { const view = mount(" { it("highlights the active parameter and shows its and the signature's docs", () => { const dom = renderSignature(pair); expect(dom.querySelector(".lt-assist-signature-label")?.textContent).toBe("pair(int $a, string $b): void"); expect(dom.querySelector(".lt-assist-signature-active")?.textContent).toBe("string $b"); expect(dom.querySelectorAll(".lt-assist-signature-doc")).toHaveLength(1); const first = renderSignature({ ...pair, activeParameter: 0 }); expect(first.querySelector(".lt-assist-signature-active")?.textContent).toBe("int $a"); expect(first.querySelectorAll(".lt-assist-signature-doc")).toHaveLength(2); expect(first.querySelector(".lt-assist-signature-doc code")?.textContent).toBe("first"); }); it("counts several signatures and lets a signature override the active parameter", () => { const two: SignatureHelpResult = { signatures: [...pair.signatures, { label: "pair(int $a)", parameters: [{ label: [5, 11] }], activeParameter: 0 }], activeSignature: 1, activeParameter: 3, }; const dom = renderSignature(two); expect(dom.querySelector(".lt-assist-signature-count")?.textContent).toBe("2 of 2"); expect(dom.querySelector(".lt-assist-signature-active")?.textContent).toBe("int $a"); }); it("checks the untrusted result", () => { expect(toSignatureHelp({ signatures: [] })).toBeNull(); expect(toSignatureHelp(null)).toBeNull(); const help = toSignatureHelp({ signatures: [{ label: "f(a)", parameters: [{ label: [2, 3] }, { label: [2, 99] }, { label: "a" }, "junk"] }], activeSignature: 7, activeParameter: -1, }); expect(help?.activeSignature).toBe(0); expect(help?.activeParameter).toBe(0); expect(help?.signatures[0]?.parameters.map((p) => p.label)).toEqual([[2, 3], null, null, null]); }); }); describe("signature help while typing", () => { it("opens on (, follows the cursor and closes with Escape", async () => { const transport = fakeTransport({ signatures: [{ label: "pair(int $a, string $b): void", parameters: [{ label: [5, 11] }, { label: [13, 22] }] }], activeSignature: 0, activeParameter: 0, }); const view = mount(" { expect(view.state.field(signatureField)).not.toBeNull(); }); expect(transport.calls[0]).toEqual({ command: "lsp.signatureHelp", params: { id: 5, position: { line: 1, column: 5 } } }); expect(view.dom.querySelector(".lt-assist-signature-active")?.textContent).toBe("int $a"); view.dispatch({ changes: { from: 11, insert: "1" }, selection: { anchor: 12 }, userEvent: "input.type" }); await vi.waitFor(() => { expect(transport.calls).toHaveLength(2); }); const escape = new KeyboardEvent("keydown", { key: "Escape", bubbles: true }); view.contentDOM.dispatchEvent(escape); expect(view.state.field(signatureField)).toBeNull(); }); it("closes when the server has no signatures any more", async () => { const transport = fakeTransport({ signatures: [], activeSignature: 0, activeParameter: 0 }); const view = mount(" { expect(view.state.field(signatureField)).toBeNull(); }); }); it("ignores typing that does not start an argument", async () => { const transport = fakeTransport(null); const view = mount(" setTimeout(resolve, 20)); expect(transport.calls).toHaveLength(0); }); }); describe("assist commands", () => { it("are enabled only with a PHP editor and show quick documentation at the cursor", async () => { expect(assistEnabled()).toBe(false); const transport = fakeTransport({ contents: "**Foo** docs" }); const view = mount(" v !== view); expect(assistEnabled()).toBe(false); }); });