Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

5.3 KB
Raw Blame
#!/bin/bash

# Standalone end-to-end plugin integration test.
#
# This builds JavaScript examples from the Owncast plugin SDK and a small Rust
# fixture directly to wasm. It installs all of them into a real Owncast
# instance, then exercises chat and HTTP through them. Run by CI via the
# Earthly `+plugin-tests` target.
#
# The feature examples come from the SDK so this suite does not duplicate their
# source. The native-wasm fixture lives here because it tests the core loader
# without a language SDK or runtime script.
#
# Env overrides:
#   PLUGIN_SDK_DIR    path to an existing SDK checkout; if set, no clone happens
#                     (use this for local runs: PLUGIN_SDK_DIR=~/src/plugin-sdk ./run.sh)
#   PLUGIN_SDK_REPO   git URL of the plugin SDK (default: owncast/plugin-sdk)
#   PLUGIN_SDK_REF    branch/tag/sha to build from (default: main)

set -e

# When a git hook (lefthook pre-push) runs this script, git exports GIT_DIR
# and friends pointing at this repository. They leak into every git command
# below, so the SDK clone's `git checkout` would target the Owncast repo
# instead of the clone. From a git worktree GIT_DIR is an absolute path and
# the checkout fails outright. Drop them so git commands resolve their
# repository from the working directory.
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE

# shellcheck disable=SC1091  # tools.sh is sourced at runtime; not available to the linter
source ../tools.sh

REPO_ROOT="$(git rev-parse --show-toplevel)"
PLUGIN_SDK_DIR="${PLUGIN_SDK_DIR:-}"
PLUGIN_SDK_REPO="${PLUGIN_SDK_REPO:-https://github.com/owncast/plugin-sdk}"
# Always build the examples from the latest SDK code. The host and the SDK
# share a contract (the host-function ABI, the register()/manifest agreement,
# the UI hooks); pinning to an old SDK revision hides breakage on either side.
# Tracking main means a host change that diverges from the SDK — or an SDK
# change that diverges from the host — fails this suite instead of silently
# drifting. Override with PLUGIN_SDK_REF to reproduce against a specific ref.
PLUGIN_SDK_REF="${PLUGIN_SDK_REF:-main}"

# SDK example plugins exercised by the tests in this directory.
PLUGIN_NAMES=(profanity-filter echo-bot overlay styles-demo scripts-demo viewer-gate page-content-demo tabs-demo)
NATIVE_WASM_PROJECT="${REPO_ROOT}/test/automated/plugins/native-wasm-plugin"

PLUGIN_DIR="${REPO_ROOT}/data/plugins"

# Resolve the SDK source: an existing local checkout, or a fresh clone.
if [[ -n "$PLUGIN_SDK_DIR" ]]; then
	echo "Using local plugin SDK at ${PLUGIN_SDK_DIR}"
	SDK_DIR="$PLUGIN_SDK_DIR"
	CLONED_SDK=0
else
	echo "Cloning the plugin SDK (${PLUGIN_SDK_REPO}@${PLUGIN_SDK_REF})..."
	SDK_DIR="$(mktemp -d)"
	CLONED_SDK=1
	# Never block on an interactive credential prompt — fail clearly instead.
	export GIT_TERMINAL_PROMPT=0
	git clone "$PLUGIN_SDK_REPO" "$SDK_DIR"
	(
		cd "$SDK_DIR"
		git checkout "$PLUGIN_SDK_REF"
	)
fi

# Tear down the installed plugins (and the SDK clone, but never a local SDK
# checkout the caller pointed us at) on top of tools.sh's server/database
# cleanup, which `finish` performs.
plugins_finish() {
	rm -rf "$PLUGIN_DIR"
	if [[ "$CLONED_SDK" == "1" ]]; then
		rm -rf "$SDK_DIR"
	fi
	finish
}
trap plugins_finish EXIT

# Install the SDK package's own dependencies. The build CLI (owncast-plugin)
# runs from sdks/js/bin and requires esbuild/jszip resolvable from there, and
# its postinstall fetches extism-js + binaryen. build-plugin.sh only installs
# each example's deps, not the SDK package's, so do it here once up front.
echo "Installing plugin SDK build dependencies..."
(cd "${SDK_DIR}/sdks/js" && npm install --no-audit --no-fund)

# Package examples with a host binary built against this core checkout. The
# released binary can lag pre-release manifest changes shared with SDK main.
(
	HOST_WORK_DIR="$(mktemp -d)"
	trap 'rm -rf "$HOST_WORK_DIR"' EXIT
	cd "$HOST_WORK_DIR"
	go work init "$REPO_ROOT" "${SDK_DIR}/host-runtime"
	cd "${SDK_DIR}/host-runtime"
	GOWORK="${HOST_WORK_DIR}/go.work" go build \
		-o "${SDK_DIR}/sdks/js/bin/.cache/owncast-plugin-test" \
		./cmd/owncast-plugin-test
)

echo "Building example plugins..."
rm -rf "$PLUGIN_DIR"
mkdir -p "$PLUGIN_DIR"
for name in "${PLUGIN_NAMES[@]}"; do
	echo "  building ${name}..."
	# build-plugin.sh runs `npm install` (whose postinstall fetches extism-js
	# and friends) then `npm run build`, emitting an .ocpkg into the SDK's
	# plugins/ directory.
	(cd "$SDK_DIR" && ./tools/build-plugin.sh "examples/js/${name}")
	cp "${SDK_DIR}/plugins/${name}.ocpkg" "${PLUGIN_DIR}/"
done

NATIVE_WASM_ARTIFACT="${NATIVE_WASM_PROJECT}/native_wasm_test.wasm"
if [[ ! -f "$NATIVE_WASM_ARTIFACT" ]]; then
	echo "Building native wasm fixture..."
	(
		cd "$NATIVE_WASM_PROJECT"
		cargo build --locked --release --target wasm32-unknown-unknown
	)
	NATIVE_WASM_ARTIFACT="${NATIVE_WASM_PROJECT}/target/wasm32-unknown-unknown/release/native_wasm_test.wasm"
fi
cp "$NATIVE_WASM_ARTIFACT" "${PLUGIN_DIR}/native-wasm-test.wasm"
cp "${NATIVE_WASM_PROJECT}/plugin.manifest.json" \
	"${PLUGIN_DIR}/native-wasm-test.manifest.json"

# Install the JS test framework for this suite.
npm install --quiet --no-progress

install_ffmpeg
start_owncast

# Run the tests against the running instance. Also write machine-readable
# results for CI artifact upload; jest keeps human-readable progress on stderr.
mkdir -p results
npm test -- --json --outputFile=results/jest-results.json