-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfailure_test.go
More file actions
57 lines (51 loc) · 1.87 KB
/
Copy pathfailure_test.go
File metadata and controls
57 lines (51 loc) · 1.87 KB
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
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
dalle "github.com/TrueBlocks/trueblocks-dalle/v6"
)
// TestSimulatedOpenAIFailure injects a failing generateAnnotatedImage to ensure the handler
// logs the error path without panicking and still responds 200 with standard message.
func TestSimulatedOpenAIFailure(t *testing.T) {
dalle.SetupTest(t, dalle.SetupTestOptions{Series: []string{"empty"}})
app := NewApp()
// Prepare injection
called := 0
original := generateAnnotatedImage
generateAnnotatedImage = func(series, addr string, skip bool, ttl time.Duration) (string, error) {
called++
return "", fmt.Errorf("forced failure for testing")
}
defer func() { generateAnnotatedImage = original }()
// Force synchronous path
prevDebug := isDebugging
isDebugging = true
defer func() { isDebugging = prevDebug }()
// Use a unique address unlikely to have prior progress to better isolate this test
addr := "0x5555555555555555555555555555555555555555"
url := "/dalle/empty/" + addr + "?generate=1"
r := httptest.NewRequest(http.MethodGet, url, nil)
w := httptest.NewRecorder()
app.handleDalleDress(w, r)
resp := w.Result()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
bodyBytes, _ := io.ReadAll(resp.Body)
body := string(bodyBytes)
trimmed := strings.TrimSpace(body)
// Handler now always returns JSON progress (or {} if no progress yet) instead of a static message.
if !strings.HasPrefix(trimmed, "{") || !strings.HasSuffix(trimmed, "}\n") && !strings.HasSuffix(trimmed, "}") {
// Basic sanity check that we received JSON; we don't assert specific fields because
// the injected failure prevents progress initialization.
t.Fatalf("expected JSON progress body, got %q", body)
}
if called != 1 {
t.Fatalf("expected injected generator to be called once, got %d", called)
}
}