Skip to content

test(hooks): decouple resolveProject tests from checkout directory name - #1138

Merged
rohitg00 merged 2 commits into
rohitg00:mainfrom
dmazhukov:fix/1137-hook-project-test-cwd
Aug 2, 2026
Merged

rohitg00 merged 2 commits into
rohitg00:mainfrom
dmazhukov:fix/1137-hook-project-test-cwd

Conversation

@dmazhukov

@dmazhukov dmazhukov commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1137.

test/hook-project.test.ts asserted that resolveProject() returns the literal string "agentmemory". Since resolveProject() returns basename(git rev-parse --show-toplevel), that holds only when the working copy lives in a directory named exactly agentmemory, so 5 of the 8 tests fail on a clean main for anyone working in a fork or a worktree under a different name.

The tests now use a fixture they control instead of the checkout path. src/hooks/_project.ts is unchanged.

  • beforeAll creates a repo under os.tmpdir() named amem-fixture-repo with a nested src/hooks subdirectory and runs git init --quiet in it. The git-toplevel cases resolve against that repo, so the expected name is one the test picked.
  • The two "defaults to process.cwd()" cases stub the call with vi.spyOn(process, "cwd").mockReturnValue(repoDir) and restore it in afterEach. They still check that resolveProject() consults process.cwd() when given no argument, an empty string, or whitespace.
  • The non-git fallback case compares against basename(dir) from node:path instead of dir.split("/").pop().

Deriving the expected value from basename(process.cwd()) would have been shorter, but then the test would compute its expectation the same way the implementation does and could never fail. The tmpdir fixture keeps the two independent.

How to verify

npx vitest run test/hook-project.test.ts   # 8 passed
npm test                                   # 1431 passed, 1 skipped, 0 failed
npm run build                              # clean

Clone into a directory not named agentmemory and run the file before and after this change to see the original failure.

To check the tests still bite, I broke src/hooks/_project.ts two ways and reverted both:

  • dropping if (top) return basename(top); fails "returns git toplevel basename from a nested subdir" (expected 'hooks' to be 'amem-fixture-repo')
  • replacing the process.cwd() default with a literal path fails both "defaults to process.cwd()" tests

Summary by CodeRabbit

  • Tests
    • Improved test reliability by using isolated temporary Git repositories.
    • Added coverage for Git root detection, nested directories, fallback behavior, default directories, mocked working directories, and whitespace-only inputs.
    • Enhanced handling of non-repository paths, including constrained Git searches and symlink resolution.
    • Ensured test fixtures are cleaned up and mocks are restored after each test.

test/hook-project.test.ts asserted that resolveProject() returns the
literal "agentmemory", which only holds when the repository is cloned
into a directory named exactly "agentmemory". Any fork, worktree or
rename made five tests fail on a clean checkout, blocking the "npm test
must pass" gate in CONTRIBUTING.md.

Run the git-toplevel cases against a throwaway repo created under
tmpdir() with a name the test controls, and stub process.cwd() with
vi.spyOn for the cwd-fallback cases. The assertions still exercise the
real behaviour: dropping the git-toplevel branch or the process.cwd()
default from resolveProject() still fails the suite.

Signed-off-by: Dmitrii Zhukov <dmitry0983@gmail.com>
@vercel

vercel Bot commented Aug 2, 2026

Copy link
Copy Markdown

@dmazhukov is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 795899cf-656c-400a-8419-d9e7e5aa0cbf

📥 Commits

Reviewing files that changed from the base of the PR and between b68513d and 453728b.

📒 Files selected for processing (1)
  • test/hook-project.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/hook-project.test.ts

📝 Walkthrough

Walkthrough

The tests now create a temporary initialized Git repository with a controlled basename. They validate Git-root, nested-directory, fallback-basename, environment override, and default-directory behavior without depending on the checkout directory name.

Changes

Project resolution test portability

Layer / File(s) Summary
Controlled repository and mock setup
test/hook-project.test.ts
Tests create and initialize a temporary Git repository, create a nested directory, remove the fixture during teardown, and restore Vitest mocks.
Resolution case coverage
test/hook-project.test.ts
Assertions use the fixture basename and cover environment overrides, Git roots, non-repository fallback paths, and omitted, empty, or whitespace-only cwd arguments.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states that hook tests no longer depend on the checkout directory name.
Linked Issues check ✅ Passed The tests address issue #1137 by using controlled repositories and dynamic paths while preserving resolveProject() behavior coverage.
Out of Scope Changes check ✅ Passed All changes support reliable resolveProject() tests and fixture cleanup; no unrelated code changes are identified.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/hook-project.test.ts`:
- Line 14: Update the test cleanup in the hook-project suite’s afterEach block
to restore process.env.AGENTMEMORY_PROJECT_NAME from the captured originalEnv
value after each test, while retaining the existing Vitest mock cleanup.
- Around line 69-75: Update the fallback fixture in the resolveProject test so
Git cannot discover a parent worktree: create it outside any repository or
temporarily set and restore GIT_CEILING_DIRECTORIES around the assertion,
ensuring cleanup and environment restoration occur in all cases.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 42801fd0-9518-4f98-a6d3-05db21d6ea4a

📥 Commits

Reviewing files that changed from the base of the PR and between 8c90741 and b68513d.

📒 Files selected for processing (1)
  • test/hook-project.test.ts

Comment thread test/hook-project.test.ts Resolved
Comment thread test/hook-project.test.ts Resolved
The fallback case created its fixture under os.tmpdir() and assumed that
is outside any repository. It is not guaranteed: with TMPDIR pointing at
a working directory, git rev-parse walks up from the fixture, finds the
enclosing repo and returns its basename, so the fallback under test never
runs. Verified by pointing TMPDIR at a checkout — the assertion failed
with the repository's name in place of the fixture's.

Set GIT_CEILING_DIRECTORIES to the fixture's parent for the duration of
the assertion and restore it afterwards. The path is resolved first
because git compares ceiling entries after resolving symlinks, and
os.tmpdir() is a symlink on macOS.

Signed-off-by: Dmitrii Zhukov <dmitry0983@gmail.com>
@rohitg00
rohitg00 merged commit a7114c7 into rohitg00:main Aug 2, 2026
1 of 2 checks passed
ChronodeAi added a commit to ChronodeAi/agentmemory that referenced this pull request Aug 24, 2026
The managed-hook assertion matched the substring 'agentmemory', which
only holds when the repo is cloned into a directory literally named
agentmemory; worktrees and fork checkouts with other names failed it.
Assert the <pluginRoot>/scripts/<hook>.mjs command shape instead —
same decoupling upstream applied to resolveProject tests in rohitg00#1138.
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/hook-project.test.ts fails unless the repo is cloned into a directory named exactly agentmemory

2 participants