From 5c732cf2aa0af447797432c22b6284789597af66 Mon Sep 17 00:00:00 2001 From: Jordan Ritter Date: Mon, 20 Apr 2026 10:25:04 -0700 Subject: [PATCH] Fix analytics-server test path resolution for worktree/source-tree runs 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). --- src/__tests__/analytics-server.test.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/__tests__/analytics-server.test.ts b/src/__tests__/analytics-server.test.ts index e580ffc..9b9f431 100644 --- a/src/__tests__/analytics-server.test.ts +++ b/src/__tests__/analytics-server.test.ts @@ -2,7 +2,6 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import express, { Request, Response } from "express"; import http from "node:http"; import path from "node:path"; -import { fileURLToPath } from "node:url"; const mockGetAnalyticsSummary = vi.fn(); const mockGetTopQueries = vi.fn(); @@ -53,7 +52,10 @@ function buildTestApp() { const app = express(); app.use(express.json()); - const __dirname = path.dirname(fileURLToPath(import.meta.url)); + // Resolve docs/analytics.html from the repo root. Vitest runs from the + // repo root, so process.cwd() is stable regardless of whether __dirname + // points into src/__tests__ (source tree) or dist/__tests__ (built). + const analyticsHtmlPath = path.join(process.cwd(), "docs", "analytics.html"); // Dashboard HTML route — mirrors server.ts /analytics app.get("/analytics", (_req: Request, res: Response) => { @@ -61,7 +63,11 @@ function buildTestApp() { res.status(404).json({ error: "Analytics not enabled" }); return; } - res.sendFile(path.resolve(__dirname, "../../docs/analytics.html")); + // `dotfiles: "allow"` is required so the file serves from paths that + // contain a dot-prefixed segment (e.g. git worktrees under `.claude/`). + // Without it, Express's `send` returns 404 for any path containing a + // dotfile component, which is unrelated to the actual file's existence. + res.sendFile(analyticsHtmlPath, { dotfiles: "allow" }); }); // API routes with analyticsAuth middleware