fix natives loading in keybinding tests

This commit is contained in:
can1357
2026-04-08 18:52:42 +02:00
parent 81fc76671a
commit 25efae9ae9
4 changed files with 42 additions and 8 deletions
@@ -1,18 +1,18 @@
import { describe, expect, it, vi } from "bun:test";
import { defaultEditorTheme } from "../../tui/test/test-themes";
import { CustomEditor } from "../src/modes/components/custom-editor";
function ctrl(key: string): string {
return String.fromCharCode(key.toLowerCase().charCodeAt(0) & 31);
}
async function createEditor() {
const { CustomEditor } = await import("../src/modes/components/custom-editor");
function createEditor() {
return new CustomEditor(defaultEditorTheme);
}
describe("CustomEditor temporary model selector keybinding", () => {
it("triggers the temporary selector from a remapped action key instead of Alt+P", async () => {
const editor = await createEditor();
it("triggers the temporary selector from a remapped action key instead of Alt+P", () => {
const editor = createEditor();
const onSelectModelTemporary = vi.fn();
editor.onSelectModelTemporary = onSelectModelTemporary;
editor.setActionKeys("app.model.selectTemporary", ["ctrl+y"]);
@@ -24,8 +24,8 @@ describe("CustomEditor temporary model selector keybinding", () => {
expect(onSelectModelTemporary).toHaveBeenCalledTimes(1);
});
it("removes the default Alt+P shortcut when the action is disabled", async () => {
const editor = await createEditor();
it("removes the default Alt+P shortcut when the action is disabled", () => {
const editor = createEditor();
const onSelectModelTemporary = vi.fn();
editor.onSelectModelTemporary = onSelectModelTemporary;
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "bun:test";
import { InputController } from "../src/modes/controllers/input-controller";
import type { InteractiveModeContext } from "../src/modes/types";
type FakeEditor = {
@@ -30,7 +31,6 @@ type FakeEditor = {
};
async function createContext() {
const { InputController } = await import("../src/modes/controllers/input-controller");
let editorText = "";
const keyMap: Record<string, string[]> = {
"app.model.selectTemporary": ["ctrl+y"],
+9 -1
View File
@@ -16,7 +16,7 @@ function getNativesDir() {
return path.join(os.homedir(), ".omp", "natives");
}
const packageJson = require("../package.json");
const { embeddedAddon } = require("./embedded-addon");
let embeddedAddon = null;
const require_ = createRequire(__filename);
const platformTag = `${process.platform}-${process.arch}`;
@@ -33,6 +33,14 @@ const isCompiledBinary =
__filename.includes("$bunfs") ||
__filename.includes("~BUN") ||
__filename.includes("%7EBUN");
if (isCompiledBinary) {
try {
({ embeddedAddon } = require("./embedded-addon"));
} catch {
embeddedAddon = null;
}
}
const SUPPORTED_PLATFORMS = ["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64", "win32-x64"];
function getVariantOverride() {
+26
View File
@@ -135,6 +135,31 @@ async function installBinary(src: string, dest: string): Promise<void> {
}
}
}
async function patchGeneratedIndexLoader(): Promise<void> {
const indexPath = path.join(nativeDir, "index.js");
let content = await Bun.file(indexPath).text();
const embeddedLoadPatch = "let embeddedAddon = null;\n";
if (!content.includes(embeddedLoadPatch)) {
content = content.replace(/const \{ embeddedAddon \} = require\("\.\/embedded-addon"\);\n/, embeddedLoadPatch);
}
const lazyLoadPatch = [
"if (isCompiledBinary) {",
"\ttry {",
'\t\t({ embeddedAddon } = require("./embedded-addon"));',
"\t} catch {",
"\t\tembeddedAddon = null;",
"\t}",
"}",
"",
].join("\n");
if (!content.includes(lazyLoadPatch)) {
content = content.replace(
/(const isCompiledBinary =[\s\S]*?__filename\.includes\("%7EBUN"\);\n)/,
`$1\n${lazyLoadPatch}`,
);
}
await Bun.write(indexPath, content);
}
async function resolveBuiltAddonPath(canonicalFilename: string): Promise<string> {
const canonicalFilenames = new Set([
@@ -229,5 +254,6 @@ if (builtAddonPath !== canonicalAddonPath) {
// Generate runtime enum exports from const enums in index.d.ts
await $`bun ${path.join(import.meta.dir, "gen-enums.ts")}`;
await patchGeneratedIndexLoader();
console.log("Build complete.");