import { describe, expect, it } from 'vitest'; import { globPattern, invalidGlob, pathsNeedingReview } from '@wishforge/contracts/path-rules'; import { codeReviewPolicyInput } from '@wishforge/contracts'; describe('code review path rules', () => { it('matches directories, one level and any depth', () => { expect(globPattern('backend/').test('backend/api/server.go')).toBe(true); expect(globPattern('backend/').test('backend-tools/x')).toBe(false); expect(globPattern('*.go').test('main.go')).toBe(true); expect(globPattern('*.go').test('cmd/main.go')).toBe(false); expect(globPattern('**/*.go').test('main.go')).toBe(true); expect(globPattern('**/*.go').test('internal/store/db.go')).toBe(true); expect(globPattern('src/?.ts').test('src/a.ts')).toBe(true); expect(globPattern('src/?.ts').test('src/ab.ts')).toBe(false); expect(globPattern('./go.mod').test('go.mod')).toBe(true); // Other regular expression characters are literal. expect(globPattern('a+b.(c)').test('a+b.(c)')).toBe(true); expect(globPattern('a+b.(c)').test('aab.c')).toBe(false); }); it('holds what the policy names, minus its exceptions', () => { const policy = { paths: ['**/*.go', 'api/'], exceptPaths: ['**/*_test.go', 'api/docs/'] }; expect( pathsNeedingReview( ['web/app.js', 'cmd/main.go', 'cmd/main_test.go', 'api/routes.ts', 'api/docs/readme.md'], policy, ), ).toEqual(['cmd/main.go', 'api/routes.ts']); expect(pathsNeedingReview(['cmd/main.go'], undefined)).toEqual([]); expect(pathsNeedingReview(['cmd/main.go'], { paths: [], exceptPaths: [] })).toEqual([]); }); it('refuses patterns outside the repository', () => { expect(invalidGlob('/etc/passwd')).toBeTruthy(); expect(invalidGlob('../other/')).toBeTruthy(); expect(invalidGlob('a\\b')).toBeTruthy(); expect(invalidGlob('backend/')).toBeUndefined(); expect(() => codeReviewPolicyInput.parse({ paths: ['../x'] })).toThrow(); expect(codeReviewPolicyInput.parse({ paths: ['backend/'] })).toEqual({ paths: ['backend/'], exceptPaths: [], trustedMembers: [], }); }); });