package web import ( "context" "html" "os/exec" "path/filepath" "regexp" "strings" "testing" "time" ) // TestScripts runs the pages in testdata, which drive assets/app.js and // report PASS or FAIL lines, in a headless browser. func TestScripts(t *testing.T) { var browser string for _, name := range []string{"chromium", "chromium-browser", "google-chrome"} { if p, err := exec.LookPath(name); err == nil { browser = p break } } if browser == "" { t.Skip("no Chromium to run the editor in") } pages, err := filepath.Glob("testdata/*.html") if err != nil || len(pages) == 0 { t.Fatalf("no test pages: %v", err) } for _, rel := range pages { t.Run(filepath.Base(rel), func(t *testing.T) { page, err := filepath.Abs(rel) if err != nil { t.Fatal(err) } ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() out, err := exec.CommandContext(ctx, browser, "--headless", "--disable-gpu", "--allow-file-access-from-files", "--virtual-time-budget=3000", "--dump-dom", "file://"+page).Output() if err != nil { t.Fatalf("running %s: %v", browser, err) } m := regexp.MustCompile(`(?s)
(.*?)`).FindSubmatch(out) if m == nil || len(m[1]) == 0 { t.Fatal("the page produced no results") } for _, line := range strings.Split(string(m[1]), "\n") { if !strings.HasPrefix(line, "PASS ") { t.Error(html.UnescapeString(line)) } } }) } }