Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
icecrasher321 committed Mar 19, 2026
commit ad8fb838b9389ef142cf6c43809412aaacdc1a5a
114 changes: 114 additions & 0 deletions apps/sim/lib/copilot/vfs/operations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import { glob, grep } from '@/lib/copilot/vfs/operations'

function vfsFromEntries(entries: [string, string][]): Map<string, string> {
return new Map(entries)
}

describe('glob', () => {
it('matches one path segment for single star (files listing pattern)', () => {
const files = vfsFromEntries([
['files/a/meta.json', '{}'],
['files/a/b/meta.json', '{}'],
['uploads/x.png', ''],
])
const hits = glob(files, 'files/*/meta.json')
expect(hits).toContain('files/a/meta.json')
expect(hits).not.toContain('files/a/b/meta.json')
})

it('matches nested paths with double star', () => {
const files = vfsFromEntries([
['workflows/W/state.json', ''],
['workflows/W/sub/state.json', ''],
])
const hits = glob(files, 'workflows/**/state.json')
expect(hits.sort()).toEqual(['workflows/W/state.json', 'workflows/W/sub/state.json'].sort())
})

it('includes virtual directory prefixes when pattern matches descendants', () => {
const files = vfsFromEntries([['files/a/meta.json', '{}']])
const hits = glob(files, 'files/**')
expect(hits).toContain('files')
expect(hits).toContain('files/a')
expect(hits).toContain('files/a/meta.json')
})

it('treats braces literally when nobrace is set (matches old builder)', () => {
const files = vfsFromEntries([
['weird{brace}/x', ''],
['weirdA/x', ''],
])
const hits = glob(files, 'weird{brace}/*')
expect(hits).toContain('weird{brace}/x')
expect(hits).not.toContain('weirdA/x')
})
})

describe('grep', () => {
it('returns content matches per line in default mode', () => {
const files = vfsFromEntries([['a.txt', 'hello\nworld\nhello']])
const matches = grep(files, 'hello', undefined, { outputMode: 'content' })
expect(matches).toHaveLength(2)
expect(matches[0]).toMatchObject({ path: 'a.txt', line: 1, content: 'hello' })
expect(matches[1]).toMatchObject({ path: 'a.txt', line: 3, content: 'hello' })
})

it('strips CR before end-of-line matching on CRLF content', () => {
const files = vfsFromEntries([['x.txt', 'foo\r\n']])
const matches = grep(files, 'foo$', undefined, { outputMode: 'content' })
expect(matches).toHaveLength(1)
expect(matches[0]?.content).toBe('foo')
})

it('counts matching lines', () => {
const files = vfsFromEntries([['a.txt', 'a\nb\na']])
const counts = grep(files, 'a', undefined, { outputMode: 'count' })
expect(counts).toEqual([{ path: 'a.txt', count: 2 }])
})

it('files_with_matches scans whole file (can match across newlines with dot-all style pattern)', () => {
const files = vfsFromEntries([['a.txt', 'foo\nbar']])
const multiline = grep(files, 'foo[\\s\\S]*bar', undefined, {
outputMode: 'files_with_matches',
})
expect(multiline).toContain('a.txt')

const lineOnly = grep(files, 'foo[\\s\\S]*bar', undefined, { outputMode: 'content' })
expect(lineOnly).toHaveLength(0)
})

it('scopes to directory prefix without matching unrelated prefixes', () => {
const files = vfsFromEntries([
['workflows/a/x', 'needle'],
['workflowsManual/x', 'needle'],
])
const hits = grep(files, 'needle', 'workflows', { outputMode: 'files_with_matches' })
expect(hits).toContain('workflows/a/x')
expect(hits).not.toContain('workflowsManual/x')
})

it('scopes with glob pattern when path contains metacharacters', () => {
const files = vfsFromEntries([
['workflows/A/state.json', '{"x":1}'],
['workflows/B/sub/state.json', '{"x":1}'],
['workflows/C/other.json', '{"x":1}'],
])
const hits = grep(files, '1', 'workflows/*/state.json', { outputMode: 'files_with_matches' })
expect(hits).toEqual(['workflows/A/state.json'])
})

it('returns empty array for invalid regex pattern', () => {
const files = vfsFromEntries([['a.txt', 'x']])
expect(grep(files, '(unclosed', undefined, { outputMode: 'content' })).toEqual([])
})

it('respects ignoreCase', () => {
const files = vfsFromEntries([['a.txt', 'Hello']])
const hits = grep(files, 'hello', undefined, { outputMode: 'content', ignoreCase: true })
expect(hits).toHaveLength(1)
})
})
9 changes: 4 additions & 5 deletions apps/sim/lib/copilot/vfs/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ const VFS_GLOB_OPTIONS: micromatch.Options = {
noext: true,
}
Comment thread
icecrasher321 marked this conversation as resolved.

/**
* Returns true when `filePath` is `scope` or a descendant path (`scope/...`), matching how
* `grep -r pattern dir` limits to a directory. If `scope` looks like a glob, filters with
* micromatch `isMatch` and {@link VFS_GLOB_OPTIONS}.
*/
/**
* Splits VFS text into lines for line-oriented grep. Strips a trailing CR so Windows-style
* CRLF payloads still match patterns anchored at line end (`$`).
Expand All @@ -58,6 +53,10 @@ function splitLinesForGrep(content: string): string[] {
return content.split('\n').map((line) => line.replace(/\r$/, ''))
}

/**
* Returns true when `filePath` is `scope` or a descendant path (`scope/...`). If `scope` looks
* like a glob, filters with micromatch `isMatch` and `VFS_GLOB_OPTIONS`.
*/
function pathWithinGrepScope(filePath: string, scope: string): boolean {
const looksLikeGlob =
/[*?[{]/.test(scope) || scope.includes('!(') || scope.includes('@(') || scope.includes('+(')
Expand Down
Loading