rastrillo / aviso Public

Clone
git clone https://amadan.net/rastrillo/aviso

Plain git — no account needed to clone.

Download

Download this file

1package aviso
2
3import (
4 "bytes"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "testing"
9)
10
11func TestJSEmbedded(t *testing.T) {
12 if !bytes.Contains(JS(), []byte("export async function enable(")) {
13 t.Fatal("JS() does not look like push.mjs")
14 }
15 if !bytes.Contains(WorkerJS(), []byte("AvisoSW")) {
16 t.Fatal("WorkerJS() does not look like aviso-sw.js")
17 }
18}
19
20// runNodeTests materialises the embedded files beside the named test
21// in a temp dir and runs `node --test` there — so what is tested is
22// the bytes the binary serves, not a sibling file that could drift.
23// Skipped without node, rastrillo/crypto's rule; CI has node.
24func runNodeTests(t *testing.T, testFile string, files map[string][]byte) {
25 t.Helper()
26 node, err := exec.LookPath("node")
27 if err != nil {
28 t.Skip("node not on PATH; JS half not exercised")
29 }
30 dir := t.TempDir()
31 src, err := os.ReadFile(filepath.Join("js", testFile))
32 if err != nil {
33 t.Fatal(err)
34 }
35 files[testFile] = src
36 for name, b := range files {
37 if err := os.WriteFile(filepath.Join(dir, name), b, 0o644); err != nil {
38 t.Fatal(err)
39 }
40 }
41 cmd := exec.Command(node, "--test", testFile)
42 cmd.Dir = dir
43 if out, err := cmd.CombinedOutput(); err != nil {
44 t.Fatalf("node --test %s failed: %v\n%s", testFile, err, out)
45 }
46}
47
48func TestPushModule(t *testing.T) {
49 runNodeTests(t, "push.test.mjs", map[string][]byte{"push.mjs": JS()})
50}
51
52func TestWorkerHelper(t *testing.T) {
53 runNodeTests(t, "aviso-sw.test.mjs", map[string][]byte{"aviso-sw.js": WorkerJS()})
54}
55