Skip to content
pi-c Artifact [e8e5d41497]
Login

Artifact [e8e5d41497]

Artifact e8e5d41497ef8148d8cdfccb6908c4480a5e7c6c5a48380da11b943225041a7b:


# pic harness plus S0/S1 qualification gates.
CC ?= cc
PYTHON ?= python3
export CC
# A developer's own publisher credentials never reach a test or a build: /share would default to the real
# publisher (docs/dev/share.md).
unexport HTML_PUBLISHER_TOKEN HTML_PUBLISHER_URL

.PHONY: all check metadata test test-reporting replay dst sanitize coverage-mcdc \
        fuzz-smoke bench filc-remote qualify help assertion-guard local-evidence test-context test-context-sanitize test-remote \
        pic pic-dev test-pic test-pic-run test-docs test-sqlite3 test-sse test-tui test-highlight test-decode test-http test-webpush test-files test-models test-userdb test-settings test-cred test-keyhealth test-noise test-capacity test-xfer test-prompts test-skills test-provider fuzz-decode fuzz-http fuzz-chat fuzz-responses bench-pic bench-pic-record install-pic

all: check

check: test-pic metadata test-reporting replay coverage-mcdc assertion-guard test-remote
	@printf '%s\n' 'PASS: pic crash/recovery suite and local qualification gates.'

metadata:
	$(PYTHON) -I -B test/test_contracts.py
	$(PYTHON) -I -B tools/check_contracts.py

test-reporting:
	$(PYTHON) -I -B test/test_toolchain.py

test:
	sh tools/test-native.sh normal

# The normal suite includes the fixed 100-seed deterministic replay campaign.
dst: test

replay: test
	sh tools/test-native.sh replay

sanitize:
	sh tools/test-native.sh sanitize

coverage-mcdc:
	sh tools/mcdc.sh

assertion-guard:
	sh tools/test-assertions.sh

fuzz-smoke:
	sh tools/test-native.sh fuzz

bench:
	sh tools/benchmark.sh

test-context:
	sh test/context/test-driver.sh normal

test-context-sanitize:
	sh test/context/test-driver.sh sanitize

test-remote:
	sh test/test_remote_lifecycle.sh
	$(PYTHON) -I -B test/remote/test_collect.py

# Run actual local gates before snapshotting metadata; never reuse a stale PASS.
local-evidence:
	mkdir -p build/qualification
	rm -f build/qualification/local-evidence.json
	$(MAKE) check sanitize fuzz-smoke bench > build/qualification/local-checks.log 2>&1
	$(PYTHON) -I -B tools/evidence.py --log build/qualification/local-checks.log
	$(PYTHON) -I -B tools/check_contracts.py --evidence build/qualification/local-evidence.json

# Remote evidence gate; actual host operations remain explicit.
filc-remote:
	@test -n "$(REMOTE_RUN)" || { printf '%s\n' 'GATED: supply REMOTE_RUN=/absolute/completed/filc-run; see docs/dev/filc.md'; exit 2; }
	sh tools/remote/run.sh reconcile "$(REMOTE_RUN)" > "$(REMOTE_RUN)/operator-reconcile.log"
	$(PYTHON) -I -B tools/remote/report.py "$(REMOTE_RUN)"
	$(PYTHON) -I -B tools/check_contracts.py --evidence "$(REMOTE_RUN)/qualification.json"

qualify: check sanitize fuzz-smoke bench
	@printf '%s\n' 'GATED: local fixture evidence is not release qualification.' \
	 'Production runtime/release contracts remain unqualified; remote fixture evidence is a separate explicit gate.'
	@exit 2

help:
	@printf '%s\n' 'S0 only: check, metadata, test, dst, replay, sanitize, coverage-mcdc,' \
	 'fuzz-smoke, bench, test-context, test-context-sanitize, test-remote.' \
	 'filc-remote verifies an explicit REMOTE_RUN; full production qualify remains GATED.'

# ---- pic: the harness ------------------------------------------------------
# Release has no test code and no fault hooks; dev adds ASan/UBSan and crash points.
PIC_SRC := src/store.c src/prune.c src/trash.c src/image.c src/platform.c src/input.c src/userdb.c src/sqlar.c src/catalog.c src/catalog_fetch.c src/loop.c src/filelog.c src/rewind.c src/listings.c src/compact.c src/events.c src/export_pi.c src/export_html.c src/import_pi.c src/import_claude.c src/tree.c src/commands.c src/rpc_core.c src/settings.c src/inbox.c src/ask.c src/perm.c src/wake.c src/monitor.c src/goal.c src/attach.c src/peer.c src/noise.c src/remote.c src/doctor.c src/folder.c src/rpc.c src/tui/app.c src/tui/width.c src/tui/term.c src/tui/render.c src/tui/editor.c src/tui/fuzzy.c src/tui/atfile.c src/tui/clipboard.c src/tui/view.c src/tui/transcript.c src/tui/viewport.c src/tui/markdown.c src/tui/highlight.c src/tui/theme.c src/tui/themes.c src/tui/keymap.c src/tui/mjson.c src/tui/remote.c src/tui/panel.c src/tui/modelpanel.c src/skills.c src/hooks.c src/procs.c src/ignore.c src/provider_fake.c src/provider_anthropic.c src/tools.c src/toolpar.c src/toolreg.c src/search.c src/htmltext.c src/webfetch.c src/websearch.c src/websearch_native.c src/share.c src/bash.c src/context.c src/prompts.c src/verify.c src/update.c \
	src/sse.c src/anthropic.c src/openai_chat.c src/openai_responses.c src/gemini.c src/provider_http.c src/provider_chat.c \
	src/provider_responses.c src/provider_gemini.c src/http.c src/net.c src/xfer.c src/cred.c src/keyhealth.c src/keyprobe.c src/oauth.c src/oauth_openai.c src/oauth_openrouter.c src/webpush.c \
	src/model.c src/models.c src/endpoints.c src/endpoints_fetch.c src/mcp.c src/mcp_wire.c src/mcp_stdio.c src/mcp_serve.c src/codemode.c src/jsengine_quickjs.c src/toolsearch.c src/transform.c src/help.c src/main.c
