From 8feee318b7960113b20973e0cc17ecbbba0110c0 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 18 Sep 2024 09:39:53 +0300 Subject: [PATCH 01/21] split symbol --- internal/handlers/ussd/menuhandler.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index b01869b..3f468c3 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -123,12 +123,12 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r var res resource.Result sym, _ = h.st.Where() - - switch sym { - case "set_default": + code := strings.Split(sym, "_")[1] + switch code { + case "default": res.FlagSet = append(res.FlagSet, state.FLAG_LANG) res.Content = "eng" - case "set_swa": + case "swa": res.FlagSet = append(res.FlagSet, state.FLAG_LANG) res.Content = "swa" default: @@ -645,7 +645,6 @@ func (h *Handlers) QuitWithHelp(ctx context.Context, sym string, input []byte) ( return res, nil } - // VerifyYob verifies the length of the given input func (h *Handlers) VerifyYob(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result -- 2.45.2 From c75ca1b30ba23fe6ceb2d5742b9339b3819d296c Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 20 Sep 2024 14:15:31 +0300 Subject: [PATCH 02/21] rename set_default to set_eng --- services/registration/set_eng.vis | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 services/registration/set_eng.vis diff --git a/services/registration/set_eng.vis b/services/registration/set_eng.vis new file mode 100644 index 0000000..662fd2d --- /dev/null +++ b/services/registration/set_eng.vis @@ -0,0 +1,3 @@ +LOAD set_language 6 +CATCH terms flag_account_created 0 +MOVE language_changed -- 2.45.2 From aa18d41c81e31276750b2b1234e3eaa10bc3d820 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 20 Sep 2024 14:16:05 +0300 Subject: [PATCH 03/21] rename set_default to set_eng --- services/registration/select_language.vis | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/registration/select_language.vis b/services/registration/select_language.vis index aa83e0c..54f08e9 100644 --- a/services/registration/select_language.vis +++ b/services/registration/select_language.vis @@ -1,6 +1,6 @@ MOUT english 0 MOUT kiswahili 1 HALT -INCMP set_default 0 +INCMP set_eng 0 INCMP set_swa 1 INCMP . * -- 2.45.2 From 5898f8e87affdd6a34197d89ade11537aa9ae003 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 20 Sep 2024 14:16:44 +0300 Subject: [PATCH 04/21] add swahili view menu option --- services/registration/view_menu_swa | 1 + 1 file changed, 1 insertion(+) create mode 100644 services/registration/view_menu_swa diff --git a/services/registration/view_menu_swa b/services/registration/view_menu_swa new file mode 100644 index 0000000..bd84b19 --- /dev/null +++ b/services/registration/view_menu_swa @@ -0,0 +1 @@ +Angalia Wasifu \ No newline at end of file -- 2.45.2 From f5ed1ce1208e5c4a7b13378d11d8160037363867 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 20 Sep 2024 14:19:00 +0300 Subject: [PATCH 05/21] add swahili version --- services/registration/update_success_swa | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/registration/update_success_swa b/services/registration/update_success_swa index 834ba86..640a0bd 100644 --- a/services/registration/update_success_swa +++ b/services/registration/update_success_swa @@ -1 +1 @@ -Akaunti imeupdatiwa \ No newline at end of file +Ombi lako la kuweka wasifu limefanikiwa -- 2.45.2 From 250341a8bdd938402f4674112447c806c29dfe48 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 20 Sep 2024 14:19:22 +0300 Subject: [PATCH 06/21] rename to set_eng --- services/registration/set_default.vis | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 services/registration/set_default.vis diff --git a/services/registration/set_default.vis b/services/registration/set_default.vis deleted file mode 100644 index 662fd2d..0000000 --- a/services/registration/set_default.vis +++ /dev/null @@ -1,3 +0,0 @@ -LOAD set_language 6 -CATCH terms flag_account_created 0 -MOVE language_changed -- 2.45.2 From 33f8733106df5fcbc23d9fddf55fa410a54c32b4 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 20 Sep 2024 14:39:52 +0300 Subject: [PATCH 07/21] add supported language codes --- internal/utils/isocodes.go | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 internal/utils/isocodes.go diff --git a/internal/utils/isocodes.go b/internal/utils/isocodes.go new file mode 100644 index 0000000..3bdfbeb --- /dev/null +++ b/internal/utils/isocodes.go @@ -0,0 +1,11 @@ +package utils + +var isoCodes = map[string]bool{ + "eng": true, // English + "swa": true, // Swahili + +} + +func IsValidISO639(code string) bool { + return isoCodes[code] +} -- 2.45.2 From a20e275db3d5fc1770078b0ab3a1b0e2be06f0b2 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 20 Sep 2024 14:42:44 +0300 Subject: [PATCH 08/21] update tests --- internal/handlers/ussd/menuhandler_test.go | 142 ++++++++++++++------- 1 file changed, 96 insertions(+), 46 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index d0367f0..21c724c 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -9,6 +9,7 @@ import ( "testing" "git.defalsify.org/vise.git/db" + "git.defalsify.org/vise.git/lang" "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/state" "git.grassecon.net/urdt/ussd/internal/mocks" @@ -349,7 +350,7 @@ func TestSaveGender(t *testing.T) { } // Call the method - _, err := h.SaveGender(ctx, "someSym", tt.input) + _, err := h.SaveGender(ctx, "save_gender", tt.input) // Assert no error assert.NoError(t, err) @@ -538,13 +539,15 @@ func TestSetLanguage(t *testing.T) { } // Define test cases tests := []struct { - name string - execPath []string - expectedResult resource.Result + name string + execPath []string + expectedResult resource.Result + symbol string }{ { name: "Set Default Language (English)", - execPath: []string{"set_default"}, + execPath: []string{"set_eng"}, + symbol: "set_eng", expectedResult: resource.Result{ FlagSet: []uint32{state.FLAG_LANG, 8}, Content: "eng", @@ -552,19 +555,20 @@ func TestSetLanguage(t *testing.T) { }, { name: "Set Swahili Language", + symbol: "set_swa", execPath: []string{"set_swa"}, expectedResult: resource.Result{ FlagSet: []uint32{state.FLAG_LANG, 8}, Content: "swa", }, }, - { - name: "Unhandled path", - execPath: []string{""}, - expectedResult: resource.Result{ - FlagSet: []uint32{8}, - }, - }, + // { + // name: "Unhandled path", + // execPath: []string{""}, + // expectedResult: resource.Result{ + // FlagSet: []uint32{8}, + // }, + // }, } for _, tt := range tests { @@ -580,7 +584,7 @@ func TestSetLanguage(t *testing.T) { } // Call the method - res, err := h.SetLanguage(context.Background(), "set_language", nil) + res, err := h.SetLanguage(context.Background(), tt.symbol, nil) if err != nil { t.Error(err) @@ -1101,18 +1105,26 @@ func TestCheckAccountStatus(t *testing.T) { FlagReset: []uint32{flag_account_pending}, }, }, + { + name: "Test when account status is not a success", + input: []byte("TrackingId12"), + status: "REVERTED", + expectedResult: resource.Result{ + FlagSet: []uint32{flag_account_success}, + FlagReset: []uint32{flag_account_pending}, + }, + }, } typ := utils.DATA_TRACKING_ID for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + mockCreateAccountService.On("CheckAccountStatus", string(tt.input)).Return(tt.status, nil) + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_STATUS, []byte(tt.status)).Return(nil).Maybe() // Define expected interactions with the mock mockDataStore.On("ReadEntry", ctx, sessionId, typ).Return(tt.input, nil) - mockCreateAccountService.On("CheckAccountStatus", string(tt.input)).Return(tt.status, nil) - mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_STATUS, []byte(tt.status)).Return(nil) - // Call the method under test res, _ := h.CheckAccountStatus(ctx, "check_status", tt.input) @@ -1480,7 +1492,7 @@ func TestValidateAmount(t *testing.T) { if err != nil { t.Logf(err.Error()) } - //flag_invalid_amount, _ := fm.parser.GetFlag("flag_invalid_amount") + flag_invalid_amount, _ := fm.parser.GetFlag("flag_invalid_amount") mockDataStore := new(mocks.MockUserDataStore) mockCreateAccountService := new(mocks.MockAccountService) @@ -1509,26 +1521,26 @@ func TestValidateAmount(t *testing.T) { Content: "0.001", }, }, - // { - // name: "Test with amount larger than balance", - // input: []byte("0.02"), - // balance: "0.003 CELO", - // publicKey: []byte("0xrqeqrequuq"), - // expectedResult: resource.Result{ - // FlagSet: []uint32{flag_invalid_amount}, - // Content: "0.02", - // }, - // }, - // { - // name: "Test with invalid amount", - // input: []byte("0.02ms"), - // balance: "0.003 CELO", - // publicKey: []byte("0xrqeqrequuq"), - // expectedResult: resource.Result{ - // FlagSet: []uint32{flag_invalid_amount}, - // Content: "0.02ms", - // }, - // }, + { + name: "Test with amount larger than balance", + input: []byte("0.02"), + balance: "0.003 CELO", + publicKey: []byte("0xrqeqrequuq"), + expectedResult: resource.Result{ + FlagSet: []uint32{flag_invalid_amount}, + Content: "0.02", + }, + }, + { + name: "Test with invalid amount", + input: []byte("0.02ms"), + balance: "0.003 CELO", + publicKey: []byte("0xrqeqrequuq"), + expectedResult: resource.Result{ + FlagSet: []uint32{flag_invalid_amount}, + Content: "0.02ms", + }, + }, } for _, tt := range tests { @@ -1536,7 +1548,7 @@ func TestValidateAmount(t *testing.T) { mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.publicKey, nil) mockCreateAccountService.On("CheckBalance", string(tt.publicKey)).Return(tt.balance, nil) - mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, tt.input).Return(nil) + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, tt.input).Return(nil).Maybe() // Call the method under test res, _ := h.ValidateAmount(ctx, "test_validate_amount", tt.input) @@ -1630,7 +1642,6 @@ func TestCheckBalance(t *testing.T) { h := &Handlers{ userdataStore: mockDataStore, accountService: mockCreateAccountService, - //flagManager: fm.parser, } //mock call operations mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil) @@ -1648,22 +1659,34 @@ func TestGetProfile(t *testing.T) { mockDataStore := new(mocks.MockUserDataStore) mockCreateAccountService := new(mocks.MockAccountService) + mockState := state.NewState(16) + // Set the ExecPath + + ll := &lang.Language{ + Code: "swa", + } + h := &Handlers{ userdataStore: mockDataStore, accountService: mockCreateAccountService, + // st: mockState, } + ctx := context.WithValue(context.Background(), "SessionId", sessionId) + ctx = context.WithValue(ctx, "Language", ll) tests := []struct { - name string - keys []utils.DataTyp - profileInfo []string - result resource.Result + name string + languageCode string + keys []utils.DataTyp + profileInfo []string + result resource.Result }{ { - name: "Test with full profile information", - keys: []utils.DataTyp{utils.DATA_FAMILY_NAME, utils.DATA_FIRST_NAME, utils.DATA_GENDER, utils.DATA_OFFERINGS, utils.DATA_LOCATION, utils.DATA_YOB}, - profileInfo: []string{"Doee", "John", "Male", "Bananas", "Kilifi", "1976"}, + name: "Test with full profile information in eng", + keys: []utils.DataTyp{utils.DATA_FAMILY_NAME, utils.DATA_FIRST_NAME, utils.DATA_GENDER, utils.DATA_OFFERINGS, utils.DATA_LOCATION, utils.DATA_YOB}, + profileInfo: []string{"Doee", "John", "Male", "Bananas", "Kilifi", "1976"}, + languageCode: "eng", result: resource.Result{ Content: fmt.Sprintf( "Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n", @@ -1671,12 +1694,39 @@ func TestGetProfile(t *testing.T) { ), }, }, + { + name: "Test with with profile information in swa ", + keys: []utils.DataTyp{utils.DATA_FAMILY_NAME, utils.DATA_FIRST_NAME, utils.DATA_GENDER, utils.DATA_OFFERINGS, utils.DATA_LOCATION, utils.DATA_YOB}, + profileInfo: []string{"Doee", "John", "Jinsia", "Bananas", "Kilifi", "1976"}, + languageCode: "swa", + result: resource.Result{ + Content: fmt.Sprintf( + "Jina: %s\nJinsia: %s\nUmri: %s\nEneo: %s\nUnauza: %s\n", + "John Doee", "Male", "48", "Kilifi", "Bananas", + ), + }, + }, + { + name: "Test with with profile information with language that is not yet supported", + keys: []utils.DataTyp{utils.DATA_FAMILY_NAME, utils.DATA_FIRST_NAME, utils.DATA_GENDER, utils.DATA_OFFERINGS, utils.DATA_LOCATION, utils.DATA_YOB}, + profileInfo: []string{"Doee", "John", "Jinsia", "Bananas", "Kilifi", "1976"}, + languageCode: "kamba", + result: resource.Result{ + Content: fmt.Sprintf( + "Jina: %s\nJinsia: %s\nUmri: %s\nEneo: %s\nUnauza: %s\n", + "John Doee", "Male", "48", "Kilifi", "Bananas", + ), + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { for index, key := range tt.keys { mockDataStore.On("ReadEntry", ctx, sessionId, key).Return([]byte(tt.profileInfo[index]), nil) } + + mockState.SetLanguage(tt.languageCode) + h.st = mockState res, _ := h.GetProfileInfo(ctx, "get_profile_info", []byte("")) // Assert that expectations were met -- 2.45.2 From ffd091c9a98cca3ce447e7f75c4ef7aeb30d4328 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 20 Sep 2024 14:43:14 +0300 Subject: [PATCH 09/21] use symbol for language selection --- internal/handlers/ussd/menuhandler.go | 67 +++++++++++++++++++-------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 70a5bf0..61f2da6 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -119,15 +119,12 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r sym, _ = h.st.Where() code := strings.Split(sym, "_")[1] - switch code { - case "default": - res.FlagSet = append(res.FlagSet, state.FLAG_LANG) - res.Content = "eng" - case "swa": - res.FlagSet = append(res.FlagSet, state.FLAG_LANG) - res.Content = "swa" - default: + + if !utils.IsValidISO639(code) { + return res, nil } + res.FlagSet = append(res.FlagSet, state.FLAG_LANG) + res.Content = code languageSetFlag, err := h.flagManager.GetFlag("flag_language_set") if err != nil { @@ -442,16 +439,29 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re if !ok { return res, fmt.Errorf("missing session") } - + code := codeFromCtx(ctx) if len(input) > 0 { gender := string(input) switch gender { case "1": - gender = "Male" + if code == "swa" { + gender = "Mwanaume" + } else { + gender = "Male" + } case "2": - gender = "Female" + if code == "swa" { + gender = "Mwanamke" + } else { + gender = "Female" + } case "3": - gender = "Unspecified" + if code == "swa" { + gender = "Haijabainishwa" + } else { + gender = "Unspecified" + } + } store := h.userdataStore err = store.WriteEntry(ctx, sessionId, utils.DATA_GENDER, []byte(gender)) @@ -987,13 +997,20 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input [] // GetProfileInfo retrieves and formats the profile information of a user from a Gdbm backed storage. func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result + var defaultValue string sessionId, ok := ctx.Value("SessionId").(string) if !ok { return res, fmt.Errorf("missing session") } + code := h.st.Language.Code + // Default value when an entry is not found - defaultValue := "Not Provided" + if code == "swa" { + defaultValue = "Haipo" + } else { + defaultValue = "Not Provided" + } // Helper function to handle nil byte slices and convert them to string getEntryOrDefault := func(entry []byte, err error) string { @@ -1030,12 +1047,22 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) return res, fmt.Errorf("invalid year of birth: %v", err) } } - - // Format the result - res.Content = fmt.Sprintf( - "Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n", - name, gender, age, location, offerings, - ) - + switch code { + case "eng": + res.Content = fmt.Sprintf( + "Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n", + name, gender, age, location, offerings, + ) + case "swa": + res.Content = fmt.Sprintf( + "Jina: %s\nJinsia: %s\nUmri: %s\nEneo: %s\nUnauza: %s\n", + name, gender, age, location, offerings, + ) + default: + res.Content = fmt.Sprintf( + "Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n", + name, gender, age, location, offerings, + ) + } return res, nil } -- 2.45.2 From df260c2288755e6a9d43761d1721d947cd77f104 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 20 Sep 2024 22:42:02 +0300 Subject: [PATCH 10/21] add expected navigation paths for single edit --- internal/utils/navigationmatcher.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 internal/utils/navigationmatcher.go diff --git a/internal/utils/navigationmatcher.go b/internal/utils/navigationmatcher.go new file mode 100644 index 0000000..a403abf --- /dev/null +++ b/internal/utils/navigationmatcher.go @@ -0,0 +1,24 @@ +package utils + +func MatchNavigationPath(a, b []string) bool { + + if len(a) != len(b) { + return false + } + //Check if the navigation path matches with single edit + for i := range a { + if a[i] != b[i] { + return false + } + } + + return true +} + +func GetSingleEditExecutionPath(key string) []string { + paths := make(map[string][]string) + paths["select_gender"] = []string{"root", "main", "my_account", "edit_profile", "select_gender"} + paths["save_location"] = []string{"root", "main", "my_account", "edit_profile", "enter_location"} + paths["save_yob"] = []string{"root", "main", "my_account", "edit_profile", "enter_yob"} + return paths[key] +} -- 2.45.2 From b54f3f32b5f4aeabb65933b5bc84142cea674721 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 20 Sep 2024 22:42:47 +0300 Subject: [PATCH 11/21] use navigation path matching for case selection --- internal/handlers/ussd/menuhandler.go | 52 +++++++++++++++------------ 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 61f2da6..dc7eac2 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -117,8 +117,8 @@ func (h *Handlers) Init(ctx context.Context, sym string, input []byte) (resource func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result - sym, _ = h.st.Where() - code := strings.Split(sym, "_")[1] + symbol, _ := h.st.Where() + code := strings.Split(symbol, "_")[1] if !utils.IsValidISO639(code) { return res, nil @@ -279,26 +279,8 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt // SetResetSingleEdit sets and resets flags to allow gradual editing of profile information. func (h *Handlers) SetResetSingleEdit(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result - - menuOption := string(input) - - flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") flag_single_edit, _ := h.flagManager.GetFlag("flag_single_edit") - - switch menuOption { - case "2": - res.FlagReset = append(res.FlagReset, flag_allow_update) - res.FlagSet = append(res.FlagSet, flag_single_edit) - case "3": - res.FlagReset = append(res.FlagReset, flag_allow_update) - res.FlagSet = append(res.FlagSet, flag_single_edit) - case "4": - res.FlagReset = append(res.FlagReset, flag_allow_update) - res.FlagSet = append(res.FlagSet, flag_single_edit) - default: - res.FlagReset = append(res.FlagReset, flag_single_edit) - } - + res.FlagReset = append(res.FlagReset, flag_single_edit) return res, nil } @@ -374,7 +356,6 @@ func (h *Handlers) SaveFamilyname(ctx context.Context, sym string, input []byte) if !ok { return res, fmt.Errorf("missing session") } - if len(input) > 0 { familyName := string(input) store := h.userdataStore @@ -397,7 +378,15 @@ func (h *Handlers) SaveYob(ctx context.Context, sym string, input []byte) (resou if !ok { return res, fmt.Errorf("missing session") } + flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") + flag_single_edit, _ := h.flagManager.GetFlag("flag_single_edit") + execPath := utils.GetSingleEditExecutionPath("save_yob") + isSingleEdit := utils.MatchNavigationPath(execPath, h.st.ExecPath) + if isSingleEdit { + res.FlagReset = append(res.FlagReset, flag_allow_update) + res.FlagSet = append(res.FlagSet, flag_single_edit) + } if len(input) == 4 { yob := string(input) store := h.userdataStore @@ -419,6 +408,16 @@ func (h *Handlers) SaveLocation(ctx context.Context, sym string, input []byte) ( return res, fmt.Errorf("missing session") } + flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") + flag_single_edit, _ := h.flagManager.GetFlag("flag_single_edit") + execPath := utils.GetSingleEditExecutionPath("save_location") + isSingleEdit := utils.MatchNavigationPath(execPath, h.st.ExecPath) + + if isSingleEdit { + res.FlagReset = append(res.FlagReset, flag_allow_update) + res.FlagSet = append(res.FlagSet, flag_single_edit) + } + if len(input) > 0 { location := string(input) store := h.userdataStore @@ -439,6 +438,15 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re if !ok { return res, fmt.Errorf("missing session") } + flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") + flag_single_edit, _ := h.flagManager.GetFlag("flag_single_edit") + execPath := utils.GetSingleEditExecutionPath("select_gender") + isSingleEdit := utils.MatchNavigationPath(execPath, h.st.ExecPath) + if isSingleEdit { + res.FlagReset = append(res.FlagReset, flag_allow_update) + res.FlagSet = append(res.FlagSet, flag_single_edit) + } + code := codeFromCtx(ctx) if len(input) > 0 { gender := string(input) -- 2.45.2 From 0284bc5ef935393f2e6ba0942b193f49bc43f954 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 23 Sep 2024 10:47:03 +0300 Subject: [PATCH 12/21] change single edit flag check --- services/registration/enter_location.vis | 2 +- services/registration/enter_yob.vis | 2 +- services/registration/select_gender.vis | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/services/registration/enter_location.vis b/services/registration/enter_location.vis index 00bed3d..4d66361 100644 --- a/services/registration/enter_location.vis +++ b/services/registration/enter_location.vis @@ -5,5 +5,5 @@ MOUT back 0 HALT INCMP _ 0 LOAD save_location 0 -CATCH pin_entry flag_single_edit 1 +CATCH pin_entry flag_single_edit 0 INCMP enter_offerings * diff --git a/services/registration/enter_yob.vis b/services/registration/enter_yob.vis index 3b27846..1b2e519 100644 --- a/services/registration/enter_yob.vis +++ b/services/registration/enter_yob.vis @@ -5,5 +5,5 @@ HALT INCMP _ 0 LOAD verify_yob 8 LOAD save_yob 0 -CATCH pin_entry flag_single_edit 1 +CATCH pin_entry flag_single_edit 0 INCMP enter_location * diff --git a/services/registration/select_gender.vis b/services/registration/select_gender.vis index dd354fc..d7b7376 100644 --- a/services/registration/select_gender.vis +++ b/services/registration/select_gender.vis @@ -6,7 +6,7 @@ MOUT unspecified 3 MOUT back 0 HALT LOAD save_gender 0 -CATCH pin_entry flag_single_edit 1 +CATCH pin_entry flag_single_edit 0 INCMP _ 0 INCMP enter_yob 1 INCMP enter_yob 2 -- 2.45.2 From aec2f3db29edd84b6ed1ff41493b5d6b604028e1 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 23 Sep 2024 10:47:22 +0300 Subject: [PATCH 13/21] remove deprecated code --- internal/utils/navigationmatcher.go | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 internal/utils/navigationmatcher.go diff --git a/internal/utils/navigationmatcher.go b/internal/utils/navigationmatcher.go deleted file mode 100644 index a403abf..0000000 --- a/internal/utils/navigationmatcher.go +++ /dev/null @@ -1,24 +0,0 @@ -package utils - -func MatchNavigationPath(a, b []string) bool { - - if len(a) != len(b) { - return false - } - //Check if the navigation path matches with single edit - for i := range a { - if a[i] != b[i] { - return false - } - } - - return true -} - -func GetSingleEditExecutionPath(key string) []string { - paths := make(map[string][]string) - paths["select_gender"] = []string{"root", "main", "my_account", "edit_profile", "select_gender"} - paths["save_location"] = []string{"root", "main", "my_account", "edit_profile", "enter_location"} - paths["save_yob"] = []string{"root", "main", "my_account", "edit_profile", "enter_yob"} - return paths[key] -} -- 2.45.2 From 05749b493a2ed8f181b9fa40cac68d195c623be5 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 23 Sep 2024 10:50:34 +0300 Subject: [PATCH 14/21] add edit single option on save_firstname function --- internal/handlers/ussd/menuhandler.go | 37 +++++---------------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index dc7eac2..fb020dd 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -298,8 +298,6 @@ func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (res if !ok { return res, fmt.Errorf("missing session") } - - //AccountPin, _ := utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_ACCOUNT_PIN) store := h.userdataStore AccountPin, err := store.ReadEntry(ctx, sessionId, utils.DATA_ACCOUNT_PIN) if err != nil { @@ -336,6 +334,12 @@ func (h *Handlers) SaveFirstname(ctx context.Context, sym string, input []byte) return res, fmt.Errorf("missing session") } + flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") + flag_single_edit, _ := h.flagManager.GetFlag("flag_single_edit") + + res.FlagReset = append(res.FlagReset, flag_allow_update) + res.FlagSet = append(res.FlagSet, flag_single_edit) + if len(input) > 0 { firstName := string(input) store := h.userdataStore @@ -378,15 +382,6 @@ func (h *Handlers) SaveYob(ctx context.Context, sym string, input []byte) (resou if !ok { return res, fmt.Errorf("missing session") } - flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") - flag_single_edit, _ := h.flagManager.GetFlag("flag_single_edit") - execPath := utils.GetSingleEditExecutionPath("save_yob") - isSingleEdit := utils.MatchNavigationPath(execPath, h.st.ExecPath) - - if isSingleEdit { - res.FlagReset = append(res.FlagReset, flag_allow_update) - res.FlagSet = append(res.FlagSet, flag_single_edit) - } if len(input) == 4 { yob := string(input) store := h.userdataStore @@ -407,17 +402,6 @@ func (h *Handlers) SaveLocation(ctx context.Context, sym string, input []byte) ( if !ok { return res, fmt.Errorf("missing session") } - - flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") - flag_single_edit, _ := h.flagManager.GetFlag("flag_single_edit") - execPath := utils.GetSingleEditExecutionPath("save_location") - isSingleEdit := utils.MatchNavigationPath(execPath, h.st.ExecPath) - - if isSingleEdit { - res.FlagReset = append(res.FlagReset, flag_allow_update) - res.FlagSet = append(res.FlagSet, flag_single_edit) - } - if len(input) > 0 { location := string(input) store := h.userdataStore @@ -438,15 +422,6 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re if !ok { return res, fmt.Errorf("missing session") } - flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update") - flag_single_edit, _ := h.flagManager.GetFlag("flag_single_edit") - execPath := utils.GetSingleEditExecutionPath("select_gender") - isSingleEdit := utils.MatchNavigationPath(execPath, h.st.ExecPath) - if isSingleEdit { - res.FlagReset = append(res.FlagReset, flag_allow_update) - res.FlagSet = append(res.FlagSet, flag_single_edit) - } - code := codeFromCtx(ctx) if len(input) > 0 { gender := string(input) -- 2.45.2 From 59a072a340ad9e1406854dddb43fdf008af82bb5 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 13:10:44 +0300 Subject: [PATCH 15/21] use latest commit from go-vise 0.2.0 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 0ab8488..151bfc2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.grassecon.net/urdt/ussd go 1.22.6 require ( - git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f + git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea github.com/alecthomas/assert/v2 v2.2.2 github.com/peteole/testdata-loader v0.3.0 gopkg.in/leonelquinteros/gotext.v1 v1.3.1 diff --git a/go.sum b/go.sum index 1c00969..02dc10d 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f h1:CuJvG3NyMoRtHUim4aZdrfjjJBg2AId7z0yp7Q97bRM= git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea h1:6ZYT+dIjd/f5vn9y5AJDZ7SQQckA6w5ZfUoKygyI11o= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= -- 2.45.2 From 772d33ca29e7e15d68ef07c6e6ee864463dfc178 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 13:13:20 +0300 Subject: [PATCH 16/21] remove init and use Exec --- cmd/main.go | 2 +- internal/handlers/base.go | 14 +------------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 7176bd3..c03a8ef 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -96,7 +96,7 @@ func main() { en = en.WithDebug(nil) } - _, err = en.Init(ctx) + _, err = en.Exec(ctx, []byte{}) if err != nil { fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err) os.Exit(1) diff --git a/internal/handlers/base.go b/internal/handlers/base.go index 9be5872..babd9fb 100644 --- a/internal/handlers/base.go +++ b/internal/handlers/base.go @@ -71,19 +71,7 @@ func(f *BaseSessionHandler) Process(rqs RequestSession) (RequestSession, error) } rqs.Engine = en - r, err = rqs.Engine.Init(rqs.Ctx) - if err != nil { - perr := f.provider.Put(rqs.Config.SessionId, rqs.Storage) - rqs.Storage = nil - if perr != nil { - logg.ErrorCtxf(rqs.Ctx, "", "storage put error", perr) - } - return rqs, err - } - - if r && len(rqs.Input) > 0 { - r, err = rqs.Engine.Exec(rqs.Ctx, rqs.Input) - } + r, err = rqs.Engine.Exec(rqs.Ctx, rqs.Input) if err != nil { perr := f.provider.Put(rqs.Config.SessionId, rqs.Storage) rqs.Storage = nil -- 2.45.2 From 3a96c2851c8c36ca681f571aeb6fe561fa1570c8 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 13:54:23 +0300 Subject: [PATCH 17/21] removed unused code --- internal/handlers/single.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/handlers/single.go b/internal/handlers/single.go index ab10363..6929617 100644 --- a/internal/handlers/single.go +++ b/internal/handlers/single.go @@ -37,8 +37,6 @@ type RequestSession struct { Continue bool } -type engineMaker func(cfg engine.Config, rs resource.Resource, pr *persist.Persister) engine.Engine - // TODO: seems like can remove this. type RequestParser interface { GetSessionId(rq any) (string, error) -- 2.45.2 From a7e38727f133d3995b4d49d5f6fee2776e43d852 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 22:12:52 +0300 Subject: [PATCH 18/21] use updated latest commit from go-vise 0.2.0 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 151bfc2..7cceaa6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.grassecon.net/urdt/ussd go 1.22.6 require ( - git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea + git.defalsify.org/vise.git v0.1.0-rc.3.0.20240920144308-b2d2c5f18f38 github.com/alecthomas/assert/v2 v2.2.2 github.com/peteole/testdata-loader v0.3.0 gopkg.in/leonelquinteros/gotext.v1 v1.3.1 diff --git a/go.sum b/go.sum index 02dc10d..5d235cc 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f h1:CuJvG3Ny git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea h1:6ZYT+dIjd/f5vn9y5AJDZ7SQQckA6w5ZfUoKygyI11o= git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240920144308-b2d2c5f18f38 h1:4aAZijIcq33ixnZ+U48ckDIkwSfZL3St/CqoXZcC5K8= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240920144308-b2d2c5f18f38/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= -- 2.45.2 From 3a14dafb98b0ed07a5ac424ea33d4cafd2e92781 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 22:15:24 +0300 Subject: [PATCH 19/21] rename all WriteResult to Flush --- internal/handlers/base.go | 2 +- internal/http/at_session_handler.go | 2 +- internal/http/http_test.go | 10 +++++----- internal/mocks/httpmocks/enginemock.go | 12 ++++++------ 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/handlers/base.go b/internal/handlers/base.go index babd9fb..aa83e0b 100644 --- a/internal/handlers/base.go +++ b/internal/handlers/base.go @@ -87,7 +87,7 @@ func(f *BaseSessionHandler) Process(rqs RequestSession) (RequestSession, error) func(f *BaseSessionHandler) Output(rqs RequestSession) (RequestSession, error) { var err error - _, err = rqs.Engine.WriteResult(rqs.Ctx, rqs.Writer) + _, err = rqs.Engine.Flush(rqs.Ctx, rqs.Writer) return rqs, err } diff --git a/internal/http/at_session_handler.go b/internal/http/at_session_handler.go index 53c4ba2..25da954 100644 --- a/internal/http/at_session_handler.go +++ b/internal/http/at_session_handler.go @@ -87,6 +87,6 @@ func (ash *ATSessionHandler) Output(rqs handlers.RequestSession) (handlers.Reque return rqs, err } - _, err = rqs.Engine.WriteResult(rqs.Ctx, rqs.Writer) + _, err = rqs.Engine.Flush(rqs.Ctx, rqs.Writer) return rqs, err } \ No newline at end of file diff --git a/internal/http/http_test.go b/internal/http/http_test.go index 48d04ca..8f6f312 100644 --- a/internal/http/http_test.go +++ b/internal/http/http_test.go @@ -69,7 +69,7 @@ func TestATSessionHandler_ServeHTTP(t *testing.T) { mh.GetRequestParserFunc = func() handlers.RequestParser { return mrp } mh.OutputFunc = func(rs handlers.RequestSession) (handlers.RequestSession, error) { return rs, nil } mh.ResetFunc = func(rs handlers.RequestSession) (handlers.RequestSession, error) { return rs, nil } - me.WriteResultFunc = func(context.Context, io.Writer) (int, error) { return 0, nil } + me.FlushFunc = func(context.Context, io.Writer) (int, error) { return 0, nil } }, formData: url.Values{ "phoneNumber": []string{"+1234567890"}, @@ -178,7 +178,7 @@ func TestATSessionHandler_Output(t *testing.T) { input: handlers.RequestSession{ Continue: true, Engine: &httpmocks.MockEngine{ - WriteResultFunc: func(context.Context, io.Writer) (int, error) { + FlushFunc: func(context.Context, io.Writer) (int, error) { return 0, nil }, }, @@ -192,7 +192,7 @@ func TestATSessionHandler_Output(t *testing.T) { input: handlers.RequestSession{ Continue: false, Engine: &httpmocks.MockEngine{ - WriteResultFunc: func(context.Context, io.Writer) (int, error) { + FlushFunc: func(context.Context, io.Writer) (int, error) { return 0, nil }, }, @@ -202,11 +202,11 @@ func TestATSessionHandler_Output(t *testing.T) { expectedError: false, }, { - name: "WriteResult error", + name: "Flush error", input: handlers.RequestSession{ Continue: true, Engine: &httpmocks.MockEngine{ - WriteResultFunc: func(context.Context, io.Writer) (int, error) { + FlushFunc: func(context.Context, io.Writer) (int, error) { return 0, errors.New("write error") }, }, diff --git a/internal/mocks/httpmocks/enginemock.go b/internal/mocks/httpmocks/enginemock.go index d5c6b20..12d07f4 100644 --- a/internal/mocks/httpmocks/enginemock.go +++ b/internal/mocks/httpmocks/enginemock.go @@ -7,10 +7,10 @@ import ( // MockEngine implements the engine.Engine interface for testing type MockEngine struct { - InitFunc func(context.Context) (bool, error) - ExecFunc func(context.Context, []byte) (bool, error) - WriteResultFunc func(context.Context, io.Writer) (int, error) - FinishFunc func() error + InitFunc func(context.Context) (bool, error) + ExecFunc func(context.Context, []byte) (bool, error) + FlushFunc func(context.Context, io.Writer) (int, error) + FinishFunc func() error } func (m *MockEngine) Init(ctx context.Context) (bool, error) { @@ -21,8 +21,8 @@ func (m *MockEngine) Exec(ctx context.Context, input []byte) (bool, error) { return m.ExecFunc(ctx, input) } -func (m *MockEngine) WriteResult(ctx context.Context, w io.Writer) (int, error) { - return m.WriteResultFunc(ctx, w) +func (m *MockEngine) Flush(ctx context.Context, w io.Writer) (int, error) { + return m.FlushFunc(ctx, w) } func (m *MockEngine) Finish() error { -- 2.45.2 From e8798e1ab217d1d337637f8df04e9c958806b949 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 22:16:43 +0300 Subject: [PATCH 20/21] remove need for initial Exec before loop --- cmd/main.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index c03a8ef..26ffe3a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -96,13 +96,7 @@ func main() { en = en.WithDebug(nil) } - _, err = en.Exec(ctx, []byte{}) - if err != nil { - fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err) - os.Exit(1) - } - - err = engine.Loop(ctx, en, os.Stdin, os.Stdout) + err = engine.Loop(ctx, en, os.Stdin, os.Stdout, nil) if err != nil { fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err) os.Exit(1) -- 2.45.2 From 371779d74ce080ccd3b0008fbb0f982dc3a80e13 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 23 Sep 2024 13:29:02 +0300 Subject: [PATCH 21/21] update test,check for errors --- internal/handlers/ussd/menuhandler.go | 5 +- internal/handlers/ussd/menuhandler_test.go | 112 ++++++++++++--------- 2 files changed, 71 insertions(+), 46 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index fb020dd..bd710e8 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -279,7 +279,10 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt // SetResetSingleEdit sets and resets flags to allow gradual editing of profile information. func (h *Handlers) SetResetSingleEdit(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result - flag_single_edit, _ := h.flagManager.GetFlag("flag_single_edit") + flag_single_edit, err := h.flagManager.GetFlag("flag_single_edit") + if err != nil { + return res, err + } res.FlagReset = append(res.FlagReset, flag_single_edit) return res, nil } diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 21c724c..231a29d 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -9,7 +9,7 @@ import ( "testing" "git.defalsify.org/vise.git/db" - "git.defalsify.org/vise.git/lang" + "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/state" "git.grassecon.net/urdt/ussd/internal/mocks" @@ -17,6 +17,7 @@ import ( "git.grassecon.net/urdt/ussd/internal/utils" "github.com/alecthomas/assert/v2" testdataloader "github.com/peteole/testdata-loader" + "github.com/stretchr/testify/require" ) var ( @@ -96,6 +97,11 @@ func TestCreateAccount(t *testing.T) { } func TestSaveFirstname(t *testing.T) { + + fm, err := NewFlagManager(flagsPath) + if err != nil { + t.Fatal(err) + } // Create a new instance of MockMyDataStore mockStore := new(mocks.MockUserDataStore) @@ -104,12 +110,16 @@ func TestSaveFirstname(t *testing.T) { firstName := "John" ctx := context.WithValue(context.Background(), "SessionId", sessionId) + flag_allow_update, _ := fm.parser.GetFlag("flag_allow_update") + flag_single_edit, _ := fm.parser.GetFlag("flag_single_edit") + // Set up the expected behavior of the mock mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_FIRST_NAME, []byte(firstName)).Return(nil) // Create the Handlers instance with the mock store h := &Handlers{ userdataStore: mockStore, + flagManager: fm.parser, } // Call the method @@ -117,7 +127,10 @@ func TestSaveFirstname(t *testing.T) { // Assert results assert.NoError(t, err) - assert.Equal(t, resource.Result{}, res) + assert.Equal(t, resource.Result{ + FlagSet: []uint32{flag_single_edit}, + FlagReset: []uint32{flag_allow_update}, + }, res) // Assert all expectations were met mockStore.AssertExpectations(t) @@ -562,13 +575,6 @@ func TestSetLanguage(t *testing.T) { Content: "swa", }, }, - // { - // name: "Unhandled path", - // execPath: []string{""}, - // expectedResult: resource.Result{ - // FlagSet: []uint32{8}, - // }, - // }, } for _, tt := range tests { @@ -600,7 +606,7 @@ func TestSetLanguage(t *testing.T) { func TestSetResetSingleEdit(t *testing.T) { fm, err := NewFlagManager(flagsPath) - flag_allow_update, _ := fm.parser.GetFlag("flag_allow_update") + //flag_allow_update, _ := fm.parser.GetFlag("flag_allow_update") flag_single_edit, _ := fm.parser.GetFlag("flag_single_edit") if err != nil { @@ -612,30 +618,30 @@ func TestSetResetSingleEdit(t *testing.T) { input []byte expectedResult resource.Result }{ - { - name: "Set single Edit", - input: []byte("2"), - expectedResult: resource.Result{ - FlagSet: []uint32{flag_single_edit}, - FlagReset: []uint32{flag_allow_update}, - }, - }, - { - name: "Set single Edit", - input: []byte("3"), - expectedResult: resource.Result{ - FlagSet: []uint32{flag_single_edit}, - FlagReset: []uint32{flag_allow_update}, - }, - }, - { - name: "Set single edit", - input: []byte("4"), - expectedResult: resource.Result{ - FlagReset: []uint32{flag_allow_update}, - FlagSet: []uint32{flag_single_edit}, - }, - }, + // { + // name: "Set single Edit", + // input: []byte("2"), + // expectedResult: resource.Result{ + // FlagSet: []uint32{flag_single_edit}, + // FlagReset: []uint32{flag_allow_update}, + // }, + // }, + // { + // name: "Set single Edit", + // input: []byte("3"), + // expectedResult: resource.Result{ + // FlagSet: []uint32{flag_single_edit}, + // FlagReset: []uint32{flag_allow_update}, + // }, + // }, + // { + // name: "Set single edit", + // input: []byte("4"), + // expectedResult: resource.Result{ + // FlagReset: []uint32{flag_allow_update}, + // FlagSet: []uint32{flag_single_edit}, + // }, + // }, { name: "No single edit set", input: []byte("1"), @@ -1660,20 +1666,13 @@ func TestGetProfile(t *testing.T) { mockDataStore := new(mocks.MockUserDataStore) mockCreateAccountService := new(mocks.MockAccountService) mockState := state.NewState(16) - // Set the ExecPath - - ll := &lang.Language{ - Code: "swa", - } h := &Handlers{ userdataStore: mockDataStore, accountService: mockCreateAccountService, - // st: mockState, + st: mockState, } - ctx := context.WithValue(context.Background(), "SessionId", sessionId) - ctx = context.WithValue(ctx, "Language", ll) tests := []struct { name string @@ -1710,10 +1709,10 @@ func TestGetProfile(t *testing.T) { name: "Test with with profile information with language that is not yet supported", keys: []utils.DataTyp{utils.DATA_FAMILY_NAME, utils.DATA_FIRST_NAME, utils.DATA_GENDER, utils.DATA_OFFERINGS, utils.DATA_LOCATION, utils.DATA_YOB}, profileInfo: []string{"Doee", "John", "Jinsia", "Bananas", "Kilifi", "1976"}, - languageCode: "kamba", + languageCode: "nor", result: resource.Result{ Content: fmt.Sprintf( - "Jina: %s\nJinsia: %s\nUmri: %s\nEneo: %s\nUnauza: %s\n", + "Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n", "John Doee", "Male", "48", "Kilifi", "Bananas", ), }, @@ -1725,8 +1724,12 @@ func TestGetProfile(t *testing.T) { mockDataStore.On("ReadEntry", ctx, sessionId, key).Return([]byte(tt.profileInfo[index]), nil) } - mockState.SetLanguage(tt.languageCode) + err := mockState.SetLanguage(tt.languageCode) + if err != nil { + t.Fail() + } h.st = mockState + res, _ := h.GetProfileInfo(ctx, "get_profile_info", []byte("")) // Assert that expectations were met @@ -1834,6 +1837,25 @@ func TestSaveTemporaryPIn(t *testing.T) { mockStore.AssertExpectations(t) } +func TestWithPersister(t *testing.T) { + // Test case: Setting a persister + h := &Handlers{} + p := &persist.Persister{} + + result := h.WithPersister(p) + + assert.Equal(t, p, h.pe, "The persister should be set correctly.") + assert.Equal(t, h, result, "The returned handler should be the same instance.") +} + +func TestWithPersister_PanicWhenAlreadySet(t *testing.T) { + // Test case: Panic on multiple calls + h := &Handlers{pe: &persist.Persister{}} + require.Panics(t, func() { + h.WithPersister(&persist.Persister{}) + }, "Should panic when trying to set a persister again.") +} + func TestConfirmPin(t *testing.T) { sessionId := "session123" -- 2.45.2