summaryrefslogtreecommitdiff
path: root/internal/handlers/handlers_test.go
blob: ef973c9ec8723d42075e3d78f4a31f7d4a554bb6 (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
package handlers

import (
	"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.WaitUntilStateNetworkidle,
		Timeout:   playwright.Float(10000),
	})
	assert.NoError(err, "Failed to load page")

	// Wait for question to appear
	err = page.Locator(".question-text").WaitFor(playwright.LocatorWaitForOptions{
		State:   playwright.WaitForSelectorStateVisible,
		Timeout: playwright.Float(5000),
	})
	assert.NoError(err, "Question text did not appear")

	questionText, err := page.Locator(".question-text").TextContent()
	assert.NoError(err, "Failed to get question text")
	assert.Contains(questionText, "Zweifel", "Question text mismatch")

	// Verify all options are present and clickable
	options := []string{"Haben", "Hast", "Hat", "Habt"}
	for _, opt := range options {
		locator := page.Locator("button:has-text('" + opt + "')")
		err := locator.WaitFor(playwright.LocatorWaitForOptions{
			State:   playwright.WaitForSelectorStateVisible,
			Timeout: playwright.Float(3000),
		})
		assert.NoError(err, "Option button '%s' not found", opt)
		visible, err := locator.IsVisible()
		assert.NoError(err, "Failed to check visibility for '%s'", opt)
		assert.True(visible, "Option button '%s' not visible", opt)
	}

	// 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
}