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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ and this project adheres to

### Fixed

🐛(frontend) abort check media status unmount #2194
- 🐛(frontend) abort check media status unmount #2194
- ✨(backend) order pinned documents by last updated at #2028

## [v4.8.6] - 2026-04-08

Expand Down
1 change: 1 addition & 0 deletions src/backend/core/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ def favorite_list(self, request, *args, **kwargs):
queryset = self.queryset.filter(path_list)
queryset = queryset.filter(id__in=favorite_documents_ids)
queryset = queryset.filter(ancestors_deleted_at__isnull=True)
queryset = queryset.order_by("-updated_at")
queryset = queryset.annotate_user_roles(user)
queryset = queryset.annotate(
is_favorite=db.Value(True, output_field=db.BooleanField())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Test for the document favorite_list endpoint."""

from datetime import timedelta

from django.utils import timezone

import pytest
from rest_framework.test import APIClient

Expand Down Expand Up @@ -111,8 +115,50 @@ def test_api_document_favorite_list_with_favorite_children():

content = response.json()["results"]

assert content[0]["id"] == str(children[0].id)
assert content[0]["id"] == str(access.document.id)
assert content[1]["id"] == str(children[1].id)
assert content[2]["id"] == str(children[0].id)
Comment on lines +118 to +120
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Avoid strict ordering assertions in the non-order-specific test.

This test now asserts exact index order without controlling timestamps, so it can become flaky when updated_at values collide. Since ordering is already validated in test_api_document_favorite_list_sorted_by_updated_at, this test should only verify membership.

✅ Suggested test stabilization
-    assert content[0]["id"] == str(access.document.id)
-    assert content[1]["id"] == str(children[1].id)
-    assert content[2]["id"] == str(children[0].id)
+    ids = {item["id"] for item in content}
+    assert ids == {
+        str(access.document.id),
+        str(children[0].id),
+        str(children[1].id),
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
assert content[0]["id"] == str(access.document.id)
assert content[1]["id"] == str(children[1].id)
assert content[2]["id"] == str(children[0].id)
ids = {item["id"] for item in content}
assert ids == {
str(access.document.id),
str(children[0].id),
str(children[1].id),
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/backend/core/tests/documents/test_api_documents_favorite_list.py` around
lines 118 - 120, The test test_api_documents_favorite_list currently asserts
strict ordering on content by checking content[0], content[1], content[2], which
can be flaky because timestamps may collide; change these assertions to verify
membership only (e.g., compare sets of IDs or use assertIn for each expected id)
so the test asserts that the expected document ids (access.document.id and
children[0].id/children[1].id) are present in the response without assuming
order.



def test_api_document_favorite_list_sorted_by_updated_at():
"""
Authenticated users should receive their favorite documents including children
sorted by last updated_at timestamp.
"""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)

root = factories.DocumentFactory(creator=user, users=[user])
children = factories.DocumentFactory.create_batch(
2, parent=root, favorited_by=[user]
)

access = factories.UserDocumentAccessFactory(
user=user, role=models.RoleChoices.READER, document__favorited_by=[user]
)

other_root = factories.DocumentFactory(creator=user, users=[user])
factories.DocumentFactory.create_batch(2, parent=other_root)

now = timezone.now()

models.Document.objects.filter(pk=children[0].pk).update(
updated_at=now + timedelta(seconds=2)
)
models.Document.objects.filter(pk=children[1].pk).update(
updated_at=now + timedelta(seconds=3)
)

response = client.get("/api/v1.0/documents/favorite_list/")

assert response.status_code == 200
assert response.json()["count"] == 3

content = response.json()["results"]

assert content[0]["id"] == str(children[1].id)
assert content[1]["id"] == str(children[0].id)
assert content[2]["id"] == str(access.document.id)


Expand Down
Loading