Files
node_server/tests/function_defaults.test.mjs
2026-10-22 17:07:08 +08:00

47 lines
2.4 KiB
JavaScript

import assert from 'node:assert/strict';
import {EventEmitter} from 'node:events';
import {createRequire} from 'node:module';
import test from 'node:test';
import {tsImport} from 'tsx/esm/api';
const {create_function_manager} = await tsImport('../bot/function/init.ts', import.meta.url);
const require = createRequire(import.meta.url);
const mf = createRequire(require.resolve('@wp2508/mineflayer'));
const {Vec3} = mf('vec3');
test('manager fills omitted need_look defaults and preserves explicit values', async () => {
const bot = new EventEmitter();
bot.entity = {id: 1, position: new Vec3(0, 0, 0), eyeHeight: 1.62, height: 1.8};
bot._client = {};
bot.entities = {2: {id: 2, name: 'cow', position: new Vec3(2, 0, 0), width: 1, height: 2}};
let looks = 0;
let attacks = 0;
bot.lookAt = async () => { looks++; };
bot.attack = () => { attacks++; };
const block = {name: 'stone', type: 1, position: new Vec3(2, 1, 0)};
bot.blockAt = () => block;
bot.activateBlock = async (_block, _face, _delta, forceLook) => assert.equal(forceLook, 'ignore');
const manager = await create_function_manager({bot_instance: bot, bot_status: 'online'});
try {
for (const category of manager.get_function_list().filter(category => ['entity', 'block'].includes(category.path))) {
for (const fn of category.function_list) {
assert.equal(fn.args.at(-1).name, 'need_look');
assert.equal(fn.args.at(-1).default, false);
}
}
assert.equal(await manager.run('entity', 'attack_nearest_entity', ['cow', 3, true]), true);
assert.equal(looks, 0);
assert.equal(await manager.run('entity', 'attack_nearest_entity', ['cow', 3, true, false]), true);
assert.equal(looks, 0);
assert.equal(await manager.run('entity', 'attack_nearest_entity', ['cow', 3, true, true]), true);
assert.equal(looks, 1);
assert.equal(attacks, 3);
assert.equal(await manager.run('block', 'use_position_block', [2, 1, 0, 1]), true);
assert.equal(looks, 1);
assert.equal(await manager.run('block', 'use_position_block', [2, 1, 0, 1, true]), true);
assert.equal(looks, 2);
for (const args of [['cow', 3], ['cow', 3, true, null], ['cow', 3, true, 'true']])
await assert.rejects(manager.run('entity', 'attack_nearest_entity', args));
} finally { manager.dispose(); }
});