Skip to content

Commit

Permalink
update: test
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Dec 18, 2024
1 parent 7c9fa1c commit 6c737c4
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 19 deletions.
34 changes: 34 additions & 0 deletions Untitled-1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def fibonacci_memoization(n, memo={}):\n",
" if n <= 0:\n",
" raise ValueError(\"Input should be a positive integer.\")\n",
" if n in memo:\n",
" return memo[n]\n",
" if n == 1:\n",
" return 0\n",
" elif n == 2:\n",
" return 1\n",
" else:\n",
" memo[n] = fibonacci_memoization(n - 1, memo) + fibonacci_memoization(n - 2, memo)\n",
" return memo[n]\n",
"\n",
"# Example usage:\n",
"print(fibonacci_memoization(10))"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
10 changes: 5 additions & 5 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
UserMessage,
UserMessageWithOptionalId
} from '@/lib/types/chat'
import { cn, findClosestRepositoryMatch, nanoid } from '@/lib/utils'
import { cn, findClosestGitRepository, nanoid } from '@/lib/utils'

import { ChatPanel, ChatPanelRef } from './chat-panel'
import { ChatScrollAnchor } from './chat-scroll-anchor'
Expand Down Expand Up @@ -539,10 +539,10 @@ function ChatRenderer(
// get default repo
if (workspaceGitRepositories?.length && repos?.length) {
const defaultGitUrl = workspaceGitRepositories[0].url
const targetGirUrl = findClosestRepositoryMatch(
defaultGitUrl,
repos.map(x => x.gitUrl)
)
const targetGirUrl = findClosestGitRepository(
repos.map(x => ({ url: x.gitUrl })),
defaultGitUrl
)?.url
if (targetGirUrl) {
const repo = repos.find(x => x.gitUrl === targetGirUrl)
if (repo) {
Expand Down
24 changes: 17 additions & 7 deletions ee/tabby-ui/lib/utils/repository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { go } from 'fuzzysort'
import gitUrlParse from 'git-url-parse'
import type { GitRepository } from 'tabby-chat-panel'

export const findClosestRepositoryMatch = (
target: string,
gitUrls: string[]
) => {
const results = go(target.replace(/\.git$/, ''), gitUrls)
return results.length > 0 ? results[0].target : null
export function findClosestGitRepository(
repositories: GitRepository[],
gitUrl: string
): GitRepository | undefined {
const gitSearch = gitUrlParse(gitUrl)
if (!gitSearch) {
return undefined
}

const repos = repositories.filter(repo => {
const search = gitUrlParse(repo.url)
return search.name === gitSearch.name
})
// If there're multiple matches, we pick the one with highest alphabetical order
return repos.sort((a, b) => b.url.localeCompare(a.url))[0]
}
6 changes: 5 additions & 1 deletion ee/tabby-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"start": "next start",
"lint": "next lint && pnpm format:check",
"lint:fix": "next lint --fix && pnpm format:write",
"test": "vitest run",
"preview": "next build && next start",
"type-check": "tsc --noEmit",
"format:write": "prettier --write \"{app,lib,components}/**/*.{ts,tsx,mdx}\" --cache",
Expand Down Expand Up @@ -77,6 +78,7 @@
"focus-trap-react": "^10.1.1",
"framer-motion": "^11.11.7",
"fuzzysort": "^3.0.2",
"git-url-parse": "^16.0.0",
"graphql": "^16.8.1",
"graphql-ws": "^5.16.0",
"humanize-duration": "^3.31.0",
Expand Down Expand Up @@ -134,6 +136,7 @@
"@tailwindcss/typography": "^0.5.9",
"@types/color": "^3.0.6",
"@types/dompurify": "^3.0.5",
"@types/git-url-parse": "^9.0.3",
"@types/he": "^1.2.3",
"@types/humanize-duration": "^3.27.4",
"@types/lodash-es": "^4.17.10",
Expand Down Expand Up @@ -162,6 +165,7 @@
"tailwind-merge": "^1.12.0",
"tailwindcss": "^3.3.1",
"tailwindcss-animate": "^1.0.5",
"typescript": "^5.1.3"
"typescript": "^5.1.3",
"vitest": "^1.5.2"
}
}
102 changes: 102 additions & 0 deletions ee/tabby-ui/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { describe, expect, it } from 'vitest'
import { findClosestGitRepository } from '../lib/utils/repository'
import type { GitRepository } from 'tabby-chat-panel'

describe('findClosestGitRepository', () => {
it('should match .git suffix', () => {
const repositories: GitRepository[] = [
{ url: 'https://github.com/example/test' },
]
const result = findClosestGitRepository(repositories, 'https://github.com/example/test.git')
expect(result).toEqual(repositories[0])
})

it('should match auth in URL', () => {
const repositories: GitRepository[] = [
{ url: 'https://github.com/example/test' },
]
const result = findClosestGitRepository(repositories, 'https://[email protected]/example/test')
expect(result).toEqual(repositories[0])
})

it('should not match different names', () => {
const repositories: GitRepository[] = [
{ url: 'https://github.com/example/anoth-repo' },
]
const result = findClosestGitRepository(repositories, 'https://github.com/example/another-repo')
expect(result).toBeUndefined()
})

it('should not match repositories with a common prefix', () => {
const repositories: GitRepository[] = [
{ url: 'https://github.com/TabbyML/registry-tabby' },
]
const result = findClosestGitRepository(repositories, 'https://github.com/TabbyML/tabby')
expect(result).toBeUndefined()
})

it('should not match entirely different repository names', () => {
const repositories: GitRepository[] = [
{ url: 'https://github.com/TabbyML/uptime' },
]
const result = findClosestGitRepository(repositories, 'https://github.com/TabbyML/tabby')
expect(result).toBeUndefined()
})

it('should not match URL without repository name', () => {
const repositories: GitRepository[] = [
{ url: 'https://github.com/TabbyML/tabby' },
]
const result = findClosestGitRepository(repositories, 'https://github.com')
expect(result).toBeUndefined()
})

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

it('should not match multiple close matches', () => {
const repositories: GitRepository[] = [
{ url: 'https://bitbucket.com/CrabbyML/crabby' },
{ url: 'https://gitlab.com/TabbyML/registry-tabby' },
]
const result = findClosestGitRepository(repositories, '[email protected]:TabbyML/tabby')
expect(result).toBeUndefined()
})

it('should match different protocol and suffix', () => {
const repositories: GitRepository[] = [
{ url: 'https://github.com/TabbyML/tabby' },
]
const result = findClosestGitRepository(repositories, '[email protected]:TabbyML/tabby.git')
expect(result).toEqual(repositories[0])
})

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

it('should match URL without organization', () => {
const repositories: GitRepository[] = [
{ url: 'https://custom-git.com/TabbyML/tabby' },
]
const result = findClosestGitRepository(repositories, 'https://custom-git.com/tabby')
expect(result).toEqual(repositories[0])
})

it('should match local URL', () => {
const repositories: GitRepository[] = [
{ url: 'file:///home/TabbyML/tabby' },
]
const result = findClosestGitRepository(repositories, '[email protected]:TabbyML/tabby.git')
expect(result).toEqual(repositories[0])
})
})
6 changes: 4 additions & 2 deletions ee/tabby-ui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"next-auth.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
, "lib/patch-fetch.js" ],
".next/types/**/*.ts",
"lib/patch-fetch.js",
"test"
],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 6c737c4

Please sign in to comment.