# Everything the generated pic.c is made from besides the .c list: every internal header and .inc.
# The wildcard misses the two generated .inc on a clean tree, so they are named.
PIC_HDR := $(wildcard src/*.h src/tui/*.h src/*.inc src/tui/*.inc) src/changelog.inc src/manual.inc

# /changelog's text is compiled in; CHANGELOG.md is the source. The .inc is
# built, never committed, so landings do not conflict on it. pic.c lists it as a
# prerequisite, so a clean tree builds it first.
src/changelog.inc: CHANGELOG.md tools/changelog-inc.sh
	sh tools/changelog-inc.sh CHANGELOG.md > [email protected] && mv -f [email protected] $@

# PIC_SRC, one per line, for the Fil-C run (tools/remote/payload.sh), which builds pic from it as
# make does. Generated, never committed.
src/pic-src.txt: Makefile
	printf '%s\n' $(PIC_SRC) > [email protected] && mv -f [email protected] $@

# pic help's topics: docs/manual and docs/reference, compiled in the same way.
src/manual.inc: $(wildcard docs/manual/*.md docs/reference/*.md) tools/manual-inc.sh
	sh tools/manual-inc.sh > [email protected] && mv -f [email protected] $@

# The release bundle: pic.c (all of pic's sources as one translation unit) beside the vendored
# dependency sources, built with one plain cc line (docs/dev/amalgamation.md). VERSION is the
# newest CHANGELOG date.
PIC_AMAL_VERSION ?= $(shell awk '/^\#\# [0-9]/ { gsub(/[^0-9]/, "", $$2); print $$2; exit }' CHANGELOG.md)
PIC_AMAL_NAME := pic-amalgamation-$(PIC_AMAL_VERSION)
PIC_AMAL_OUT := build/amalgamation
PIC_AMAL_RUNS := crash print tools
.PHONY: amalgamation test-amal amalgam-names $(PIC_AMAL_RUNS:%=run-amal-%)
amalgamation: src/changelog.inc src/manual.inc
	sh tools/amalgamation.sh $(PIC_AMAL_OUT) $(PIC_AMAL_NAME) $(PIC_SRC)
$(PIC_AMAL_OUT)/$(PIC_AMAL_NAME).tar.gz: $(PIC_SRC) $(PIC_HDR) tools/amalgamate.sh tools/amalgamation.sh $(wildcard vendor/*/*.[ch] vendor/bearssl/*/*.[ch] vendor/bearssl/src/*/*.[ch])
	$(MAKE) amalgamation

# The tarball, unpacked and built with the README's own cc line, then a slice of the suite
# against that binary: crash and recovery, print mode and the tools.
$(PIC_AMAL_OUT)/test/pic: $(PIC_AMAL_OUT)/$(PIC_AMAL_NAME).tar.gz
	rm -rf $(PIC_AMAL_OUT)/test && mkdir -p $(PIC_AMAL_OUT)/test
	tar -C $(PIC_AMAL_OUT)/test -xzf $<
	cd $(PIC_AMAL_OUT)/test/$(PIC_AMAL_NAME) && sh -c "$$(sed -n 's/^    \(cc .*\)$$/\1/p' README)" && mv pic ../pic

PIC_AMAL_ENV := PIC="$(CURDIR)/$(PIC_AMAL_OUT)/test/pic" SERVER="$(CURDIR)/build/pic/fake_server" DRIVE="$(CURDIR)/build/pic/pty_drive"
$(PIC_AMAL_RUNS:%=run-amal-%): run-amal-%: $(PIC_AMAL_OUT)/test/pic build/pic/fake_server build/pic/pty_drive build/pic/sqlite3
	@$(PIC_RUN) amal-$* $(PIC_AMAL_ENV) sh test/pic/$*.sh $(if $(filter print,$*),,--no-faults)

# The names check and the bundle's slice; test-pic runs both.
test-amal: amalgam-names $(PIC_AMAL_RUNS:%=run-amal-%)

amalgam-names: src/changelog.inc src/manual.inc
	@$(PIC_RUN) amal-names sh test/pic/amalgam.sh $(PIC_SRC)

# Each source compiles alone with -std=c11 -pedantic and none of PIC_WARN's feature-test macros, as a
# project that vendors it builds it (docs/dev/amalgamation.md).
test-standalone: src/changelog.inc src/manual.inc
	@$(PIC_RUN) standalone sh test/pic/standalone.sh

PIC_VENDOR := vendor/libfossil
# Dependencies are vendored and compiled in (vendor/*/SHA256SUMS); the binary
# links only libc, libm and pthread. No iconv, no -ldl, no system zlib.
PIC_SQLITE := -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_MATH_FUNCTIONS -DSQLITE_THREADSAFE=1 \
	-DSQLITE_OMIT_LOAD_EXTENSION
PIC_VFLAGS := $(PIC_SQLITE) -DHAVE_LIBFOSSIL_CONFIG_H -DWITHOUT_ICONV -DZ_HAVE_UNISTD_H -w \
	-I$(PIC_VENDOR) -Ivendor/zlib
PIC_WARN := -std=c11 -Wall -Wextra -Wpedantic -Werror -Wshadow -Wconversion \
	-Wstrict-prototypes -Wmissing-prototypes -Wvla -Wcast-qual -D_DEFAULT_SOURCE -D_DARWIN_C_SOURCE
# gcc's -Wformat-truncation (no clang equivalent) fires on snprintf into the
# fixed error and display buffers, where a clipped message is the intended
# outcome; its absence from the fatal set keeps gcc builds working. Both
# compilers accept the flag silently. _GNU_SOURCE is what glibc needs to
# declare strptime (Darwin ignores it; it has _DARWIN_C_SOURCE already).
PIC_WARN += -Wno-format-truncation -D_GNU_SOURCE
PIC_INC := -Isrc -isystem $(PIC_VENDOR) -isystem vendor/zlib -isystem vendor/qrcodegen -isystem vendor/quickjs \
	-DHAVE_LIBFOSSIL_CONFIG_H
PIC_SAN := -O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined -fno-sanitize-recover=all
PIC_LIBS := -lm -lpthread
PIC_ZLIB := adler32 compress crc32 deflate inffast inflate inftrees trees uncompr zutil

build/pic/release/vendor/%.o: $(PIC_VENDOR)/%.c
	@mkdir -p $(@D)
	$(CC) -O2 $(PIC_VFLAGS) -c -o $@ $<

build/pic/dev/vendor/%.o: $(PIC_VENDOR)/%.c
	@mkdir -p $(@D)
	$(CC) $(PIC_SAN) $(PIC_VFLAGS) -c -o $@ $<

build/pic/release/vendor/zlib/%.o: vendor/zlib/%.c
	@mkdir -p $(@D)
	$(CC) -O2 $(PIC_VFLAGS) -c -o $@ $<

build/pic/dev/vendor/zlib/%.o: vendor/zlib/%.c
	@mkdir -p $(@D)
	$(CC) $(PIC_SAN) $(PIC_VFLAGS) -c -o $@ $<

# Nayuki's QR Code generator (vendor/qrcodegen): the QR code /remote-control draws.
build/pic/release/vendor/qrcodegen/%.o: vendor/qrcodegen/%.c
	@mkdir -p $(@D)
	$(CC) -O2 -w -c -o $@ $<

build/pic/dev/vendor/qrcodegen/%.o: vendor/qrcodegen/%.c
	@mkdir -p $(@D)
	$(CC) $(PIC_SAN) -w -c -o $@ $<

