summaryrefslogtreecommitdiff
path: root/internal/handlers/handlers_test.go
blob: be5607ff7369e6ea0a1d47373d4d4f8b3a2b3465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package handlers

import (
	"fmt"
	"testing"

	"github.com/playwright-community/playwright-go"
	"github.com/stretchr/testify/assert"
)

func TestQuestionFlow(t *testing.T) {
	assert := assert.New(t)

	// Use running server
	serverURL := "http://localhost:9000"

	// 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(serverURL, playwright.PageGotoOptions{
		WaitUntil: playwright.WaitUntilStateDomcontentloaded,
		Timeout:   playwright.Float(2000),
	})
	assert.NoError(err, "Failed to load page")

	// Wait for main content to load
	err = page.Locator("body").WaitFor(playwright.LocatorWaitForOptions{
		State:   playwright.WaitForSelectorStateVisible,
		Timeout: playwright.Float(2000),
	})
	assert.NoError(err, "Page content did not load")

	// Verify question text exists and contains expected content
	questionLocator := page.Locator("[data-testid='question']")
	visible, err := questionLocator.IsVisible()
	if err != nil || !visible {
		assert.FailNow("Question text not visible", err)
	}
	
	questionText, err := questionLocator.TextContent()
	if err != nil {
		assert.FailNow("Failed to get question text", err)
	}
	assert.Contains(questionText, "___ hast du heute Zeit?", "Question text should contain the actual question text from DB")

	// Set longer timeout for CI environment
	page.SetDefaultTimeout(5000)

	// Verify options using test IDs - first wait for them to be visible
	for i := 1; i <= 4; i++ {
		testID := fmt.Sprintf("option%d", i)
		locator := page.Locator(fmt.Sprintf("[data-testid='%s']", testID))
		visible, err := locator.IsVisible()
		if err != nil {
			assert.FailNowf("Option check failed", "Option %d: %v", i, err)
		}
		assert.True(visible, "Option %d not visible", i)
	}

	// Test correct answer (option3 is correct per DB data)
	err = page.Locator("[data-testid='option3']").Click()
	assert.NoError(err)
	
	// Wait for feedback to update and verify
	err = page.Locator("[data-testid='feedback']:has-text('zu welchem Zeitpunkt')").WaitFor()
	assert.NoError(err, "Correct answer feedback not shown")
	
	// Test wrong answer
	err = page.Locator("[data-testid='option1']").Click()
	assert.NoError(err)
	
	// Wait for feedback to update and verify - using actual explanation from DB
	err = page.Locator("[data-testid='feedback']:has-text('Wann: zu welchem Zeitpunkt')").WaitFor()
	assert.NoError(err, "Wrong answer feedback not shown")
}

type MockRepo struct{}

func (m *MockRepo) DBGetDefaultsMap() (map[string]string, error) {
	return map[string]string{}, nil
}