clone

a desktop tv and program guide for over-the-air broadcasts: what's on air, what your tuner gets, live tv, recordings

main
airwaves / frontend / embed_test.go
embed_test.go 1.9K
1package frontend23import (4 "io/fs"5 "regexp"6 "slices"7 "testing"8)910// TestDist: the interface is embedded with index.html at the root, and11// the page loads the web app's stand-in for Wails first, then every file12// of the app (js/), none missing.13func TestDist(t *testing.T) {14 d := Dist()15 for _, name := range []string{"index.html", "web.js", "style.css", "js/core.js", "vendor/hls.min.js"} {16 if _, err := fs.Stat(d, name); err != nil {17 t.Errorf("%s: %v", name, err)18 }19 }20 page, err := fs.ReadFile(d, "index.html")21 if err != nil {22 t.Fatal(err)23 }24 var scripts []string25 for _, m := range regexp.MustCompile(`<script src="([^"]+)"></script>`).FindAllStringSubmatch(string(page), -1) {26 scripts = append(scripts, m[1])27 if _, err := fs.Stat(d, m[1]); err != nil {28 t.Errorf("the page loads %s: %v", m[1], err)29 }30 }31 web, core := slices.Index(scripts, "web.js"), slices.Index(scripts, "js/core.js")32 if web < 0 || core < 0 || web > core {33 t.Errorf("scripts %v: web.js must come before the app", scripts)34 }35 files, err := fs.Glob(d, "js/*.js")36 if err != nil {37 t.Fatal(err)38 }39 for _, f := range files {40 if !slices.Contains(scripts, f) {41 t.Errorf("%s isn't loaded by the page", f)42 }43 }44}4546// TestNoNameTwice: the app's files share one scope, as plain scripts do,47// so a name declared at the top of two of them stops the second loading.48func TestNoNameTwice(t *testing.T) {49 d := Dist()50 files, err := fs.Glob(d, "js/*.js")51 if err != nil {52 t.Fatal(err)53 }54 decl := regexp.MustCompile(`(?m)^(?:const|let|var|class|function|async function) ([A-Za-z_$][\w$]*)`)55 where := map[string]string{}56 for _, f := range files {57 src, err := fs.ReadFile(d, f)58 if err != nil {59 t.Fatal(err)60 }61 for _, m := range decl.FindAllStringSubmatch(string(src), -1) {62 if prev, ok := where[m[1]]; ok {63 t.Errorf("%s is declared in %s and %s", m[1], prev, f)64 }65 where[m[1]] = f66 }67 }68}