package openai import ( "testing" "github.com/stretchr/testify/assert" ) func TestIsCodexCLIRequest(t *testing.T) { tests := []struct { name string userAgent string expected bool }{ {"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) { result := IsCodexCLIRequest(tt.userAgent) assert.Equal(t, tt.expected, result) }) } } func TestIsCodexOfficialClientRequest(t *testing.T) { tests := []struct { name string userAgent string expected bool }{ {"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) { result := IsCodexOfficialClientRequest(tt.userAgent) assert.Equal(t, tt.expected, result) }) } } func TestIsCodexOfficialClientOriginator(t *testing.T) { tests := []struct { name string originator string expected bool }{ {"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) { result := IsCodexOfficialClientOriginator(tt.originator) assert.Equal(t, tt.expected, result) }) } } func TestIsCodexOfficialClientByHeaders(t *testing.T) { tests := []struct { name string userAgent string originator string expected bool }{ {"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) { 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) }