# Bellard's QuickJS (vendor/quickjs): the codemode tool's JavaScript engine, which runs only in its
# own worker process. Its Makefile builds it with -fwrapv and these two defines.
PIC_QJS := quickjs cutils dtoa libregexp libunicode
PIC_QJSFLAGS := -w -fwrapv -D_GNU_SOURCE -DCONFIG_VERSION=\"$(shell cat vendor/quickjs/VERSION)\"
build/pic/release/vendor/quickjs/%.o: vendor/quickjs/%.c
	@mkdir -p $(@D)
	$(CC) -O2 $(PIC_QJSFLAGS) -c -o $@ $<

build/pic/dev/vendor/quickjs/%.o: vendor/quickjs/%.c
	@mkdir -p $(@D)
	$(CC) $(PIC_SAN) $(PIC_QJSFLAGS) -c -o $@ $<

PIC_REL_V := build/pic/release/vendor/sqlite3.o build/pic/release/vendor/libfossil.o \
	$(PIC_ZLIB:%=build/pic/release/vendor/zlib/%.o) build/pic/release/vendor/qrcodegen/qrcodegen.o \
	$(PIC_QJS:%=build/pic/release/vendor/quickjs/%.o)
PIC_DEV_V := build/pic/dev/vendor/sqlite3.o build/pic/dev/vendor/libfossil.o \
	$(PIC_ZLIB:%=build/pic/dev/vendor/zlib/%.o) build/pic/dev/vendor/qrcodegen/qrcodegen.o \
	$(PIC_QJS:%=build/pic/dev/vendor/quickjs/%.o)

# BearSSL makes no system call of its own: net.c seeds it and gives it the date.
PIC_BRFLAGS := -w -Ivendor/bearssl/inc -Ivendor/bearssl/src -DBR_RDRAND=0 -DBR_USE_URANDOM=0 \
	-DBR_USE_WIN32_RAND=0 -DBR_USE_UNIX_TIME=0 -DBR_USE_WIN32_TIME=0
PIC_BR_SRC := $(sort $(wildcard vendor/bearssl/src/*.c vendor/bearssl/src/*/*.c))
PIC_BR_REL := $(PIC_BR_SRC:vendor/bearssl/src/%.c=build/pic/release/vendor/bearssl/%.o)
PIC_BR_DEV := $(PIC_BR_SRC:vendor/bearssl/src/%.c=build/pic/dev/vendor/bearssl/%.o)

build/pic/release/vendor/bearssl/%.o: vendor/bearssl/src/%.c
	@mkdir -p $(@D)
	$(CC) -O2 $(PIC_BRFLAGS) -c -o $@ $<

build/pic/dev/vendor/bearssl/%.o: vendor/bearssl/src/%.c
	@mkdir -p $(@D)
	$(CC) $(PIC_SAN) $(PIC_BRFLAGS) -c -o $@ $<

build/pic/release/vendor/libbearssl.a: $(PIC_BR_REL)
	ar rcs $@ $^

build/pic/dev/vendor/libbearssl.a: $(PIC_BR_DEV)
	ar rcs $@ $^

PIC_NET := src/net.c src/input.c src/platform.c src/http.c
PIC_NET_INC := -isystem vendor/bearssl/inc

build/pic/dev/test_http: test/pic/test_http.c $(PIC_NET) src/http.h src/net.h build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) -Isrc $(PIC_NET_INC) -o $@ test/pic/test_http.c $(PIC_NET) \
		build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

# Installs the release binary for everyday use, only after the full suite
# passes. Copy then rename, so a pic already running keeps its own binary.
PIC_INSTALL_DIR ?= $(shell printf %s "$$HOME")/.local/bin
install-pic: test-pic
	@mkdir -p $(PIC_INSTALL_DIR)
	cp build/pic/release/pic $(PIC_INSTALL_DIR)/.pic.new
	chmod 755 $(PIC_INSTALL_DIR)/.pic.new
	mv -f $(PIC_INSTALL_DIR)/.pic.new $(PIC_INSTALL_DIR)/pic
	@printf 'installed %s/pic\n' "$(PIC_INSTALL_DIR)"

# pic's own code is one generated translation unit per flavor, made by the generator the release
# bundle uses (docs/dev/amalgamation.md). Dev's has #line markers, so compiler errors, sanitizer
# traces and a debugger name src/FILE.c:LINE; release's has none. Dev adds the sanitizers and
# crash points; release has neither. The unit depends on every source, header and .inc.
build/pic/release/pic.c: $(PIC_SRC) $(PIC_HDR) tools/amalgamate.sh
	@mkdir -p $(@D)
	sh tools/amalgamate.sh $(PIC_SRC) > [email protected] && mv -f [email protected] $@

build/pic/dev/pic.c: $(PIC_SRC) $(PIC_HDR) tools/amalgamate.sh
	@mkdir -p $(@D)
	sh tools/amalgamate.sh -l $(PIC_SRC) > [email protected] && mv -f [email protected] $@

# The same without main.c, for the tests and the benchmark that bring their own main.
PIC_LIB_SRC := $(filter-out src/main.c,$(PIC_SRC))
build/pic/release/pic-lib.c: $(PIC_SRC) $(PIC_HDR) tools/amalgamate.sh
	@mkdir -p $(@D)
	sh tools/amalgamate.sh $(PIC_LIB_SRC) > [email protected] && mv -f [email protected] $@

build/pic/dev/pic-lib.c: $(PIC_SRC) $(PIC_HDR) tools/amalgamate.sh
	@mkdir -p $(@D)
	sh tools/amalgamate.sh -l $(PIC_LIB_SRC) > [email protected] && mv -f [email protected] $@

# Kept after a build: the unit is what a debugger and a compiler error read.
.PRECIOUS: build/pic/release/pic.c build/pic/dev/pic.c build/pic/release/pic-lib.c build/pic/dev/pic-lib.c

build/pic/dev/%.o: build/pic/dev/%.c
	$(CC) $(PIC_WARN) $(PIC_SAN) -DPIC_FAULTS $(PIC_INC) $(PIC_NET_INC) -c -o $@ $<

build/pic/release/%.o: build/pic/release/%.c
	$(CC) $(PIC_WARN) -O2 $(PIC_INC) $(PIC_NET_INC) -c -o $@ $<

# Benchmarks run on release-flag objects, so they measure what users run.
build/pic/pic_bench: bench/pic_bench.c build/pic/release/pic-lib.o $(PIC_REL_V) build/pic/release/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) -O2 $(PIC_INC) $(PIC_NET_INC) -o $@ bench/pic_bench.c build/pic/release/pic-lib.o \
		$(PIC_REL_V) build/pic/release/vendor/libbearssl.a $(PIC_LIBS)

