test(cursor): isolate cursor-models shared-cache tests per HAPI_HOME (#1268)

The four cursorModels* CLI test files share one on-disk cache path
($HAPI_HOME/cache/cursor-models.json, defaulting to /tmp/hapi when
HAPI_HOME is unset). Two files already isolate it (cursorModelsStaleLock
via a PID-namespaced home; handlers/cursorModels via a unique temp home),
but cursorModels.test.ts and cursorModelsSharedCache.test.ts do not.

Under vitest's parallel file execution, cursorModelsSharedCache's
afterEach(_resetSharedCursorModelsCacheForTests) rmSyncs that shared file
between the other file's write and read, so the read returns null and
"inherits cliModelSkus from shared cache" fails with
`expected undefined to deeply equal [...]`. Passes in isolation; fails at
random in the full parallel suite.

Give both un-isolated files their own mkdtemp HAPI_HOME at module load so
each test file's cache path is unique regardless of worker-process reuse.

Fixes heavygee/hapi#101

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
HeavyGee
2026-07-31 10:43:33 +01:00
committed by GitHub
co-authored by Cursor
parent 212e58cba2
commit 743c0a06ec
2 changed files with 16 additions and 0 deletions
@@ -1,5 +1,13 @@
import { afterEach, describe, expect, test, vi } from 'vitest'
import { setCursorAcpModelsSnapshot } from '@/cursor/utils/cursorAcpModelsBridge'
import { mkdtempSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
// Isolate the on-disk cursor-models cache to this file's own HAPI_HOME so
// parallel vitest workers don't race on the shared $HAPI_HOME/cache path
// (see heavygee/hapi#101).
process.env.HAPI_HOME = mkdtempSync(join(tmpdir(), 'hapi-cursor-models-'))
const { spawnMock } = vi.hoisted(() => ({
spawnMock: vi.fn()
@@ -1,10 +1,18 @@
import { afterEach, describe, expect, test } from 'vitest';
import { mkdtempSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import {
readSharedCursorModelsCache,
writeSharedCursorModelsCache,
_resetSharedCursorModelsCacheForTests
} from './cursorModelsSharedCache';
// Isolate the on-disk cursor-models cache to this file's own HAPI_HOME so
// parallel vitest workers don't race on the shared $HAPI_HOME/cache path
// (see heavygee/hapi#101).
process.env.HAPI_HOME = mkdtempSync(join(tmpdir(), 'hapi-cursor-models-shared-cache-'));
afterEach(() => {
_resetSharedCursorModelsCacheForTests();
});