@@ -93,3 +93,86 @@ func TestThread(t *testing.T) {
93
93
_ , err = client .DeleteThread (ctx , threadID )
94
94
checks .NoError (t , err , "DeleteThread error" )
95
95
}
96
+
97
+ // TestAzureThread Tests the thread endpoint of the API using the Azure mocked server.
98
+ func TestAzureThread (t * testing.T ) {
99
+ threadID := "thread_abc123"
100
+ client , server , teardown := setupAzureTestServer ()
101
+ defer teardown ()
102
+
103
+ server .RegisterHandler (
104
+ "/openai/threads/" + threadID ,
105
+ func (w http.ResponseWriter , r * http.Request ) {
106
+ switch r .Method {
107
+ case http .MethodGet :
108
+ resBytes , _ := json .Marshal (openai.Thread {
109
+ ID : threadID ,
110
+ Object : "thread" ,
111
+ CreatedAt : 1234567890 ,
112
+ })
113
+ fmt .Fprintln (w , string (resBytes ))
114
+ case http .MethodPost :
115
+ var request openai.ThreadRequest
116
+ err := json .NewDecoder (r .Body ).Decode (& request )
117
+ checks .NoError (t , err , "Decode error" )
118
+
119
+ resBytes , _ := json .Marshal (openai.Thread {
120
+ ID : threadID ,
121
+ Object : "thread" ,
122
+ CreatedAt : 1234567890 ,
123
+ })
124
+ fmt .Fprintln (w , string (resBytes ))
125
+ case http .MethodDelete :
126
+ fmt .Fprintln (w , `{
127
+ "id": "thread_abc123",
128
+ "object": "thread.deleted",
129
+ "deleted": true
130
+ }` )
131
+ }
132
+ },
133
+ )
134
+
135
+ server .RegisterHandler (
136
+ "/openai/threads" ,
137
+ func (w http.ResponseWriter , r * http.Request ) {
138
+ if r .Method == http .MethodPost {
139
+ var request openai.ModifyThreadRequest
140
+ err := json .NewDecoder (r .Body ).Decode (& request )
141
+ checks .NoError (t , err , "Decode error" )
142
+
143
+ resBytes , _ := json .Marshal (openai.Thread {
144
+ ID : threadID ,
145
+ Object : "thread" ,
146
+ CreatedAt : 1234567890 ,
147
+ Metadata : request .Metadata ,
148
+ })
149
+ fmt .Fprintln (w , string (resBytes ))
150
+ }
151
+ },
152
+ )
153
+
154
+ ctx := context .Background ()
155
+
156
+ _ , err := client .CreateThread (ctx , openai.ThreadRequest {
157
+ Messages : []openai.ThreadMessage {
158
+ {
159
+ Role : openai .ThreadMessageRoleUser ,
160
+ Content : "Hello, World!" ,
161
+ },
162
+ },
163
+ })
164
+ checks .NoError (t , err , "CreateThread error" )
165
+
166
+ _ , err = client .RetrieveThread (ctx , threadID )
167
+ checks .NoError (t , err , "RetrieveThread error" )
168
+
169
+ _ , err = client .ModifyThread (ctx , threadID , openai.ModifyThreadRequest {
170
+ Metadata : map [string ]interface {}{
171
+ "key" : "value" ,
172
+ },
173
+ })
174
+ checks .NoError (t , err , "ModifyThread error" )
175
+
176
+ _ , err = client .DeleteThread (ctx , threadID )
177
+ checks .NoError (t , err , "DeleteThread error" )
178
+ }
0 commit comments