bench-pic: build/pic/pic_bench build/pic/release/pic
	sh bench/pic-bench.sh build/pic/pic_bench build/pic/release/pic

bench-pic-record: build/pic/pic_bench build/pic/release/pic
	sh bench/pic-bench.sh build/pic/pic_bench build/pic/release/pic --record

build/pic/fake_server: test/pic/fake_server.c
	@mkdir -p $(@D)
	$(CC) -std=c11 -Wall -Wextra -Werror -D_DEFAULT_SOURCE -D_DARWIN_C_SOURCE -O1 -o $@ test/pic/fake_server.c

build/pic/fake_mcp: test/pic/fake_mcp.c
	@mkdir -p $(@D)
	$(CC) -std=c11 -Wall -Wextra -Werror -D_DEFAULT_SOURCE -D_DARWIN_C_SOURCE -O1 -o $@ test/pic/fake_mcp.c

build/pic/fake_oauth: test/pic/fake_oauth.c
	@mkdir -p $(@D)
	$(CC) -std=c11 -Wall -Wextra -Werror -D_DEFAULT_SOURCE -D_DARWIN_C_SOURCE -O1 -o $@ test/pic/fake_oauth.c

# The sign-in machinery against the fake authorization server: PKCE, the callback's state check,
# a pasted redirect, the device-code poll. A HOME of its own.
PIC_OAUTH_UNITS := src/oauth.c src/oauth_openai.c src/oauth_openrouter.c src/cred.c src/userdb.c src/sqlar.c src/http.c src/net.c src/input.c src/platform.c
build/pic/dev/test_oauth: test/pic/test_oauth.c $(PIC_OAUTH_UNITS) src/oauth.h src/cred.h src/userdb.h src/http.h src/net.h \
		src/ca_bundle.inc $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) -DPIC_FAULTS $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/test_oauth.c $(PIC_OAUTH_UNITS) \
		$(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

test-oauth: build/pic/dev/test_oauth build/pic/fake_oauth
	rm -rf build/pic/oauth-home && mkdir -p build/pic/oauth-home
	HOME="$(CURDIR)/build/pic/oauth-home" FAKE_OAUTH="$(CURDIR)/build/pic/fake_oauth" build/pic/dev/test_oauth \
		test/pic/responses-streams/reasoning_text.sse

test-http: build/pic/dev/test_http
	build/pic/dev/test_http

build/pic/dev/test_webpush: test/pic/test_webpush.c src/webpush.c src/webpush.h src/noise.c src/noise.h $(PIC_NET) \
		src/http.h src/net.h build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) -Isrc $(PIC_NET_INC) -o $@ test/pic/test_webpush.c src/webpush.c src/noise.c $(PIC_NET) \
		build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

test-webpush: build/pic/dev/test_webpush build/pic/fake_server
	build/pic/dev/test_webpush build/pic/fake_server "$(CURDIR)/build/pic/webpush-work"

build/pic/dev/test_files: test/pic/test_files.c src/tools.c src/image.c src/image.h src/pic.h $(PIC_NET) build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/test_files.c src/tools.c src/image.c $(PIC_NET) \
		build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

test-files: build/pic/dev/test_files
	build/pic/dev/test_files

build/pic/dev/test_prompts: test/pic/test_prompts.c src/prompts.c src/prompts.h src/input.c src/platform.c src/platform.h
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) -Isrc -o $@ test/pic/test_prompts.c src/prompts.c src/input.c src/platform.c

test-prompts: build/pic/dev/test_prompts
	build/pic/dev/test_prompts

build/pic/dev/test_skills: test/pic/test_skills.c src/skills.c src/skills.h src/ignore.c src/ignore.h src/prompts.c src/prompts.h src/input.c src/platform.c src/platform.h src/userdb.c src/userdb.h src/procs.c src/sqlar.c $(PIC_DEV_V)
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) -o $@ test/pic/test_skills.c src/skills.c src/ignore.c src/prompts.c src/input.c src/platform.c src/userdb.c src/procs.c src/sqlar.c $(PIC_DEV_V) $(PIC_LIBS)

test-skills: build/pic/dev/test_skills

