Repository navigation
refactor(tests): extract setupTempWorkspace helper (closes #862) - #865
Merged
Merged
Conversation
Mini's #849 + #855 per-process workspace pattern was inline in two test files. Extracting to a shared helper so future workspace-touching tests adopt it by default rather than reinventing. ## The helper `tests/_helpers/temp-workspace.ts` exports: ```ts export function setupTempWorkspace(name: string): { workspace: string; cleanup: () => void; } ``` Module-level call. Sets `process.env.SUTANDO_WORKSPACE` to a fresh `mkdtempSync(... 'sutando-test-<name>-')` so: 1. The code under test's `resolveWorkspace()` at module init resolves to the temp dir. 2. Any spawned subprocess can be passed `SUTANDO_WORKSPACE: workspace` in its env so its own `resolveWorkspace()` resolves identically. `cleanup()` is idempotent — safe to call in `after()` without guarding. ## Refactor - `tests/agent-state-endpoint.test.ts` — replaces inline `mkdtempSync` + `rmSync` + `process.env.SUTANDO_WORKSPACE = ...` with `setupTempWorkspace('agent-state')`. Spawn env passes `SUTANDO_WORKSPACE: TEMP_WORKSPACE` unchanged. - `tests/get-core-status-tool.test.ts` — same refactor with name `'getcore'`. Net: 22+/22- (per-test files shrink, helper file adds the abstraction). ## Verified - `npx tsc --noEmit` ✓ clean. - `npx tsx --test tests/get-core-status-tool.test.ts` — all 6 cases pass. (Note: `node --test` directly doesn't work for `.ts` test files; tests run via the existing `npm test` script which uses `tsx --test`.) ## Closes #862 ## What I'm NOT doing - Doc note in `docs/workspace-design.md` test-isolation section — deferred to the same PR that ratifies the RFC (#858), so the doc's test-isolation section can reference the helper that's now in main. Cleaner than landing the doc reference before the helper itself is in main.
…ce helper Issue #862 follow-through. Previous commit cf26b53 was silently incomplete: .gitignore line 42 (`tests/*`) shadowed the new helper file, so the commit contained only the test refactors and the branch was broken at HEAD. - Add `!tests/_helpers/` + `!tests/_helpers/*.ts` exceptions (matching the existing `!tests/*.test.ts` pattern for canonical test sources). - Add the helper itself (`setupTempWorkspace(name)`) that two tests already imported from in cf26b53. Closes #862.
sonichi
reviewed
May 19, 2026
sonichi
left a comment
Owner
There was a problem hiding this comment.
LGTM. Clean factoring of the per-process temp-workspace pattern that #849 established. Verified locally on commit head:
tests/_helpers/temp-workspace.tsdoc-comment is excellent — explains the module-init vs env-read timing subtlety that bites people (and is the reasonsetupTempWorkspacewritesprocess.env.SUTANDO_WORKSPACEunconditionally, not just returns the path)..gitignoreallowlist fortests/_helpers/is necessary — the existingtests/*rule was shadowing the new dir; without these two lines the helper wouldn't get committed.grep -rln "mkdtempSync.*sutando-test" tests/confirms the 2 migrated files are the only ones using this pattern. No other test file silently kept the boilerplate.npx tsc --noEmitclean,npx tsx --test tests/get-core-status-tool.test.ts→ 6/6 pass.- CI green.
Nit (non-blocking, future PR): the helper's spawn-child guidance ("pass SUTANDO_WORKSPACE: workspace in the child's env") could be a tiny convenience overload — setupTempWorkspace('foo', { forChildSpawn: true }) returning { ..., childEnv: { SUTANDO_WORKSPACE: workspace } } — but only worth adding when a third spawn-based test file shows up. Not now.
Ready to merge.
3 tasks done
liususan091219
pushed a commit
to liususan091219/sutando-fork
that referenced
this pull request
May 28, 2026
… (sonichi#865) * test: extract setupTempWorkspace() helper (closes sonichi#862) Mini's sonichi#849 + sonichi#855 per-process workspace pattern was inline in two test files. Extracting to a shared helper so future workspace-touching tests adopt it by default rather than reinventing. ## The helper `tests/_helpers/temp-workspace.ts` exports: ```ts export function setupTempWorkspace(name: string): { workspace: string; cleanup: () => void; } ``` Module-level call. Sets `process.env.SUTANDO_WORKSPACE` to a fresh `mkdtempSync(... 'sutando-test-<name>-')` so: 1. The code under test's `resolveWorkspace()` at module init resolves to the temp dir. 2. Any spawned subprocess can be passed `SUTANDO_WORKSPACE: workspace` in its env so its own `resolveWorkspace()` resolves identically. `cleanup()` is idempotent — safe to call in `after()` without guarding. ## Refactor - `tests/agent-state-endpoint.test.ts` — replaces inline `mkdtempSync` + `rmSync` + `process.env.SUTANDO_WORKSPACE = ...` with `setupTempWorkspace('agent-state')`. Spawn env passes `SUTANDO_WORKSPACE: TEMP_WORKSPACE` unchanged. - `tests/get-core-status-tool.test.ts` — same refactor with name `'getcore'`. Net: 22+/22- (per-test files shrink, helper file adds the abstraction). ## Verified - `npx tsc --noEmit` ✓ clean. - `npx tsx --test tests/get-core-status-tool.test.ts` — all 6 cases pass. (Note: `node --test` directly doesn't work for `.ts` test files; tests run via the existing `npm test` script which uses `tsx --test`.) ## Closes sonichi#862 ## What I'm NOT doing - Doc note in `docs/workspace-design.md` test-isolation section — deferred to the same PR that ratifies the RFC (sonichi#858), so the doc's test-isolation section can reference the helper that's now in main. Cleaner than landing the doc reference before the helper itself is in main. * fix(tests): allowlist tests/_helpers/ in .gitignore, add temp-workspace helper Issue sonichi#862 follow-through. Previous commit cf26b53 was silently incomplete: .gitignore line 42 (`tests/*`) shadowed the new helper file, so the commit contained only the test refactors and the branch was broken at HEAD. - Add `!tests/_helpers/` + `!tests/_helpers/*.ts` exceptions (matching the existing `!tests/*.test.ts` pattern for canonical test sources). - Add the helper itself (`setupTempWorkspace(name)`) that two tests already imported from in cf26b53. Closes sonichi#862.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extract Mini's per-process
mkdtempSynctest isolation pattern (established by #849) into a shared helper, eliminating the boilerplate duplication flagged by sonichi-bot on #858 LGTM follow-up.Before — each test file:
After:
Changes
tests/_helpers/temp-workspace.ts— new helper withsetupTempWorkspace(name)returning{ workspace, cleanup }.tests/agent-state-endpoint.test.ts— uses helper, ditches inlinemkdtempSync/rmSyncboilerplate.tests/get-core-status-tool.test.ts— same..gitignore— allowlisttests/_helpers/(the existingtests/*rule shadowed the new dir).Test plan
npx tsc --noEmit— clean.npx tsx --test tests/get-core-status-tool.test.ts— all 6 cases pass.tests/agent-state-endpoint.test.ts(spawn-based, slow locally).Closes #862.