68 lines
2.5 KiB
TypeScript
68 lines
2.5 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test";
|
|
import * as path from "node:path";
|
|
import { createLspWritethrough } from "@oh-my-pi/pi-coding-agent/lsp";
|
|
import * as lspConfig from "@oh-my-pi/pi-coding-agent/lsp/config";
|
|
import { TempDir } from "@oh-my-pi/pi-utils";
|
|
|
|
describe("createLspWritethrough batching", () => {
|
|
let tempDir: TempDir;
|
|
|
|
beforeEach(() => {
|
|
tempDir = TempDir.createSync("@omp-lsp-batch-");
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
tempDir.removeSync();
|
|
});
|
|
|
|
it("defers LSP work until the batch flush", async () => {
|
|
const loadConfigSpy = vi
|
|
.spyOn(lspConfig, "loadConfig")
|
|
.mockReturnValue({ servers: {}, idleTimeoutMs: undefined });
|
|
const getServersSpy = vi.spyOn(lspConfig, "getServersForFile").mockReturnValue([]);
|
|
const writethrough = createLspWritethrough(tempDir.path(), { enableFormat: true, enableDiagnostics: true });
|
|
|
|
const fileA = path.join(tempDir.path(), "a.ts");
|
|
const fileB = path.join(tempDir.path(), "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")
|
|
.mockReturnValue({ servers: {}, idleTimeoutMs: undefined });
|
|
const getServersSpy = vi.spyOn(lspConfig, "getServersForFile").mockReturnValue([]);
|
|
const writethrough = createLspWritethrough(tempDir.path(), { enableFormat: true, enableDiagnostics: true });
|
|
|
|
const filePath = path.join(tempDir.path(), "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");
|
|
});
|
|
});
|