# Hooks under an executor without spawn (the no-OS build's shape).
build/pic/dev/test_hooks: test/pic/test_hooks.c build/pic/dev/pic-lib.o $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/test_hooks.c build/pic/dev/pic-lib.o \
		$(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

test-hooks: build/pic/dev/test_hooks
	ASAN_OPTIONS=detect_leaks=0 build/pic/dev/test_hooks
	build/pic/dev/test_skills

# A toy provider adapter registered through provider_http.h alone (docs/dev/providers.md).
build/pic/dev/test_adapter: test/pic/test_adapter.c build/pic/dev/pic-lib.o $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/test_adapter.c build/pic/dev/pic-lib.o \
		$(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

test-adapter: build/pic/dev/test_adapter
	ASAN_OPTIONS=detect_leaks=0 build/pic/dev/test_adapter

build/pic/dev/test_mcp: test/pic/test_mcp.c build/pic/dev/pic-lib.o $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/test_mcp.c build/pic/dev/pic-lib.o \
		$(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

test-mcp: build/pic/dev/test_mcp
	ASAN_OPTIONS=detect_leaks=0 build/pic/dev/test_mcp

build/pic/dev/test_models: test/pic/test_models.c src/model.c src/model.h src/models.c src/models.h src/endpoints.c src/endpoints.h src/userdb.c src/sqlar.c src/catalog.c src/catalog.h src/model_catalog.inc src/cred.c src/cred.h $(PIC_DEV_V) \
		build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/test_models.c src/model.c src/models.c src/endpoints.c src/input.c src/platform.c src/catalog.c src/userdb.c src/sqlar.c \
		src/cred.c $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

test-models: build/pic/dev/test_models
	build/pic/dev/test_models

build/pic/dev/test_userdb: test/pic/test_userdb.c src/userdb.c src/userdb.h src/procs.c src/procs.h src/sqlar.c src/platform.c src/input.c $(PIC_DEV_V)
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) -o $@ test/pic/test_userdb.c src/userdb.c src/procs.c src/sqlar.c src/input.c src/platform.c $(PIC_DEV_V) $(PIC_LIBS)

build/pic/dev/test_cred: test/pic/test_cred.c src/cred.c src/cred.h src/userdb.c src/userdb.h src/sqlar.c src/platform.c src/platform.h src/input.c \
		$(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/test_cred.c src/cred.c src/userdb.c src/sqlar.c \
		src/input.c src/platform.c $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

# The loopback relay and the scripted phone that test/pic/remote.sh drives pic remote with.
build/pic/fake_relay: test/pic/fake_relay.c src/noise.c src/noise.h src/platform.c src/input.c $(PIC_REL_V) build/pic/release/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) -O1 $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/fake_relay.c src/noise.c src/platform.c src/input.c \
		$(PIC_REL_V) build/pic/release/vendor/libbearssl.a $(PIC_LIBS)

build/pic/fake_phone: test/pic/fake_phone.c src/noise.c src/noise.h $(PIC_NET) src/sse.c src/sse.h $(PIC_REL_V) build/pic/release/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) -O1 $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/fake_phone.c src/noise.c src/sse.c $(PIC_NET) \
		$(PIC_REL_V) build/pic/release/vendor/libbearssl.a $(PIC_LIBS)

run-remote-dev run-remote-rel: build/pic/fake_relay build/pic/fake_phone

# pic remote against the real relay Worker under wrangler dev (a pic-relay checkout, PIC_RELAY_DIR,
# default ~/pic-relay), local only; skipped without node or wrangler. Dev build: its passphrase seam.
# pic remote under faults against the test relay (relay and pic remote killed, the network
# dropped, the stream silent, pic remote stopped): every command once, every event in order.
run-remote-soak-dev: build/pic/dev/pic build/pic/fake_server build/pic/fake_relay build/pic/fake_phone build/pic/sqlite3
	@$(PIC_RUN) remote-soak-dev $(PIC_DEV_ENV) SQLITE3="$(CURDIR)/build/pic/sqlite3" sh test/pic/remote-soak.sh

# The checkout is found from the real HOME, read when the Makefile is: the tests' HOME is private.
PIC_RELAY_DIR := $(if $(PIC_RELAY_DIR),$(PIC_RELAY_DIR),$(HOME)/pic-relay)
# node itself, found now: a version manager's shim reads its trust from the real HOME.
PIC_NODE := $(if $(PIC_NODE),$(PIC_NODE),$(shell node -p process.execPath 2> /dev/null))
run-remote-interop-dev: build/pic/dev/pic build/pic/fake_server build/pic/fake_phone build/pic/sqlite3
	@$(PIC_RUN) remote-interop-dev $(PIC_DEV_ENV) SQLITE3="$(CURDIR)/build/pic/sqlite3" PIC_RELAY_DIR="$(PIC_RELAY_DIR)" PIC_NODE="$(PIC_NODE)" \
		sh test/pic/remote-interop.sh

# Remote control's cryptography against the shared vectors (docs/design/remote-protocol.md).
build/pic/dev/test_noise: test/pic/test_noise.c src/noise.c src/noise.h src/platform.c src/platform.h src/input.c \
		$(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/test_noise.c src/noise.c src/input.c src/platform.c \
		$(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

test-noise: build/pic/dev/test_noise
	build/pic/dev/test_noise test/pic/remote-vectors.json

# Exact-capacity tests: every parser and decoder that fills a caller's buffer, into a heap buffer of exactly
# the size it needs, under the sanitizers (docs/dev/testing.md).
# The terminal's units are set here, before their first use, so make sees them as prerequisites.
PIC_TUI_UNITS := src/tui/width.c src/tui/term.c src/tui/render.c src/tui/editor.c src/tui/fuzzy.c src/tui/atfile.c src/tui/view.c src/tui/transcript.c src/tui/viewport.c src/tui/markdown.c src/tui/highlight.c src/tui/theme.c src/tui/themes.c src/tui/keymap.c src/tui/mjson.c src/tui/remote.c src/tui/panel.c src/tui/modelpanel.c src/ignore.c src/input.c src/platform.c
PIC_CAPACITY_UNITS := $(sort src/noise.c src/image.c src/webpush.c src/sse.c src/sqlar.c src/keyhealth.c $(PIC_OAUTH_UNITS) $(PIC_NET) \
	src/provider_http.c src/models.c src/endpoints.c src/prompts.c src/catalog.c src/model.c src/webfetch.c src/htmltext.c $(PIC_TUI_UNITS))
build/pic/dev/test_capacity: test/pic/test_capacity.c $(PIC_CAPACITY_UNITS) $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/test_capacity.c $(PIC_CAPACITY_UNITS) \
		$(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

test-capacity: build/pic/dev/test_capacity
	rm -rf build/pic/capacity-home && mkdir -p build/pic/capacity-home
	HOME="$(CURDIR)/build/pic/capacity-home" build/pic/dev/test_capacity

# A HOME of its own: the store test needs a pic.sqlite with no credentials.
build/pic/dev/test_keyhealth: test/pic/test_keyhealth.c src/keyhealth.c src/keyhealth.h src/cred.h
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) -Isrc -o $@ test/pic/test_keyhealth.c src/keyhealth.c

test-keyhealth: build/pic/dev/test_keyhealth
	TZ=UTC build/pic/dev/test_keyhealth

test-cred: build/pic/dev/test_cred
	rm -rf build/pic/cred-home && mkdir -p build/pic/cred-home
	HOME="$(CURDIR)/build/pic/cred-home" build/pic/dev/test_cred

build/pic/dev/test_xfer: test/pic/test_xfer.c src/xfer.c src/xfer.h src/net.c src/net.h src/input.c src/platform.c $(PIC_DEV_V) \
		build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/test_xfer.c src/xfer.c src/net.c src/input.c \
		src/platform.c $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

# A fresh HOME: the test checks that no password reached ~/.fossil.
test-xfer: build/pic/dev/test_xfer
	rm -rf build/pic/xfer-home && mkdir -p build/pic/xfer-home
	HOME="$(CURDIR)/build/pic/xfer-home" build/pic/dev/test_xfer

build/pic/dev/test_settings: test/pic/test_settings.c src/settings.c src/settings.h src/userdb.c src/userdb.h src/procs.c src/sqlar.c src/platform.c src/input.c src/model.c $(PIC_DEV_V)
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) -o $@ test/pic/test_settings.c src/settings.c src/userdb.c src/procs.c src/sqlar.c src/input.c src/platform.c src/model.c $(PIC_DEV_V) $(PIC_LIBS)

# The settings list's checks and writes, in a HOME of its own.
test-settings: build/pic/dev/test_settings
	rm -rf build/pic/settings-home && mkdir -p build/pic/settings-home
	HOME="$(CURDIR)/build/pic/settings-home" build/pic/dev/test_settings

# A HOME of its own: the busy and contention cases need a fresh pic.sqlite.
test-userdb: build/pic/dev/test_userdb
	rm -rf build/pic/userdb-home && mkdir -p build/pic/userdb-home
	HOME="$(CURDIR)/build/pic/userdb-home" build/pic/dev/test_userdb

# The spike: one live streaming request over pic's own HTTPS stack.
build/pic/dev/live_probe: test/pic/live_probe.c $(PIC_NET) src/net.h src/http.h src/anthropic.c src/sse.c \
		$(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) $(PIC_NET_INC) -o $@ test/pic/live_probe.c $(PIC_NET) \
		src/anthropic.c src/sse.c $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

build/pic/release/pic: build/pic/release/pic.o $(PIC_REL_V) build/pic/release/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) -O2 -o $@ build/pic/release/pic.o $(PIC_REL_V) build/pic/release/vendor/libbearssl.a $(PIC_LIBS)

build/pic/dev/pic: build/pic/dev/pic.o $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a
	@mkdir -p $(@D)
	$(CC) $(PIC_SAN) -o $@ build/pic/dev/pic.o $(PIC_DEV_V) build/pic/dev/vendor/libbearssl.a $(PIC_LIBS)

# The sqlite3 command the tests use, built from the vendored shell.c and
# sqlite3.c (the release object, same flags) with zlib for sqlar_uncompress.
# Tests put build/pic first on PATH, so no host sqlite3 is ever needed.
build/pic/vendor-shell.o: $(PIC_VENDOR)/shell.c
	@mkdir -p $(@D)
	$(CC) -O2 $(PIC_VFLAGS) -DSQLITE_HAVE_ZLIB -c -o $@ $<

build/pic/sqlite3: build/pic/vendor-shell.o build/pic/release/vendor/sqlite3.o $(PIC_ZLIB:%=build/pic/release/vendor/zlib/%.o)
	$(CC) -O2 -o $@ $^ $(PIC_LIBS)

test-sqlite3: build/pic/sqlite3
	sh test/pic/sqlite3.sh build/pic/sqlite3 $(PIC_VENDOR)/sqlite3.h

pic: build/pic/release/pic
pic-dev: build/pic/dev/pic

# The no-OS build's codemode: QuickJS in pic's own process (PIC_CODEMODE_INPROC), since such a
# platform has no processes to put a worker in. The same sources and engine, in a build of its own.
build/pic/inproc/pic.o: build/pic/release/pic.c
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) -O2 -DPIC_CODEMODE_INPROC $(PIC_INC) $(PIC_NET_INC) -c -o $@ build/pic/release/pic.c

