diff --git a/test_engine/menu_traversal_test.go b/test_engine/menu_traversal_test.go index e7fe2bf..dc55782 100644 --- a/test_engine/menu_traversal_test.go +++ b/test_engine/menu_traversal_test.go @@ -8,10 +8,9 @@ import ( "os" "regexp" "testing" - "time" "git.grassecon.net/urdt/ussd/driver" - enginetest "git.grassecon.net/urdt/ussd/engine" + "git.grassecon.net/urdt/ussd/internal/testutil" "github.com/gofrs/uuid" ) @@ -55,7 +54,7 @@ func TestMain(m *testing.M) { } func TestAccountCreationSuccessful(t *testing.T) { - en, fn := enginetest.TestEngine(sessionID) + en, fn, eventChannel := testutil.TestEngine(sessionID) defer fn() ctx := context.Background() sessions := testData @@ -76,14 +75,18 @@ func TestAccountCreationSuccessful(t *testing.T) { t.Fatalf("Test case '%s' failed during Flush: %v", group.Name, err) } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } } } - // Adding a sleep after the test to wait for registration to complete the process - time.Sleep(5 * time.Second) + <-eventChannel + } func TestAccountRegistrationRejectTerms(t *testing.T) { @@ -94,7 +97,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) { t.Fail() } edgeCaseSessionID := v.String() - en, fn := enginetest.TestEngine(edgeCaseSessionID) + en, fn, _ := testutil.TestEngine(edgeCaseSessionID) defer fn() ctx := context.Background() sessions := testData @@ -116,7 +119,11 @@ func TestAccountRegistrationRejectTerms(t *testing.T) { } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -125,7 +132,7 @@ func TestAccountRegistrationRejectTerms(t *testing.T) { } func TestSendWithInvalidInputs(t *testing.T) { - en, fn := enginetest.TestEngine(sessionID) + en, fn, _ := testutil.TestEngine(sessionID) defer fn() ctx := context.Background() sessions := testData @@ -153,8 +160,11 @@ func TestSendWithInvalidInputs(t *testing.T) { // Replace placeholder {public_key} with the actual dynamic public key expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1) - - if !bytes.Equal(b, expectedContent) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expectedContent, b) } } @@ -163,7 +173,7 @@ func TestSendWithInvalidInputs(t *testing.T) { } func TestMyAccount_Check_My_Balance(t *testing.T) { - en, fn := enginetest.TestEngine(sessionID) + en, fn, _ := testutil.TestEngine(sessionID) defer fn() ctx := context.Background() sessions := testData @@ -185,7 +195,11 @@ func TestMyAccount_Check_My_Balance(t *testing.T) { t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err) } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -194,7 +208,7 @@ func TestMyAccount_Check_My_Balance(t *testing.T) { } func TestMainMenuHelp(t *testing.T) { - en, fn := enginetest.TestEngine(sessionID) + en, fn, _ := testutil.TestEngine(sessionID) defer fn() ctx := context.Background() sessions := testData @@ -216,7 +230,11 @@ func TestMainMenuHelp(t *testing.T) { } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -225,7 +243,7 @@ func TestMainMenuHelp(t *testing.T) { } func TestMainMenuQuit(t *testing.T) { - en, fn := enginetest.TestEngine(sessionID) + en, fn, _ := testutil.TestEngine(sessionID) defer fn() ctx := context.Background() sessions := testData @@ -247,7 +265,11 @@ func TestMainMenuQuit(t *testing.T) { } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -256,7 +278,7 @@ func TestMainMenuQuit(t *testing.T) { } func TestMyAccount_Check_Community_Balance(t *testing.T) { - en, fn := enginetest.TestEngine(sessionID) + en, fn, _ := testutil.TestEngine(sessionID) defer fn() ctx := context.Background() sessions := testData @@ -278,7 +300,11 @@ func TestMyAccount_Check_Community_Balance(t *testing.T) { t.Errorf("Test case '%s' failed during Flush: %v", group.Name, err) } b := w.Bytes() - if !bytes.Equal(b, []byte(step.ExpectedContent)) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) } } @@ -287,7 +313,7 @@ func TestMyAccount_Check_Community_Balance(t *testing.T) { } func TestMyAccount_MyAddress(t *testing.T) { - en, fn := enginetest.TestEngine(sessionID) + en, fn, _ := testutil.TestEngine(sessionID) defer fn() ctx := context.Background() sessions := testData @@ -311,10 +337,12 @@ func TestMyAccount_MyAddress(t *testing.T) { b := w.Bytes() publicKey := extractPublicKey(b) - expectedContent := bytes.Replace([]byte(step.ExpectedContent), []byte("{public_key}"), []byte(publicKey), -1) - - if !bytes.Equal(b, expectedContent) { + match, err := step.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", step.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expectedContent, b) } } @@ -327,7 +355,7 @@ func TestGroups(t *testing.T) { if err != nil { log.Fatalf("Failed to load test groups: %v", err) } - en, fn := enginetest.TestEngine(sessionID) + en, fn, _ := testutil.TestEngine(sessionID) defer fn() ctx := context.Background() // Create test cases from loaded groups @@ -347,7 +375,11 @@ func TestGroups(t *testing.T) { t.Errorf("Test case '%s' failed during Flush: %v", tt.Name, err) } b := w.Bytes() - if !bytes.Equal(b, []byte(tt.ExpectedContent)) { + match, err := tt.MatchesExpectedContent(b) + if err != nil { + t.Fatalf("Error compiling regex for step '%s': %v", tt.Input, err) + } + if !match { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", tt.ExpectedContent, b) }