Files
oh-my-pi/packages/coding-agent/test/core/python-kernel-session.test.ts
T
can1357 89e6479661 refactor: migrated environment variable access from process.env to Bun.env across 71 files
- Migrated all environment variable access from Node.js `process.env` to Bun runtime `Bun.env` API across the entire codebase.
- Updated 71 files including source code, tests, documentation, and configuration to use Bun's native environment variable API.
- Added comprehensive environment variables documentation in `packages/coding-agent/docs/environment-variables.md` covering 100+ environment variables organized by category.
- Updated CHANGELOG entries to reflect migration from `process.env` to `Bun.env` and environment variable prefix changes (PI_* vs OMP_*).
- Maintained identical behavior and logic across all changes - only the environment variable access method was updated.
2026-02-05 04:51:18 +01:00

92 lines
2.8 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { disposeAllKernelSessions, executePython } from "@oh-my-pi/pi-coding-agent/ipy/executor";
import type { KernelExecuteOptions, KernelExecuteResult } from "@oh-my-pi/pi-coding-agent/ipy/kernel";
import { PythonKernel } from "@oh-my-pi/pi-coding-agent/ipy/kernel";
import { TempDir } from "@oh-my-pi/pi-utils";
class FakeKernel {
executeCalls = 0;
shutdownCalls = 0;
alive = true;
readonly id: string;
constructor(id: string) {
this.id = id;
}
async execute(_code: string, options?: KernelExecuteOptions): Promise<KernelExecuteResult> {
this.executeCalls += 1;
options?.onChunk?.("ok\n");
return { status: "ok", cancelled: false, timedOut: false, stdinRequested: false };
}
async shutdown(): Promise<void> {
this.shutdownCalls += 1;
this.alive = false;
}
isAlive(): boolean {
return this.alive;
}
async ping(): Promise<boolean> {
return this.alive;
}
}
describe("executePython kernel reuse", () => {
const originalStart = PythonKernel.start;
let startCalls = 0;
let kernels: FakeKernel[] = [];
beforeEach(() => {
Bun.env.PI_PYTHON_SKIP_CHECK = "1";
startCalls = 0;
kernels = [];
PythonKernel.start = (async () => {
startCalls += 1;
const kernel = new FakeKernel(`kernel-${startCalls}`);
kernels.push(kernel);
return kernel as unknown as PythonKernel;
}) as typeof PythonKernel.start;
});
afterEach(async () => {
PythonKernel.start = originalStart;
await disposeAllKernelSessions();
});
it("reuses kernels for session mode", async () => {
using tempDir = TempDir.createSync("@python-kernel-session-");
await executePython("print('one')", { cwd: tempDir.path(), sessionId: "session-a", kernelMode: "session" });
await executePython("print('two')", { cwd: tempDir.path(), sessionId: "session-a", kernelMode: "session" });
expect(startCalls).toBe(1);
expect(kernels[0]?.executeCalls).toBe(2);
});
it("creates and disposes per-call kernels", async () => {
using tempDir = TempDir.createSync("@python-kernel-session-");
await executePython("print('one')", { cwd: tempDir.path(), kernelMode: "per-call" });
await executePython("print('two')", { cwd: tempDir.path(), kernelMode: "per-call" });
expect(startCalls).toBe(2);
expect(kernels[0]?.shutdownCalls).toBe(1);
expect(kernels[1]?.shutdownCalls).toBe(1);
});
it("resets the session kernel when requested", async () => {
using tempDir = TempDir.createSync("@python-kernel-session-");
await executePython("print('one')", { cwd: tempDir.path(), sessionId: "session-b", kernelMode: "session" });
await executePython("print('two')", {
cwd: tempDir.path(),
sessionId: "session-b",
kernelMode: "session",
reset: true,
});
expect(startCalls).toBe(2);
expect(kernels[0]?.shutdownCalls).toBe(1);
});
});