Files
oh-my-pi/packages/coding-agent/test/tools/lsp-batching.test.ts
T
can1357 b2c94bef1a refactor(deps): restructured codebase to use centralized pi-utils package
- Created new @oh-my-pi/pi-utils workspace package with shared utilities for logging, process management, stream handling, and temporary directory management.
- Migrated all packages to use centralized logger from @oh-my-pi/pi-utils instead of local winston implementations.
- Replaced custom process spawning and stream reading implementations with standardized cspawn and readLines utilities across all modules.
- Converted synchronous file operations and process spawning to async patterns using Bun shell syntax and fs/promises.
- Added streaming edit abort functionality with configurable setting to abort on patch preview failures.
- Updated test framework from vitest to bun:test across all test suites.
2026-01-20 05:33:21 +01:00

68 lines
2.5 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test";
import { join } from "node:path";
import { createTempDirSync } from "@oh-my-pi/pi-utils";
import * as lspConfig from "../../src/core/tools/lsp/config";
import { createLspWritethrough } from "../../src/core/tools/lsp/index";
describe("createLspWritethrough batching", () => {
let tempDir: ReturnType<typeof createTempDirSync>;
beforeEach(() => {
tempDir = createTempDirSync("@omp-lsp-batch-");
});
afterEach(() => {
vi.restoreAllMocks();
tempDir.remove();
});
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.path, { enableFormat: true, enableDiagnostics: true });
const fileA = join(tempDir.path, "a.ts");
const fileB = 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")
.mockResolvedValue({ servers: {}, idleTimeoutMs: undefined });
const getServersSpy = vi.spyOn(lspConfig, "getServersForFile").mockReturnValue([]);
const writethrough = createLspWritethrough(tempDir.path, { enableFormat: true, enableDiagnostics: true });
const filePath = 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");
});
});