package aviso import ( "bytes" "os" "os/exec" "path/filepath" "testing" ) func TestJSEmbedded(t *testing.T) { if !bytes.Contains(JS(), []byte("export async function enable(")) { t.Fatal("JS() does not look like push.mjs") } if !bytes.Contains(WorkerJS(), []byte("AvisoSW")) { t.Fatal("WorkerJS() does not look like aviso-sw.js") } } // runNodeTests materialises the embedded files beside the named test // in a temp dir and runs `node --test` there — so what is tested is // the bytes the binary serves, not a sibling file that could drift. // Skipped without node, rastrillo/crypto's rule; CI has node. func runNodeTests(t *testing.T, testFile string, files map[string][]byte) { t.Helper() node, err := exec.LookPath("node") if err != nil { t.Skip("node not on PATH; JS half not exercised") } dir := t.TempDir() src, err := os.ReadFile(filepath.Join("js", testFile)) if err != nil { t.Fatal(err) } files[testFile] = src for name, b := range files { if err := os.WriteFile(filepath.Join(dir, name), b, 0o644); err != nil { t.Fatal(err) } } cmd := exec.Command(node, "--test", testFile) cmd.Dir = dir if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("node --test %s failed: %v\n%s", testFile, err, out) } } func TestPushModule(t *testing.T) { runNodeTests(t, "push.test.mjs", map[string][]byte{"push.mjs": JS()}) } func TestWorkerHelper(t *testing.T) { runNodeTests(t, "aviso-sw.test.mjs", map[string][]byte{"aviso-sw.js": WorkerJS()}) }