Files
huangguo_server/common/laosiji_app/laosiji_test.go
T
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

110 lines
2.7 KiB
Go

package laosiji_app
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestEncryptDecryptRoundTrip(t *testing.T) {
const (
key = "1234567890abcdef"
input = `{"uid":204,"name":"测试用户"}`
)
encrypted, err := encryptBase64(input, key)
if err != nil {
t.Fatalf("encryptBase64() error = %v", err)
}
decrypted, err := decryptBase64(encrypted, key)
if err != nil {
t.Fatalf("decryptBase64() error = %v", err)
}
if decrypted != input {
t.Fatalf("decryptBase64() = %q, want %q", decrypted, input)
}
}
func TestGetUserName(t *testing.T) {
if got := GetUserName("test", 204, 123); got != "TEST-204_123" {
t.Fatalf("test username = %q", got)
}
if got := GetUserName("prod", 204, 123); got != "JHA-204_123" {
t.Fatalf("prod username = %q", got)
}
}
func TestConfigured(t *testing.T) {
original := cfg
t.Cleanup(func() { cfg = original })
Init(Config{})
if Configured() {
t.Fatal("empty config must not be configured")
}
Init(Config{AppID: "app", APIKey: "1234567890abcdef", APIURL: "https://example.com/"})
if !Configured() {
t.Fatal("complete config must be configured")
}
if cfg.APIURL != "https://example.com" {
t.Fatalf("APIURL = %q", cfg.APIURL)
}
}
func TestGetAiMateURLRequestAndResponse(t *testing.T) {
const key = "1234567890abcdef"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/lsjapi/aiGirlFriend/auth" {
t.Errorf("path = %q", r.URL.Path)
}
if r.Header.Get("appid") != "test-app" {
t.Errorf("appid = %q", r.Header.Get("appid"))
}
body, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("read request: %v", err)
return
}
plainText, err := decryptBase64(string(body), key)
if err != nil {
t.Errorf("decrypt request: %v", err)
return
}
var request map[string]interface{}
if err = json.Unmarshal([]byte(plainText), &request); err != nil {
t.Errorf("unmarshal request: %v", err)
return
}
if request["username"] != "TEST-204_99" || request["asset"] != "12.30" {
t.Errorf("request = %#v", request)
}
data, err := encryptBase64(`{"auth_url":"https://example.com/ai"}`, key)
if err != nil {
t.Errorf("encrypt response: %v", err)
return
}
_ = json.NewEncoder(w).Encode(Response{Status: "y", Data: data})
}))
defer server.Close()
original := cfg
t.Cleanup(func() { cfg = original })
Init(Config{AppID: "test-app", APIKey: key, APIURL: server.URL})
response, err := GetAiMateURL(context.Background(), GetAiMateURLReq{
Username: "TEST-204_99",
Asset: "12.30",
Currency: "CNY",
Theme: "dark",
})
if err != nil {
t.Fatalf("GetAiMateURL() error = %v", err)
}
if response.AuthURL != "https://example.com/ai" {
t.Fatalf("AuthURL = %q", response.AuthURL)
}
}