Files
orca/mobile/src/tasks/smart-source-paste-intent.test.ts
T

139 lines
4.1 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
deriveRepoSlug,
findRepoMatchingSlug,
findRepoMatchingSlugForPaste,
resolvePasteIntent,
type PasteRepoCandidate
} from './smart-source-paste-intent'
import type { RpcClient } from '../transport/rpc-client'
describe('resolvePasteIntent', () => {
it('classifies a GitHub issue/PR URL as a github-link', () => {
expect(resolvePasteIntent('https://github.com/acme/widgets/pull/12')).toEqual({
kind: 'github-link',
link: { slug: { owner: 'acme', repo: 'widgets' }, number: 12, type: 'pr' }
})
})
it('classifies a bare #number as a github-number', () => {
expect(resolvePasteIntent('#42')).toEqual({ kind: 'github-number', number: 42 })
})
it('classifies a GitLab MR URL as a gitlab-link', () => {
const intent = resolvePasteIntent('https://gitlab.com/group/proj/-/merge_requests/8')
expect(intent?.kind).toBe('gitlab-link')
if (intent?.kind === 'gitlab-link') {
expect(intent.link).toMatchObject({ number: 8, type: 'mr' })
}
})
it('returns null for plain search text', () => {
expect(resolvePasteIntent('login bug')).toBeNull()
})
it('rejects oversized paste intents before any exact lookup', () => {
expect(
resolvePasteIntent(`https://github.com/acme/widgets/issues/12/${'x'.repeat(2048)}`)
).toBeNull()
})
})
describe('deriveRepoSlug', () => {
it('prefers the upstream identity', () => {
expect(deriveRepoSlug({ upstream: { owner: 'up', repo: 'stream' } })).toEqual({
owner: 'up',
repo: 'stream'
})
})
it('parses an SSH remote URL', () => {
expect(
deriveRepoSlug({ gitRemoteIdentity: { remoteUrl: 'git@github.com:acme/widgets.git' } })
).toEqual({ owner: 'acme', repo: 'widgets' })
})
it('parses an HTTPS remote URL', () => {
expect(
deriveRepoSlug({ gitRemoteIdentity: { remoteUrl: 'https://github.com/acme/widgets' } })
).toEqual({ owner: 'acme', repo: 'widgets' })
})
it('returns null when no slug can be derived', () => {
expect(deriveRepoSlug({})).toBeNull()
})
})
describe('findRepoMatchingSlug', () => {
const repos: PasteRepoCandidate[] = [
{ id: 'a', displayName: 'A', slug: { owner: 'acme', repo: 'widgets' } },
{ id: 'b', displayName: 'B', slug: null }
]
it('matches case-insensitively', () => {
expect(findRepoMatchingSlug(repos, { owner: 'Acme', repo: 'Widgets' })?.id).toBe('a')
})
it('returns null when no repo matches', () => {
expect(findRepoMatchingSlug(repos, { owner: 'other', repo: 'thing' })).toBeNull()
})
it('falls back to the host-aware repo slug RPC for SSH and enterprise repos', async () => {
const calls: string[] = []
const client = {
sendRequest: async (_method: string, params: unknown) => {
const repo = (params as { repo: string }).repo
calls.push(repo)
return {
ok: true,
result: repo === 'id:b' ? { owner: 'enterprise', repo: 'widgets' } : null
}
}
} as unknown as RpcClient
await expect(
findRepoMatchingSlugForPaste(
client,
repos,
{ owner: 'enterprise', repo: 'widgets' },
new Map()
)
).resolves.toMatchObject({ id: 'b' })
expect(calls).toEqual(['id:a', 'id:b'])
})
it('keeps local matching usable when an older desktop lacks the repo slug RPC', async () => {
const client = {
sendRequest: async () => ({
ok: false,
error: { code: 'method_not_found', message: 'Unknown method: github.repoSlug' }
})
} as unknown as RpcClient
await expect(
findRepoMatchingSlugForPaste(
client,
repos,
{ owner: 'enterprise', repo: 'widgets' },
new Map()
)
).resolves.toBeNull()
})
it('keeps local matching usable when the optional repo slug lookup rejects', async () => {
const client = {
sendRequest: async () => {
throw new Error('connection closed')
}
} as unknown as RpcClient
await expect(
findRepoMatchingSlugForPaste(
client,
repos,
{ owner: 'enterprise', repo: 'widgets' },
new Map()
)
).resolves.toBeNull()
})
})