Skip to content

flurdy/agent-skills / skills/pr-status

pr-status

Verified

Show enriched status of your open PRs — CI checks, approvals, unresolved review threads, and linked Jira discussion, with transition-driven suggested next actions.

tests/test_feedback_inventory.py

View on GitHub
from __future__ import annotations

import contextlib
import copy
import importlib.util
import io
import json
from pathlib import Path
import unittest
from unittest import mock

TEST_DIR = Path(__file__).resolve().parent
SCRIPT = TEST_DIR.parent / "scripts" / "gh-pr-feedback.py"
SPEC = importlib.util.spec_from_file_location("gh_pr_feedback", SCRIPT)
if SPEC is None or SPEC.loader is None:
    raise RuntimeError(f"cannot load {SCRIPT}")
feedback = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(feedback)


class FixtureClient:
    def __init__(self, fixture: dict):
        self.fixture = copy.deepcopy(fixture)
        self.connection_calls: list[tuple[int, str, str]] = []
        self.rest_calls: list[str] = []

    def fetch_initial(self, owner: str, repo: str, numbers: list[int], page_size: int) -> dict:
        return copy.deepcopy(self.fixture["initial"])

    def fetch_connection(
        self,
        owner: str,
        repo: str,
        number: int,
        source: str,
        after: str,
        page_size: int,
    ) -> dict:
        self.connection_calls.append((number, source, after))
        value = self.fixture.get("connectionPages", {}).get(f"{number}:{source}:{after}")
        if value is None:
            raise RuntimeError(f"missing fixture page: {number}:{source}:{after}")
        if "error" in value:
            raise RuntimeError(value["error"])
        return copy.deepcopy(value)

    def rest(self, endpoint: str) -> object:
        self.rest_calls.append(endpoint)
        value = self.fixture.get("rest", {}).get(endpoint)
        if value is None:
            raise RuntimeError(f"missing fixture REST response: {endpoint}")
        if isinstance(value, dict) and "error" in value:
            raise RuntimeError(value["error"])
        return copy.deepcopy(value)


def load_fixture(name: str) -> dict:
    return json.loads((TEST_DIR / "fixtures" / name).read_text())


def by_identity(result: dict) -> dict[str, dict]:
    return {record["identity"]: record for record in result["records"]}


