|
| 1 | +/* |
| 2 | +Copyright 2026 The llm-d Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package steps |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/llm-d/llm-d-router/pkg/coordinator/gateway" |
| 24 | +) |
| 25 | + |
| 26 | +func TestCapSingleTokenOutput(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + format gateway.RequestFormat |
| 30 | + body map[string]any |
| 31 | + want map[string]any |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "chat completions caps output fields and forces non-streaming", |
| 35 | + format: gateway.FormatChatCompletions, |
| 36 | + body: map[string]any{ |
| 37 | + "model": "m", |
| 38 | + "max_tokens": 100, |
| 39 | + "min_tokens": 5, |
| 40 | + "max_completion_tokens": 100, |
| 41 | + "stream": true, |
| 42 | + "stream_options": map[string]any{"include_usage": true}, |
| 43 | + }, |
| 44 | + want: map[string]any{ |
| 45 | + "model": "m", |
| 46 | + "max_tokens": 1, |
| 47 | + "max_completion_tokens": 1, |
| 48 | + "stream": false, |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "max_completion_tokens is not added when the client omitted it", |
| 53 | + format: gateway.FormatChatCompletions, |
| 54 | + body: map[string]any{"model": "m"}, |
| 55 | + want: map[string]any{ |
| 56 | + "model": "m", |
| 57 | + "max_tokens": 1, |
| 58 | + "stream": false, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "completions caps max_tokens, strips min_tokens, forces non-streaming", |
| 63 | + format: gateway.FormatCompletions, |
| 64 | + body: map[string]any{"model": "m", "max_tokens": 100, "min_tokens": 5}, |
| 65 | + want: map[string]any{"model": "m", "max_tokens": 1, "stream": false}, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "streaming is forced false and stream_options stripped", |
| 69 | + format: gateway.FormatCompletions, |
| 70 | + body: map[string]any{"stream": true, "stream_options": map[string]any{"include_usage": true}}, |
| 71 | + want: map[string]any{"stream": false, "max_tokens": 1}, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "generate caps max_tokens and strips min_tokens inside sampling_params", |
| 75 | + format: gateway.FormatGenerate, |
| 76 | + body: map[string]any{ |
| 77 | + "model": "m", |
| 78 | + "sampling_params": map[string]any{"max_tokens": 100, "min_tokens": 5}, |
| 79 | + }, |
| 80 | + want: map[string]any{ |
| 81 | + "model": "m", |
| 82 | + "sampling_params": map[string]any{"max_tokens": 1}, |
| 83 | + "stream": false, |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "generate synthesizes sampling_params when absent", |
| 88 | + format: gateway.FormatGenerate, |
| 89 | + body: map[string]any{"model": "m"}, |
| 90 | + want: map[string]any{ |
| 91 | + "model": "m", |
| 92 | + "sampling_params": map[string]any{"max_tokens": 1}, |
| 93 | + "stream": false, |
| 94 | + }, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "generate preserves other sampling_params entries", |
| 98 | + format: gateway.FormatGenerate, |
| 99 | + body: map[string]any{ |
| 100 | + "sampling_params": map[string]any{ |
| 101 | + "extra_args": map[string]any{"kv_transfer_params": "x"}, |
| 102 | + }, |
| 103 | + }, |
| 104 | + want: map[string]any{ |
| 105 | + "sampling_params": map[string]any{ |
| 106 | + "max_tokens": 1, |
| 107 | + "extra_args": map[string]any{"kv_transfer_params": "x"}, |
| 108 | + }, |
| 109 | + "stream": false, |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, tt := range tests { |
| 115 | + t.Run(tt.name, func(t *testing.T) { |
| 116 | + capSingleTokenOutput(tt.body, tt.format) |
| 117 | + if !reflect.DeepEqual(tt.body, tt.want) { |
| 118 | + t.Fatalf("got %v, want %v", tt.body, tt.want) |
| 119 | + } |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments