Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/task/MaaFW/embedded_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,8 @@ def _build_inner_task(self, route: Any) -> "MaaFWPluginAutoProxyTask":
project_update_logs=self._take_project_update_logs(),
signal_tracker=self.signal_tracker,
)
task.history = self.history
task.callbacks = self.callbacks
task.maafw_runtime_pool_root = route.root
task.maafw_runtime_pool_id = route.pool_id
return task
Expand Down
2 changes: 2 additions & 0 deletions changelog.d/fix-maafw-history-finalization.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
project: mfw
修复任务结束时报错、账号收尾中断及历史记录未保存的问题
75 changes: 74 additions & 1 deletion tests/task/test_maafw_stop_reservation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""停止已运行的 MFW 账号后,同一项目仍可再次运行。"""
"""MFW 账号收尾保留历史,停止后同一项目仍可再次运行。"""

Check warning on line 1 in tests/task/test_maafw_stop_reservation.py

View workflow job for this annotation

GitHub Actions / 检查是否删掉了他人近期合入的代码

可能回退了 #1892 的改动

这里删掉了 #1892(youngslin,2026-11-09 21:04 UTC 合入)加入的 1 行(原第 1 行)。如果不是有意修改,多半是同步分支时带回了旧版本的文件。

import asyncio
import sys
import threading
import uuid
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock

import pytest

Expand All @@ -19,6 +22,76 @@
from tests.task_stop_support import StopTask, add_script


@pytest.mark.asyncio
async def test_manager_built_account_finishes_and_saves_history(
isolated_app_paths, monkeypatch, tmp_path
):
from app.core.history import HistoryStore
from app.models.task import TaskItem
from app.task import history_core
from app.task.MaaFW import embedded_manager
from app.task.MaaFW.tools.embedded import runner_task
from app.utils import UTC4

config = await activate_config(MaaFWConfig())
user_id = config.UserData.add(
MaaFWUserConfig, payload={"Notify": {"Enabled": False}}
)
await config.UserData.commit()
info = TaskItem()
script = await add_script(
info=info, script_id=str(config.uid), user_id=str(user_id)
)
manager = embedded_manager.MaaFWEmbeddedManager(script)
manager.script_config = config
manager.user_config = config.UserData
manager.history = history_core.TaskHistorySession(info)
storage = HistoryStore()
await storage.initialize(video_limit_bytes=0)
monkeypatch.setattr(history_core, "History", storage)
monkeypatch.setattr(runner_task.Notify, "send_notice", AsyncMock())
monkeypatch.setattr(runner_task.Notify, "push_statistics_report", AsyncMock())
account = manager._build_inner_task(
SimpleNamespace(root=tmp_path / "runtime-pool", pool_id="test")
)
manager.inner_task = account
manager._inner_finalized = False
temporary_history = Path(account._history_temp.name)
try:
# 只隔离游戏执行,使用真实的账号装配、日志、收尾与历史存储。
await account.prepare()
account.check_result = "Pass"
account.run_complete = True
account._history_statuses[id(account.cur_user_log)] = "success"
account._append_log("completed account log")
await manager.callbacks.drain()
assert "completed account log" in "".join(account.cur_user_log.content)
await manager._finalize_inner_task()
await manager._finalize_inner_task()
assert script.user_list[0].status == "完成"
assert config.UserData[user_id].Data.ProxyTimes == 1
assert not temporary_history.exists()
day = account.cur_user_log_started_at.astimezone(UTC4).date()
groups = await storage.search(start_date=day, end_date=day, mode="DAILY")
records = [
record
for users in groups.values()
for bucket in users.values()
for record in bucket.index
]
assert len(records) == 1
assert records[0].status == "success"
detail = await storage.get_detail(record_id=records[0].id)
assert "completed account log" in detail.log_content
finally:
await account.callbacks.close()
await manager.callbacks.close()
await manager.history.close()
await storage.close()
await account._release_project_path()
account._history_temp.cleanup()


@pytest.mark.asyncio
async def test_stopped_account_releases_project_after_emulator_close_is_cancelled(
isolated_app_paths,
Expand Down
Loading