mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-23 14:45:46 +00:00
fix: prefer source plugin-sdk root alias in tests
This commit is contained in:
@@ -7,6 +7,7 @@ let monolithicSdk = null;
|
||||
let diagnosticEventsModule = null;
|
||||
const jitiLoaders = new Map();
|
||||
const pluginSdkSubpathsCache = new Map();
|
||||
const shouldPreferSourceInTests = Boolean(process.env.VITEST) || process.env.NODE_ENV === "test";
|
||||
|
||||
function emptyPluginConfigSchema() {
|
||||
function error(message) {
|
||||
@@ -142,7 +143,7 @@ function loadMonolithicSdk() {
|
||||
}
|
||||
|
||||
const distCandidate = path.resolve(__dirname, "..", "..", "dist", "plugin-sdk", "compat.js");
|
||||
if (fs.existsSync(distCandidate)) {
|
||||
if (!shouldPreferSourceInTests && fs.existsSync(distCandidate)) {
|
||||
try {
|
||||
monolithicSdk = getJiti(true)(distCandidate);
|
||||
return monolithicSdk;
|
||||
@@ -168,7 +169,7 @@ function loadDiagnosticEventsModule() {
|
||||
"infra",
|
||||
"diagnostic-events.js",
|
||||
);
|
||||
if (fs.existsSync(distCandidate)) {
|
||||
if (!shouldPreferSourceInTests && fs.existsSync(distCandidate)) {
|
||||
try {
|
||||
diagnosticEventsModule = getJiti(true)(distCandidate);
|
||||
return diagnosticEventsModule;
|
||||
|
||||
@@ -22,6 +22,7 @@ type EmptySchema = {
|
||||
|
||||
function loadRootAliasWithStubs(options?: {
|
||||
distExists?: boolean;
|
||||
env?: Record<string, string | undefined>;
|
||||
monolithicExports?: Record<string | symbol, unknown>;
|
||||
}) {
|
||||
let createJitiCalls = 0;
|
||||
@@ -33,7 +34,11 @@ function loadRootAliasWithStubs(options?: {
|
||||
};
|
||||
const wrapper = vm.runInNewContext(
|
||||
`(function (exports, require, module, __filename, __dirname) {${rootAliasSource}\n})`,
|
||||
{},
|
||||
{
|
||||
process: {
|
||||
env: options?.env ?? {},
|
||||
},
|
||||
},
|
||||
{ filename: rootAliasPath },
|
||||
) as (
|
||||
exports: Record<string, unknown>,
|
||||
@@ -157,6 +162,19 @@ describe("plugin-sdk root alias", () => {
|
||||
expect(lazyModule.createJitiOptions.at(-1)?.tryNative).toBe(true);
|
||||
});
|
||||
|
||||
it("prefers source loading under vitest even when compat resolves to dist", () => {
|
||||
const lazyModule = loadRootAliasWithStubs({
|
||||
distExists: true,
|
||||
env: { VITEST: "1" },
|
||||
monolithicExports: {
|
||||
slowHelper: () => "loaded",
|
||||
},
|
||||
});
|
||||
|
||||
expect((lazyModule.moduleExports.slowHelper as () => string)()).toBe("loaded");
|
||||
expect(lazyModule.createJitiOptions.at(-1)?.tryNative).toBe(false);
|
||||
});
|
||||
|
||||
it("forwards delegateCompactionToRuntime through the compat-backed root alias", () => {
|
||||
const delegateCompactionToRuntime = () => "delegated";
|
||||
const lazyModule = loadRootAliasWithStubs({
|
||||
|
||||
Reference in New Issue
Block a user