afc2276a12
- Extracted OpenAI compatibility detection and resolution logic into dedicated `openai-completions-compat` module. - Refactored `detectCompat()` and `getCompat()` to delegate to new compat module functions with simplified conditional logic. - Fixed OAuth redirect URI validation to preserve exact configured values without trailing slash normalization. - Improved session deletion to return boolean status and display error messages in UI instead of silently failing. - Added `/session delete` command with Delete key support and confirmation dialogs for session management.
110 lines
3.6 KiB
TypeScript
110 lines
3.6 KiB
TypeScript
import { describe, expect, it, vi } from "bun:test";
|
|
import type { InteractiveModeContext } from "@oh-my-pi/pi-coding-agent/modes/types";
|
|
import { executeBuiltinSlashCommand } from "@oh-my-pi/pi-coding-agent/slash-commands/builtin-registry";
|
|
|
|
function createRuntimeHarness(options?: {
|
|
handleSessionCommand?: InteractiveModeContext["handleSessionCommand"];
|
|
handleSessionDeleteCommand?: InteractiveModeContext["handleSessionDeleteCommand"];
|
|
}) {
|
|
const setText = vi.fn();
|
|
const handleSessionCommand =
|
|
options?.handleSessionCommand ??
|
|
vi.fn(async () => {
|
|
return;
|
|
});
|
|
const handleSessionDeleteCommand =
|
|
options?.handleSessionDeleteCommand ??
|
|
vi.fn(async () => {
|
|
return;
|
|
});
|
|
|
|
return {
|
|
setText,
|
|
handleSessionCommand,
|
|
handleSessionDeleteCommand,
|
|
runtime: {
|
|
ctx: {
|
|
editor: { setText } as unknown as InteractiveModeContext["editor"],
|
|
handleSessionCommand,
|
|
handleSessionDeleteCommand,
|
|
} as InteractiveModeContext,
|
|
handleBackgroundCommand: () => {},
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("/session slash command", () => {
|
|
it("awaits session info before resolving the default command", async () => {
|
|
const deferred = Promise.withResolvers<void>();
|
|
const handleSessionCommand = vi.fn(() => deferred.promise);
|
|
const harness = createRuntimeHarness({ handleSessionCommand });
|
|
|
|
let settled = false;
|
|
const execution = executeBuiltinSlashCommand("/session", harness.runtime).then(result => {
|
|
settled = true;
|
|
return result;
|
|
});
|
|
|
|
await Promise.resolve();
|
|
|
|
expect(handleSessionCommand).toHaveBeenCalledTimes(1);
|
|
expect(harness.handleSessionDeleteCommand).not.toHaveBeenCalled();
|
|
expect(harness.setText).not.toHaveBeenCalled();
|
|
expect(settled).toBe(false);
|
|
|
|
deferred.resolve();
|
|
|
|
expect(await execution).toBe(true);
|
|
expect(settled).toBe(true);
|
|
expect(harness.setText).toHaveBeenCalledWith("");
|
|
});
|
|
|
|
it("propagates session info failures through executeBuiltinSlashCommand", async () => {
|
|
const infoError = new Error("info failed");
|
|
const handleSessionCommand = vi.fn(async () => {
|
|
throw infoError;
|
|
});
|
|
const harness = createRuntimeHarness({ handleSessionCommand });
|
|
|
|
await expect(executeBuiltinSlashCommand("/session info", harness.runtime)).rejects.toBe(infoError);
|
|
expect(handleSessionCommand).toHaveBeenCalledTimes(1);
|
|
expect(harness.handleSessionDeleteCommand).not.toHaveBeenCalled();
|
|
expect(harness.setText).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("awaits session deletion before resolving the builtin command", async () => {
|
|
const deferred = Promise.withResolvers<void>();
|
|
const handleSessionDeleteCommand = vi.fn(() => deferred.promise);
|
|
const harness = createRuntimeHarness({ handleSessionDeleteCommand });
|
|
|
|
let settled = false;
|
|
const execution = executeBuiltinSlashCommand("/session delete", harness.runtime).then(result => {
|
|
settled = true;
|
|
return result;
|
|
});
|
|
|
|
await Promise.resolve();
|
|
|
|
expect(handleSessionDeleteCommand).toHaveBeenCalledTimes(1);
|
|
expect(harness.setText).toHaveBeenCalledWith("");
|
|
expect(settled).toBe(false);
|
|
|
|
deferred.resolve();
|
|
|
|
expect(await execution).toBe(true);
|
|
expect(settled).toBe(true);
|
|
});
|
|
|
|
it("propagates session deletion failures through executeBuiltinSlashCommand", async () => {
|
|
const deleteError = new Error("delete failed");
|
|
const handleSessionDeleteCommand = vi.fn(async () => {
|
|
throw deleteError;
|
|
});
|
|
const harness = createRuntimeHarness({ handleSessionDeleteCommand });
|
|
|
|
await expect(executeBuiltinSlashCommand("/session delete", harness.runtime)).rejects.toBe(deleteError);
|
|
expect(handleSessionDeleteCommand).toHaveBeenCalledTimes(1);
|
|
expect(harness.setText).toHaveBeenCalledWith("");
|
|
});
|
|
});
|