class FeedbackInventoryTests(unittest.TestCase):
    def run_cli(self, fixture: dict, *arguments: str) -> dict:
        output = io.StringIO()
        with mock.patch.object(feedback, "GhClient", return_value=FixtureClient(fixture)):
            with contextlib.redirect_stdout(output):
                self.assertEqual(0, feedback.main(["acme", "widgets", *arguments]))
        return json.loads(output.getvalue())

    def test_compact_poll_preserves_comparison_and_triage_without_content(self) -> None:
        fixture = load_fixture("all-sources.json")
        full = self.run_cli(fixture, "42")
        compact = self.run_cli(fixture, "42", "--compact")

        self.assertEqual("compact", compact.pop("recordFormat"))
        self.assertFalse(compact.pop("unchanged"))
        self.assertEqual(len(full["records"]), compact.pop("recordCount"))
        self.assertRegex(compact.pop("fingerprint"), r"^[0-9a-f]{64}$")
        full_records = full.pop("records")
        compact_records = compact.pop("records")
        self.assertEqual(full, compact, "poll must preserve caps and fetch completeness")
        self.assertEqual(len(full_records), len(compact_records))
        fields = {
            "identity", "updateKey", "stateKey", "pr", "source", "lifecycle",
            "selfAuthored", "hasLaterSelfReply", "authorKind", "reviewState",
            "semanticType", "actionability",
        }
        for original, polled in zip(full_records, compact_records, strict=True):
            self.assertEqual({key: value for key, value in original.items() if key in fields}, polled)
            self.assertNotIn("rawBody", polled)
            self.assertNotIn("gist", polled)
            self.assertNotIn("targets", polled)

    def test_matching_complete_poll_returns_only_receipt(self) -> None:
        fixture = load_fixture("all-sources.json")
        first = self.run_cli(fixture, "42", "--compact")
        second = self.run_cli(fixture, "42", "--compact", "--if-fingerprint", first["fingerprint"])
        self.assertTrue(second["unchanged"])
        self.assertNotIn("records", second, "omitted records are not an empty inventory")
        self.assertEqual(first["recordCount"], second["recordCount"])
        self.assertEqual(first["fingerprint"], second["fingerprint"])
        self.assertLess(len(json.dumps(second)), 600)

    def test_conditional_poll_cannot_hide_edits_lifecycle_or_scope_changes(self) -> None:
        fixture = load_fixture("all-sources.json")
        original = self.run_cli(fixture, "42", "--compact")
        for kind in ("body", "lifecycle", "self-reply", "scope", "limits"):
            with self.subTest(change=kind):
                changed = copy.deepcopy(fixture)
                thread = changed["initial"]["data"]["repository"]["pr0"]["reviewThreads"]["nodes"][0]
                flags = []
                if kind == "body":
                    thread["comments"]["nodes"][0]["body"] = "New detail even with unchanged timestamp"
                elif kind == "lifecycle":
                    thread["isResolved"] = True
                elif kind == "self-reply":
                    changed["initial"]["data"]["viewer"]["login"] = "different-viewer"
                elif kind == "limits":
                    flags = ["--body-limit", "100"]
                result = self.run_cli(
                    changed, "43" if kind == "scope" else "42", "--compact",
                    "--if-fingerprint", original["fingerprint"], *flags,
                )
                self.assertFalse(result["unchanged"])
                self.assertIn("records", result)
                self.assertNotEqual(original["fingerprint"], result["fingerprint"])

    def test_partial_poll_never_returns_reusable_fingerprint(self) -> None:
        fixture = load_fixture("partial-failure.json")
        result = self.run_cli(fixture, "9", "--compact", "--if-fingerprint", "0" * 64)
        self.assertTrue(result["partial"])
        self.assertFalse(result["unchanged"])
        self.assertNotIn("fingerprint", result)
        self.assertIn("records", result)
        self.assertTrue(result["errors"])

    def test_conditional_cli_requires_compact_and_valid_fingerprint(self) -> None:
        for flags in (["--if-fingerprint", "0" * 64], ["--compact", "--if-fingerprint", "bad"]):
            with self.subTest(flags=flags), contextlib.redirect_stderr(io.StringIO()):
                with self.assertRaises(SystemExit):
                    feedback.parse_args(["acme", "widgets", "42", *flags])

    def test_default_cli_keeps_full_inventory_contract(self) -> None:
        fixture = load_fixture("all-sources.json")
        expected = feedback.collect_inventory(FixtureClient(fixture), "acme", "widgets", [42])
        self.assertEqual(expected, self.run_cli(fixture, "42"))

    def test_compact_poll_preserves_partial_errors_and_available_records(self) -> None:
        for fixture_name, number, options in (
            ("partial-failure.json", "9", []),
            ("pagination.json", "7", ["--max-source-items", "1"]),
            ("all-sources.json", "42", ["--max-records-per-pr", "1"]),
        ):
            with self.subTest(fixture=fixture_name):
                fixture = load_fixture(fixture_name)
                full = self.run_cli(fixture, number, *options)
                compact = self.run_cli(fixture, number, *options, "--compact")
                self.assertTrue(compact["partial"])
                self.assertEqual(full["errors"], compact["errors"])
                self.assertEqual(full["limits"], compact["limits"])
                self.assertEqual(list(by_identity(full)), list(by_identity(compact)))

    def test_compact_poll_retains_pr_identity_in_batch(self) -> None:
        fixture = load_fixture("all-sources.json")
        repository = fixture["initial"]["data"]["repository"]
        repository["pr1"] = copy.deepcopy(repository["pr0"])
        result = self.run_cli(fixture, "42", "43", "--compact")
        self.assertFalse(result["partial"])
        self.assertEqual([42, 43], result["pullRequests"])
        self.assertEqual({42, 43}, {record["pr"] for record in result["records"]})

    def test_compact_poll_can_hydrate_an_exact_update_without_body_loss(self) -> None:
        fixture = load_fixture("all-sources.json")
        comment = fixture["initial"]["data"]["repository"]["pr0"]["comments"]["nodes"][0]
        comment["body"] = "Review detail. " * 300
        poll = self.run_cli(fixture, "42", "--compact")
        record = by_identity(poll)[f"conversation:{comment['id']}"]
        selected = self.run_cli(
            fixture, "42", "--identity", record["identity"],
            "--expected-update-key", record["updateKey"],
        )
        self.assertEqual("matched", selected["selection"]["status"])
        self.assertEqual(comment["body"], selected["records"][0]["rawBody"])
        self.assertFalse(selected["records"][0]["bodyTruncated"])

    def test_compact_and_focused_modes_are_mutually_exclusive(self) -> None:
        errors = io.StringIO()
        with contextlib.redirect_stderr(errors):
            with self.assertRaises(SystemExit) as raised:
                feedback.parse_args([
                    "acme", "widgets", "42", "--compact", "--identity", "conversation:C1",
                    "--expected-update-key", "conversation:C1@one",
                ])
        self.assertEqual(2, raised.exception.code)
        self.assertIn("--compact cannot be used with --identity", errors.getvalue())

    def test_caps_body_limit_at_githubs_maximum_comment_size(self) -> None:
        config = feedback.InventoryConfig(body_limit=1_000_000)

        self.assertEqual(feedback.MAX_BODY_CHARS, config.body_limit)

    def test_selects_one_unchanged_identity_for_full_body_validation(self) -> None:
        inventory = {
            "records": [
                {"identity": "conversation:C1", "updateKey": "conversation:C1@one", "rawBody": "full"},
                {"identity": "inline:C2", "updateKey": "inline:C2@two", "rawBody": "other"},
            ]
        }

        selected = feedback.select_inventory_record(
            inventory,
            "conversation:C1",
            "conversation:C1@one",
        )

        self.assertEqual("matched", selected["selection"]["status"])
        self.assertEqual(["conversation:C1"], [record["identity"] for record in selected["records"]])
        self.assertEqual(2, len(inventory["records"]), "selection must not mutate the normal inventory")

    def test_selection_withholds_missing_or_edited_feedback(self) -> None:
        inventory = {
            "records": [
                {"identity": "conversation:C1", "updateKey": "conversation:C1@new", "rawBody": "changed"},
            ]
        }

        stale = feedback.select_inventory_record(
            inventory,
            "conversation:C1",
            "conversation:C1@old",
        )
        missing = feedback.select_inventory_record(
            inventory,
            "conversation:missing",
            "conversation:missing@old",
        )

        self.assertEqual("stale", stale["selection"]["status"])
        self.assertEqual("conversation:C1@new", stale["selection"]["observedUpdateKey"])
        self.assertEqual([], stale["records"])
        self.assertEqual("not-found", missing["selection"]["status"])
        self.assertEqual([], missing["records"])

    def test_focused_cli_requires_one_pr_and_an_expected_update_key(self) -> None:
        with contextlib.redirect_stderr(io.StringIO()):
            with self.assertRaises(SystemExit):
                feedback.parse_args(["acme", "widgets", "42", "--identity", "conversation:C1"])
            with self.assertRaises(SystemExit):
                feedback.parse_args(
                    [
                        "acme",
                        "widgets",
                        "42",
                        "43",
                        "--identity",
                        "conversation:C1",
                        "--expected-update-key",
                        "conversation:C1@one",
                    ]
                )
            with self.assertRaises(SystemExit):
                feedback.parse_args(
                    [
                        "acme",
                        "widgets",
                        "42",
                        "--identity",
                        "",
                        "--expected-update-key",
                        "",
                    ]
                )

    def test_focused_cli_uses_full_bounded_body_and_emits_one_record(self) -> None:
        captured = {}

        def collect(client, owner, repo, numbers, config):
            captured["body_limit"] = config.body_limit
            return {
                "partial": False,
                "records": [
                    {
                        "identity": "conversation:C1",
                        "updateKey": "conversation:C1@one",
                        "rawBody": "full",
                        "bodyTruncated": False,
                    },
                    {
                        "identity": "inline:C2",
                        "updateKey": "inline:C2@two",
                        "rawBody": "other",
                        "bodyTruncated": False,
                    },
                ],
            }

        output = io.StringIO()
        with mock.patch.object(feedback, "collect_inventory", side_effect=collect):
            with contextlib.redirect_stdout(output):
                exit_code = feedback.main(
                    [
                        "acme",
                        "widgets",
                        "42",
                        "--identity",
                        "conversation:C1",
                        "--expected-update-key",
                        "conversation:C1@one",
                    ]
                )

        result = json.loads(output.getvalue())
        self.assertEqual(0, exit_code)
        self.assertEqual(feedback.MAX_BODY_CHARS, captured["body_limit"])
        self.assertEqual("matched", result["selection"]["status"])
        self.assertEqual(["conversation:C1"], [record["identity"] for record in result["records"]])

    def test_normalizes_every_feedback_surface_and_response_target(self) -> None:
        result = feedback.collect_inventory(
            FixtureClient(load_fixture("all-sources.json")),
            "acme",
            "widgets",
            [42],
            feedback.InventoryConfig(body_limit=80),
        )
        records = by_identity(result)

        self.assertEqual(1, result["schemaVersion"])
        self.assertFalse(result["partial"])
        self.assertEqual(
            {"inline_review", "review_summary", "conversation", "check_annotation"},
            {record["source"] for record in records.values()},
        )
        self.assertEqual({"root", "reply"}, {records["inline:C1"]["role"], records["inline:C2"]["role"]})
        self.assertEqual("C1", records["inline:C1"]["nodeId"])
        self.assertEqual(101, records["inline:C1"]["databaseId"])
        self.assertEqual("T1", records["inline:C1"]["threadId"])
        self.assertEqual(101, records["inline:C2"]["targets"]["reply"]["commentId"])
        self.assertEqual("T1", records["inline:C2"]["targets"]["resolveThreadId"])
        self.assertEqual("C1", records["inline:C2"]["replyToNodeId"])
        self.assertTrue(records["inline:C1"]["hasLaterSelfReply"])
        self.assertTrue(records["inline:C1"]["threadHasSelfReply"])
        self.assertEqual(201, records["review:R1"]["databaseId"])
        self.assertEqual(301, records["conversation:I1"]["databaseId"])
        self.assertEqual("conversation", records["conversation:I1"]["targets"]["reply"]["surface"])
        self.assertIsNone(records[next(key for key in records if key.startswith("check_annotation:"))]["targets"]["reply"])

        self.assertEqual("human", records["inline:C1"]["authorKind"])
        self.assertEqual("self", records["inline:C2"]["authorKind"])
        self.assertEqual("bot", records["inline:C3"]["authorKind"])
        self.assertTrue(records["inline:C2"]["selfAuthored"])
        self.assertEqual("suppressed", records["inline:C2"]["actionability"])

        self.assertEqual("unresolved", records["inline:C1"]["lifecycle"])
        self.assertEqual("resolved", records["inline:C3"]["lifecycle"])
        self.assertTrue(records["inline:C3"]["isResolved"])
        self.assertEqual("outdated", records["inline:C4"]["lifecycle"])
        self.assertTrue(records["inline:C4"]["isOutdated"])
        self.assertEqual("dismissed", records["review:R4"]["lifecycle"])
        self.assertNotIn("review:R5", records, "pending draft reviews are not observable inventory")

        self.assertEqual("change_request", records["inline:C1"]["semanticType"])
        self.assertEqual("suggestion", records["inline:C3"]["semanticType"])
        self.assertEqual("security_claim", records["inline:C4"]["semanticType"])
        self.assertEqual("question", records["review:R1"]["semanticType"])
        self.assertEqual("approval", records["review:R2"]["semanticType"])
        self.assertEqual("blocking_claim", records["review:R3"]["semanticType"])
        self.assertEqual("automated_status", records["conversation:I2"]["semanticType"])
        self.assertEqual("noise", records["conversation:I2"]["actionability"])
        self.assertTrue(records["review:R3"]["requiresValidation"])

        self.assertTrue(records["conversation:I1"]["bodyTruncated"])
        self.assertLessEqual(len(records["conversation:I1"]["rawBody"]), 80)
        annotations = [record for record in records.values() if record["source"] == "check_annotation"]
        self.assertEqual({"src/app.py"}, {record["path"] for record in annotations})
        self.assertEqual({"candidate", "informational"}, {record["actionability"] for record in annotations})
        self.assertEqual(13, len(records))

    def test_identity_is_stable_and_update_key_changes_for_an_edit(self) -> None:
        original = load_fixture("all-sources.json")
        first = feedback.collect_inventory(FixtureClient(original), "acme", "widgets", [42])
        repeated = feedback.collect_inventory(FixtureClient(original), "acme", "widgets", [42])
        self.assertEqual(first, repeated)

        edited = copy.deepcopy(original)
        change = load_fixture("edited-comment.json")
        comments = edited["initial"]["data"]["repository"]["pr0"]["reviewThreads"]["nodes"][0]["comments"]["nodes"]
        node = next(item for item in comments if item["id"] == change["nodeId"])
        node["body"] = change["body"]
        node["updatedAt"] = change["updatedAt"]
        after_edit = feedback.collect_inventory(FixtureClient(edited), "acme", "widgets", [42])

        before_record = by_identity(first)["inline:C1"]
        edited_record = by_identity(after_edit)["inline:C1"]
        self.assertEqual(before_record["identity"], edited_record["identity"])
        self.assertNotEqual(before_record["updateKey"], edited_record["updateKey"])
        self.assertEqual(change["updatedAt"], edited_record["updatedAt"])

    def test_state_key_changes_when_thread_lifecycle_changes_without_a_comment_edit(self) -> None:
        original = load_fixture("all-sources.json")
        resolved = copy.deepcopy(original)
        resolved["initial"]["data"]["repository"]["pr0"]["reviewThreads"]["nodes"][0]["isResolved"] = True

        before = by_identity(feedback.collect_inventory(FixtureClient(original), "acme", "widgets", [42]))["inline:C1"]
        after = by_identity(feedback.collect_inventory(FixtureClient(resolved), "acme", "widgets", [42]))["inline:C1"]

        self.assertEqual(before["identity"], after["identity"])
        self.assertEqual(before["updateKey"], after["updateKey"])
        self.assertNotEqual(before["stateKey"], after["stateKey"])
        self.assertEqual("resolved", after["lifecycle"])

    def test_flags_outdated_unanswered_threads_as_needing_closure(self) -> None:
        fixture = load_fixture("all-sources.json")
        threads = fixture["initial"]["data"]["repository"]["pr0"]["reviewThreads"]["nodes"]
        answered = copy.deepcopy(threads[0])
        answered.update({"id": "T9", "isOutdated": True})
        for index, comment in enumerate(answered["comments"]["nodes"]):
            comment["id"] = f"C9{index}"
        threads.append(answered)

        records = by_identity(feedback.collect_inventory(FixtureClient(fixture), "acme", "widgets", [42]))

        self.assertTrue(records["inline:C4"]["needsClosure"], "outdated, unresolved and unanswered")
        self.assertEqual("suppressed", records["inline:C4"]["actionability"], "other consumers unchanged")
        self.assertFalse(records["inline:C90"]["needsClosure"], "already self-answered")
        self.assertFalse(records["inline:C91"]["needsClosure"], "self-authored")
        self.assertFalse(records["inline:C1"]["needsClosure"], "current thread")
        self.assertFalse(records["inline:C3"]["needsClosure"], "resolved thread")

    def test_paginates_connections_and_deduplicates_stable_ids(self) -> None:
        client = FixtureClient(load_fixture("pagination.json"))
        result = feedback.collect_inventory(client, "acme", "widgets", [7])

        self.assertFalse(result["partial"])
        self.assertEqual(["inline:PC1", "inline:PC2"], sorted(record["identity"] for record in result["records"]))
        self.assertEqual([(7, "reviewThreads", "threads-1")], client.connection_calls)

    def test_marks_bounded_truncation_without_fetching_beyond_the_cap(self) -> None:
        client = FixtureClient(load_fixture("pagination.json"))
        result = feedback.collect_inventory(
            client,
            "acme",
            "widgets",
            [7],
            feedback.InventoryConfig(max_source_items=1),
        )

        self.assertTrue(result["partial"])
        self.assertEqual([], client.connection_calls)
        self.assertEqual(1, len(result["records"]))
        self.assertIn("truncated", {error["kind"] for error in result["errors"]})

    def test_reports_bounded_rest_annotation_truncation(self) -> None:
        result = feedback.collect_inventory(
            FixtureClient(load_fixture("all-sources.json")),
            "acme",
            "widgets",
            [42],
            feedback.InventoryConfig(max_source_items=1),
        )

        self.assertTrue(result["partial"])
        self.assertIn(
            ("check_annotations", "truncated"),
            {(error["source"], error["kind"]) for error in result["errors"]},
        )
        self.assertEqual(1, sum(record["source"] == "check_annotation" for record in result["records"]))

    def test_preserves_available_records_and_reports_partial_api_failures(self) -> None:
        result = feedback.collect_inventory(
            FixtureClient(load_fixture("partial-failure.json")),
            "acme",
            "widgets",
            [9],
        )

        self.assertTrue(result["partial"])
        self.assertEqual(["inline:FC1"], [record["identity"] for record in result["records"]])
        self.assertEqual({"reviews", "check_runs"}, {error["source"] for error in result["errors"]})
        self.assertTrue(all(error["retryable"] for error in result["errors"]))

    def test_missing_viewer_marks_self_classification_partial(self) -> None:
        fixture = load_fixture("all-sources.json")
        fixture["initial"]["data"]["viewer"] = None

        result = feedback.collect_inventory(FixtureClient(fixture), "acme", "widgets", [42])

        self.assertTrue(result["partial"])
        self.assertIn("viewer", {error["source"] for error in result["errors"]})
        self.assertFalse(by_identity(result)["inline:C2"]["selfAuthored"])

    def test_initial_failure_is_machine_readable(self) -> None:
        class BrokenClient(FixtureClient):
            def fetch_initial(self, owner: str, repo: str, numbers: list[int], page_size: int) -> dict:
                raise RuntimeError("GraphQL unavailable")

        result = feedback.collect_inventory(BrokenClient({}), "acme", "widgets", [1])
        self.assertTrue(result["partial"])
        self.assertEqual([], result["records"])
        self.assertEqual("initial", result["errors"][0]["source"])
        self.assertEqual("api", result["errors"][0]["kind"])


if __name__ == "__main__":
    unittest.main()
planning-and-task-breakdownaddyosmani/agent-skillsBreaks work into ordered tasks. Use when you have a spec or clear requirements and need to break work into implementable tasks. Use when a task feels too large to start, when you need to estimate scope, or when parallel work is possible.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
screenpipethedotmack/claude-memSearch screen and audio history recorded by Screenpipe and optionally save selected context into claude-mem. Use when the user asks to recall desktop activity, a meeting, or something seen in another app through Screenpipe.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
contact-widgetnexu-io/open-designSelf-contained floating chat widget with welcome screen, social links, meeting button, and message input. Single HTML file, zero dependencies.Stars: 108kUpdated Claude CodeCodexVerifiedInstall
meeting-notesnexu-io/open-designMeeting notes page — title bar with attendees, agenda checklist, decisions block, action items table with owners + dates, and a "next meeting" footer. Use when the brief mentions "meeting notes", "minutes", "1:1 notes", "all-hands recap", or "会议纪要".Stars: 108kUpdated Claude CodeCodexVerifiedInstall