Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317import { mkdir, mkdtemp, realpath, rm, symlink,} from 'node:fs/promises';import { homedir, tmpdir,} from 'node:os';import { join, } from 'node:path';
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { isAllowedWorktreeDir, isPathUnder, resolveUvCacheDir,} from './allowed-worktree-dirs.ts';
//region Test fixtures
/** Disposable temporary directory used by allowed-worktree-dirs tests. */type TempDirectory = { /** Absolute path to temporary directory. */ readonly path: string; /** Deletes temporary directory after test exits. */ readonly [Symbol.asyncDispose]: () => Promise<void>;};
/** Creates disposable temporary directory for allowlist fixtures. @returns Temporary directory that removes itself when disposed. @example ```ts await using tempDirectory = await createTempDirectory(); console.log(tempDirectory.path); ``` */async function createTempDirectory(): Promise<TempDirectory> { /** Absolute temporary directory path for one test case. */ const path = await mkdtemp(join( tmpdir(), 'cli-git-allowed-worktree-dirs-', ),);
return { path, async [Symbol.asyncDispose](): Promise<void> { await rm( path, { recursive: true, force: true, }, ); }, };}
//endregion Test fixtures
await describe({ name: resolveUvCacheDir.name, children: [ it({ name: 'returns UV_CACHE_DIR when set to a non-empty value', fn: async function testUvCacheDirOverride(): Promise<void> { expect( resolveUvCacheDir({ env: { UV_CACHE_DIR: '/custom/uv-cache', XDG_CACHE_HOME: '/ignored', }, },), ).toBe('/custom/uv-cache',); }, },), it({ name: 'derives the cache from XDG_CACHE_HOME when UV_CACHE_DIR is unset', fn: async function testXdgCacheHome(): Promise<void> { expect( resolveUvCacheDir({ env: { XDG_CACHE_HOME: '/custom/xdg', }, },), ).toBe(join( '/custom/xdg', 'uv', ),); }, },), it({ name: 'falls back to ~/.cache/uv when no cache env is set', fn: async function testHomeFallback(): Promise<void> { expect( resolveUvCacheDir({ env: {}, },), ).toBe(join( homedir(), '.cache', 'uv', ),); }, },), it({ name: 'treats an empty UV_CACHE_DIR as unset', fn: async function testEmptyUvCacheDir(): Promise<void> { expect( resolveUvCacheDir({ env: { UV_CACHE_DIR: '', }, },), ).toBe(join( homedir(), '.cache', 'uv', ),); }, },), ],},);
await describe({ name: isPathUnder.name, children: [ it({ name: 'reports a direct child as under the parent', fn: async function testDirectChild(): Promise<void> { expect(isPathUnder({ parent: '/a/b', child: '/a/b/c', },),).toBe(true,); }, },), it({ name: 'reports a deep descendant as under the parent', fn: async function testDeepDescendant(): Promise<void> { expect(isPathUnder({ parent: '/a/b', child: '/a/b/c/d/e', },),).toBe(true,); }, },), it({ name: 'reports an identical path as under the parent', fn: async function testIdenticalPath(): Promise<void> { expect(isPathUnder({ parent: '/a/b', child: '/a/b', },),).toBe(true,); }, },), it({ name: 'rejects a sibling that shares a name prefix', fn: async function testSiblingPrefix(): Promise<void> { expect(isPathUnder({ parent: '/a/b', child: '/a/bc', },),).toBe(false,); }, },), it({ name: 'rejects an ancestor of the parent', fn: async function testAncestor(): Promise<void> { expect(isPathUnder({ parent: '/a/b', child: '/a', },),).toBe(false,); }, },), it({ name: 'rejects an unrelated path', fn: async function testUnrelated(): Promise<void> { expect(isPathUnder({ parent: '/a/b', child: '/x/y', },),).toBe(false,); }, },), ],},);
await describe({ name: isAllowedWorktreeDir.name, children: [ it({ name: 'reports a git-dir under an allowed directory', fn: async function testUnderAllowed(): Promise<void> { await using tempDirectory = await createTempDirectory(); /** Canonical temp root; allowed entry and candidate share it. */ const root = await realpath(tempDirectory.path,);
expect( await isAllowedWorktreeDir({ candidatePath: join( root, 'repo', '.git', ), allowedDirs: [root,], },), ).toBe(true,); }, },), it({ name: 'rejects a git-dir that exists outside every allowed directory', fn: async function testOutsideAllowed(): Promise<void> { await using tempDirectory = await createTempDirectory(); /** Canonical temp root holding both the allowed dir and the candidate. */ const root = await realpath(tempDirectory.path,); /** Existing allowed directory that does not contain the candidate. */ const allowed = join( root, 'allowed', ); await mkdir(allowed,);
expect( await isAllowedWorktreeDir({ candidatePath: join( root, 'elsewhere', '.git', ), allowedDirs: [allowed,], },), ).toBe(false,); }, },), it({ name: 'skips allowed entries that do not exist and matches a later real one', fn: async function testNonExistentEntrySkipped(): Promise<void> { await using tempDirectory = await createTempDirectory(); /** Canonical temp root used as the real allowed entry. */ const root = await realpath(tempDirectory.path,);
expect( await isAllowedWorktreeDir({ candidatePath: join( root, 'repo', '.git', ), allowedDirs: [ join( root, 'does-not-exist', ), root, ], },), ).toBe(true,); }, },), it({ name: 'returns false when the only allowed entry does not exist', fn: async function testOnlyNonExistentEntry(): Promise<void> { await using tempDirectory = await createTempDirectory(); /** Canonical temp root used to build a non-existent allowed entry. */ const root = await realpath(tempDirectory.path,);
expect( await isAllowedWorktreeDir({ candidatePath: join( root, 'repo', '.git', ), allowedDirs: [join( root, 'does-not-exist', ),], },), ).toBe(false,); }, },), it({ name: 'resolves a symlinked allowed entry before matching', fn: async function testSymlinkedAllowedEntry(): Promise<void> { await using tempDirectory = await createTempDirectory(); /** Canonical temp root holding the real cache and its symlink. */ const root = await realpath(tempDirectory.path,); /** Real cache directory the candidate lives under. */ const realCache = join( root, 'real-cache', ); await mkdir(realCache,); /** Symlink pointing at the real cache; passed as the allowed entry. */ const linkedCache = join( root, 'linked-cache', ); await symlink( realCache, linkedCache, );
// Caller contract: candidatePath is already realpath-resolved, so it // points through the real target rather than the symlink. A literal // string compare against the symlink would miss; realpath must align them. expect( await isAllowedWorktreeDir({ candidatePath: join( realCache, 'repo', '.git', ), allowedDirs: [linkedCache,], },), ).toBe(true,); }, },), ],},);