build/pic/inproc/pic: build/pic/inproc/pic.o $(PIC_REL_V) build/pic/release/vendor/libbearssl.a
	$(CC) -O2 -o $@ build/pic/inproc/pic.o $(PIC_REL_V) build/pic/release/vendor/libbearssl.a $(PIC_LIBS)

pic-inproc: build/pic/inproc/pic

# Tests and benchmarks never touch the real ~/.pic: a private HOME, no network catalog.
test-amal $(PIC_AMAL_RUNS:%=run-amal-%) test-pic test-pic-run test-sqlite3 test-sse test-tui test-highlight test-decode test-http test-webpush test-files test-models test-userdb test-cred test-keyhealth test-oauth test-xfer test-prompts test-skills test-hooks test-adapter test-provider bench-pic bench-pic-record: export HOME := $(CURDIR)/build/pic
test-amal $(PIC_AMAL_RUNS:%=run-amal-%) test-pic test-pic-run test-sqlite3 test-sse test-tui test-highlight test-decode test-http test-webpush test-files test-models test-userdb test-cred test-xfer test-prompts test-skills test-hooks test-provider bench-pic bench-pic-record: export PIC_MODEL_CATALOG := offline
test-amal $(PIC_AMAL_RUNS:%=run-amal-%) test-pic test-pic-run test-sqlite3 test-sse test-tui test-highlight test-decode test-http test-webpush test-files test-models test-userdb test-cred test-xfer test-prompts test-skills test-hooks test-provider test-sqlite3 bench-pic bench-pic-record: export SQLITE3 := $(CURDIR)/build/pic/sqlite3
test-amal $(PIC_AMAL_RUNS:%=run-amal-%) test-pic test-pic-run test-sqlite3 test-sse test-tui test-highlight test-decode test-http test-webpush test-files test-models test-userdb test-cred test-xfer test-prompts test-skills test-hooks test-provider bench-pic bench-pic-record: export PATH := $(CURDIR)/build/pic:$(PATH)

# test-pic builds everything and runs every suite in parallel, one job per
# core by default (JOBS=1 restores the serial order). Each script owns a
# private temp dir, HOME and server port, so they do not share state; each
# runs under test/run-logged.sh, which keeps its output in build/pic/logs and
# prints the log only when the script fails.
JOBS ?= $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
PIC_LOGDIR := build/pic/logs
PIC_RUN := sh test/run-logged.sh $(PIC_LOGDIR)
PIC_DEV_ENV := PIC_NO_KEY_CHECKS=1 ASAN_OPTIONS=detect_leaks=0 SQLITE3="$(CURDIR)/build/pic/sqlite3" PIC_NO_AUTO_UPDATE=1 PIC="$(CURDIR)/build/pic/dev/pic" RELAY="$(CURDIR)/build/pic/fake_relay" PHONE="$(CURDIR)/build/pic/fake_phone" SERVER="$(CURDIR)/build/pic/fake_server" DRIVE="$(CURDIR)/build/pic/pty_drive" OAUTH="$(CURDIR)/build/pic/fake_oauth" MCP="$(CURDIR)/build/pic/fake_mcp"
PIC_REL_ENV := PIC_NO_KEY_CHECKS=1 SQLITE3="$(CURDIR)/build/pic/sqlite3" PIC_NO_AUTO_UPDATE=1 PIC="$(CURDIR)/build/pic/release/pic" RELAY="$(CURDIR)/build/pic/fake_relay" PHONE="$(CURDIR)/build/pic/fake_phone" SERVER="$(CURDIR)/build/pic/fake_server" DRIVE="$(CURDIR)/build/pic/pty_drive" OAUTH="$(CURDIR)/build/pic/fake_oauth" MCP="$(CURDIR)/build/pic/fake_mcp"

