feat: 掉线重连后自动恢复扩地任务(autoResumeAfterReconnect)

本服会周期性把机器人踢下线(实测约 1~6 分钟一次,踢出原因是
"An internal error occurred in your connection"),每次都会走到「世界已退出,任务停止」。
原来的行为是就此停住 —— 机器人虽然能被原点自救送回真实世界(实测 4/4 成功),
却再也不会继续扩地,于是永远停在原地。

现在世界退出时把本次任务的配置记下来(resumeConfig),
重新进入世界后自动 start() 接上;状态文案改为「世界已退出,等待重连后自动恢复」,
并打印「已重新进入世界,自动恢复扩地任务」。

只有因世界退出而停的那次会登记:手动停止 / 到开垦时段外 / 耐久保护停机
都会在 stop() 里清空 resumeConfig,不会被重连重新拉起。
开关 autoResumeAfterReconnect 默认开启,可关。

测试 470 → 473。
这个提交包含在:
lyizai
2026-10-01 16:34:56 +08:00
父节点 5513ef9f5f
当前提交 0bed12e189
共修改 6 个文件,包含 106 行新增和 5 行删除
+43 -1
查看文件
@@ -87,6 +87,8 @@ class FakeBotManager {
this.connection = { username: 'TestBot' };
this.commands = [];
this._model = model;
/** 设为 false 模拟「世界已退出」(掉线 / 被踢 / 被切到登录服) */
this.spawned = true;
}
isReady() {
@@ -94,7 +96,7 @@ class FakeBotManager {
}
isSpawned() {
return true;
return this.spawned;
}
sendCommand(command) {
@@ -147,6 +149,46 @@ function setup(options) {
return { controller, botManager, dataService, model, bot, logger, config };
}
test('世界退出后任务暂停,重连进入世界时自动恢复', async () => {
// 本服会周期性把机器人踢下线(实测约 1~6 分钟一次)。没有自动恢复的话,
// 机器人虽然能被原点自救送回真实世界,任务却再也不会继续 —— 扩地永远跑不长。
const { controller, botManager, config } = setup();
await controller.start(config);
assert.strictEqual(controller.isRunning(), true);
botManager.spawned = false;
controller.tick();
assert.strictEqual(controller.isRunning(), false, '世界退出应停止任务');
assert.match(controller.status, /等待重连后自动恢复/);
botManager.spawned = true;
controller.tick();
assert.strictEqual(controller.isRunning(), true, '重连后应自动恢复任务');
assert.ok(controller.target, '恢复后应重新选出目标区块');
assert.strictEqual(botManager.pathing.pathCalls, 2, '恢复后应重新下发寻路');
});
test('自动恢复可以关闭', async () => {
const { controller, botManager, config } = setup({ config: { autoResumeAfterReconnect: false } });
await controller.start(config);
botManager.spawned = false;
controller.tick();
botManager.spawned = true;
controller.tick();
assert.strictEqual(controller.isRunning(), false, '关掉开关后不应自动恢复');
});
test('手动停止的任务不会被自动恢复', async () => {
const { controller, botManager, config } = setup();
await controller.start(config);
controller.stop();
botManager.spawned = false;
controller.tick();
botManager.spawned = true;
controller.tick();
assert.strictEqual(controller.isRunning(), false, '手动停止不应被重连重新拉起');
});
test('start:自动识别所在邦国并下发开垦模式指令', async () => {
const { controller, botManager, config, model } = setup({ selfName: '' });
const ok = await controller.start(config);