|
8 | 8 |
|
9 | 9 | from core.models import ( |
10 | 10 | Content, |
| 11 | + ContentClusterMembership, |
11 | 12 | Entity, |
12 | 13 | EntityAuthoritySnapshot, |
13 | 14 | EntityCandidate, |
|
24 | 25 | SkillResult, |
25 | 26 | SkillStatus, |
26 | 27 | TopicCentroidSnapshot, |
| 28 | + TopicCluster, |
| 29 | + TopicVelocitySnapshot, |
27 | 30 | UserFeedback, |
28 | 31 | ) |
29 | 32 | from projects.model_support import SourcePluginName |
@@ -540,6 +543,115 @@ def test_topic_centroid_summary_action_returns_latest_snapshot_and_averages(self |
540 | 543 | self.assertAlmostEqual(response.json()["avg_drift_from_previous"], 0.2) |
541 | 544 | self.assertAlmostEqual(response.json()["avg_drift_from_week_ago"], 0.3) |
542 | 545 |
|
| 546 | + def test_topic_cluster_list_returns_current_velocity_annotation(self): |
| 547 | + cluster = TopicCluster.objects.create( |
| 548 | + project=self.owner_project, |
| 549 | + first_seen_at="2026-04-22T00:00:00Z", |
| 550 | + last_seen_at="2026-04-24T00:00:00Z", |
| 551 | + is_active=True, |
| 552 | + member_count=3, |
| 553 | + dominant_entity=self.owner_entity, |
| 554 | + ) |
| 555 | + TopicVelocitySnapshot.objects.create( |
| 556 | + cluster=cluster, |
| 557 | + project=self.owner_project, |
| 558 | + window_count=4, |
| 559 | + trailing_mean=1.5, |
| 560 | + trailing_stddev=0.5, |
| 561 | + z_score=3.0, |
| 562 | + velocity_score=1.0, |
| 563 | + ) |
| 564 | + |
| 565 | + response = self.client.get( |
| 566 | + reverse( |
| 567 | + "v1:project-topic-cluster-list", |
| 568 | + kwargs={"project_id": self.owner_project.id}, |
| 569 | + ), |
| 570 | + {"ordering": "-velocity_score"}, |
| 571 | + ) |
| 572 | + |
| 573 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 574 | + self.assertEqual(len(response.json()), 1) |
| 575 | + self.assertEqual(response.json()[0]["id"], cluster.id) |
| 576 | + self.assertEqual(response.json()[0]["member_count"], 3) |
| 577 | + self.assertEqual( |
| 578 | + response.json()[0]["dominant_entity"]["id"], self.owner_entity.id |
| 579 | + ) |
| 580 | + self.assertAlmostEqual(response.json()[0]["velocity_score"], 1.0) |
| 581 | + self.assertAlmostEqual(response.json()[0]["z_score"], 3.0) |
| 582 | + self.assertEqual(response.json()[0]["window_count"], 4) |
| 583 | + |
| 584 | + def test_topic_cluster_detail_and_velocity_history_action_return_memberships(self): |
| 585 | + cluster = TopicCluster.objects.create( |
| 586 | + project=self.owner_project, |
| 587 | + first_seen_at="2026-04-22T00:00:00Z", |
| 588 | + last_seen_at="2026-04-24T00:00:00Z", |
| 589 | + is_active=True, |
| 590 | + member_count=1, |
| 591 | + dominant_entity=self.owner_entity, |
| 592 | + ) |
| 593 | + ContentClusterMembership.objects.create( |
| 594 | + content=self.owner_content, |
| 595 | + cluster=cluster, |
| 596 | + project=self.owner_project, |
| 597 | + similarity=0.92, |
| 598 | + ) |
| 599 | + first_snapshot = TopicVelocitySnapshot.objects.create( |
| 600 | + cluster=cluster, |
| 601 | + project=self.owner_project, |
| 602 | + window_count=2, |
| 603 | + trailing_mean=1.0, |
| 604 | + trailing_stddev=0.2, |
| 605 | + z_score=1.5, |
| 606 | + velocity_score=0.75, |
| 607 | + ) |
| 608 | + second_snapshot = TopicVelocitySnapshot.objects.create( |
| 609 | + cluster=cluster, |
| 610 | + project=self.owner_project, |
| 611 | + window_count=3, |
| 612 | + trailing_mean=1.0, |
| 613 | + trailing_stddev=0.3, |
| 614 | + z_score=3.0, |
| 615 | + velocity_score=1.0, |
| 616 | + ) |
| 617 | + TopicVelocitySnapshot.objects.filter(pk=first_snapshot.pk).update( |
| 618 | + computed_at="2026-04-23T00:00:00Z" |
| 619 | + ) |
| 620 | + TopicVelocitySnapshot.objects.filter(pk=second_snapshot.pk).update( |
| 621 | + computed_at="2026-04-24T00:00:00Z" |
| 622 | + ) |
| 623 | + |
| 624 | + detail_response = self.client.get( |
| 625 | + reverse( |
| 626 | + "v1:project-topic-cluster-detail", |
| 627 | + kwargs={"project_id": self.owner_project.id, "pk": cluster.id}, |
| 628 | + ) |
| 629 | + ) |
| 630 | + history_response = self.client.get( |
| 631 | + reverse( |
| 632 | + "v1:project-topic-cluster-velocity-history", |
| 633 | + kwargs={"project_id": self.owner_project.id, "pk": cluster.id}, |
| 634 | + ), |
| 635 | + {"limit": 1}, |
| 636 | + ) |
| 637 | + |
| 638 | + self.assertEqual(detail_response.status_code, status.HTTP_200_OK) |
| 639 | + self.assertEqual(detail_response.json()["id"], cluster.id) |
| 640 | + self.assertEqual(len(detail_response.json()["memberships"]), 1) |
| 641 | + self.assertEqual( |
| 642 | + detail_response.json()["memberships"][0]["content"]["id"], |
| 643 | + self.owner_content.id, |
| 644 | + ) |
| 645 | + self.assertEqual(len(detail_response.json()["velocity_history"]), 2) |
| 646 | + self.assertEqual( |
| 647 | + detail_response.json()["velocity_history"][0]["id"], |
| 648 | + second_snapshot.id, |
| 649 | + ) |
| 650 | + |
| 651 | + self.assertEqual(history_response.status_code, status.HTTP_200_OK) |
| 652 | + self.assertEqual(len(history_response.json()), 1) |
| 653 | + self.assertEqual(history_response.json()[0]["id"], second_snapshot.id) |
| 654 | + |
543 | 655 | def test_content_detail_includes_duplicate_state(self): |
544 | 656 | canonical = self.owner_content |
545 | 657 | canonical.canonical_url = "https://example.com/owner" |
|
0 commit comments