2025-07-23 18:25:15 +00:00
import { expect , test } from 'vitest'
import { SBChatMessage } from './types' ;
import { renderHook } from '@testing-library/react-hooks' ;
import { useExtractReferences } from './useExtractReferences' ;
import { getFileReferenceId } from './utils' ;
test ( 'useExtractReferences extracts file references from text content' , ( ) = > {
const message : SBChatMessage = {
id : 'msg1' ,
role : 'assistant' ,
parts : [
{
type : 'text' ,
2025-07-24 17:21:00 +00:00
text : 'The auth flow is implemented in @file:{github.com/sourcebot-dev/sourcebot::auth.ts} and uses sessions @file:{github.com/sourcebot-dev/sourcebot::auth.ts:45-60}.'
2025-07-23 18:25:15 +00:00
}
]
} ;
const { result } = renderHook ( ( ) = > useExtractReferences ( message ) ) ;
expect ( result . current ) . toHaveLength ( 2 ) ;
expect ( result . current [ 0 ] ) . toMatchObject ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'auth.ts' ,
id : getFileReferenceId ( { repo : 'github.com/sourcebot-dev/sourcebot' , path : 'auth.ts' } ) ,
2025-07-23 18:25:15 +00:00
type : 'file' ,
} ) ;
expect ( result . current [ 1 ] ) . toMatchObject ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'auth.ts' ,
2025-07-23 18:25:15 +00:00
id : getFileReferenceId ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'auth.ts' ,
2025-07-23 18:25:15 +00:00
range : {
startLine : 45 ,
endLine : 60 ,
}
} ) ,
type : 'file' ,
range : {
startLine : 45 ,
endLine : 60 ,
}
} ) ;
} ) ;
test ( 'useExtractReferences extracts file references from reasoning content' , ( ) = > {
const message : SBChatMessage = {
id : 'msg1' ,
role : 'assistant' ,
parts : [
{
type : 'reasoning' ,
2025-07-24 17:21:00 +00:00
text : 'The auth flow is implemented in @file:{github.com/sourcebot-dev/sourcebot::auth.ts} and uses sessions @file:{github.com/sourcebot-dev/sourcebot::auth.ts:45-60}.'
2025-07-23 18:25:15 +00:00
}
]
} ;
const { result } = renderHook ( ( ) = > useExtractReferences ( message ) ) ;
expect ( result . current ) . toHaveLength ( 2 ) ;
expect ( result . current [ 0 ] ) . toMatchObject ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'auth.ts' ,
id : getFileReferenceId ( { repo : 'github.com/sourcebot-dev/sourcebot' , path : 'auth.ts' } ) ,
2025-07-23 18:25:15 +00:00
type : 'file' ,
} ) ;
expect ( result . current [ 1 ] ) . toMatchObject ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'auth.ts' ,
2025-07-23 18:25:15 +00:00
id : getFileReferenceId ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'auth.ts' ,
2025-07-23 18:25:15 +00:00
range : {
startLine : 45 ,
endLine : 60 ,
}
} ) ,
type : 'file' ,
range : {
startLine : 45 ,
endLine : 60 ,
}
} ) ;
} ) ;
test ( 'useExtractReferences extracts file references from multi-part' , ( ) = > {
const message : SBChatMessage = {
id : 'msg1' ,
role : 'assistant' ,
parts : [
{
type : 'text' ,
2025-07-24 17:21:00 +00:00
text : 'The auth flow is implemented in @file:{github.com/sourcebot-dev/sourcebot::auth.ts}.'
2025-07-23 18:25:15 +00:00
} ,
{
type : 'reasoning' ,
2025-07-24 17:21:00 +00:00
text : 'We need to check the session handling in @file:{github.com/sourcebot-dev/sourcebot::session.ts:10-20}.'
2025-07-23 18:25:15 +00:00
} ,
{
type : 'text' ,
2025-07-24 17:21:00 +00:00
text : 'The configuration is stored in @file:{github.com/sourcebot-dev/sourcebot::config.json} and @file:{github.com/sourcebot-dev/sourcebot::utils.ts:5}.'
2025-07-23 18:25:15 +00:00
}
]
} ;
const { result } = renderHook ( ( ) = > useExtractReferences ( message ) ) ;
expect ( result . current ) . toHaveLength ( 4 ) ;
// From text part
expect ( result . current [ 0 ] ) . toMatchObject ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'auth.ts' ,
id : getFileReferenceId ( { repo : 'github.com/sourcebot-dev/sourcebot' , path : 'auth.ts' } ) ,
2025-07-23 18:25:15 +00:00
type : 'file' ,
} ) ;
// From reasoning part
expect ( result . current [ 1 ] ) . toMatchObject ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'session.ts' ,
2025-07-23 18:25:15 +00:00
id : getFileReferenceId ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'session.ts' ,
2025-07-23 18:25:15 +00:00
range : {
startLine : 10 ,
endLine : 20 ,
}
} ) ,
type : 'file' ,
range : {
startLine : 10 ,
endLine : 20 ,
}
} ) ;
expect ( result . current [ 2 ] ) . toMatchObject ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'config.json' ,
id : getFileReferenceId ( { repo : 'github.com/sourcebot-dev/sourcebot' , path : 'config.json' } ) ,
2025-07-23 18:25:15 +00:00
type : 'file' ,
} ) ;
expect ( result . current [ 3 ] ) . toMatchObject ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'utils.ts' ,
2025-07-23 18:25:15 +00:00
id : getFileReferenceId ( {
2025-07-24 17:21:00 +00:00
repo : 'github.com/sourcebot-dev/sourcebot' ,
path : 'utils.ts' ,
2025-07-23 18:25:15 +00:00
range : {
startLine : 5 ,
endLine : 5 ,
}
} ) ,
type : 'file' ,
range : {
startLine : 5 ,
endLine : 5 ,
}
} ) ;
} ) ;