summaryrefslogtreecommitdiffhomepage
path: root/packages/core/tests/tools
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-06-02 20:06:10 +0900
committerAdam Malczewski <[email protected]>2026-06-02 20:06:10 +0900
commit9d8cf7005ba4c0bb8ade0775f54c2557aa1c5683 (patch)
tree8b8dd1408a55001c4865002ea6fd8250f6806b15 /packages/core/tests/tools
parent3307fec107fb8d1e7cb063bfdbfadf4b1d4f8c71 (diff)
parentf164f65992975e39ae42828672bd330e07e0e9e2 (diff)
downloaddispatch-9d8cf7005ba4c0bb8ade0775f54c2557aa1c5683.tar.gz
dispatch-9d8cf7005ba4c0bb8ade0775f54c2557aa1c5683.zip
Merge branch 'dev' into fix/wake-haiku-model-select
Diffstat (limited to 'packages/core/tests/tools')
-rw-r--r--packages/core/tests/tools/lsp-tool.test.ts110
-rw-r--r--packages/core/tests/tools/write-file.test.ts46
2 files changed, 156 insertions, 0 deletions
diff --git a/packages/core/tests/tools/lsp-tool.test.ts b/packages/core/tests/tools/lsp-tool.test.ts
new file mode 100644
index 0000000..7f26522
--- /dev/null
+++ b/packages/core/tests/tools/lsp-tool.test.ts
@@ -0,0 +1,110 @@
+import { describe, expect, it, vi } from "vitest";
+import type { LspManager } from "../../src/lsp/manager.js";
+import type { ResolvedLspServer } from "../../src/lsp/server.js";
+import { createLspTool, type LspToolContext } from "../../src/tools/lsp.js";
+
+const SERVER: ResolvedLspServer = {
+ id: "luau-lsp",
+ extensions: [".luau"],
+ spawn: () => ({ process: {} as never }),
+};
+
+function makeManager(overrides: Partial<LspManager> = {}): LspManager {
+ return {
+ hasServerForFile: vi.fn(() => true),
+ touchFile: vi.fn(async () => {}),
+ getDiagnostics: vi.fn(() => ({})),
+ request: vi.fn(async () => []),
+ getClients: vi.fn(async () => []),
+ shutdownAll: vi.fn(async () => {}),
+ ...overrides,
+ } as unknown as LspManager;
+}
+
+function ctx(manager: LspManager, servers = [SERVER]): () => LspToolContext {
+ return () => ({ manager, workingDirectory: "/work", servers });
+}
+
+describe("createLspTool", () => {
+ it("exposes the expected schema/name", () => {
+ const tool = createLspTool(ctx(makeManager()));
+ expect(tool.name).toBe("lsp");
+ expect(tool.description).toMatch(/luau-lsp/i);
+ });
+
+ it("errors when no servers are configured", async () => {
+ const tool = createLspTool(ctx(makeManager(), []));
+ const out = await tool.execute({ operation: "diagnostics", path: "a.luau" });
+ expect(out).toMatch(/no LSP servers are configured/i);
+ });
+
+ it("errors when no server matches the file", async () => {
+ const manager = makeManager({ hasServerForFile: vi.fn(() => false) as never });
+ const tool = createLspTool(ctx(manager));
+ const out = await tool.execute({ operation: "diagnostics", path: "a.ts" });
+ expect(out).toMatch(/no configured LSP server matches/i);
+ });
+
+ it("diagnostics: touches the file then reports errors", async () => {
+ const touchFile = vi.fn(async () => {});
+ const getDiagnostics = vi.fn(() => ({
+ "/work/a.luau": [
+ {
+ range: { start: { line: 2, character: 1 }, end: { line: 2, character: 9 } },
+ severity: 1,
+ message: "bad type",
+ },
+ ],
+ }));
+ const manager = makeManager({
+ touchFile: touchFile as never,
+ getDiagnostics: getDiagnostics as never,
+ });
+ const tool = createLspTool(ctx(manager));
+ const out = await tool.execute({ operation: "diagnostics", path: "a.luau" });
+ expect(touchFile).toHaveBeenCalledOnce();
+ expect(out).toContain("ERROR [3:2] bad type");
+ });
+
+ it("diagnostics: reports clean when no errors", async () => {
+ const tool = createLspTool(ctx(makeManager()));
+ const out = await tool.execute({ operation: "diagnostics", path: "a.luau" });
+ expect(out).toMatch(/No errors reported/i);
+ });
+
+ it("hover: requires line and character", async () => {
+ const tool = createLspTool(ctx(makeManager()));
+ const out = await tool.execute({ operation: "hover", path: "a.luau" });
+ expect(out).toMatch(/requires both 'line' and 'character'/i);
+ });
+
+ it("hover: converts 1-based coords to 0-based on the wire", async () => {
+ const request = vi.fn(async () => [{ contents: "hi" }]);
+ const manager = makeManager({ request: request as never });
+ const tool = createLspTool(ctx(manager));
+ await tool.execute({ operation: "hover", path: "a.luau", line: 5, character: 3 });
+ expect(request).toHaveBeenCalledOnce();
+ const arg = request.mock.calls[0]?.[0] as { method: string; params: { position: unknown } };
+ expect(arg.method).toBe("textDocument/hover");
+ expect(arg.params.position).toEqual({ line: 4, character: 2 });
+ });
+
+ it("references: includes declaration context", async () => {
+ const request = vi.fn(async () => []);
+ const manager = makeManager({ request: request as never });
+ const tool = createLspTool(ctx(manager));
+ await tool.execute({ operation: "references", path: "a.luau", line: 1, character: 1 });
+ const arg = request.mock.calls[0]?.[0] as { params: { context?: unknown } };
+ expect(arg.params.context).toEqual({ includeDeclaration: true });
+ });
+
+ it("documentSymbol: does not require a position", async () => {
+ const request = vi.fn(async () => [{ name: "foo" }]);
+ const manager = makeManager({ request: request as never });
+ const tool = createLspTool(ctx(manager));
+ const out = await tool.execute({ operation: "documentSymbol", path: "a.luau" });
+ const arg = request.mock.calls[0]?.[0] as { method: string };
+ expect(arg.method).toBe("textDocument/documentSymbol");
+ expect(out).toContain("foo");
+ });
+});
diff --git a/packages/core/tests/tools/write-file.test.ts b/packages/core/tests/tools/write-file.test.ts
index f071e12..0dedbfc 100644
--- a/packages/core/tests/tools/write-file.test.ts
+++ b/packages/core/tests/tools/write-file.test.ts
@@ -103,4 +103,50 @@ describe("write_file tool", () => {
expect(entries).toEqual([]);
});
});
+
+ describe("onAfterWrite hook", () => {
+ it("appends the hook's returned string to a successful write", async () => {
+ const tool = createWriteFileTool(workDir, async (abs) => `DIAGNOSTICS for ${abs}`);
+ const result = await tool.execute({ path: "a.luau", content: "local x = 1" });
+ expect(result).toMatch(/successfully wrote/i);
+ expect(result).toContain("DIAGNOSTICS for");
+ expect(result).toContain(join(workDir, "a.luau"));
+ });
+
+ it("does not append when the hook returns empty string", async () => {
+ const tool = createWriteFileTool(workDir, async () => "");
+ const result = await tool.execute({ path: "a.luau", content: "local x = 1" });
+ expect(result.trim()).toMatch(/^Successfully wrote to "a\.luau"\.$/);
+ });
+
+ it("does not run the hook when the write is blocked (traversal)", async () => {
+ let called = false;
+ const tool = createWriteFileTool(workDir, async () => {
+ called = true;
+ return "should not appear";
+ });
+ const result = await tool.execute({ path: "../evil.txt", content: "bad" });
+ expect(result).toMatch(/outside the working directory/i);
+ expect(called).toBe(false);
+ });
+
+ it("swallows hook errors so a throwing hook never fails the write", async () => {
+ const tool = createWriteFileTool(workDir, async () => {
+ throw new Error("lsp blew up");
+ });
+ const result = await tool.execute({ path: "a.luau", content: "local x = 1" });
+ expect(result).toMatch(/successfully wrote/i);
+ expect(result).not.toContain("lsp blew up");
+ });
+
+ it("passes the canonical absolute path to the hook", async () => {
+ let seen = "";
+ const tool = createWriteFileTool(workDir, async (abs) => {
+ seen = abs;
+ return "";
+ });
+ await tool.execute({ path: "nested/b.luau", content: "x" });
+ expect(seen).toBe(join(workDir, "nested/b.luau"));
+ });
+ });
});