"""
补充 Process 列表接口的 node_count 字段覆盖

ProcessViewSet.list() 会对 queryset annotate(node_count)，对应 ProcessListSerializer 的只读字段。
"""

from django.contrib.auth import get_user_model
from django.test import TestCase
from rest_framework import status
from rest_framework.test import APIClient

from stateflow import models

User = get_user_model()


class ProcessListNodeCountAPITestCase(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create_user(username="testuser", password="testpass")
        self.client.force_authenticate(user=self.user)

        self.state1 = models.State.objects.create(name="状态1")
        self.state2 = models.State.objects.create(name="状态2")

    def test_process_list_contains_node_count(self):
        process = models.Process.objects.create(name="带节点计数的流程")
        models.ProcessNode.objects.create(process=process, state=self.state1, order=0)
        models.ProcessNode.objects.create(process=process, state=self.state2, order=1)

        resp = self.client.get("/api/v1/stateflow/processes/?limit=10&offset=0")
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(resp.data["count"], 1)

        item = resp.data["results"][0]
        self.assertIn("node_count", item)
        self.assertEqual(item["node_count"], 2)


