1 # morly - the ORLY Nostr relay framework, the server side of musiquay.
2 #
3 # The web app lives in ../musiquay; this repo is the native relay that serves
4 # it (static files, /blossom/ blobs, /proxy/ media and the Nostr WebSocket
5 # endpoint) and that musiquay's protocols are added to.
6 7 MOXIE ?= $(CURDIR)/../moxie/moxie
8 # An inherited MOXIEROOT is ignored on purpose: `?=` cannot tell an exported
9 # stale root from an intended one, and a stale root silently compiles a
10 # different tree's src/ and pkg/. Pass it on the command line to build against
11 # another tree: make MOXIEROOT=/path/to/moxie ...
12 ifeq ($(origin MOXIEROOT),environment)
13 MOXIEROOT := $(CURDIR)/../moxie
14 endif
15 MOXIEROOT ?= $(CURDIR)/../moxie
16 17 # Moxie closures do not capture: a closure receives what it needs through its
18 # parameters. The relay predates that enforcement in places, so the build opts
19 # out explicitly.
20 CAPTURES ?= -allow-closure-captures
21 22 # === Relay ===
23 build:
24 MOXIEROOT=$(MOXIEROOT) $(MOXIE) build $(CAPTURES) -o morly .
25 26 build-test-relay:
27 MOXIEROOT=$(MOXIEROOT) $(MOXIE) build $(CAPTURES) -o morly-test .
28 29 # Run the relay against a frontend build (defaults to the musiquay web app).
30 STATIC ?= $(CURDIR)/../musiquay/web/static
31 DATA ?= $(CURDIR)/data
32 PORT ?= 3334
33 serve: build
34 ORLY_STATIC_DIR=$(STATIC) ORLY_DATA_DIR=$(DATA) \
35 ORLY_LISTEN=127.0.0.1 ORLY_PORT=$(PORT) ./morly relay
36 37 # === Test ===
38 # Two layers. `test-unit` runs every package carrying a *_test.mx through the
39 # compiler's own harness. `test-integration` starts a real relay process and
40 # drives it over HTTP and WebSocket - the ORLY_* env contract, the round-trip,
41 # the policy limits. No browser and no frontend build: the app's browser suite
42 # is in ../smesh/test and drives a relay built here.
43 #
44 # ORLY_STATIC_DIR optionally points the integration relay at a frontend build;
45 # unset, the static-file checks are not in play.
46 UNIT_SKIP ?=
47 test-unit:
48 @set -e ; for d in $$( { [ -f main_test.mx ] && echo . ; find pkg -name '*_test.mx' | xargs -n1 dirname ; } | sort -u ) ; do \
49 case " $(UNIT_SKIP) " in *" $$d "*) echo "=== unit: $$d (skipped)" ; continue ;; esac ; \
50 echo "=== unit: $$d" ; \
51 MOXIEROOT=$(MOXIEROOT) $(MOXIE) test $(CAPTURES) ./$$d || exit 1 ; \
52 done
53 54 test-integration: build
55 rm -rf /tmp/morly-test-data
56 MORLY_RELAY_BIN=$(CURDIR)/morly python3 -m pytest test/ --tb=short
57 58 test: test-unit test-integration
59 60 # === Coverage ===
61 # `make cover` measures the integration suite's statement coverage of the relay:
62 # the relay is built with -cover, which makes the compiler emit a coverage hit
63 # per statement and write <output>.cover_sites.json (id -> pkg.Func@file:line);
64 # MOXIE_COVER_OUT makes the instrumented runtime append its counters to one file
65 # when a domain exits, which the fixture's SIGTERM triggers; mxcover joins the
66 # table to the counters and prints the per-file table plus every statement that
67 # never ran.
68 #
69 # The file is appended to, not rewritten, because each test starts a fresh relay
70 # and the relay is a spawn tree - the store, the ingest workers, the proxy, the
71 # blossom and broadcast workers each run in a forked domain with a private copy
72 # of the counter table - so every process and domain contributes its own dump.
73 # The target deletes the file first, so one run's report is one run's coverage.
74 #
75 # This is the integration half only. The unit tests are separate binaries run by
76 # the compiler's harness with their own site tables, so their hits are not in
77 # this report; `moxie test -cover ./<pkg>` is the unit-side measurement.
78 #
79 # The report is the work list. A coverage run rebuilds the whole closure (a
80 # cached package would have no hits to count) and the instrumented relay is
81 # slower, so this is not part of `make test`.
82 COVER_BIN := $(CURDIR)/morly-test
83 COVER_SITES := $(COVER_BIN).cover_sites.json
84 COVER_DIR := $(CURDIR)/tmp
85 COVER_COUNTS := $(COVER_DIR)/cover.counts
86 COVER_TOOL := $(COVER_DIR)/mxcover
87 # morly resolves through MOXIEPATH/MANIFEST rather than a module file, so the
88 # prefix the site table uses is the import path, not something read from disk.
89 COVER_PREFIX := git.smesh.lol/morly
90 cover:
91 @mkdir -p $(COVER_DIR)
92 MOXIEROOT=$(MOXIEROOT) $(MOXIE) build -cover $(CAPTURES) -o $(COVER_BIN) .
93 rm -f $(COVER_COUNTS)
94 @rc=0 ; MORLY_RELAY_BIN=$(COVER_BIN) MOXIE_COVER_OUT=$(COVER_COUNTS) \
95 python3 -m pytest test/ -q --tb=short || rc=$$? ; \
96 MOXIEROOT=$(MOXIEROOT) $(MOXIE) build -o $(COVER_TOOL) $(MOXIEROOT)/cmd/mxcover || rc=$$? ; \
97 $(COVER_TOOL) $(COVER_SITES) $(COVER_COUNTS) $(COVER_PREFIX) || rc=$$? ; \
98 exit $$rc
99 100 .PHONY: build build-test-relay serve test-unit test-integration test cover
101