Skip to content
← All exercises

Tags that leak between tickets

Python · A tagging helper in a support-ticket service · Logic · difficulty 2/3 · ~8 min

An AI assistant was asked for tagging helpers in a support-ticket service:

Write add_tags(new_tags, existing) that returns the existing tags plus the new ones, normalized (lowercase, trimmed, inner spaces as dashes) and without duplicates. existing should be optional. Also write tag_ticket(ticket, new_tags) that returns a copy of the ticket with the new tags added.

The helpers are used by an API handler that tags many tickets per request, and by a nightly job that re-tags old tickets.

The visible tests pass. Review the code before it ships: what happens on the second call, and to the ticket you passed in?

Repair the code until the hidden tests pass.

code.py

Visible tests (they pass on the code as given)
from exercise import Ticket, add_tags, normalize_tag, tag_ticket


def test_normalizes_case_and_spaces():
    assert normalize_tag("  Billing Issue ") == "billing-issue"


def test_adds_new_tags_after_the_existing_ones():
    assert add_tags(["Urgent", "urgent"], ["vip"]) == ["vip", "urgent"]


def test_tagged_copy_has_old_and_new_tags():
    ticket = Ticket(1, "Refund request", ["billing"])
    assert tag_ticket(ticket, ["Refund"]).tags == ["billing", "refund"]