Skip to content

Commit

Permalink
update: test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Dec 19, 2024
1 parent f61f185 commit 8b47b04
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 57 deletions.
13 changes: 5 additions & 8 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -539,15 +539,12 @@ function ChatRenderer(
// get default repo
if (workspaceGitRepositories?.length && repos?.length) {
const defaultGitUrl = workspaceGitRepositories[0].url
const targetGirUrl = findClosestGitRepository(
repos.map(x => ({ url: x.gitUrl })),
const repo = findClosestGitRepository(
repos.map(x => ({ url: x.gitUrl, sourceId: x.sourceId })),
defaultGitUrl
)?.url
if (targetGirUrl) {
const repo = repos.find(x => x.gitUrl === targetGirUrl)
if (repo) {
setSelectedRepoId(repo.sourceId)
}
)
if (repo) {
setSelectedRepoId(repo.sourceId)
}
}

Expand Down
24 changes: 5 additions & 19 deletions ee/tabby-ui/lib/utils/repository.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import gitUrlParse from 'git-url-parse'
import type { GitRepository } from 'tabby-chat-panel'

export function findClosestGitRepository(
repositories: GitRepository[],
export function findClosestGitRepository<T extends GitRepository>(
repositories: T[],
gitUrl: string
): GitRepository | undefined {
): T | undefined {
const targetSearch = gitUrlParse(gitUrl)
if (!targetSearch) {
return undefined
}

const filteredRepos = repositories.filter(repo => {
const search = gitUrlParse(repo.url)
const isSameResource =
search.resource === targetSearch.resource || search.protocol === 'file'
return isSameResource && search.name === targetSearch.name
return search.name === targetSearch.name
})

if (filteredRepos.length === 0) {
return undefined
} else {
// If there're multiple matches, we pick the one with highest alphabetical order
return filteredRepos.reduce((min, current) => {
const minUrl = canonicalizeUrl(min.url)
const currentUrl = canonicalizeUrl(current.url)
return minUrl > currentUrl ? min : current
})
return filteredRepos.sort((a, b) => a.url.localeCompare(b.url))[0]
}
}

export function canonicalizeUrl(url: string): string {
const strippedUrl = url.replace(/\.git$/, '')
const parsedUrl = new URL(strippedUrl)
parsedUrl.username = ''
parsedUrl.password = ''
return parsedUrl.toString()
}
33 changes: 3 additions & 30 deletions ee/tabby-ui/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
import { describe, expect, it } from 'vitest'
import { findClosestGitRepository, canonicalizeUrl } from '../lib/utils/repository'
import { findClosestGitRepository } from '../lib/utils/repository'
import type { GitRepository } from 'tabby-chat-panel'

describe('canonicalizeUrl', () => {
it('should remove auth info from URL', () => {
const result = canonicalizeUrl('https://abc:dev@github.com/');
expect(result).toBe('https://github.com/');
});

it('should remove token from URL', () => {
const result = canonicalizeUrl('https://token@github.com/TabbyML/tabby');
expect(result).toBe('https://github.com/TabbyML/tabby');
});

it('should return the same URL if no auth info is present', () => {
const result = canonicalizeUrl('https://github.com/TabbyML/tabby');
expect(result).toBe('https://github.com/TabbyML/tabby');
});

it('should remove .git suffix from URL', () => {
const result = canonicalizeUrl('https://github.com/TabbyML/tabby.git');
expect(result).toBe('https://github.com/TabbyML/tabby');
});

it('should handle file URLs correctly', () => {
const result = canonicalizeUrl('file:///home/TabbyML/tabby');
expect(result).toBe('file:///home/TabbyML/tabby');
});
});

describe('findClosestGitRepository', () => {
it('should match .git suffix', () => {
const repositories: GitRepository[] = [
Expand Down Expand Up @@ -78,12 +51,12 @@ describe('findClosestGitRepository', () => {
expect(result).toBeUndefined()
})

it('should not match different host', () => {
it('should match different host', () => {
const repositories: GitRepository[] = [
{ url: 'https://github.com/TabbyML/tabby' },
]
const result = findClosestGitRepository(repositories, 'https://bitbucket.com/TabbyML/tabby')
expect(result).toBeUndefined()
expect(result).toEqual(repositories[0])
})

it('should not match multiple close matches', () => {
Expand Down

0 comments on commit 8b47b04

Please sign in to comment.