Skip to content

test(fence): 四个写数据目录的测试文件在围栏外直接拒跑 - #3047

Merged
deepcoldy merged 1 commit into
masterfrom
test/fence-four-files-2998
Nov 7, 2026
Merged

deepcoldy merged 1 commit into
masterfrom
test/fence-four-files-2998

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

Fixes #2998

症状

用 0 字节 vitest 配置(绕过 setupFiles)单跑下面四个文件时,它们照常全部跑完,并把数据写进继承来的 SESSION_DATA_DIR。在 botmux 会话里,这个变量指向线上的 ~/.botmux/data。

在临时 HOME 里复现到的写入:

文件 改前(绕过围栏) 写入
test/mojo-skill-injection-modes.test.ts 11 passed data/mojo-containment-handles.json
test/scheduler-silent-execute.test.ts 79 passed data/prompt-ctx/sess-live/*、bots/cli_app_test/schedules.json
test/trigger-session-root-message.test.ts 82 passed data/async-triggers/sess_existing.json
test/dashboard-ipc.test.ts 377 passed data/queues/*、data/session-stores/test-app/sessions.db*、data/read-isolation/*、bots/cli_ipc_test_bot001/schedules.json

根因

这四个文件没有导入 test/helpers/require-fenced-data-dir.ts。围栏只通过 setupFiles 生效,绕过它时没有任何东西阻止文件运行。它们也没有 mock node:fs,所以 test-hygiene 的 fs-mock 扫描覆盖不到。

修法

和 #2997 同一做法:

  • 四个文件都把 ./helpers/require-fenced-data-dir.js 作为第一个 import。围栏没生效时整个文件在导入阶段就失败。
  • 会写家目录推导路径的两个文件再加一道断言:
    • mojo-skill-injection-modes 断言 mojo 隔离工作区根在 __TEST_DATA_DIR_FENCE 里。这个根取 BOTMUX_MOJO_WORKSPACE_ROOT,未设置时由 homedir() 推导。
    • dashboard-ipc 断言 homedir() 在围栏里。

只改测试文件,不动 src 和 helper。

验证

所有命令都经 scripts/fenced-run.ts 在临时 HOME 和私有 tmux socket 下运行,PATH 里去掉了 ~/.botmux,并清掉了 BOTMUX_* 环境变量。

  • 绕过围栏(0 字节 vitest 配置,逐个文件):
    • 改前:四个文件全部通过,并产生上表里的写入。
    • 改后:四个文件都报 refusing to run: data-dir fence not active,no tests,临时 HOME 里没有写入任何文件。
  • 只伪造 __TEST_DATA_DIR_FENCE 和 SESSION_DATA_DIR、HOME 仍在围栏外时:
    • mojo 文件报 refusing to run: mojo workspace … is not inside the test fence。
    • dashboard-ipc 报 refusing to run: HOME … is not inside the test fence。
  • 正常围栏下运行:
    • vitest run 四个文件:549 passed,3 skipped。
    • test/test-hygiene.test.ts:28 passed。
  • bun run build(含 tsc)通过。

mojo-skill-injection-modes、scheduler-silent-execute、
trigger-session-root-message、dashboard-ipc 没有导入数据目录围栏守卫。
绕过 setupFiles 运行时(例如 0 字节 vitest 配置)它们照常跑完,并把
mojo-containment-handles.json、prompt-ctx、async-triggers、queues、
session-stores、bots/<appId>/schedules.json 写进继承来的 SESSION_DATA_DIR,
在 botmux 会话里就是线上数据目录。

四个文件把 ./helpers/require-fenced-data-dir.js 作为第一个 import;
mojo 文件额外断言 mojo 隔离工作区根在围栏内,dashboard-ipc 额外断言
homedir() 在围栏内。
@github-actions

Copy link
Copy Markdown

🚀 Released in v3.59.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(fence): 另有四个测试在绕过围栏时会写线上 ~/.botmux 数据目录

1 participant