diff options
Diffstat (limited to 'internal/handlers/handlers_test.go')
-rw-r--r-- | internal/handlers/handlers_test.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/internal/handlers/handlers_test.go b/internal/handlers/handlers_test.go new file mode 100644 index 0000000..a597d7a --- /dev/null +++ b/internal/handlers/handlers_test.go @@ -0,0 +1,76 @@ +package handlers + +import ( + "context" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/playwright-community/playwright-go" + "github.com/stretchr/testify/assert" +) + +func TestQuestionFlow(t *testing.T) { + assert := assert.New(t) + + // Setup test server + h := NewHandlers( + config.Config{}, + slog.New(slog.NewJSONHandler(os.Stdout, nil), + &MockRepo{}, + ) + ts := httptest.NewServer(http.HandlerFunc(h.MainPage)) + defer ts.Close() + + // Setup Playwright + pw, err := playwright.Run() + assert.NoError(err) + defer pw.Stop() + + browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{ + Headless: playwright.Bool(true), // Change to false for debugging + }) + assert.NoError(err) + defer browser.Close() + + page, err := browser.NewPage() + assert.NoError(err) + + // Test 1: Load page and verify question + _, err = page.Goto(ts.URL) + assert.NoError(err) + + questionText, err := page.Locator(".question-text").TextContent() + assert.NoError(err) + assert.Contains(questionText, "Zweifel") + + // Verify all options are present + options := []string{"Haben", "Hast", "Hat", "Habt"} + for _, opt := range options { + _, err := page.Locator("button:has-text('" + opt + "')").TextContent() + assert.NoError(err) + } + + // Test 2: Click correct answer + err = page.Locator("button:has-text('Hast')").Click() + assert.NoError(err) + + feedback, err := page.Locator("#feedback").TextContent() + assert.NoError(err) + assert.Contains(feedback, "Correct") + + // Test 3: Click wrong answer + err = page.Locator("button:has-text('Haben')").Click() + assert.NoError(err) + + feedback, err = page.Locator("#feedback").TextContent() + assert.NoError(err) + assert.Contains(feedback, "Wrong") +} + +type MockRepo struct{} + +func (m *MockRepo) DBGetDefaultsMap() (map[string]string, error) { + return map[string]string{}, nil +} |