#!/bin/sh
# Runs the flag testing examples with networking disabled and every network attempt logged
# (net, dns, http, https and fetch are wrapped by no-network.cjs), then a control run that should attempt requests.
# Usage: sh benchmarks/testing-flags/run.sh > benchmarks/testing-flags/results-network-none.txt
set -e
DIR=$(cd "$(dirname "$0")" && pwd)
cd "$DIR" && npm ci --silent >&2
IMAGE=node:22-alpine
echo "# $IMAGE, docker --network none, NODE_OPTIONS=--require ./no-network.cjs ($(date -u +%Y-%m-%dT%H:%M:%SZ))"
docker run --rm --network none -v "$DIR:/t" -w /t -e NODE_OPTIONS=--require=/t/no-network.cjs "$IMAGE" sh -c '
  node --version
  echo "## test suite"
  node --test --test-reporter=tap *.test.js > /tmp/out.txt 2>&1 || true
  grep -E "^(ok|not ok|# (tests|pass|fail))" /tmp/out.txt
  echo "network attempts during the suite: $(grep -c "NETWORK ATTEMPT" /tmp/out.txt || true)"
  echo "## control: LaunchDarkly TestData with default options"
  node control/launchdarkly-default-options.mjs > /tmp/control.txt 2>&1 || true
  echo "network attempts in the control: $(grep -c "NETWORK ATTEMPT" /tmp/control.txt || true)"
  grep "NETWORK ATTEMPT" /tmp/control.txt | sort -u | head -5
'
