Files
WRNexusJS/packages/language-server/test/api-call-completion.test.ts
T
ClintchizandClaude Opus 5 5429057a38
Quality / quality (ubuntu-latest) (push) Failing after 9m55s
Quality / quality (windows-latest) (push) Canceled after 0s
fix(language-server): gate api hover to actual api.<name> references
Hover fired on whatever word was under the cursor, so a local variable
colliding with a declared block name reported the block's method and path
instead of its own hover info. Completion was already gated this way.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-20 19:03:46 +05:30

103 lines
2.9 KiB
TypeScript

import { expect, test } from "bun:test";
import { apiCallCompletions, apiCallHover, isApiReferenceAt } from "../src/server.ts";
const SOURCE = `page Search {
apis {
searchUsers POST /api/users {
request { body { name?: string } }
response { return data.users }
}
listTeams GET /api/teams {
response { return data.teams }
}
}
functions {
client async function go(): Promise<void> {
await api.
}
}
view { <main>x</main> }
}
`;
test("api. offers every declared block with method and path", () => {
const items = apiCallCompletions(SOURCE);
const labels = items.map((item) => item.label);
expect(labels).toContain("searchUsers");
expect(labels).toContain("listTeams");
const search = items.find((item) => item.label === "searchUsers")!;
expect(search.detail).toContain("POST");
expect(search.detail).toContain("/api/users");
});
test("hovering a block name reports its method, path and request fields", () => {
const hover = apiCallHover(SOURCE, "searchUsers");
expect(hover).toContain("POST");
expect(hover).toContain("/api/users");
expect(hover).toContain("name");
});
test("a page with no apis block offers nothing", () => {
expect(apiCallCompletions(`page P { view { <main>x</main> } }`)).toEqual([]);
});
test("a badly-broken document (unclosed braces, truncated apis block) answers rather than throwing", () => {
const broken = `page Search {
apis {
searchUsers POST /api/users {
request { body { name?: string
`;
expect(() => apiCallCompletions(broken)).not.toThrow();
expect(apiCallCompletions(broken)).toEqual([]);
expect(() => apiCallHover(broken, "searchUsers")).not.toThrow();
expect(apiCallHover(broken, "searchUsers")).toBeUndefined();
});
test("an ssr {} data block, which the parser now rejects with a ParseError, still answers rather than throwing", () => {
const legacy = `page Search {
ssr { api x GET /api/x { response { return data } } }
view { <main>x</main> }
}
`;
expect(() => apiCallCompletions(legacy)).not.toThrow();
expect(apiCallCompletions(legacy)).toEqual([]);
});
test("a bare identifier that merely collides with a block name is not an api reference", () => {
// `searchUsers` here is a local variable, not `api.searchUsers` -- hovering it must
// not report the API block's method and path.
const source = `page Search {
apis {
searchUsers POST /api/users {
response { return data.users }
}
}
functions {
client function go() {
const searchUsers = 1
void api.searchUsers({ name: "x" })
return searchUsers
}
}
view { <main>x</main> }
}
`;
const collision = source.indexOf("const searchUsers") + "const ".length;
const reference = source.indexOf("searchUsers POST");
expect(isApiReferenceAt(source, collision)).toBe(false);
expect(isApiReferenceAt(source, source.indexOf("api.searchUsers") + 4)).toBe(true);
expect(reference).toBeGreaterThan(-1);
});