# name:script, with a "!" suffix on the name when the release run takes --no-faults.
PIC_SCRIPTS := crash!:test/pic/crash.sh tools!:test/pic/tools.sh migrate:test/pic/migrate.sh holders!:test/pic/holders.sh folder!:test/pic/folder.sh \
	sessions!:test/pic/sessions.sh compact!:test/pic/compact.sh images!:test/pic/images.sh \
	branch!:test/pic/branch.sh compact_settings:test/pic/compact_settings.sh \
	events:test/pic/events.sh interrupt:test/pic/interrupt.sh steer:test/pic/steer.sh \
	bash:test/pic/bash.sh chat:test/pic/chat.sh export_pi:test/pic/export_pi.sh import_claude:test/pic/import_claude.sh \
	catalog:test/pic/catalog.sh cost:test/pic/cost.sh slash:test/pic/slash.sh identity!:test/pic/identity.sh projects:test/pic/projects.sh \
	prompts:test/pic/prompts.sh skills:test/pic/skills.sh hooks!:test/pic/hooks.sh guard!:test/pic/guard.sh tui!:test/tui/tui.sh paste:test/tui/paste.sh themes:test/tui/themes.sh search:test/tui/search.sh settings:test/tui/settings.sh replies:test/tui/replies.sh mouse!:test/tui/mouse.sh links!:test/tui/links.sh share:test/tui/share.sh \
	provider!:test/pic/provider.sh bench!:test/pic/bench.sh ps:test/pic/ps.sh print:test/pic/print.sh modelchoice:test/pic/modelchoice.sh sessionmodel:test/pic/sessionmodel.sh authcmd!:test/pic/authcmd.sh keyexpiry!:test/pic/keyexpiry.sh endpoints!:test/pic/endpoints.sh toolset:test/pic/toolset.sh codemode!:test/pic/codemode.sh \
	creds!:test/pic/creds.sh keysource!:test/pic/keysource.sh keys!:test/pic/keys.sh probes!:test/pic/probes.sh footerclick!:test/tui/footerclick.sh keyprompt!:test/tui/keyprompt.sh auth!:test/tui/auth.sh mcptui:test/tui/mcp.sh keyprobe:test/tui/keyprobe.sh modelpick:test/tui/modelchoice.sh lastmodel:test/tui/lastmodel.sh oauth!:test/pic/oauth.sh rpc:test/pic/rpc.sh help:test/pic/help.sh parallel!:test/pic/parallel.sh shared!:test/pic/shared.sh \
	rewind!:test/pic/rewind.sh registry!:test/pic/registry.sh mcp!:test/pic/mcp.sh mcpreach:test/pic/mcp_reach.sh mcpmore:test/pic/mcp_more.sh toolsearch!:test/pic/toolsearch.sh grapevine!:test/pic/grapevine.sh remote!:test/pic/remote.sh agent!:test/pic/remote-agent.sh rc!:test/tui/rc.sh wake!:test/pic/wake.sh webfetch!:test/pic/webfetch.sh websearch!:test/pic/websearch.sh websearchnative!:test/pic/websearchnative.sh monitor!:test/pic/monitor.sh goal!:test/pic/goal.sh ask!:test/pic/ask.sh perms!:test/pic/permissions.sh permtui:test/tui/permissions.sh loop!:test/tui/loop.sh update!:test/tui/update.sh ergo!:test/tui/ergo.sh autoupdate:test/pic/autoupdate.sh
PIC_SCRIPT_NAMES := $(foreach e,$(PIC_SCRIPTS),$(subst !,,$(firstword $(subst :, ,$(e)))))
PIC_RUN_TARGETS := $(foreach n,$(PIC_SCRIPT_NAMES),run-$(n)-dev run-$(n)-rel)

define pic_script_rules
run-$(1)-dev: build/pic/dev/pic build/pic/fake_server build/pic/fake_mcp build/pic/fake_oauth build/pic/pty_drive build/pic/sqlite3 build/pic/fake_relay build/pic/fake_phone
	@$$(PIC_RUN) $(1)-dev $$(PIC_DEV_ENV) sh $(2)
run-$(1)-rel: build/pic/release/pic build/pic/fake_server build/pic/fake_mcp build/pic/fake_oauth build/pic/pty_drive build/pic/sqlite3 build/pic/fake_relay build/pic/fake_phone
	@$$(PIC_RUN) $(1)-rel $$(PIC_REL_ENV) sh $(2) $(3)
endef
$(foreach e,$(PIC_SCRIPTS),$(eval $(call pic_script_rules,$(subst !,,$(firstword $(subst :, ,$(e)))),$(lastword $(subst :, ,$(e))),$(if $(findstring !,$(firstword $(subst :, ,$(e)))),--no-faults))))

.PHONY: $(PIC_RUN_TARGETS) run-remote-interop-dev run-remote-soak-dev test-pic-run run-engines pic-inproc
test-pic:
	@rm -rf $(PIC_LOGDIR) && mkdir -p $(PIC_LOGDIR)
	$(MAKE) -k -j$(JOBS) test-pic-run PIC_RELAY_DIR="$(PIC_RELAY_DIR)" PIC_NODE="$(PIC_NODE)"

test-provider: run-provider-dev run-provider-rel

# The release build's half of test-pic: pic update --full runs it before it installs a build over itself.
PIC_REL_TARGETS := $(foreach n,$(PIC_SCRIPT_NAMES),run-$(n)-rel)
# The light gate pic update uses: the docs checks and the quickest release suites that open a journal,
# run pic -p against the fake provider and chat. Trunk's landings already passed everything on the forge.
test-smoke:
	@rm -rf $(PIC_LOGDIR) && mkdir -p $(PIC_LOGDIR)
	$(MAKE) -k test-smoke-run
test-smoke-run: test-docs run-folder-rel run-chat-rel run-print-rel
.PHONY: test-smoke test-smoke-run test-standalone
test-release:
	@rm -rf $(PIC_LOGDIR) && mkdir -p $(PIC_LOGDIR)
	$(MAKE) -k test-release-run
test-release-run: test-docs $(PIC_REL_TARGETS)
.PHONY: test-release test-release-run

# The reference in docs/reference/ against src/, the README budget, links and
# the generated key table (docs/dev/testing.md).
# Check 5 links the core files alone with the vendored libraries (docs/dev/tiers.md).
test-docs: export TIERS_CORE_LINK := $(PIC_REL_V) build/pic/release/vendor/libbearssl.a $(PIC_LIBS)
test-docs: export TIERS_LINK_ROOT := $(CURDIR)
test-docs: $(PIC_REL_V) build/pic/release/vendor/libbearssl.a build/pic/release/pic
	sh test/pic/docs.sh
	sh test/pic/tiers.sh
	sh test/pic/tiers.sh --selftest

# The same codemode scripts on QuickJS in its worker (the release build) and in process (the no-OS shape).
run-engines: build/pic/release/pic build/pic/inproc/pic build/pic/sqlite3
	@$(PIC_RUN) engines $(PIC_REL_ENV) PIC_INPROC="$(CURDIR)/build/pic/inproc/pic" sh test/pic/codemode_engines.sh

test-pic-run: run-engines test-docs test-amal test-standalone test-sse test-tui test-highlight test-decode test-http test-webpush test-files test-models test-userdb test-settings test-cred test-keyhealth test-noise test-capacity test-oauth test-xfer test-prompts test-skills test-hooks test-adapter test-mcp test-sqlite3 $(PIC_RUN_TARGETS) run-remote-interop-dev run-remote-soak-dev

build/pic/dev/test_sse: test/pic/test_sse.c src/sse.c src/sse.h
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) -Isrc -o $@ test/pic/test_sse.c src/sse.c

test-sse: build/pic/dev/test_sse
	build/pic/dev/test_sse

