summaryrefslogtreecommitdiff
path: root/internal/handlers/handlers_test.go
blob: a3507f3fce4eee625aa1c3056c6dca8fce18d676 (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
94
95
96
97
98
99
100
101
102
103
104
105
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(1000),
	})
	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(1000),
	})
	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, "___ du keine Zweifel daran?", "Question text should match database entry")
	assert.Contains(questionText, "(Test Question)", "Question text should contain test indicator")

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

	// 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 (option1 is correct per DB data based on correct_index=0)
	// First wait for option to be clickable
	err = page.Locator("[data-testid='option1']").WaitFor(playwright.LocatorWaitForOptions{
		State:   playwright.WaitForSelectorStateVisible,
		Timeout: playwright.Float(1000),
	})
	assert.NoError(err, "Option 1 not visible")

	page.Locator("[data-testid='option1']").Click()

	// Verify feedback contains expected explanation
	err = page.Locator("#feedback:has-text('zu welchem Zeitpunkt')").WaitFor()
	assert.NoError(err, "Correct answer feedback not shown")

	// Test wrong answer (option3 is incorrect)
	err = page.Locator("[data-testid='option2']").WaitFor(playwright.LocatorWaitForOptions{
		State:   playwright.WaitForSelectorStateVisible,
		Timeout: playwright.Float(1000),
	})
	assert.NoError(err, "Option 3 not visible")

	page.Locator("[data-testid='option3']").Click()

	// Verify feedback contains expected explanation
	err = page.Locator("#feedback:has-text('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
}