Files
orca/src/shared/node-markdown-document-discovery.test.ts
T
JahyunBaekandClaude Opus 5 aa95bdb11a test(shared): stop two suites asserting POSIX separators on Windows (#16511)
Both files describe paths with POSIX literals while their subjects compose
paths through `node:path`, so the assertions only hold where the separator
happens to be `/`.

`node-markdown-document-discovery` keys its fake tree at `/repo/docs` and
`/repo/one`, but `discoverMarkdownRelativePaths` descends with
`join(absoluteDirectoryPath, entry.name)` — `\repo\docs` on win32. The child
lookup misses, `readDirectory` yields nothing, and the walk stops at the root:
`docs/guide.mdx` disappears and the depth-limit case never reaches its limit,
so it resolves `[]` instead of rejecting. Keying the children with `join` walks
the tree the subject actually walks.

`git-fetch-head-lock` expects `cwd: '/tmp/repo'` from a subject that returns
`path.resolve(cwd, 'repo')`, which is `C:\tmp\repo` on win32. Asserting through
`path.resolve` pins the behaviour — that `-C` and `--git-dir` are resolved
against the cwd — rather than the separator of whichever machine runs the suite.

Verified on Windows 11: the two files go from 3 failed / 12 passed to
14 passed / 1 skipped, and the wider `src/shared` run shows no regression.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-29 14:23:22 -07:00

78 lines
2.6 KiB
TypeScript

import type { Dirent } from 'node:fs'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { MarkdownDocumentListingCapacityError } from './markdown-document-listing-limits'
import { discoverMarkdownRelativePaths } from './node-markdown-document-discovery'
function entry(name: string, kind: 'directory' | 'file' | 'symlink' = 'file'): Dirent {
return {
name,
isDirectory: () => kind === 'directory',
isFile: () => kind === 'file',
isSymbolicLink: () => kind === 'symlink'
} as Dirent
}
function reader(entriesByPath: Record<string, Dirent[]>) {
return async (path: string): Promise<AsyncIterable<Dirent>> => ({
async *[Symbol.asyncIterator]() {
yield* entriesByPath[path] ?? []
}
})
}
describe('bounded Markdown document discovery', () => {
it('preserves depth-first discovery and skips excluded and symlinked directories', async () => {
// Why join() for the child key: the subject descends with path.join, so a '/repo/docs'
// literal never matches on Windows and the walk silently stops at the root.
const result = await discoverMarkdownRelativePaths('/repo', {
readDirectory: reader({
'/repo': [
entry('README.md'),
entry('.git', 'directory'),
entry('docs', 'directory'),
entry('linked', 'symlink')
],
[join('/repo', 'docs')]: [entry('guide.mdx'), entry('app.ts')]
}),
shouldDescend: (_relativePath, name) => name !== '.git'
})
expect(result).toEqual(['README.md', 'docs/guide.mdx'])
})
it('stops consuming a wide directory at the visited-entry limit', async () => {
let yielded = 0
const readDirectory = async (): Promise<AsyncIterable<Dirent>> => ({
async *[Symbol.asyncIterator]() {
for (let index = 0; index < 10_000; index += 1) {
yielded += 1
yield entry(`source-${index}.ts`)
}
}
})
await expect(
discoverMarkdownRelativePaths('/repo', {
limits: { maxVisitedEntries: 2 },
readDirectory,
shouldDescend: () => true
})
).rejects.toBeInstanceOf(MarkdownDocumentListingCapacityError)
expect(yielded).toBe(3)
})
it('rejects a directory deeper than the configured traversal limit', async () => {
await expect(
discoverMarkdownRelativePaths('/repo', {
limits: { maxDepth: 1 },
readDirectory: reader({
'/repo': [entry('one', 'directory')],
[join('/repo', 'one')]: [entry('two', 'directory')]
}),
shouldDescend: () => true
})
).rejects.toBeInstanceOf(MarkdownDocumentListingCapacityError)
})
})