Files
hyperframes/packages/core/src/cacheDir.test.ts
T
Miguel Ángel de6ec3811e fix: cache paths follow HYPERFRAMES_CACHE_DIR (#6716)
* fix: cache paths follow HYPERFRAMES_CACHE_DIR

Every place HyperFrames keeps downloaded or built files now takes its
folder from one helper, hyperframesCacheDir() in @hyperframes/core. The
default stays ~/.cache/hyperframes; HYPERFRAMES_CACHE_DIR moves it.

* fix: register the cache-dir subpath for bun and node, and build inspector from core source

* test: the cache-dir override test uses a folder that is absolute on every platform

* fix: the opencv cache follows HYPERFRAMES_CACHE_DIR

* fix(cli): npm installs run in their staging dir, so no cache path reaches a command line

* fix(cli): whisper.cpp is cloned inside its build dir, so no cache path is a git argument

* test(cli): the whisper clone test finds no system whisper on Windows either
2026-11-01 22:44:11 +00:00

60 lines
2.3 KiB
TypeScript

import { readdirSync, readFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { hyperframesCacheDir } from "./cacheDir.js";
afterEach(() => vi.unstubAllEnvs());
describe("hyperframesCacheDir", () => {
it("defaults to ~/.cache/hyperframes", () => {
vi.stubEnv("HYPERFRAMES_CACHE_DIR", "");
expect(hyperframesCacheDir()).toBe(join(homedir(), ".cache", "hyperframes"));
expect(hyperframesCacheDir("chrome")).toBe(join(homedir(), ".cache", "hyperframes", "chrome"));
});
it("follows HYPERFRAMES_CACHE_DIR, read on each call, a relative one resolved", () => {
const elsewhere = join(tmpdir(), "elsewhere-cache");
vi.stubEnv("HYPERFRAMES_CACHE_DIR", elsewhere);
expect(hyperframesCacheDir("whisper", "models")).toBe(join(elsewhere, "whisper", "models"));
vi.stubEnv("HYPERFRAMES_CACHE_DIR", "rel/cache");
expect(hyperframesCacheDir("tts")).toBe(join(resolve("rel/cache"), "tts"));
});
});
/** Source files under every package's src, tests and test helpers left out. */
function sources(dir: string): string[] {
return readdirSync(dir, { recursive: true, withFileTypes: true })
.filter((entry) => entry.isFile() && /\.(m?[jt]sx?)$/.test(entry.name))
.filter((entry) => !/\.test[.-]|__tests__/.test(join(entry.parentPath, entry.name)))
.map((entry) => join(entry.parentPath, entry.name));
}
it("every cache path in every package goes through hyperframesCacheDir", () => {
const packages = resolve(__dirname, "..", "..");
const built: string[] = [];
for (const name of readdirSync(packages)) {
let files: string[];
try {
files = sources(join(packages, name, "src"));
} catch {
continue; // a package without src
}
for (const file of files) {
if (file.endsWith(join("core", "src", "cacheDir.ts"))) continue;
// Prose about the default path is fine; code that builds it is not.
const code = readFileSync(file, "utf8")
.split("\n")
.filter((line) => !/^\s*(\/\/|\*|\/\*)/.test(line))
.join("\n");
if (
/["'`]\.cache["'`]\s*,\s*["'`]hyperframes["'`]|["'`][^"'`\n]*\.cache\/hyperframes/.test(
code,
)
)
built.push(file.slice(packages.length + 1));
}
}
expect(built).toEqual([]);
});