|
| 1 | +"""Tests for a2a.client.service_parameters module.""" |
| 2 | + |
| 3 | +from a2a.client.service_parameters import ( |
| 4 | + ServiceParametersFactory, |
| 5 | + with_a2a_extensions, |
| 6 | +) |
| 7 | +from a2a.extensions.common import HTTP_EXTENSION_HEADER |
| 8 | + |
| 9 | + |
| 10 | +def test_with_a2a_extensions_sets_header_when_empty(): |
| 11 | + """First call on empty parameters sets the joined URIs.""" |
| 12 | + parameters = ServiceParametersFactory.create( |
| 13 | + [with_a2a_extensions(['ext-b', 'ext-a'])] |
| 14 | + ) |
| 15 | + |
| 16 | + assert parameters[HTTP_EXTENSION_HEADER] == 'ext-a,ext-b' |
| 17 | + |
| 18 | + |
| 19 | +def test_with_a2a_extensions_merges_disjoint_calls(): |
| 20 | + """A second call with disjoint URIs unions both sets.""" |
| 21 | + parameters = ServiceParametersFactory.create( |
| 22 | + [ |
| 23 | + with_a2a_extensions(['ext-a']), |
| 24 | + with_a2a_extensions(['ext-b']), |
| 25 | + ] |
| 26 | + ) |
| 27 | + |
| 28 | + assert parameters[HTTP_EXTENSION_HEADER] == 'ext-a,ext-b' |
| 29 | + |
| 30 | + |
| 31 | +def test_with_a2a_extensions_deduplicates_overlapping(): |
| 32 | + """Overlapping URIs do not produce duplicates.""" |
| 33 | + parameters = ServiceParametersFactory.create( |
| 34 | + [ |
| 35 | + with_a2a_extensions(['ext-a', 'ext-b']), |
| 36 | + with_a2a_extensions(['ext-b', 'ext-c']), |
| 37 | + ] |
| 38 | + ) |
| 39 | + |
| 40 | + assert parameters[HTTP_EXTENSION_HEADER] == 'ext-a,ext-b,ext-c' |
| 41 | + |
| 42 | + |
| 43 | +def test_with_a2a_extensions_empty_is_noop(): |
| 44 | + """Calling with an empty list leaves any existing header untouched.""" |
| 45 | + parameters = ServiceParametersFactory.create( |
| 46 | + [ |
| 47 | + with_a2a_extensions(['ext-a']), |
| 48 | + with_a2a_extensions([]), |
| 49 | + ] |
| 50 | + ) |
| 51 | + |
| 52 | + assert parameters[HTTP_EXTENSION_HEADER] == 'ext-a' |
| 53 | + |
| 54 | + |
| 55 | +def test_with_a2a_extensions_empty_does_not_create_header(): |
| 56 | + """Calling with an empty list on empty parameters adds nothing.""" |
| 57 | + parameters = ServiceParametersFactory.create([with_a2a_extensions([])]) |
| 58 | + |
| 59 | + assert HTTP_EXTENSION_HEADER not in parameters |
| 60 | + |
| 61 | + |
| 62 | +def test_with_a2a_extensions_output_is_sorted(): |
| 63 | + """Output ordering is deterministic (sorted) regardless of input order.""" |
| 64 | + parameters = ServiceParametersFactory.create( |
| 65 | + [with_a2a_extensions(['c', 'a', 'b'])] |
| 66 | + ) |
| 67 | + |
| 68 | + assert parameters[HTTP_EXTENSION_HEADER] == 'a,b,c' |
| 69 | + |
| 70 | + |
| 71 | +def test_with_a2a_extensions_merges_existing_header_value(): |
| 72 | + """Existing comma-separated header values are parsed and merged.""" |
| 73 | + base = ServiceParametersFactory.create_from( |
| 74 | + {HTTP_EXTENSION_HEADER: 'ext-a, ext-b'}, |
| 75 | + [with_a2a_extensions(['ext-c'])], |
| 76 | + ) |
| 77 | + |
| 78 | + assert base[HTTP_EXTENSION_HEADER] == 'ext-a,ext-b,ext-c' |
0 commit comments