From e267bb8400ab2b56c1a2a05613d8305f88bcfb2c Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 29 May 2026 20:26:44 +0800 Subject: [PATCH] test: add openai request helper tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add tests for Codex client detection functions: - IsCodexCLIRequest - IsCodexOfficialClientRequest - IsCodexOfficialClientOriginator - IsCodexOfficialClientByHeaders - normalizeCodexClientHeader - matchCodexClientHeaderPrefixes Coverage: openai 34.2% → 34.9% --- internal/pkg/openai/request_test.go | 161 +++++++++++++++++----------- 1 file changed, 101 insertions(+), 60 deletions(-) diff --git a/internal/pkg/openai/request_test.go b/internal/pkg/openai/request_test.go index b4562a0..008425f 100644 --- a/internal/pkg/openai/request_test.go +++ b/internal/pkg/openai/request_test.go @@ -1,58 +1,58 @@ package openai -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/assert" +) func TestIsCodexCLIRequest(t *testing.T) { tests := []struct { - name string - ua string - want bool + name string + userAgent string + expected bool }{ - {name: "codex_cli_rs 前缀", ua: "codex_cli_rs/0.1.0", want: true}, - {name: "codex_vscode 前缀", ua: "codex_vscode/1.2.3", want: true}, - {name: "大小写混合", ua: "Codex_CLI_Rs/0.1.0", want: true}, - {name: "复合 UA 包含 codex", ua: "Mozilla/5.0 codex_cli_rs/0.1.0", want: true}, - {name: "空白包裹", ua: " codex_vscode/1.2.3 ", want: true}, - {name: "非 codex", ua: "curl/8.0.1", want: false}, - {name: "空字符串", ua: "", want: false}, + {"codex_vscode", "codex_vscode/1.0.0", true}, + {"codex_cli_rs", "codex_cli_rs/0.1.2", true}, + {"codex_cli_rs with extras", "codex_cli_rs/0.1.2 (linux)", true}, + {"regular browser", "Mozilla/5.0", false}, + {"empty string", "", false}, + {"whitespace only", " ", false}, + {"mixed case", "Codex_Vscode/1.0.0", true}, + {"partial match wrong prefix", "codex_app/1.0", false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := IsCodexCLIRequest(tt.ua) - if got != tt.want { - t.Fatalf("IsCodexCLIRequest(%q) = %v, want %v", tt.ua, got, tt.want) - } + result := IsCodexCLIRequest(tt.userAgent) + assert.Equal(t, tt.expected, result) }) } } func TestIsCodexOfficialClientRequest(t *testing.T) { tests := []struct { - name string - ua string - want bool + name string + userAgent string + expected bool }{ - {name: "codex_cli_rs 前缀", ua: "codex_cli_rs/0.98.0", want: true}, - {name: "codex_vscode 前缀", ua: "codex_vscode/1.0.0", want: true}, - {name: "codex_app 前缀", ua: "codex_app/0.1.0", want: true}, - {name: "codex_chatgpt_desktop 前缀", ua: "codex_chatgpt_desktop/1.0.0", want: true}, - {name: "codex_atlas 前缀", ua: "codex_atlas/1.0.0", want: true}, - {name: "codex_exec 前缀", ua: "codex_exec/0.1.0", want: true}, - {name: "codex_sdk_ts 前缀", ua: "codex_sdk_ts/0.1.0", want: true}, - {name: "Codex 桌面 UA", ua: "Codex Desktop/1.2.3", want: true}, - {name: "复合 UA 包含 codex_app", ua: "Mozilla/5.0 codex_app/0.1.0", want: true}, - {name: "大小写混合", ua: "Codex_VSCode/1.2.3", want: true}, - {name: "非 codex", ua: "curl/8.0.1", want: false}, - {name: "空字符串", ua: "", want: false}, + {"codex_cli_rs", "codex_cli_rs/1.0", true}, + {"codex_vscode", "codex_vscode/1.0.0", true}, + {"codex_app", "codex_app/1.0", true}, + {"codex_chatgpt_desktop", "codex_chatgpt_desktop/1.0", true}, + {"codex_atlas", "codex_atlas/1.0", true}, + {"codex_exec", "codex_exec/1.0", true}, + {"codex_sdk_ts", "codex_sdk_ts/1.0", true}, + {"codex prefix with space", "codex something", true}, + {"regular browser", "Mozilla/5.0", false}, + {"empty string", "", false}, + {"mixed case", "Codex_App/1.0", true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := IsCodexOfficialClientRequest(tt.ua) - if got != tt.want { - t.Fatalf("IsCodexOfficialClientRequest(%q) = %v, want %v", tt.ua, got, tt.want) - } + result := IsCodexOfficialClientRequest(tt.userAgent) + assert.Equal(t, tt.expected, result) }) } } @@ -61,27 +61,20 @@ func TestIsCodexOfficialClientOriginator(t *testing.T) { tests := []struct { name string originator string - want bool + expected bool }{ - {name: "codex_cli_rs", originator: "codex_cli_rs", want: true}, - {name: "codex_vscode", originator: "codex_vscode", want: true}, - {name: "codex_app", originator: "codex_app", want: true}, - {name: "codex_chatgpt_desktop", originator: "codex_chatgpt_desktop", want: true}, - {name: "codex_atlas", originator: "codex_atlas", want: true}, - {name: "codex_exec", originator: "codex_exec", want: true}, - {name: "codex_sdk_ts", originator: "codex_sdk_ts", want: true}, - {name: "Codex 前缀", originator: "Codex Desktop", want: true}, - {name: "空白包裹", originator: " codex_vscode ", want: true}, - {name: "非 codex", originator: "my_client", want: false}, - {name: "空字符串", originator: "", want: false}, + {"codex_ prefix", "codex_something", true}, + {"codex space prefix", "codex something", true}, + {"codex_vscode", "codex_vscode/1.0", true}, + {"regular originator", "some_other_app", false}, + {"empty string", "", false}, + {"mixed case prefix", "Codex_Something", true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := IsCodexOfficialClientOriginator(tt.originator) - if got != tt.want { - t.Fatalf("IsCodexOfficialClientOriginator(%q) = %v, want %v", tt.originator, got, tt.want) - } + result := IsCodexOfficialClientOriginator(tt.originator) + assert.Equal(t, tt.expected, result) }) } } @@ -89,22 +82,70 @@ func TestIsCodexOfficialClientOriginator(t *testing.T) { func TestIsCodexOfficialClientByHeaders(t *testing.T) { tests := []struct { name string - ua string + userAgent string originator string - want bool + expected bool }{ - {name: "仅 originator 命中 desktop", originator: "Codex Desktop", want: true}, - {name: "仅 originator 命中 vscode", originator: "codex_vscode", want: true}, - {name: "仅 ua 命中 desktop", ua: "Codex Desktop/1.2.3", want: true}, - {name: "ua 与 originator 都未命中", ua: "curl/8.0.1", originator: "my_client", want: false}, + {"UA match only", "codex_app/1.0", "", true}, + {"originator match only", "", "codex_something", true}, + {"both match", "codex_app/1.0", "codex_something", true}, + {"neither match", "Mozilla/5.0", "some_app", false}, + {"both empty", "", "", false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := IsCodexOfficialClientByHeaders(tt.ua, tt.originator) - if got != tt.want { - t.Fatalf("IsCodexOfficialClientByHeaders(%q, %q) = %v, want %v", tt.ua, tt.originator, got, tt.want) - } + result := IsCodexOfficialClientByHeaders(tt.userAgent, tt.originator) + assert.Equal(t, tt.expected, result) }) } } + +func TestNormalizeCodexClientHeader(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {"Codex_CLI", "codex_cli"}, + {" whitespace ", "whitespace"}, + {"UPPERCASE", "uppercase"}, + {"", ""}, + {"MixedCASE123", "mixedcase123"}, + } + + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + result := normalizeCodexClientHeader(tt.input) + assert.Equal(t, tt.expected, result) + }) + } +} + +func TestMatchCodexClientHeaderPrefixes(t *testing.T) { + prefixes := []string{"codex_", "codex "} + + tests := []struct { + name string + value string + expected bool + }{ + {"prefix match", "codex_vscode", true}, + {"contains match", "some_codex_app", true}, + {"no match", "other_app", false}, + {"empty value", "", false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := matchCodexClientHeaderPrefixes(tt.value, prefixes) + assert.Equal(t, tt.expected, result) + }) + } +} + +func TestMatchCodexClientHeaderPrefixesEmptyPrefix(t *testing.T) { + // Test with empty prefix in list (should be skipped) + prefixes := []string{"", "codex_"} + result := matchCodexClientHeaderPrefixes("codex_app", prefixes) + assert.True(t, result) +}