- Added ToolCallContext interface with batch execution metadata including batchId, index, and total. - Enhanced getToolContext() to accept optional ToolCallContext parameter for batch-aware tool execution. - Implemented LSP batching support to coalesce formatting and diagnostics operations on parallel edits. - Added comprehensive tests for tool call batch context and LSP writethrough batching functionality.
69 lines
2.5 KiB
TypeScript
69 lines
2.5 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import * as lspConfig from "../../src/core/tools/lsp/config";
|
|
import { createLspWritethrough } from "../../src/core/tools/lsp/index";
|
|
|
|
describe("createLspWritethrough batching", () => {
|
|
let tempDir: string;
|
|
|
|
beforeEach(() => {
|
|
tempDir = mkdtempSync(join(tmpdir(), "omp-lsp-batch-"));
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
rmSync(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it("defers LSP work until the batch flush", async () => {
|
|
const loadConfigSpy = vi
|
|
.spyOn(lspConfig, "loadConfig")
|
|
.mockResolvedValue({ servers: {}, idleTimeoutMs: undefined });
|
|
const getServersSpy = vi.spyOn(lspConfig, "getServersForFile").mockReturnValue([]);
|
|
const writethrough = createLspWritethrough(tempDir, { enableFormat: true, enableDiagnostics: true });
|
|
|
|
const fileA = join(tempDir, "a.ts");
|
|
const fileB = join(tempDir, "b.ts");
|
|
const batchId = `batch-${Date.now()}`;
|
|
|
|
const firstResult = await writethrough(fileA, "const a = 1;\n", undefined, undefined, {
|
|
id: batchId,
|
|
flush: false,
|
|
});
|
|
|
|
expect(firstResult).toBeUndefined();
|
|
expect(getServersSpy).toHaveBeenCalledTimes(0);
|
|
expect(loadConfigSpy).toHaveBeenCalledTimes(0);
|
|
expect(await Bun.file(fileA).text()).toBe("const a = 1;\n");
|
|
|
|
const secondResult = await writethrough(fileB, "const b = 2;\n", undefined, undefined, {
|
|
id: batchId,
|
|
flush: true,
|
|
});
|
|
|
|
expect(secondResult).toBeUndefined();
|
|
expect(getServersSpy).toHaveBeenCalledTimes(2);
|
|
expect(loadConfigSpy).toHaveBeenCalledTimes(1);
|
|
expect(await Bun.file(fileA).text()).toBe("const a = 1;\n");
|
|
expect(await Bun.file(fileB).text()).toBe("const b = 2;\n");
|
|
});
|
|
|
|
it("runs LSP immediately when no batch is provided", async () => {
|
|
const loadConfigSpy = vi
|
|
.spyOn(lspConfig, "loadConfig")
|
|
.mockResolvedValue({ servers: {}, idleTimeoutMs: undefined });
|
|
const getServersSpy = vi.spyOn(lspConfig, "getServersForFile").mockReturnValue([]);
|
|
const writethrough = createLspWritethrough(tempDir, { enableFormat: true, enableDiagnostics: true });
|
|
|
|
const filePath = join(tempDir, "single.ts");
|
|
const result = await writethrough(filePath, "const single = true;\n");
|
|
|
|
expect(result).toBeUndefined();
|
|
expect(getServersSpy).toHaveBeenCalledTimes(1);
|
|
expect(loadConfigSpy).toHaveBeenCalledTimes(1);
|
|
expect(await Bun.file(filePath).text()).toBe("const single = true;\n");
|
|
});
|
|
});
|