mirror of
https://github.com/Youzini-afk/ST-Bionic-Memory-Ecology.git
synced 2026-06-14 02:40:45 +08:00
fix(host): restore event priority and EJS rendering
This commit is contained in:
70
tests/ena-planner-ejs.mjs
Normal file
70
tests/ena-planner-ejs.mjs
Normal file
@@ -0,0 +1,70 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { createRequire } from "node:module";
|
||||
import {
|
||||
installResolveHooks,
|
||||
toDataModuleUrl,
|
||||
} from "./helpers/register-hooks-compat.mjs";
|
||||
|
||||
const extensionsShimSource = [
|
||||
"export const extension_settings = {};",
|
||||
].join("\n");
|
||||
const scriptShimSource = [
|
||||
"export function getRequestHeaders() { return {}; }",
|
||||
"export function saveSettingsDebounced() {}",
|
||||
"export function substituteParamsExtended(text = '') { return String(text ?? ''); }",
|
||||
].join("\n");
|
||||
|
||||
installResolveHooks([
|
||||
{
|
||||
specifiers: ["../../../../extensions.js"],
|
||||
url: toDataModuleUrl(extensionsShimSource),
|
||||
},
|
||||
{
|
||||
specifiers: ["../../../../../script.js"],
|
||||
url: toDataModuleUrl(scriptShimSource),
|
||||
},
|
||||
]);
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const ejs = require("../vendor/ejs.js");
|
||||
const originalWindow = globalThis.window;
|
||||
const originalWarn = console.warn;
|
||||
const warnings = [];
|
||||
|
||||
try {
|
||||
globalThis.window = { ...(originalWindow || {}), ejs };
|
||||
console.warn = (...args) => warnings.push(args);
|
||||
|
||||
const { createEnaPlannerEjsContext, renderEjsTemplate } = await import(
|
||||
"../ena-planner/ena-planner.js"
|
||||
);
|
||||
|
||||
const ctx = createEnaPlannerEjsContext({ x: "alpha" });
|
||||
assert.equal(renderEjsTemplate("<%= getvar('x') %>", ctx), "alpha");
|
||||
assert.equal(renderEjsTemplate("<% print(getvar('x')) %>", ctx), "alpha");
|
||||
|
||||
const pollutedCtx = {
|
||||
...createEnaPlannerEjsContext({ x: "safe" }),
|
||||
__append() {
|
||||
throw new Error("locals __append should not shadow EJS output");
|
||||
},
|
||||
print() {
|
||||
throw new Error("locals print should not shadow EJS output function");
|
||||
},
|
||||
};
|
||||
assert.equal(renderEjsTemplate("<%= getvar('x') %>", pollutedCtx), "safe");
|
||||
assert.equal(renderEjsTemplate("<% print(getvar('x')) %>", pollutedCtx), "safe");
|
||||
|
||||
const invalidTemplate = "before <% if ( %> after";
|
||||
assert.equal(renderEjsTemplate(invalidTemplate, ctx, "invalid"), invalidTemplate);
|
||||
assert.ok(warnings.some((args) => String(args[0]).includes("EJS render failed")));
|
||||
} finally {
|
||||
console.warn = originalWarn;
|
||||
if (originalWindow === undefined) {
|
||||
delete globalThis.window;
|
||||
} else {
|
||||
globalThis.window = originalWindow;
|
||||
}
|
||||
}
|
||||
|
||||
console.log("ena-planner-ejs tests passed");
|
||||
Reference in New Issue
Block a user