后端 (Go/Gin/GORM): - 配置(viper+env)、SQLite/Postgres 迁移、argon2id、AES-GCM 渠道密钥、JWT+refresh cookie - 用户注册/登录/刷新/登出、API Key CRUD(仅存哈希、明文一次展示) - 代理网关: /v1/chat/completions、/v1/responses、/v1/models 直通 OpenAI 渠道 非流式+流式(SSE 零缓冲转发), 用量捕获(chat 末块/responses completed 嵌套), OpenAI 错误格式(401/402/404/502), 余额检查 - 异步批量记账 + 余额流水 + 日聚合, admin 用户/余额/配置 API - 单测: crypto/jwt/apikey/流式 usage 提取 前端 (Vue3+TS+Vite+Tailwind v4): - taste-skill 设计 tokens: 深色仪表盘, 石墨+信号铜色, Outfit+JetBrains Mono - Landing/登录/注册, 控制台(仪表盘图表/密钥管理/用量明细) - 基础组件 Button/Input/Badge/Modal, ECharts 用量图 部署: docker-compose(nginx+api+postgres), 双 Dockerfile, nginx SSE 反代 联调: scripts/mockupstream 本地 mock 上游, 端到端验证通过
108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package proxy
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"testing"
|
|
)
|
|
|
|
func TestScanUsageChat(t *testing.T) {
|
|
line := []byte(`data: {"id":"x","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":9,"total_tokens":21}}`)
|
|
raw := scanUsage(line)
|
|
if raw == nil {
|
|
t.Fatal("chat usage not detected")
|
|
}
|
|
var us usageShape
|
|
if err := json.Unmarshal(raw, &us); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if us.PromptTokens != 12 || us.CompletionTokens != 9 {
|
|
t.Fatalf("usage mismatch: %+v", us)
|
|
}
|
|
}
|
|
|
|
func TestScanUsageResponsesNested(t *testing.T) {
|
|
line := []byte(`data: {"response":{"id":"r","status":"completed","usage":{"input_tokens":15,"output_tokens":11,"total_tokens":26}},"type":"response.completed"}`)
|
|
raw := scanUsage(line)
|
|
if raw == nil {
|
|
t.Fatal("responses nested usage not detected")
|
|
}
|
|
var us usageShape
|
|
if err := json.Unmarshal(raw, &us); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if us.InputTokens != 15 || us.OutputTokens != 11 {
|
|
t.Fatalf("usage mismatch: %+v", us)
|
|
}
|
|
}
|
|
|
|
func TestScanUsageIgnoresNonData(t *testing.T) {
|
|
if scanUsage([]byte("event: response.completed")) != nil {
|
|
t.Fatal("event line should be ignored")
|
|
}
|
|
if scanUsage([]byte("data: [DONE]")) != nil {
|
|
t.Fatal("[DONE] should be ignored")
|
|
}
|
|
if scanUsage([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}]}")) != nil {
|
|
t.Fatal("content chunk without usage should be ignored")
|
|
}
|
|
}
|
|
|
|
func TestExtractUsageFromFullBody(t *testing.T) {
|
|
body := []byte(`{"id":"x","choices":[{"message":{"content":"hi"}}],"usage":{"prompt_tokens":1,"completion_tokens":2}}`)
|
|
raw := extractUsage(body)
|
|
if raw == nil {
|
|
t.Fatal("usage not extracted from full body")
|
|
}
|
|
var us usageShape
|
|
_ = json.Unmarshal(raw, &us)
|
|
if us.PromptTokens != 1 || us.CompletionTokens != 2 {
|
|
t.Fatalf("usage mismatch: %+v", us)
|
|
}
|
|
}
|
|
|
|
func TestSSEScannerLines(t *testing.T) {
|
|
// 模拟分块写入的 SSE 流
|
|
data := "data: {\"a\":1}\n\ndata: {\"usage\":{\"input_tokens\":3}}\n\n"
|
|
parts := [][]byte{[]byte(data[:10]), []byte(data[10:20]), []byte(data[20:])}
|
|
reader := newChunkReader(parts)
|
|
s := newSSEScanner(reader)
|
|
var lines [][]byte
|
|
for {
|
|
line, err := s.Next()
|
|
if line != nil {
|
|
lines = append(lines, line)
|
|
}
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
if len(lines) != 4 {
|
|
t.Fatalf("expected 4 lines, got %d", len(lines))
|
|
}
|
|
// 合并后应能还原原始数据
|
|
joined := ""
|
|
for _, l := range lines {
|
|
joined += string(l)
|
|
}
|
|
if joined != string(data) {
|
|
t.Fatalf("stream corrupted:\n got: %q\nwant: %q", joined, data)
|
|
}
|
|
}
|
|
|
|
type chunkReader struct {
|
|
parts [][]byte
|
|
idx int
|
|
}
|
|
|
|
func newChunkReader(parts [][]byte) *chunkReader { return &chunkReader{parts: parts} }
|
|
|
|
func (r *chunkReader) Read(p []byte) (int, error) {
|
|
if r.idx >= len(r.parts) {
|
|
return 0, io.EOF
|
|
}
|
|
n := copy(p, r.parts[r.idx])
|
|
r.idx++
|
|
return n, nil
|
|
}
|