Files
oh-my-pi/packages/mnemopi/test/setup.ts
T
can1357 4952b28a8e test(mnemopi): reworked mnemopi test embedding setup and CI bucketing
- Removed the mnemopi Bun preload file and added explicit `./setup` imports in the changed test suites.
- Deleted the `RUN_EMBEDDINGS` flag path and shifted `MNEMOPI_NO_EMBEDDINGS` handling into per-suite before/after hooks.
- Excluded `packages/mnemopi` from the fast parallel CI test bucket with a note about missing fastembed models.
2026-06-15 02:45:03 +02:00

75 lines
2.0 KiB
TypeScript

import { afterEach, beforeEach } from "bun:test";
import * as Beam from "@oh-my-pi/pi-mnemopi/core/beam";
import * as Embeddings from "@oh-my-pi/pi-mnemopi/core/embeddings";
import type { CompleteOptions, LlmBackend } from "@oh-my-pi/pi-mnemopi/core/llm-backends";
import * as LlmBackends from "@oh-my-pi/pi-mnemopi/core/llm-backends";
import * as Memory from "@oh-my-pi/pi-mnemopi/core/memory";
type ResettableModule = Record<string, unknown>;
const RESET_FUNCTION_NAMES = [
"resetForTests",
"resetModuleStateForTests",
"resetMemoryForTests",
"resetBeamForTests",
"resetEmbeddingStateForTests",
"resetHostLlmBackendForTests",
"resetLlmBackendStateForTests",
] as const;
const RESETTABLE_MODULES: readonly ResettableModule[] = [Memory, Beam, LlmBackends, Embeddings];
function callResetFunctions(moduleExports: ResettableModule): void {
for (const name of RESET_FUNCTION_NAMES) {
const reset = moduleExports[name];
if (typeof reset === "function") {
reset();
}
}
}
export function resetModuleStateForTests(): void {
for (const moduleExports of RESETTABLE_MODULES) {
callResetFunctions(moduleExports);
}
}
export function disableLocalLlmForTests(): void {
LlmBackends.setHostLlmBackend(null);
}
export function withLocalLlm(fakeResponseOrBackend: string | LlmBackend = "fake summary"): LlmBackend {
const backend =
typeof fakeResponseOrBackend === "string"
? new FakeLocalLlmBackend(fakeResponseOrBackend)
: fakeResponseOrBackend;
LlmBackends.setHostLlmBackend(backend);
return backend;
}
class FakeLocalLlmBackend implements LlmBackend {
readonly name = "fake-local-llm";
constructor(public response: string) {}
complete(_prompt: string, _opts?: CompleteOptions): string {
return this.response;
}
createChatCompletion(): { choices: [{ message: { content: string } }] } {
return { choices: [{ message: { content: this.response } }] };
}
}
beforeEach(() => {
resetModuleStateForTests();
disableLocalLlmForTests();
});
afterEach(() => {
resetModuleStateForTests();
disableLocalLlmForTests();
});