@@ -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
1821func 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
2629func 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
4043func 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
0 commit comments