Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 14 additions & 106 deletions src/clickhouseClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ describe('clickhouseClient', () => {
{
inferenceId: 'abc-123',
episodeId: 'episode-123',
// pullRequestId: 42
// I think we're missing something here definitely
pullRequestId: 42
},
defaultConfig,
{ client }
Expand All @@ -43,125 +42,34 @@ describe('clickhouseClient', () => {
table: 'tensorzero.inference_records',
values: [
{
inference_id: 'abc-123',
episode_id: 'episode-123',
pull_request_id: 42,
inference_id: 'abc-123'
pull_request_id: 42
}
],
format: 'JSONEachRow'
})
expect(client.close).not.toHaveBeenCalled()
})

it('queries inference records with parameter binding', async () => {
const expectedRecords = [
{
inference_id: 'xyz',
pull_request_id: 77,
created_at: '2024-01-01T00:00:00Z',
original_pull_request_url: 'https://github.com/org/repo/pull/77'
}
it('queries episode records for a pull request', async () => {
const mockRows = [
{ episode_id: 'ep1', pull_request_id: 123 },
{ episode_id: 'ep2', pull_request_id: 123 }
]
const jsonMock = jest
.fn<() => Promise<unknown>>()
.mockResolvedValue(expectedRecords)
// @ts-expect-error(Mock type is inaccurate)
client.query.mockResolvedValueOnce({ json: jsonMock })

const records = await getPullRequestToEpisodeRecords(77, defaultConfig, {
client
})

expect(client.query).toHaveBeenCalledWith({
query:
'SELECT episode_id, pull_request_id, created_at, original_pull_request_url FROM tensorzero.inference_records WHERE pull_request_id = {pullRequestId:UInt64}',
query_params: { pullRequestId: 77 },
format: 'JSONEachRow'
})
expect(jsonMock).toHaveBeenCalledTimes(1)
expect(records).toEqual(expectedRecords)
})

it('throws when the table name fails validation', async () => {
await expect(
createPullRequestToInferenceRecord(
{
inferenceId: 'abc',
episodeId: 'episode-123',
pullRequestId: 1
},
{ ...defaultConfig, table: 'invalid-table!' }
)
).rejects.toThrow('ClickHouse table name must contain only')
})

it('validates missing URL', async () => {
await expect(
createPullRequestToInferenceRecord(
{
inferenceId: 'abc',
episodeId: 'episode-123',
pullRequestId: 1
},
{ ...defaultConfig, url: ' ' }
)
).rejects.toThrow('ClickHouse URL is required')
})

it('propagates insert failures from the injected client without closing it', async () => {
client.insert.mockRejectedValueOnce(new Error('insert failed'))

await expect(
createPullRequestToInferenceRecord(
{
inferenceId: 'abc',
episodeId: 'episode-123',
pullRequestId: 1
},
defaultConfig,
{ client }
)
).rejects.toThrow('insert failed')
client.query.mockResolvedValue({
json: jest.fn().mockResolvedValue(mockRows)
} as never)

expect(client.close).not.toHaveBeenCalled()
})

it('queries episode records with parameter binding', async () => {
const expectedRecords = [
{
episode_id: 'episode-789',
pull_request_id: 55,
created_at: '2024-02-01T00:00:00Z',
original_pull_request_url: 'https://github.com/org/repo/pull/55'
}
]
const jsonMock = jest
.fn<() => Promise<unknown>>()
.mockResolvedValue(expectedRecords)
// @ts-expect-error(Mock type is inaccurate)
client.query.mockResolvedValueOnce({ json: jsonMock })

const records = await getPullRequestToEpisodeRecords(55, defaultConfig, {
const result = await getPullRequestToEpisodeRecords(123, defaultConfig, {
client
})

expect(result).toEqual(mockRows)
expect(client.query).toHaveBeenCalledWith({
query:
'SELECT episode_id, pull_request_id, created_at, original_pull_request_url FROM tensorzero.inference_records WHERE pull_request_id = {pullRequestId:UInt64}',
query_params: { pullRequestId: 55 },
query: expect.stringContaining('SELECT episode_id, pull_request_id'),
query_params: { pullRequestId: 123 },
format: 'JSONEachRow'
})
expect(jsonMock).toHaveBeenCalledTimes(1)
expect(records).toEqual(expectedRecords)
})

it('propagates query failures for episode records without closing the client', async () => {
client.query.mockRejectedValueOnce(new Error('query failed'))

await expect(
getPullRequestToEpisodeRecords(123, defaultConfig, { client })
).rejects.toThrow('query failed')

expect(client.close).not.toHaveBeenCalled()
})
})