Skip to content

Fix analytics-server test path resolution - #32

Merged
jpr5 merged 1 commit into
mainfrom
fix/analytics-server-tests
Apr 20, 2026
Merged

Fix analytics-server test path resolution#32
jpr5 merged 1 commit into
mainfrom
fix/analytics-server-tests

Conversation

@jpr5

@jpr5 jpr5 commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Two tests in src/__tests__/analytics-server.test.ts were failing pre-existing:

  • GET /analytics (dashboard HTML) > returns HTML when analytics is enabled
  • GET /analytics (dashboard HTML) > serves HTML without requiring a token (auth is client-side)

Both expected status 200 but got 404.

Root cause

Two distinct issues, both in the test's local Express app's /analytics route:

  1. __dirname vs cwd mismatch. The test resolved the HTML path via path.resolve(__dirname, "../../docs/analytics.html"). That assumes __dirname is dist/__tests__/, but vitest runs tests directly from src/__tests__/, making the path one level too shallow (src/docs/analytics.html, which doesn't exist).

  2. Express send dotfile denial. When the repo sits in a path containing a dot-prefixed segment (e.g. /.claude/worktrees/agent-.../), Express's underlying send module returns 404 by default (dotfiles: "ignore") regardless of whether the file exists. This affects anyone running tests from a worktree under .claude/.

Fix

  • Resolve the HTML path from process.cwd() (vitest's project root) rather than __dirname. Stable across source and built layouts.
  • Pass { dotfiles: "allow" } to res.sendFile so the route serves files whose absolute path contains dot-prefixed segments.

Test plan

  • npx vitest run src/__tests__/analytics-server.test.ts — 11/11 pass (was 9/11)
  • npm test — 2368/2368 pass
  • npm run build — clean
  • npx prettier --check "src/**/*.ts" — clean

The test's local Express app resolved docs/analytics.html via
`path.resolve(__dirname, "../../docs/analytics.html")`, which assumed
tests ran from dist/__tests__/. Vitest runs directly from src/__tests__/,
so the path was one level too shallow and sendFile returned 404.

Additionally, when running from a git worktree (where the repo lives
under `.claude/worktrees/...`), Express's `send` module rejects any path
containing a dot-prefixed segment by default (dotfiles: "ignore" -> 404),
independent of whether the file actually exists.

Fix:
- Resolve the HTML path from `process.cwd()` (vitest's project root)
  instead of `__dirname`, so it works identically in source and built
  layouts.
- Pass `{ dotfiles: "allow" }` to `res.sendFile` so the route works
  when the repo sits under a dotfile directory (e.g. worktrees).
@jpr5
jpr5 merged commit d941e0f into main Apr 20, 2026
5 checks passed
@jpr5
jpr5 deleted the fix/analytics-server-tests branch April 20, 2026 20:15
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.

1 participant