Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 71 additions & 22 deletions internal/test/playwright_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@
"github.com/TecharoHQ/anubis"
libanubis "github.com/TecharoHQ/anubis/lib"
"github.com/playwright-community/playwright-go"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/network"
"github.com/testcontainers/testcontainers-go/wait"
)

var (
playwrightPort = flag.Int("playwright-port", 9001, "Playwright port")
playwrightServer = flag.String("playwright", "ws://localhost:9001", "Playwright server URL")
playwrightMaxTime = flag.Duration("playwright-max-time", 5*time.Second, "maximum time for Playwright requests")
playwrightMaxHardTime = flag.Duration("playwright-max-hard-time", 5*time.Minute, "maximum time for hard Playwright requests")
playwrightRunner = flag.String("playwright-runner", "npx", "how to start Playwright, can be: none,npx,docker,podman")
playwrightRunner = flag.String("playwright-runner", "testcontainer", "how to start Playwright, can be: none,npx,docker,podman")

Check failure on line 44 in internal/test/playwright_test.go

View workflow job for this annotation

GitHub Actions / Check Spelling

`testcontainer` is not a recognized word. (unrecognized-spelling)

testCases = []testCase{
{
Expand Down Expand Up @@ -168,10 +171,11 @@
})
}

func startPlaywright(t *testing.T) {
func startPlaywright(t *testing.T, anubisPort int) {
t.Helper()

if *playwrightRunner == "npx" {
switch *playwrightRunner {
case "npx":
doesCommandExist(t, "npx")

if os.Getenv("CI") == "true" {
Expand All @@ -181,7 +185,7 @@
}

daemonize(t, fmt.Sprintf("npx --yes playwright@%s run-server --port %d", playwrightVersion, *playwrightPort))
} else if *playwrightRunner == "docker" || *playwrightRunner == "podman" {
case "docker", "podman":
doesCommandExist(t, *playwrightRunner)

// docs: https://playwright.dev/docs/docker
Expand All @@ -190,19 +194,43 @@
t.Cleanup(func() {
run(t, fmt.Sprintf("%s rm --force %s", *playwrightRunner, container))
})
} else if *playwrightRunner == "none" {
case "testcontainer":

Check failure on line 197 in internal/test/playwright_test.go

View workflow job for this annotation

GitHub Actions / Check Spelling

`testcontainer` is not a recognized word. (unrecognized-spelling)

Check warning on line 197 in internal/test/playwright_test.go

View workflow job for this annotation

GitHub Actions / Check Spelling

`testcontainer` is not a recognized word. (unrecognized-spelling)
playwrightC, err := testcontainers.Run(
t.Context(),
fmt.Sprintf("mcr.microsoft.com/playwright:v%s-noble", playwrightVersion),
testcontainers.WithCmdArgs(
"npx", "-y", fmt.Sprintf("playwright@%s", playwrightVersion), "run-server", "--port=9001", "--host=0.0.0.0"),
testcontainers.WithExposedPorts("9001/tcp"),
network.WithNetworkName([]string{"anubis"}, "anubis_devcontainer_default"),
// testcontainers.WithHostPortAccess(anubisPort),
testcontainers.WithWaitStrategy(
// wait.ForListeningPort("9001/tcp"),
wait.ForLog("Listening on ws://0.0.0.0:9001/"),
),
)
testcontainers.CleanupContainer(t, playwrightC)
if err != nil {
t.Fatal(err)
}

*playwrightServer, err = playwrightC.PortEndpoint(t.Context(), "9001/tcp", "ws")
if err != nil {
t.Fatal(err)
}

case "none":
t.Log("not starting Playwright, assuming it is already running")
} else {
default:
t.Skipf("unknown runner: %s, skipping", *playwrightRunner)
}

for {
if _, err := http.Get(fmt.Sprintf("http://localhost:%d", *playwrightPort)); err != nil {
time.Sleep(500 * time.Millisecond)
continue
}
break
}
// for {
// if _, err := http.Get(strings.Replace(*playwrightServer, "ws://", "http://", 1)); err != nil {
// time.Sleep(500 * time.Millisecond)
// continue
// }
// break
// }

//nosleep:bypass XXX(Xe): Playwright doesn't have a good way to signal readiness. This is a HACK that will just let the tests pass.
time.Sleep(2 * time.Second)
Expand All @@ -219,10 +247,10 @@
return
}

startPlaywright(t)
anubisURL, anubisPort := spawnAnubis(t)

startPlaywright(t, anubisPort)
pw := setupPlaywright(t)
anubisURL := spawnAnubis(t)

browsers := []playwright.BrowserType{pw.Chromium, pw.Firefox, pw.WebKit}

Expand Down Expand Up @@ -301,11 +329,12 @@

t.Skip("NOTE(Xe)\\ these tests require HTTPS support in #364")

startPlaywright(t)
basePrefix := "/myapp"
anubisURL, anubisPort := spawnAnubisWithOptions(t, basePrefix)

startPlaywright(t, anubisPort)

pw := setupPlaywright(t)
basePrefix := "/myapp"
anubisURL := spawnAnubisWithOptions(t, basePrefix)

// Reset BasePrefix after test
t.Cleanup(func() {
Expand Down Expand Up @@ -583,11 +612,11 @@
return pw
}

func spawnAnubis(t *testing.T) string {
func spawnAnubis(t *testing.T) (string, int) {
return spawnAnubisWithOptions(t, "")
}

func spawnAnubisWithOptions(t *testing.T, basePrefix string) string {
func spawnAnubisWithOptions(t *testing.T, basePrefix string) (anubisURL string, anubisPort int) {
t.Helper()

h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -625,11 +654,31 @@
Config: &http.Server{Handler: s},
}
ts.Start()
t.Log(ts.URL)

url, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
anubisPort, err = strconv.Atoi(url.Port())
if err != nil {
anubisPort = 80
}

var anubisHost string

if os.Getenv("REMOTE_CONTAINER") == "true" {
anubisHost = "workspace"
} else {
anubisHost = "localhost"
}

anubisURL = "http://" + anubisHost + ":" + strconv.Itoa(anubisPort)

t.Logf("Anubis started on %s", anubisURL)

t.Cleanup(func() {
ts.Close()
})

return ts.URL
return
}
Loading