Fix off by one bug when sorting tasks in Habitica integration (#138993)

* Fix off-by-one bug when sorting dailies and to-dos in Habitica

* Add test
pull/138999/head
Manu 2025-02-21 16:39:24 +01:00 committed by GitHub
parent 0f7cb6b757
commit 059a6dddbe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 9 deletions

View File

@ -119,12 +119,13 @@ class BaseHabiticaListEntity(HabiticaBase, TodoListEntity):
assert self.todo_items
if previous_uid:
pos = (
self.todo_items.index(
next(item for item in self.todo_items if item.uid == previous_uid)
)
+ 1
pos = self.todo_items.index(
next(item for item in self.todo_items if item.uid == previous_uid)
)
if pos < self.todo_items.index(
next(item for item in self.todo_items if item.uid == uid)
):
pos += 1
else:
pos = 0

View File

@ -601,17 +601,19 @@ async def test_delete_completed_todo_items_exception(
@pytest.mark.parametrize(
("entity_id", "uid", "previous_uid"),
("entity_id", "uid", "second_pos", "third_pos"),
[
(
"todo.test_user_to_do_s",
"1aa3137e-ef72-4d1f-91ee-41933602f438",
"88de7cd9-af2b-49ce-9afd-bf941d87336b",
"2f6fcabc-f670-4ec3-ba65-817e8deea490",
),
(
"todo.test_user_dailies",
"2c6d136c-a1c3-4bef-b7c4-fa980784b1e1",
"564b9ac9-c53d-4638-9e7f-1cd96fe19baa",
"f2c85972-1a19-4426-bc6d-ce3337b9d99f",
),
],
ids=["todo", "daily"],
@ -623,7 +625,8 @@ async def test_move_todo_item(
hass_ws_client: WebSocketGenerator,
entity_id: str,
uid: str,
previous_uid: str,
second_pos: str,
third_pos: str,
) -> None:
"""Test move todo items."""
@ -634,13 +637,13 @@ async def test_move_todo_item(
assert config_entry.state is ConfigEntryState.LOADED
client = await hass_ws_client()
# move to second position
# move up to second position
data = {
"id": id,
"type": "todo/item/move",
"entity_id": entity_id,
"uid": uid,
"previous_uid": previous_uid,
"previous_uid": second_pos,
}
await client.send_json_auto_id(data)
resp = await client.receive_json()
@ -649,6 +652,21 @@ async def test_move_todo_item(
habitica.reorder_task.assert_awaited_once_with(UUID(uid), 1)
habitica.reorder_task.reset_mock()
# move down to third position
data = {
"id": id,
"type": "todo/item/move",
"entity_id": entity_id,
"uid": uid,
"previous_uid": third_pos,
}
await client.send_json_auto_id(data)
resp = await client.receive_json()
assert resp.get("success")
habitica.reorder_task.assert_awaited_once_with(UUID(uid), 2)
habitica.reorder_task.reset_mock()
# move to top position
data = {
"id": id,