File size: 5,205 Bytes
6bc074c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package headerbuilder

import (
	"testing"

	"aurora/internal/accounts"
)

func TestNew(t *testing.T) {
	b := New()
	if b == nil {
		t.Fatal("expected non-nil builder")
	}
}

func TestBuild(t *testing.T) {
	h := New().Build()
	if h == nil {
		t.Fatal("expected non-nil header")
	}
}

func TestWithContentType(t *testing.T) {
	h := New().WithContentType("application/json").Build()
	if h["Content-Type"] != "application/json" {
		t.Fatalf("expected application/json, got %s", h["Content-Type"])
	}
}

func TestWithAccept(t *testing.T) {
	h := New().WithAccept("text/event-stream").Build()
	if h["Accept"] != "text/event-stream" {
		t.Fatalf("expected text/event-stream, got %s", h["Accept"])
	}
}

func TestWithTargetPath(t *testing.T) {
	h := New().WithTargetPath("/backend-api/test").Build()
	if h["X-Openai-Target-Path"] != "/backend-api/test" {
		t.Fatalf("expected /backend-api/test, got %s", h["X-Openai-Target-Path"])
	}
	if h["X-Openai-Target-Route"] != "/backend-api/test" {
		t.Fatalf("expected /backend-api/test, got %s", h["X-Openai-Target-Route"])
	}
}

func TestWithAuth_PaidUser(t *testing.T) {
	acct := &accounts.Account{Type: accounts.TypeFree, Token: "test-token"}
	h := New().WithAuth(acct).Build()
	if h["Authorization"] != "Bearer test-token" {
		t.Fatalf("expected Bearer test-token, got %s", h["Authorization"])
	}
}

func TestWithAuth_FreeUser(t *testing.T) {
	acct := &accounts.Account{Type: accounts.TypeNoAuth, Token: "free-uuid"}
	h := New().WithAuth(acct).Build()
	if h["Oai-Device-Id"] != "free-uuid" {
		t.Fatalf("expected free-uuid, got %s", h["Oai-Device-Id"])
	}
}

func TestWithAuth_NilAccount(t *testing.T) {
	h := New().WithAuth(nil).Build()
	if h["Authorization"] != "" {
		t.Fatalf("expected empty, got %s", h["Authorization"])
	}
}

func TestWithTeamAccount(t *testing.T) {
	acct := &accounts.Account{Type: accounts.TypeFree, Token: "token", TeamUserID: "team-123"}
	h := New().WithTeamAccount(acct).Build()
	if h["Chatgpt-Account-Id"] != "team-123" {
		t.Fatalf("expected team-123, got %s", h["Chatgpt-Account-Id"])
	}
}

func TestWithTeamAccount_Empty(t *testing.T) {
	acct := &accounts.Account{Type: accounts.TypeFree, Token: "token"}
	h := New().WithTeamAccount(acct).Build()
	if h["Chatgpt-Account-Id"] != "" {
		t.Fatalf("expected empty, got %s", h["Chatgpt-Account-Id"])
	}
}

func TestWithConduitToken(t *testing.T) {
	h := New().WithConduitToken("conduit-123").Build()
	if h["X-Conduit-Token"] != "conduit-123" {
		t.Fatalf("expected conduit-123, got %s", h["X-Conduit-Token"])
	}
}

func TestWithConduitToken_Empty(t *testing.T) {
	h := New().WithConduitToken("").Build()
	if h["X-Conduit-Token"] != "" {
		t.Fatalf("expected empty, got %s", h["X-Conduit-Token"])
	}
}

func TestWithTurnTraceID(t *testing.T) {
	h := New().WithTurnTraceID("trace-123").Build()
	if h["X-Oai-Turn-Trace-Id"] != "trace-123" {
		t.Fatalf("expected trace-123, got %s", h["X-Oai-Turn-Trace-Id"])
	}
}

func TestWithSentinelTokens(t *testing.T) {
	sentinelTokens := SentinelTokens{
		TurnStileToken:  "turnstile-123",
		ProofOfWorkToken: "proof-123",
		TurnstileToken:  "turnstile-token-123",
	}
	h := New().WithSentinelTokens(sentinelTokens).Build()
	if h["Openai-Sentinel-Chat-Requirements-Token"] != "turnstile-123" {
		t.Fatalf("expected turnstile-123, got %s", h["Openai-Sentinel-Chat-Requirements-Token"])
	}
	if h["Openai-Sentinel-Proof-Token"] != "proof-123" {
		t.Fatalf("expected proof-123, got %s", h["Openai-Sentinel-Proof-Token"])
	}
	if h["Openai-Sentinel-Turnstile-Token"] != "turnstile-token-123" {
		t.Fatalf("expected turnstile-token-123, got %s", h["Openai-Sentinel-Turnstile-Token"])
	}
}

func TestWithConversationHeaders_MainRequest(t *testing.T) {
	h := New().WithConversationHeaders("/f/conversation").Build()
	if h["Oai-Echo-Logs"] == "" {
		t.Fatal("expected Oai-Echo-Logs for main conversation request")
	}
}

func TestWithConversationHeaders_PrepareRequest(t *testing.T) {
	h := New().WithConversationHeaders("/f/conversation/prepare").Build()
	if h["Oai-Echo-Logs"] != "" {
		t.Fatal("expected no Oai-Echo-Logs for prepare request")
	}
}

func TestNewBaseHeader(t *testing.T) {
	h := NewBaseHeader()
	if h["Origin"] != "https://chatgpt.com" {
		t.Fatalf("expected https://chatgpt.com, got %s", h["Origin"])
	}
	if h["User-Agent"] == "" {
		t.Fatal("expected non-empty User-Agent")
	}
}

func TestBuilderChaining(t *testing.T) {
	acct := &accounts.Account{Type: accounts.TypeFree, Token: "test-token", TeamUserID: "team-456"}
	h := New().
		WithBaseHeaders("conv-123").
		WithContentType("application/json").
		WithAccept("*/*").
		WithTargetPath("/backend-api/test").
		WithAuth(acct).
		WithTeamAccount(acct).
		WithConduitToken("conduit-123").
		WithTurnTraceID("trace-123").
		Build()

	if h["Content-Type"] != "application/json" {
		t.Fatalf("expected application/json, got %s", h["Content-Type"])
	}
	if h["Authorization"] != "Bearer test-token" {
		t.Fatalf("expected Bearer test-token, got %s", h["Authorization"])
	}
	if h["X-Conduit-Token"] != "conduit-123" {
		t.Fatalf("expected conduit-123, got %s", h["X-Conduit-Token"])
	}
	if h["Chatgpt-Account-Id"] != "team-456" {
		t.Fatalf("expected team-456, got %s", h["Chatgpt-Account-Id"])
	}
}