build/pic/dev/test_tui: test/tui/test_tui.c $(PIC_TUI_UNITS) build/pic/dev/vendor/sqlite3.o build/pic/dev/vendor/qrcodegen/qrcodegen.o src/tui/remote.h src/ignore.h src/tui/highlight.h src/tui/width.h src/tui/term.h src/tui/render.h src/tui/editor.h src/tui/view.h src/tui/transcript.h src/tui/viewport.h src/tui/markdown.h src/tui/theme.h src/tui/themes.h src/tui/keymap.h src/tui/mjson.h src/tui/panel.h src/tui/modelpanel.h src/tui/theme_dark.inc src/tui/theme_light.inc
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) -Isrc -isystem $(PIC_VENDOR) -isystem vendor/qrcodegen -o $@ test/tui/test_tui.c $(PIC_TUI_UNITS) build/pic/dev/vendor/sqlite3.o \
		build/pic/dev/vendor/qrcodegen/qrcodegen.o $(PIC_LIBS)

test-tui: build/pic/dev/test_tui
	build/pic/dev/test_tui

build/pic/dev/test_highlight: test/tui/test_highlight.c src/tui/highlight.c src/tui/highlight.h src/tui/width.c src/tui/width.h
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) -Isrc -o $@ test/tui/test_highlight.c src/tui/highlight.c src/tui/width.c

test-highlight: build/pic/dev/test_highlight
	build/pic/dev/test_highlight

build/pic/pty_drive: test/tui/pty_drive.c
	@mkdir -p $(@D)
	$(CC) -std=c11 -Wall -Wextra -Werror -D_DEFAULT_SOURCE -D_DARWIN_C_SOURCE -D_XOPEN_SOURCE=600 -O1 -o $@ test/tui/pty_drive.c

build/pic/dev/decode_stream: test/pic/decode_stream.c src/anthropic.c src/anthropic.h src/openai_chat.c src/openai_chat.h src/openai_responses.c src/openai_responses.h src/gemini.c src/gemini.h \
		src/sse.c src/sse.h $(PIC_DEV_V)
	@mkdir -p $(@D)
	$(CC) $(PIC_WARN) $(PIC_SAN) $(PIC_INC) -o $@ test/pic/decode_stream.c src/anthropic.c src/openai_chat.c src/openai_responses.c src/gemini.c src/sse.c \
		$(PIC_DEV_V) $(PIC_LIBS)

test-decode: build/pic/dev/decode_stream
	sh test/pic/decode.sh build/pic/dev/decode_stream

# libFuzzer needs an LLVM clang (Apple's lacks it). SQLite is rebuilt with the
# same compiler so one sanitizer runtime is linked. FUZZ_SECONDS bounds a run;
# new findings land in build/pic/fuzz/ and become fixtures when fixed.
FUZZ_CC ?= $(firstword $(wildcard /opt/homebrew/opt/llvm/bin/clang /usr/local/opt/llvm/bin/clang) clang)
FUZZ_SECONDS ?= 60
FUZZ_FLAGS := -O1 -g -fsanitize=fuzzer,address,undefined -fno-sanitize-recover=all

build/pic/fuzz/sqlite3.o: $(PIC_VENDOR)/sqlite3.c
	@mkdir -p $(@D)
	$(FUZZ_CC) -O1 -g -fsanitize=fuzzer-no-link,address,undefined $(PIC_SQLITE) -w -c -o $@ $<

build/pic/fuzz/fuzz_decode: test/fuzz/fuzz_decode.c src/anthropic.c src/anthropic.h src/sse.c src/sse.h build/pic/fuzz/sqlite3.o
	$(FUZZ_CC) $(PIC_WARN) $(FUZZ_FLAGS) -Isrc -isystem $(PIC_VENDOR) -o $@ test/fuzz/fuzz_decode.c src/anthropic.c src/sse.c build/pic/fuzz/sqlite3.o -lm -lpthread

fuzz-decode: build/pic/fuzz/fuzz_decode
	@mkdir -p build/pic/fuzz/corpus build/pic/fuzz/findings
	build/pic/fuzz/fuzz_decode -max_total_time=$(FUZZ_SECONDS) -max_len=8192 -timeout=10 -dict=test/fuzz/sse.dict \
		-artifact_prefix=build/pic/fuzz/findings/ build/pic/fuzz/corpus test/fuzz/decode-seeds

build/pic/fuzz/fuzz_http: test/fuzz/fuzz_http.c src/http.c src/http.h src/net.h
	@mkdir -p $(@D)
	$(FUZZ_CC) $(PIC_WARN) $(FUZZ_FLAGS) -Isrc -o $@ test/fuzz/fuzz_http.c src/http.c

fuzz-http: build/pic/fuzz/fuzz_http
	@mkdir -p build/pic/fuzz/http-corpus build/pic/fuzz/findings
	build/pic/fuzz/fuzz_http -max_total_time=$(FUZZ_SECONDS) -max_len=4096 -timeout=10 -dict=test/fuzz/http.dict \
		-artifact_prefix=build/pic/fuzz/findings/http- build/pic/fuzz/http-corpus test/fuzz/http-seeds

build/pic/fuzz/fuzz_chat: test/fuzz/fuzz_chat.c src/openai_chat.c src/openai_chat.h src/sse.c src/sse.h build/pic/fuzz/sqlite3.o
	$(FUZZ_CC) $(PIC_WARN) $(FUZZ_FLAGS) -Isrc -isystem $(PIC_VENDOR) -o $@ test/fuzz/fuzz_chat.c src/openai_chat.c src/sse.c \
		build/pic/fuzz/sqlite3.o -lm -lpthread

fuzz-chat: build/pic/fuzz/fuzz_chat
	@mkdir -p build/pic/fuzz/chat-corpus build/pic/fuzz/findings
	build/pic/fuzz/fuzz_chat -max_total_time=$(FUZZ_SECONDS) -max_len=8192 -timeout=10 -dict=test/fuzz/sse.dict \
		-artifact_prefix=build/pic/fuzz/findings/chat- build/pic/fuzz/chat-corpus test/fuzz/chat-seeds

build/pic/fuzz/fuzz_responses: test/fuzz/fuzz_responses.c src/openai_responses.c src/openai_responses.h src/sse.c src/sse.h \
		build/pic/fuzz/sqlite3.o
	$(FUZZ_CC) $(PIC_WARN) $(FUZZ_FLAGS) -Isrc -isystem $(PIC_VENDOR) -o $@ test/fuzz/fuzz_responses.c \
		src/openai_responses.c src/sse.c build/pic/fuzz/sqlite3.o -lm -lpthread

fuzz-responses: build/pic/fuzz/fuzz_responses
	@mkdir -p build/pic/fuzz/responses-corpus build/pic/fuzz/findings
	build/pic/fuzz/fuzz_responses -max_total_time=$(FUZZ_SECONDS) -max_len=8192 -timeout=10 -dict=test/fuzz/sse.dict \
		-artifact_prefix=build/pic/fuzz/findings/responses- build/pic/fuzz/responses-corpus test/fuzz/responses-seeds