Skip to content

Commit 7a83940

Browse files
authored
Merge pull request #122 from julwrites/staging
Fallback logic
2 parents 4b99880 + c482fd3 commit 7a83940

4 files changed

Lines changed: 53 additions & 31 deletions

File tree

pkg/app/api_client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,13 @@ func getAPIConfig(projectID string) (string, string) {
5656

5757
// If env vars are missing, try to fetch from Secret Manager
5858
if url == "" || key == "" {
59-
// Try to fetch project ID first from env
59+
// Try to fetch project ID if not provided.
6060
if projectID == "" {
61-
projectID := os.Getenv("GCLOUD_PROJECT_ID")
61+
projectID = os.Getenv("GCLOUD_PROJECT_ID")
6262

63-
// Then from secrets
6463
if projectID == "" {
6564
var err error
66-
projectID, err = getSecretFunc(projectID, "GCLOUD_PROJECT_ID")
65+
projectID, err = getSecretFunc("", "GCLOUD_PROJECT_ID")
6766
if err != nil {
6867
log.Printf("Failed to fetch GCLOUD_PROJECT_ID from Secret Manager: %v", err)
6968
}

pkg/app/passage.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import (
1515
"github.com/julwrites/ScriptureBot/pkg/utils"
1616
)
1717

18+
// GetPassageHTMLFunc is a variable to allow mocking in tests.
1819
// Deprecated: Using new API service
19-
func GetPassageHtml(ref, ver string) *html.Node {
20+
var GetPassageHTMLFunc = func(ref, ver string) *html.Node {
2021
ref = url.QueryEscape(ref)
2122
ver = url.QueryEscape(ver)
2223
query := fmt.Sprintf("https://classic.biblegateway.com/passage/?search=%s&version=%s&interface=print", ref, ver)
@@ -205,13 +206,14 @@ func GetBiblePassage(env def.SessionData) def.SessionData {
205206
var resp VerseResponse
206207
err := SubmitQuery(req, &resp, env.Secrets.PROJECT_ID)
207208
if err != nil {
208-
log.Printf("Error retrieving passage: %v", err)
209-
// Fallback or error message?
210-
// For now, let's just log it and potentially return a friendly error message to the user if critical
211-
// But sticking to existing behavior, maybe just return env unmodified or empty message?
212-
// The original code returned env if doc == nil.
213-
// Let's inform the user.
214-
env.Res.Message = "Sorry, I couldn't retrieve that passage. Please check the reference or try again later."
209+
log.Printf("Error retrieving passage from API: %v. Falling back to deprecated method.", err)
210+
// Fallback to deprecated passage retrieval logic
211+
doc := GetPassageHTMLFunc(env.Msg.Message, config.Version)
212+
if doc == nil {
213+
env.Res.Message = "Sorry, I couldn't retrieve that passage. Please check the reference or try again later."
214+
return env
215+
}
216+
env.Res.Message = GetPassage(GetReference(doc), doc, config.Version)
215217
return env
216218
}
217219

@@ -240,7 +242,7 @@ func CheckBibleReference(ref string) bool {
240242
// The prompt says "Please do not remove the original code, but mark it as 'to be deprecated'".
241243
// So I will leave the logic as is for the deprecated parts.
242244

243-
doc := GetPassageHtml(ref, "NIV")
245+
doc := GetPassageHTMLFunc(ref, "NIV")
244246
ref = GetReference(doc)
245247
return len(ref) > 0
246248
}

pkg/app/passage_test.go

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"encoding/json"
55
"net/http"
66
"net/http/httptest"
7+
"strings"
78
"testing"
89

10+
"golang.org/x/net/html"
11+
912
"github.com/julwrites/BotPlatform/pkg/def"
1013
"github.com/julwrites/ScriptureBot/pkg/utils"
1114
)
@@ -16,15 +19,15 @@ func setEnv(key, value string) func() {
1619
}
1720

1821
func TestGetBiblePassageHtml(t *testing.T) {
19-
doc := GetPassageHtml("gen 8", "NIV")
22+
doc := GetPassageHTMLFunc("gen 8", "NIV")
2023

2124
if doc == nil {
2225
t.Errorf("Could not retrieve bible passage")
2326
}
2427
}
2528

2629
func TestGetReference(t *testing.T) {
27-
doc := GetPassageHtml("gen 1", "NIV")
30+
doc := GetPassageHTMLFunc("gen 1", "NIV")
2831

2932
if doc == nil {
3033
t.Fatalf("Could not retrieve Bible passage for testing")
@@ -38,7 +41,7 @@ func TestGetReference(t *testing.T) {
3841
}
3942

4043
func TestGetPassage(t *testing.T) {
41-
doc := GetPassageHtml("john 8", "NIV")
44+
doc := GetPassageHTMLFunc("john 8", "NIV")
4245

4346
passage := GetPassage("John 8", doc, "NIV")
4447

@@ -56,20 +59,22 @@ func TestGetBiblePassage(t *testing.T) {
5659
return
5760
}
5861

59-
if len(req.Query.Verses) > 0 && req.Query.Verses[0] == "error" {
60-
w.WriteHeader(http.StatusInternalServerError)
61-
return
62+
verse := ""
63+
if len(req.Query.Verses) > 0 {
64+
verse = req.Query.Verses[0]
6265
}
6366

64-
if len(req.Query.Verses) > 0 && req.Query.Verses[0] == "empty" {
67+
switch verse {
68+
case "gen 1":
69+
resp := VerseResponse{
70+
Verse: "<p>In the beginning God created the heavens and the earth.</p>",
71+
}
72+
json.NewEncoder(w).Encode(resp)
73+
case "empty":
6574
json.NewEncoder(w).Encode(VerseResponse{})
66-
return
67-
}
68-
69-
resp := VerseResponse{
70-
Verse: "<p>In the beginning God created the heavens and the earth.</p>",
75+
default: // Any other case will trigger an error, forcing fallback
76+
w.WriteHeader(http.StatusInternalServerError)
7177
}
72-
json.NewEncoder(w).Encode(resp)
7378
}))
7479
defer ts.Close()
7580

@@ -89,13 +94,29 @@ func TestGetBiblePassage(t *testing.T) {
8994
}
9095
})
9196

92-
t.Run("Error", func(t *testing.T) {
97+
t.Run("Fallback on API error", func(t *testing.T) {
9398
var env def.SessionData
94-
env.Msg.Message = "error"
99+
env.Msg.Message = "John 1:1" // Use a valid reference to test the fallback
100+
var conf utils.UserConfig
101+
conf.Version = "NIV"
102+
env.User.Config = utils.SerializeUserConfig(conf)
103+
104+
// Mock the GetPassageHTMLFunc to avoid network calls.
105+
originalGetPassageHTML := GetPassageHTMLFunc
106+
defer func() { GetPassageHTMLFunc = originalGetPassageHTML }()
107+
GetPassageHTMLFunc = func(ref, ver string) *html.Node {
108+
// Return a mock HTML node.
109+
// The structure should be minimal but sufficient for GetPassage to parse.
110+
mockHTML := `<html><body><div class="passage-text"><div class="bcv">John 1:1</div><p>Mocked passage text.</p></div></body></html>`
111+
doc, _ := html.Parse(strings.NewReader(mockHTML))
112+
return doc
113+
}
114+
95115
env = GetBiblePassage(env)
96116

97-
if env.Res.Message != "Sorry, I couldn't retrieve that passage. Please check the reference or try again later." {
98-
t.Errorf("Expected error message, got '%s'", env.Res.Message)
117+
// The fallback should now use the mocked function.
118+
if !strings.Contains(env.Res.Message, "Mocked passage text\\.") {
119+
t.Errorf("Expected fallback message to contain 'Mocked passage text.', got: '%s'", env.Res.Message)
99120
}
100121
})
101122

pkg/app/tms.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func FormatQuery(query string, t TMSQueryType) string {
146146
query = strings.ReplaceAll(query, " \t\n", "")
147147
break
148148
case Reference:
149-
doc := GetPassageHtml(query, "NIV")
149+
doc := GetPassageHTMLFunc(query, "NIV")
150150
query = GetReference(doc)
151151
break
152152
}

0 commit comments

Comments
 (0)