|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 5 | +source "$ROOT_DIR/scripts/lib/docker-e2e-image.sh" |
| 6 | + |
| 7 | +BASE_IMAGE="$(docker_e2e_resolve_image "openclaw-browser-plugin-profiles-base-e2e" OPENCLAW_BROWSER_PLUGIN_PROFILES_BASE_IMAGE)" |
| 8 | +IMAGE_NAME="${OPENCLAW_BROWSER_PLUGIN_PROFILES_IMAGE:-openclaw-browser-plugin-profiles-e2e:${OPENCLAW_DOCKER_ALL_LANE_NAME:-local}}" |
| 9 | +CONTAINER_NAME="openclaw-browser-plugin-profiles-e2e-$$" |
| 10 | +BUILD_DIR="" |
| 11 | +PORT="18789" |
| 12 | +TOKEN="browser-plugin-profiles-token" |
| 13 | +PROFILE="qa-browser" |
| 14 | +DOCKER_COMMAND_TIMEOUT="${OPENCLAW_BROWSER_PLUGIN_PROFILES_DOCKER_TIMEOUT:-1200s}" |
| 15 | +PLAYWRIGHT_CORE_VERSION="$( |
| 16 | + node -p 'require(process.argv[1]).dependencies["playwright-core"]' \ |
| 17 | + "$ROOT_DIR/extensions/browser/package.json" |
| 18 | +)" |
| 19 | + |
| 20 | +cleanup() { |
| 21 | + if [ -n "$CONTAINER_NAME" ] && docker_e2e_docker_cmd inspect "$CONTAINER_NAME" >/dev/null 2>&1; then |
| 22 | + docker_e2e_docker_cmd rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true |
| 23 | + fi |
| 24 | + if [ -n "$BUILD_DIR" ]; then |
| 25 | + rm -rf "$BUILD_DIR" |
| 26 | + fi |
| 27 | +} |
| 28 | +trap cleanup EXIT INT TERM |
| 29 | + |
| 30 | +if [ "${OPENCLAW_BROWSER_PLUGIN_PROFILES_SKIP_BUILD:-0}" = "1" ]; then |
| 31 | + docker_e2e_docker_cmd image inspect "$IMAGE_NAME" >/dev/null |
| 32 | +else |
| 33 | + docker_e2e_build_or_reuse "$BASE_IMAGE" browser-plugin-profiles-base |
| 34 | + BUILD_DIR="$(mktemp -d "${TMPDIR:-/tmp}/openclaw-browser-plugin-profiles-build.XXXXXX")" |
| 35 | + cat >"$BUILD_DIR/Dockerfile" <<EOF |
| 36 | +FROM $BASE_IMAGE |
| 37 | +USER root |
| 38 | +ARG EXPECTED_PLAYWRIGHT_CORE |
| 39 | +ENV PLAYWRIGHT_BROWSERS_PATH=/home/appuser/.cache/ms-playwright |
| 40 | +RUN test "\$(node -p 'require("/app/node_modules/playwright-core/package.json").version')" = "\$EXPECTED_PLAYWRIGHT_CORE" \ |
| 41 | + && mkdir -p "\$PLAYWRIGHT_BROWSERS_PATH" \ |
| 42 | + && DEBIAN_FRONTEND=noninteractive node /app/node_modules/playwright-core/cli.js install --with-deps chromium \ |
| 43 | + && chown -R appuser:appuser "\$PLAYWRIGHT_BROWSERS_PATH" |
| 44 | +USER appuser |
| 45 | +EOF |
| 46 | + docker_build_run browser-plugin-profiles-build \ |
| 47 | + --build-arg "EXPECTED_PLAYWRIGHT_CORE=$PLAYWRIGHT_CORE_VERSION" \ |
| 48 | + -t "$IMAGE_NAME" -f "$BUILD_DIR/Dockerfile" "$BUILD_DIR" |
| 49 | +fi |
| 50 | + |
| 51 | +OPENCLAW_TEST_STATE_SCRIPT_B64="$(docker_e2e_test_state_shell_b64 browser-plugin-profiles empty)" |
| 52 | +docker_e2e_harness_mount_args |
| 53 | +docker_e2e_docker_cmd run -d \ |
| 54 | + "${DOCKER_E2E_HARNESS_ARGS[@]}" \ |
| 55 | + --name "$CONTAINER_NAME" \ |
| 56 | + -e COREPACK_ENABLE_DOWNLOAD_PROMPT=0 \ |
| 57 | + -e OPENCLAW_DISABLE_BONJOUR=1 \ |
| 58 | + -e OPENCLAW_GATEWAY_TOKEN="$TOKEN" \ |
| 59 | + -e OPENCLAW_SKIP_CANVAS_HOST=1 \ |
| 60 | + -e OPENCLAW_SKIP_CHANNELS=1 \ |
| 61 | + -e OPENCLAW_SKIP_CRON=1 \ |
| 62 | + -e OPENCLAW_SKIP_GMAIL_WATCHER=1 \ |
| 63 | + -e OPENCLAW_SKIP_PROVIDERS=1 \ |
| 64 | + -e "OPENCLAW_TEST_STATE_SCRIPT_B64=$OPENCLAW_TEST_STATE_SCRIPT_B64" \ |
| 65 | + "$IMAGE_NAME" \ |
| 66 | + bash -lc "set -euo pipefail |
| 67 | +source scripts/lib/openclaw-e2e-instance.sh |
| 68 | +openclaw_e2e_eval_test_state_from_b64 \"\${OPENCLAW_TEST_STATE_SCRIPT_B64:?missing test state}\" |
| 69 | +openclaw_e2e_write_state_env |
| 70 | +source /tmp/openclaw-test-state-env |
| 71 | +test -z \"\${OPENCLAW_EAGER_BROWSER_CONTROL_SERVER:-}\" |
| 72 | +entry=\"\$(openclaw_e2e_resolve_entrypoint)\" |
| 73 | +node \"\$entry\" config set browser.enabled true >/dev/null |
| 74 | +node \"\$entry\" config set browser.noSandbox true >/dev/null |
| 75 | +openclaw_e2e_exec_gateway \"\$entry\" $PORT loopback /tmp/browser-plugin-profiles-gateway.log" >/dev/null |
| 76 | + |
| 77 | +if ! docker_e2e_wait_container_bash "$CONTAINER_NAME" 240 0.5 \ |
| 78 | + "source scripts/lib/openclaw-e2e-instance.sh; openclaw_e2e_probe_tcp 127.0.0.1 $PORT"; then |
| 79 | + echo "Packaged browser Gateway failed to become ready" >&2 |
| 80 | + docker_e2e_tail_container_file_if_running \ |
| 81 | + "$CONTAINER_NAME" "/tmp/browser-plugin-profiles-gateway.log" 160 |
| 82 | + exit 1 |
| 83 | +fi |
| 84 | + |
| 85 | +if docker_e2e_docker_cmd exec "$CONTAINER_NAME" \ |
| 86 | + grep -q "Browser control service ready" /tmp/browser-plugin-profiles-gateway.log; then |
| 87 | + echo "Browser control service started eagerly" >&2 |
| 88 | + exit 1 |
| 89 | +fi |
| 90 | + |
| 91 | +if [ -z "$BUILD_DIR" ]; then |
| 92 | + BUILD_DIR="$(mktemp -d "${TMPDIR:-/tmp}/openclaw-browser-plugin-profiles-run.XXXXXX")" |
| 93 | +fi |
| 94 | +LIFECYCLE_SCRIPT="$BUILD_DIR/profile-lifecycle.sh" |
| 95 | +cat >"$LIFECYCLE_SCRIPT" <<'CONTAINER' |
| 96 | +set -euo pipefail |
| 97 | +PORT="$1" |
| 98 | +TOKEN="$2" |
| 99 | +PROFILE="$3" |
| 100 | +source /tmp/openclaw-test-state-env |
| 101 | +source scripts/lib/openclaw-e2e-instance.sh |
| 102 | +entry="$(openclaw_e2e_resolve_entrypoint)" |
| 103 | +base=(--url "ws://127.0.0.1:$PORT" --token "$TOKEN" --json) |
| 104 | +browser() { node "$entry" browser "${base[@]}" "$@"; } |
| 105 | +profile() { node "$entry" browser "${base[@]}" --browser-profile "$PROFILE" "$@"; } |
| 106 | +
|
| 107 | +browser profiles >/tmp/profiles-initial.json |
| 108 | +node -e 'const d=JSON.parse(require("node:fs").readFileSync(process.argv[1])); if (!Array.isArray(d.profiles) || d.profiles.some((p) => p.name === process.argv[2])) throw new Error("profile unexpectedly present")' \ |
| 109 | + /tmp/profiles-initial.json "$PROFILE" |
| 110 | +
|
| 111 | +browser create-profile --name "$PROFILE" --color "#1A73E8" >/tmp/profile-created.json |
| 112 | +browser profiles >/tmp/profiles-created.json |
| 113 | +node -e 'const d=JSON.parse(require("node:fs").readFileSync(process.argv[1])); if (!d.profiles.some((p) => p.name === process.argv[2])) throw new Error("created profile missing")' \ |
| 114 | + /tmp/profiles-created.json "$PROFILE" |
| 115 | +
|
| 116 | +profile status >/tmp/profile-stopped.json |
| 117 | +node -e 'const d=JSON.parse(require("node:fs").readFileSync(process.argv[1])); if (d.profile !== process.argv[2] || d.running) throw new Error("selected profile was not stopped")' \ |
| 118 | + /tmp/profile-stopped.json "$PROFILE" |
| 119 | +
|
| 120 | +profile start --headless >/tmp/profile-started.json |
| 121 | +profile status >/tmp/profile-running.json |
| 122 | +BROWSER_PID="$(node -p 'JSON.parse(require("node:fs").readFileSync(process.argv[1])).pid' /tmp/profile-running.json)" |
| 123 | +CDP_PORT="$(node -p 'JSON.parse(require("node:fs").readFileSync(process.argv[1])).cdpPort' /tmp/profile-running.json)" |
| 124 | +node -e 'const d=JSON.parse(require("node:fs").readFileSync(process.argv[1])); if (!d.running || !d.headless || !d.cdpReady || !Number.isInteger(d.pid) || !Number.isInteger(d.cdpPort)) throw new Error("headless browser status incomplete")' \ |
| 125 | + /tmp/profile-running.json |
| 126 | +kill -0 "$BROWSER_PID" |
| 127 | +node --input-type=module -e 'const response=await fetch(`http://127.0.0.1:${process.argv[1]}/json/version`); const body=await response.json(); if (!response.ok || typeof body.webSocketDebuggerUrl !== "string") throw new Error("CDP /json/version unavailable")' \ |
| 128 | + "$CDP_PORT" |
| 129 | +
|
| 130 | +profile stop >/tmp/profile-stopped-final.json |
| 131 | +for _ in $(seq 1 80); do |
| 132 | + if ! kill -0 "$BROWSER_PID" 2>/dev/null && |
| 133 | + ! openclaw_e2e_probe_tcp 127.0.0.1 "$CDP_PORT" 2>/dev/null; then |
| 134 | + break |
| 135 | + fi |
| 136 | + sleep 0.25 |
| 137 | +done |
| 138 | +! kill -0 "$BROWSER_PID" 2>/dev/null |
| 139 | +! openclaw_e2e_probe_tcp 127.0.0.1 "$CDP_PORT" 2>/dev/null |
| 140 | +profile status >/tmp/profile-closed.json |
| 141 | +node -e 'const d=JSON.parse(require("node:fs").readFileSync(process.argv[1])); if (d.running || d.cdpReady) throw new Error("browser remained open after stop")' \ |
| 142 | + /tmp/profile-closed.json |
| 143 | +
|
| 144 | +browser delete-profile --name "$PROFILE" >/tmp/profile-deleted.json |
| 145 | +browser profiles >/tmp/profiles-final.json |
| 146 | +node -e 'const d=JSON.parse(require("node:fs").readFileSync(process.argv[1])); if (d.profiles.some((p) => p.name === process.argv[2])) throw new Error("deleted profile remained listed")' \ |
| 147 | + /tmp/profiles-final.json "$PROFILE" |
| 148 | +printf 'profile=%s browserPid=%s cdpPort=%s playwrightCore=%s\n' \ |
| 149 | + "$PROFILE" "$BROWSER_PID" "$CDP_PORT" "$(node -p 'require("/app/node_modules/playwright-core/package.json").version')" |
| 150 | +CONTAINER |
| 151 | +docker_e2e_docker_cmd cp \ |
| 152 | + "$LIFECYCLE_SCRIPT" "$CONTAINER_NAME:/tmp/browser-plugin-profiles-lifecycle.sh" |
| 153 | + |
| 154 | +if ! docker_e2e_docker_cmd exec "$CONTAINER_NAME" \ |
| 155 | + bash /tmp/browser-plugin-profiles-lifecycle.sh "$PORT" "$TOKEN" "$PROFILE" |
| 156 | +then |
| 157 | + echo "Packaged browser plugin profile lifecycle failed" >&2 |
| 158 | + docker_e2e_tail_container_file_if_running \ |
| 159 | + "$CONTAINER_NAME" "/tmp/browser-plugin-profiles-gateway.log" 200 |
| 160 | + exit 1 |
| 161 | +fi |
| 162 | + |
| 163 | +READY_COUNT="$( |
| 164 | + docker_e2e_docker_cmd exec "$CONTAINER_NAME" \ |
| 165 | + grep -c "Browser control service ready" /tmp/browser-plugin-profiles-gateway.log || true |
| 166 | +)" |
| 167 | +if [ "$READY_COUNT" != "1" ]; then |
| 168 | + echo "Expected exactly one lazy browser control service start, got $READY_COUNT" >&2 |
| 169 | + exit 1 |
| 170 | +fi |
| 171 | + |
| 172 | +docker_e2e_docker_cmd stop --time 20 "$CONTAINER_NAME" >/dev/null |
| 173 | +if [ "$(docker_e2e_docker_cmd inspect --format '{{.State.Running}}' "$CONTAINER_NAME")" != "false" ]; then |
| 174 | + echo "Gateway container remained running after graceful stop" >&2 |
| 175 | + exit 1 |
| 176 | +fi |
| 177 | +docker_e2e_docker_cmd rm "$CONTAINER_NAME" >/dev/null |
| 178 | +CONTAINER_NAME="" |
| 179 | + |
| 180 | +echo "BROWSER_PLUGIN_PROFILES_PACKAGED_OK" |
0 commit comments