test(integration): add t.Helper()

This commit is contained in:
Rob Watson 2025-04-13 11:00:18 +02:00
parent c4bc32b3e8
commit 5f6b6fa3a9
2 changed files with 86 additions and 77 deletions

View File

@ -21,6 +21,8 @@ import (
) )
func setupSimulationScreen(t *testing.T) (tcell.SimulationScreen, chan<- terminal.ScreenCapture, func() []string) { func setupSimulationScreen(t *testing.T) (tcell.SimulationScreen, chan<- terminal.ScreenCapture, func() []string) {
t.Helper()
// Fetching the screen contents is tricky at this level of the test pyramid, // Fetching the screen contents is tricky at this level of the test pyramid,
// because we need to: // because we need to:
// //
@ -59,7 +61,7 @@ func setupSimulationScreen(t *testing.T) (tcell.SimulationScreen, chan<- termina
t.Cleanup(func() { t.Cleanup(func() {
if t.Failed() { if t.Failed() {
printScreen(getContents, "After failing") printScreen(t, getContents, "After failing")
} }
}) })
@ -93,6 +95,8 @@ func contentsIncludes(contents []string, search string) bool {
} }
func setupConfigService(t *testing.T, cfg config.Config) *config.Service { func setupConfigService(t *testing.T, cfg config.Config) *config.Service {
t.Helper()
tmpDir, err := os.MkdirTemp("", "octoplex_"+strings.ReplaceAll(t.Name(), "/", "_")) tmpDir, err := os.MkdirTemp("", "octoplex_"+strings.ReplaceAll(t.Name(), "/", "_"))
require.NoError(t, err) require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(tmpDir) }) t.Cleanup(func() { os.RemoveAll(tmpDir) })
@ -103,29 +107,32 @@ func setupConfigService(t *testing.T, cfg config.Config) *config.Service {
return configService return configService
} }
func printScreen(getContents func() []string, label string) { func printScreen(t *testing.T, getContents func() []string, label string) {
t.Helper()
fmt.Println(label + ":") fmt.Println(label + ":")
for _, line := range getContents() { for _, line := range getContents() {
fmt.Println(line) fmt.Println(line)
} }
} }
func sendKey(screen tcell.SimulationScreen, key tcell.Key, ch rune) { func sendKey(t *testing.T, screen tcell.SimulationScreen, key tcell.Key, ch rune) {
t.Helper()
screen.InjectKey(key, ch, tcell.ModNone) screen.InjectKey(key, ch, tcell.ModNone)
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
} }
func sendKeyShift(screen tcell.SimulationScreen, key tcell.Key, ch rune) { func sendKeys(t *testing.T, screen tcell.SimulationScreen, keys string) {
screen.InjectKey(key, ch, tcell.ModShift) t.Helper()
time.Sleep(50 * time.Millisecond)
}
func sendKeys(screen tcell.SimulationScreen, keys string) {
screen.InjectKeyBytes([]byte(keys)) screen.InjectKeyBytes([]byte(keys))
time.Sleep(500 * time.Millisecond) time.Sleep(500 * time.Millisecond)
} }
func sendBackspaces(screen tcell.SimulationScreen, n int) { func sendBackspaces(t *testing.T, screen tcell.SimulationScreen, n int) {
t.Helper()
for range n { for range n {
screen.InjectKey(tcell.KeyBackspace, ' ', tcell.ModNone) screen.InjectKey(tcell.KeyBackspace, ' ', tcell.ModNone)
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
@ -135,6 +142,8 @@ func sendBackspaces(screen tcell.SimulationScreen, n int) {
// kickFirstRTMPConn kicks the first RTMP connection from the mediaMTX server. // kickFirstRTMPConn kicks the first RTMP connection from the mediaMTX server.
func kickFirstRTMPConn(t *testing.T, srv testcontainers.Container) { func kickFirstRTMPConn(t *testing.T, srv testcontainers.Container) {
t.Helper()
type conn struct { type conn struct {
ID string `json:"id"` ID string `json:"id"`
} }

View File

@ -123,7 +123,7 @@ func testIntegration(t *testing.T, streamKey string) {
time.Second, time.Second,
"expected the mediaserver to start", "expected the mediaserver to start",
) )
printScreen(getContents, "After starting the mediaserver") printScreen(t, getContents, "After starting the mediaserver")
// Start streaming a test video to the app: // Start streaming a test video to the app:
testhelpers.StreamFLV(t, "rtmp://localhost:1935/"+wantStreamKey) testhelpers.StreamFLV(t, "rtmp://localhost:1935/"+wantStreamKey)
@ -143,19 +143,19 @@ func testIntegration(t *testing.T, streamKey string) {
time.Second, time.Second,
"expected to receive an ingress stream", "expected to receive an ingress stream",
) )
printScreen(getContents, "After receiving the ingress stream") printScreen(t, getContents, "After receiving the ingress stream")
// Add a second destination in-app: // Add a second destination in-app:
sendKey(screen, tcell.KeyRune, 'a') sendKey(t, screen, tcell.KeyRune, 'a')
sendBackspaces(screen, 30) sendBackspaces(t, screen, 30)
sendKeys(screen, "Local server 2") sendKeys(t, screen, "Local server 2")
sendKey(screen, tcell.KeyTab, ' ') sendKey(t, screen, tcell.KeyTab, ' ')
sendBackspaces(screen, 30) sendBackspaces(t, screen, 30)
sendKeys(screen, destURL2) sendKeys(t, screen, destURL2)
sendKey(screen, tcell.KeyTab, ' ') sendKey(t, screen, tcell.KeyTab, ' ')
sendKey(screen, tcell.KeyEnter, ' ') sendKey(t, screen, tcell.KeyEnter, ' ')
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -178,12 +178,12 @@ func testIntegration(t *testing.T, streamKey string) {
time.Second, time.Second,
"expected to add the destinations", "expected to add the destinations",
) )
printScreen(getContents, "After adding the destinations") printScreen(t, getContents, "After adding the destinations")
// Start destinations: // Start destinations:
sendKey(screen, tcell.KeyRune, ' ') sendKey(t, screen, tcell.KeyRune, ' ')
sendKey(screen, tcell.KeyDown, ' ') sendKey(t, screen, tcell.KeyDown, ' ')
sendKey(screen, tcell.KeyRune, ' ') sendKey(t, screen, tcell.KeyRune, ' ')
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -207,11 +207,11 @@ func testIntegration(t *testing.T, streamKey string) {
time.Second, time.Second,
"expected to start the destination streams", "expected to start the destination streams",
) )
printScreen(getContents, "After starting the destination streams") printScreen(t, getContents, "After starting the destination streams")
sendKey(screen, tcell.KeyUp, ' ') sendKey(t, screen, tcell.KeyUp, ' ')
sendKey(screen, tcell.KeyDelete, ' ') sendKey(t, screen, tcell.KeyDelete, ' ')
sendKey(screen, tcell.KeyEnter, ' ') sendKey(t, screen, tcell.KeyEnter, ' ')
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -234,10 +234,10 @@ func testIntegration(t *testing.T, streamKey string) {
time.Second, time.Second,
"expected to remove the second destination", "expected to remove the second destination",
) )
printScreen(getContents, "After removing the second destination") printScreen(t, getContents, "After removing the second destination")
// Stop remaining destination. // Stop remaining destination.
sendKey(screen, tcell.KeyRune, ' ') sendKey(t, screen, tcell.KeyRune, ' ')
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -255,7 +255,7 @@ func testIntegration(t *testing.T, streamKey string) {
"expected to stop the first destination stream", "expected to stop the first destination stream",
) )
printScreen(getContents, "After stopping the first destination") printScreen(t, getContents, "After stopping the first destination")
// TODO: // TODO:
// - Source error // - Source error
@ -337,7 +337,7 @@ func TestIntegrationRestartDestination(t *testing.T) {
time.Second, time.Second,
"expected the mediaserver to start", "expected the mediaserver to start",
) )
printScreen(getContents, "After starting the mediaserver") printScreen(t, getContents, "After starting the mediaserver")
// Start streaming a test video to the app: // Start streaming a test video to the app:
testhelpers.StreamFLV(t, "rtmp://localhost:1935/live") testhelpers.StreamFLV(t, "rtmp://localhost:1935/live")
@ -354,10 +354,10 @@ func TestIntegrationRestartDestination(t *testing.T) {
time.Second, time.Second,
"expected to receive an ingress stream", "expected to receive an ingress stream",
) )
printScreen(getContents, "After receiving the ingress stream") printScreen(t, getContents, "After receiving the ingress stream")
// Start destination: // Start destination:
sendKey(screen, tcell.KeyRune, ' ') sendKey(t, screen, tcell.KeyRune, ' ')
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -375,7 +375,7 @@ func TestIntegrationRestartDestination(t *testing.T) {
time.Second, time.Second,
"expected to start the destination stream", "expected to start the destination stream",
) )
printScreen(getContents, "After starting the destination stream") printScreen(t, getContents, "After starting the destination stream")
// Wait for enough time that the container will be restarted. // Wait for enough time that the container will be restarted.
// Then, kick the connection to force a restart. // Then, kick the connection to force a restart.
@ -398,7 +398,7 @@ func TestIntegrationRestartDestination(t *testing.T) {
time.Second, time.Second,
"expected to begin restarting", "expected to begin restarting",
) )
printScreen(getContents, "After stopping the destination server") printScreen(t, getContents, "After stopping the destination server")
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -416,10 +416,10 @@ func TestIntegrationRestartDestination(t *testing.T) {
time.Second, time.Second,
"expected to restart the destination stream", "expected to restart the destination stream",
) )
printScreen(getContents, "After restarting the destination stream") printScreen(t, getContents, "After restarting the destination stream")
// Stop destination. // Stop destination.
sendKey(screen, tcell.KeyRune, ' ') sendKey(t, screen, tcell.KeyRune, ' ')
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -437,7 +437,7 @@ func TestIntegrationRestartDestination(t *testing.T) {
"expected to stop the destination stream", "expected to stop the destination stream",
) )
printScreen(getContents, "After stopping the destination") printScreen(t, getContents, "After stopping the destination")
cancel() cancel()
@ -493,7 +493,7 @@ func TestIntegrationStartDestinationFailed(t *testing.T) {
time.Second, time.Second,
"expected the mediaserver to start", "expected the mediaserver to start",
) )
printScreen(getContents, "After starting the mediaserver") printScreen(t, getContents, "After starting the mediaserver")
// Start streaming a test video to the app: // Start streaming a test video to the app:
testhelpers.StreamFLV(t, "rtmp://localhost:1935/live") testhelpers.StreamFLV(t, "rtmp://localhost:1935/live")
@ -510,10 +510,10 @@ func TestIntegrationStartDestinationFailed(t *testing.T) {
time.Second, time.Second,
"expected to receive an ingress stream", "expected to receive an ingress stream",
) )
printScreen(getContents, "After receiving the ingress stream") printScreen(t, getContents, "After receiving the ingress stream")
// Start destination: // Start destination:
sendKey(screen, tcell.KeyRune, ' ') sendKey(t, screen, tcell.KeyRune, ' ')
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -526,7 +526,7 @@ func TestIntegrationStartDestinationFailed(t *testing.T) {
time.Second, time.Second,
"expected to see the destination start error modal", "expected to see the destination start error modal",
) )
printScreen(getContents, "After starting the destination stream fails") printScreen(t, getContents, "After starting the destination stream fails")
cancel() cancel()
@ -582,13 +582,13 @@ func TestIntegrationDestinationValidations(t *testing.T) {
time.Second, time.Second,
"expected the mediaserver to start", "expected the mediaserver to start",
) )
printScreen(getContents, "After starting the mediaserver") printScreen(t, getContents, "After starting the mediaserver")
sendKey(screen, tcell.KeyRune, 'a') sendKey(t, screen, tcell.KeyRune, 'a')
sendKey(screen, tcell.KeyTab, ' ') sendKey(t, screen, tcell.KeyTab, ' ')
sendBackspaces(screen, 10) sendBackspaces(t, screen, 10)
sendKey(screen, tcell.KeyTab, ' ') sendKey(t, screen, tcell.KeyTab, ' ')
sendKey(screen, tcell.KeyEnter, ' ') sendKey(t, screen, tcell.KeyEnter, ' ')
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -602,13 +602,13 @@ func TestIntegrationDestinationValidations(t *testing.T) {
time.Second, time.Second,
"expected a validation error for an empty URL", "expected a validation error for an empty URL",
) )
printScreen(getContents, "After entering an empty destination URL") printScreen(t, getContents, "After entering an empty destination URL")
sendKey(screen, tcell.KeyEnter, ' ') sendKey(t, screen, tcell.KeyEnter, ' ')
sendKey(screen, tcell.KeyBacktab, ' ') sendKey(t, screen, tcell.KeyBacktab, ' ')
sendKeys(screen, "nope") sendKeys(t, screen, "nope")
sendKey(screen, tcell.KeyTab, ' ') sendKey(t, screen, tcell.KeyTab, ' ')
sendKey(screen, tcell.KeyEnter, ' ') sendKey(t, screen, tcell.KeyEnter, ' ')
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -622,14 +622,14 @@ func TestIntegrationDestinationValidations(t *testing.T) {
time.Second, time.Second,
"expected a validation error for an invalid URL", "expected a validation error for an invalid URL",
) )
printScreen(getContents, "After entering an invalid destination URL") printScreen(t, getContents, "After entering an invalid destination URL")
sendKey(screen, tcell.KeyEnter, ' ') sendKey(t, screen, tcell.KeyEnter, ' ')
sendKey(screen, tcell.KeyBacktab, ' ') sendKey(t, screen, tcell.KeyBacktab, ' ')
sendBackspaces(screen, len("nope")) sendBackspaces(t, screen, len("nope"))
sendKeys(screen, "rtmp://rtmp.youtube.com:1935/live") sendKeys(t, screen, "rtmp://rtmp.youtube.com:1935/live")
sendKey(screen, tcell.KeyTab, ' ') sendKey(t, screen, tcell.KeyTab, ' ')
sendKey(screen, tcell.KeyEnter, ' ') sendKey(t, screen, tcell.KeyEnter, ' ')
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -645,14 +645,14 @@ func TestIntegrationDestinationValidations(t *testing.T) {
time.Second, time.Second,
"expected to add the destination", "expected to add the destination",
) )
printScreen(getContents, "After adding the destination") printScreen(t, getContents, "After adding the destination")
sendKey(screen, tcell.KeyRune, 'a') sendKey(t, screen, tcell.KeyRune, 'a')
sendKey(screen, tcell.KeyTab, ' ') sendKey(t, screen, tcell.KeyTab, ' ')
sendBackspaces(screen, 10) sendBackspaces(t, screen, 10)
sendKeys(screen, "rtmp://rtmp.youtube.com:1935/live") sendKeys(t, screen, "rtmp://rtmp.youtube.com:1935/live")
sendKey(screen, tcell.KeyTab, ' ') sendKey(t, screen, tcell.KeyTab, ' ')
sendKey(screen, tcell.KeyEnter, ' ') sendKey(t, screen, tcell.KeyEnter, ' ')
// Start streaming a test video to the app: // Start streaming a test video to the app:
testhelpers.StreamFLV(t, "rtmp://localhost:1935/live") testhelpers.StreamFLV(t, "rtmp://localhost:1935/live")
@ -669,7 +669,7 @@ func TestIntegrationDestinationValidations(t *testing.T) {
time.Second, time.Second,
"expected a validation error for a duplicate URL", "expected a validation error for a duplicate URL",
) )
printScreen(getContents, "After entering a duplicate destination URL") printScreen(t, getContents, "After entering a duplicate destination URL")
cancel() cancel()
<-done <-done
@ -734,9 +734,9 @@ func TestIntegrationStartupCheck(t *testing.T) {
time.Second, time.Second,
"expected to see startup check modal", "expected to see startup check modal",
) )
printScreen(getContents, "Ater displaying the startup check modal") printScreen(t, getContents, "Ater displaying the startup check modal")
sendKey(screen, tcell.KeyEnter, ' ') // quit other containers sendKey(t, screen, tcell.KeyEnter, ' ') // quit other containers
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -750,7 +750,7 @@ func TestIntegrationStartupCheck(t *testing.T) {
2*time.Second, 2*time.Second,
"expected to quit the other containers", "expected to quit the other containers",
) )
printScreen(getContents, "After quitting the other containers") printScreen(t, getContents, "After quitting the other containers")
require.EventuallyWithT( require.EventuallyWithT(
t, t,
@ -764,7 +764,7 @@ func TestIntegrationStartupCheck(t *testing.T) {
time.Second, time.Second,
"expected the mediaserver to start", "expected the mediaserver to start",
) )
printScreen(getContents, "After starting the mediaserver") printScreen(t, getContents, "After starting the mediaserver")
cancel() cancel()
<-done <-done
@ -817,10 +817,10 @@ func TestIntegrationMediaServerError(t *testing.T) {
time.Second, time.Second,
"expected to see media server error modal", "expected to see media server error modal",
) )
printScreen(getContents, "Ater displaying the media server error modal") printScreen(t, getContents, "Ater displaying the media server error modal")
// Quit the app, this should cause the done channel to receive. // Quit the app, this should cause the done channel to receive.
sendKey(screen, tcell.KeyEnter, ' ') sendKey(t, screen, tcell.KeyEnter, ' ')
<-done <-done
} }
@ -869,10 +869,10 @@ func TestIntegrationDockerClientError(t *testing.T) {
time.Second, time.Second,
"expected to see fatal error modal", "expected to see fatal error modal",
) )
printScreen(getContents, "Ater displaying the fatal error modal") printScreen(t, getContents, "Ater displaying the fatal error modal")
// Quit the app, this should cause the done channel to receive. // Quit the app, this should cause the done channel to receive.
sendKey(screen, tcell.KeyEnter, ' ') sendKey(t, screen, tcell.KeyEnter, ' ')
<-done <-done
} }