test: add shared OpenClaw test-state harness · openclaw/openclaw@0bc8b9a
steipete
·
2026-04-28
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ title: "Tests"
|
16 | 16 | - `pnpm check:changed`: runs the smart changed check gate for the diff against `origin/main`. It runs typecheck, lint, and guard commands for the affected architectural lanes, but does not run Vitest tests. Use `pnpm test:changed` or explicit `pnpm test <target>` for test proof. |
17 | 17 | - `pnpm test`: routes explicit file/directory targets through scoped Vitest lanes. Untargeted runs use fixed shard groups and expand to leaf configs for local parallel execution; the extension group always expands to the per-extension shard configs instead of one giant root-project process. |
18 | 18 | - Test wrapper runs end with a short `[test] passed|failed|skipped ... in ...` summary. Vitest's own duration line stays the per-shard detail. |
| 19 | +- Shared OpenClaw test state: use `src/test-utils/openclaw-test-state.ts` from Vitest when a test needs an isolated `HOME`, `OPENCLAW_STATE_DIR`, `OPENCLAW_CONFIG_PATH`, config fixture, workspace, agent dir, or auth-profile store. Docker/Bash E2E lanes can use `scripts/lib/openclaw-test-state.mjs shell --label <name> --scenario <name>` for an in-container shell snippet, or `node scripts/lib/openclaw-test-state.mjs -- create --label <name> --scenario <name> --env-file <path> --json` for a sourceable host env file. The `--` before `create` keeps newer Node runtimes from treating `--env-file` as a Node flag. |
19 | 20 | - Full, extension, and include-pattern shard runs update local timing data in `.artifacts/vitest-shard-timings.json`; later whole-config runs use those timings to balance slow and fast shards. Include-pattern CI shards append the shard name to the timing key, which keeps filtered shard timings visible without replacing whole-config timing data. Set `OPENCLAW_TEST_PROJECTS_TIMINGS=0` to ignore the local timing artifact. |
20 | 21 | - Selected `plugin-sdk` and `commands` test files now route through dedicated light lanes that keep only `test/setup.ts`, leaving runtime-heavy cases on their existing lanes. |
21 | 22 | - Source files with sibling tests map to that sibling before falling back to wider directory globs. Helper edits under `src/channels/plugins/contracts/test-helpers`, `src/plugin-sdk/test-helpers`, and `src/plugins/contracts` use a local import graph to run importing tests instead of broad-running every shard when the dependency path is precise. |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4,14 +4,22 @@ set -euo pipefail
|
4 | 4 | ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
5 | 5 | source "$ROOT_DIR/scripts/lib/docker-e2e-image.sh" |
6 | 6 | IMAGE_NAME="$(docker_e2e_resolve_image "openclaw-onboard-e2e" OPENCLAW_ONBOARD_E2E_IMAGE)" |
| 7 | +OPENCLAW_TEST_STATE_FUNCTION_B64="$( |
| 8 | + node "$ROOT_DIR/scripts/lib/openclaw-test-state.mjs" shell-function \ |
| 9 | + | base64 \ |
| 10 | + | tr -d '\n' |
| 11 | +)" |
7 | 12 | |
8 | 13 | docker_e2e_build_or_reuse "$IMAGE_NAME" onboard |
9 | 14 | |
10 | 15 | echo "Running onboarding E2E..." |
11 | | -docker run --rm -t "$IMAGE_NAME" bash -lc ' |
| 16 | +docker run --rm -t \ |
| 17 | + -e "OPENCLAW_TEST_STATE_FUNCTION_B64=$OPENCLAW_TEST_STATE_FUNCTION_B64" \ |
| 18 | +"$IMAGE_NAME" bash -lc ' |
12 | 19 | set -euo pipefail |
13 | 20 | trap "" PIPE |
14 | 21 | export TERM=xterm-256color |
| 22 | + eval "$(printf "%s" "${OPENCLAW_TEST_STATE_FUNCTION_B64:?missing OPENCLAW_TEST_STATE_FUNCTION_B64}" | base64 -d)" |
15 | 23 | ONBOARD_FLAGS="--flow quickstart --auth-choice skip --skip-channels --skip-skills --skip-daemon --skip-ui" |
16 | 24 | # tsdown may emit dist/index.js or dist/index.mjs depending on runtime/bundler. |
17 | 25 | if [ -f dist/index.mjs ]; then |
@@ -221,12 +229,8 @@ TRASH
|
221 | 229 | } |
222 | 230 | |
223 | 231 | set_isolated_openclaw_env() { |
224 | | - local home_dir="$1" |
225 | | - export HOME="$home_dir" |
226 | | - export OPENCLAW_HOME="$home_dir" |
227 | | - export OPENCLAW_STATE_DIR="$home_dir/.openclaw" |
228 | | - export OPENCLAW_CONFIG_PATH="$OPENCLAW_STATE_DIR/openclaw.json" |
229 | | - mkdir -p "$OPENCLAW_STATE_DIR" |
| 232 | + local label="$1" |
| 233 | + openclaw_test_state_create "$label" empty |
230 | 234 | } |
231 | 235 | |
232 | 236 | assert_file() { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,13 @@ SKIP_BUILD="${OPENCLAW_UPDATE_CHANNEL_SWITCH_E2E_SKIP_BUILD:-0}"
|
12 | 12 | PACKAGE_TGZ="$(docker_e2e_prepare_package_tgz update-channel-switch "${OPENCLAW_CURRENT_PACKAGE_TGZ:-}")" |
13 | 13 | # Bare lanes mount the package artifact instead of baking app sources into the image. |
14 | 14 | docker_e2e_package_mount_args "$PACKAGE_TGZ" |
| 15 | +OPENCLAW_TEST_STATE_SCRIPT_B64="$( |
| 16 | + node "$ROOT_DIR/scripts/lib/openclaw-test-state.mjs" shell \ |
| 17 | + --label update-channel-switch \ |
| 18 | + --scenario update-stable \ |
| 19 | + | base64 \ |
| 20 | + | tr -d '\n' |
| 21 | +)" |
15 | 22 | |
16 | 23 | docker_e2e_build_or_reuse "$IMAGE_NAME" update-channel-switch "$ROOT_DIR/scripts/e2e/Dockerfile" "$ROOT_DIR" "bare" "$SKIP_BUILD" |
17 | 24 | |
@@ -20,6 +27,7 @@ docker run --rm \
|
20 | 27 | -e COREPACK_ENABLE_DOWNLOAD_PROMPT=0 \ |
21 | 28 | -e OPENCLAW_SKIP_CHANNELS=1 \ |
22 | 29 | -e OPENCLAW_SKIP_PROVIDERS=1 \ |
| 30 | + -e "OPENCLAW_TEST_STATE_SCRIPT_B64=$OPENCLAW_TEST_STATE_SCRIPT_B64" \ |
23 | 31 | "${DOCKER_E2E_PACKAGE_ARGS[@]}" \ |
24 | 32 | "$IMAGE_NAME" \ |
25 | 33 | bash -lc 'set -euo pipefail |
@@ -156,17 +164,7 @@ NODE
|
156 | 164 | )" |
157 | 165 | export OPENCLAW_PACKAGE_ACCEPTANCE_LEGACY_COMPAT |
158 | 166 | |
159 | | -home_dir="$(mktemp -d /tmp/openclaw-update-channel-switch-home.XXXXXX)" |
160 | | -export HOME="$home_dir" |
161 | | -mkdir -p "$HOME/.openclaw" |
162 | | -cat > "$HOME/.openclaw/openclaw.json" <<'"'"'JSON'"'"' |
163 | | -{ |
164 | | - "update": { |
165 | | - "channel": "stable" |
166 | | - }, |
167 | | - "plugins": {} |
168 | | -} |
169 | | -JSON |
| 167 | +eval "$(printf "%s" "${OPENCLAW_TEST_STATE_SCRIPT_B64:?missing OPENCLAW_TEST_STATE_SCRIPT_B64}" | base64 -d)" |
170 | 168 | |
171 | 169 | export OPENCLAW_GIT_DIR="$git_root" |
172 | 170 | export OPENCLAW_UPDATE_DEV_TARGET_REF="$fixture_sha" |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。