Skip to content

Commit 521958d

Browse files
committed
refactor(testing): rename TestableDb to DbOperations
TestableDb implied it was only for testing, but it is used as a parameter type in production code. DbOperations is more accurate.
1 parent c567749 commit 521958d

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

common/src/testing/mock-db.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import type { Logger } from '@codebuff/common/types/contracts/logger'
22

33
// ============================================================================
4-
// Testable Database Interface
4+
// Database Operations Interface
55
// ============================================================================
66

77
/* eslint-disable @typescript-eslint/no-explicit-any */
88
/**
99
* Minimal database interface for dependency injection in API routes.
1010
* Both the real CodebuffPgDatabase and test mocks can satisfy this interface.
1111
*
12-
* This allows tests to provide mock implementations without type casting.
1312
* Uses `any` for table/column parameters to be compatible with Drizzle ORM's
1413
* specific table types while remaining flexible for mocks.
1514
*/
16-
export interface TestableDb {
15+
export interface DbOperations {
1716
insert: (table: any) => {
1817
values: (data: any) => PromiseLike<any>
1918
}
@@ -24,7 +23,7 @@ export interface TestableDb {
2423
}
2524
select: (columns?: any) => {
2625
from: (table: any) => {
27-
where: (condition: any) => TestableDbWhereResult
26+
where: (condition: any) => DbWhereResult
2827
}
2928
}
3029
}
@@ -35,7 +34,7 @@ export interface TestableDb {
3534
* - .orderBy(...).limit(n) for sorted queries
3635
* - .then() for promise-like resolution
3736
*/
38-
export interface TestableDbWhereResult {
37+
export interface DbWhereResult {
3938
then: <TResult = any[]>(
4039
onfulfilled?: ((value: any[]) => TResult | PromiseLike<TResult>) | null | undefined,
4140
) => PromiseLike<TResult>
@@ -364,9 +363,9 @@ export interface MockDbFactoryConfig {
364363

365364
/**
366365
* Return type of createMockDb - a complete mock database object.
367-
* Implements TestableDb for type-safe dependency injection in tests.
366+
* Implements DbOperations for type-safe dependency injection in tests.
368367
*/
369-
export type MockDb = TestableDb
368+
export type MockDb = DbOperations
370369

371370
/**
372371
* Creates a complete mock database object with insert, update, and select operations.
@@ -393,13 +392,13 @@ export type MockDb = TestableDb
393392
* })
394393
* ```
395394
*/
396-
export function createMockDb(config: MockDbFactoryConfig = {}): TestableDb {
397-
// Use type assertion since Mock types don't perfectly match TestableDb
395+
export function createMockDb(config: MockDbFactoryConfig = {}): DbOperations {
396+
// Use type assertion since Mock types don't perfectly match DbOperations
398397
// but the runtime behavior is correct
399398
return {
400399
insert: createMockDbInsert(config.insert),
401400
update: createMockDbUpdate(config.update),
402-
select: createMockDbSimpleSelect(config.select) as TestableDb['select'],
401+
select: createMockDbSimpleSelect(config.select) as DbOperations['select'],
403402
}
404403
}
405404

@@ -421,10 +420,10 @@ export function createMockDbWithErrors(config: {
421420
selectError?: Error
422421
/** Results to return from select queries (before any error is thrown) */
423422
selectResults?: unknown[]
424-
} = {}): TestableDb {
423+
} = {}): DbOperations {
425424
const { insertError, updateError, selectError, selectResults = [] } = config
426425

427-
// Use type assertion since Mock types don't perfectly match TestableDb
426+
// Use type assertion since Mock types don't perfectly match DbOperations
428427
// but the runtime behavior is correct
429428
return {
430429
insert: mock(() => ({
@@ -466,7 +465,7 @@ export function createMockDbWithErrors(config: {
466465
return cb?.(selectResults) ?? selectResults
467466
}),
468467
})),
469-
})) as TestableDb['select'],
468+
})) as DbOperations['select'],
470469
}
471470
}
472471

@@ -494,8 +493,8 @@ export function createMockDbWithErrors(config: {
494493
* })
495494
* ```
496495
*/
497-
export function createSelectOnlyMockDb(selectResults: unknown[]): TestableDb {
498-
const createWhereResult = (): TestableDbWhereResult => ({
496+
export function createSelectOnlyMockDb(selectResults: unknown[]): DbOperations {
497+
const createWhereResult = (): DbWhereResult => ({
499498
then: <TResult = unknown[]>(
500499
onfulfilled?:
501500
| ((value: unknown[]) => TResult | PromiseLike<TResult>)

packages/internal/src/utils/version-utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { and, desc, eq } from 'drizzle-orm'
22

33
import * as schema from '@codebuff/internal/db/schema'
44

5-
import type { TestableDb } from '@codebuff/common/testing/mock-db'
5+
import type { DbOperations } from '@codebuff/common/testing/mock-db'
66

77
export type Version = { major: number; minor: number; patch: number }
88

@@ -54,7 +54,7 @@ export function isGreater(v1: Version, v2: Version): boolean {
5454
export async function getLatestAgentVersion(params: {
5555
agentId: string
5656
publisherId: string
57-
db: TestableDb
57+
db: DbOperations
5858
}): Promise<Version> {
5959
const { agentId, publisherId, db } = params
6060

@@ -96,7 +96,7 @@ export async function determineNextVersion(params: {
9696
agentId: string
9797
publisherId: string
9898
providedVersion?: string
99-
db: TestableDb
99+
db: DbOperations
100100
}): Promise<Version> {
101101
const { agentId, publisherId, providedVersion, db } = params
102102

@@ -137,7 +137,7 @@ export async function versionExists(params: {
137137
agentId: string
138138
version: Version
139139
publisherId: string
140-
db: TestableDb
140+
db: DbOperations
141141
}): Promise<boolean> {
142142
const { agentId, version, publisherId, db } = params
143143

web/src/app/api/v1/agent-runs/[runId]/steps/_post.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { z } from 'zod'
88

99
import type { TrackEventFn } from '@codebuff/common/types/contracts/analytics'
1010
import type { GetUserInfoFromApiKeyFn } from '@codebuff/common/types/contracts/database'
11-
import type { TestableDb } from '@codebuff/common/testing/mock-db'
11+
import type { DbOperations } from '@codebuff/common/testing/mock-db'
1212
import type {
1313
Logger,
1414
LoggerWithContextFn,
@@ -34,7 +34,7 @@ export async function postAgentRunsSteps(params: {
3434
logger: Logger
3535
loggerWithContext: LoggerWithContextFn
3636
trackEvent: TrackEventFn
37-
db: TestableDb
37+
db: DbOperations
3838
}) {
3939
const {
4040
req,

web/src/app/api/v1/agent-runs/_post.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { z } from 'zod'
88

99
import type { TrackEventFn } from '@codebuff/common/types/contracts/analytics'
1010
import type { GetUserInfoFromApiKeyFn } from '@codebuff/common/types/contracts/database'
11-
import type { TestableDb } from '@codebuff/common/testing/mock-db'
11+
import type { DbOperations } from '@codebuff/common/testing/mock-db'
1212
import type {
1313
Logger,
1414
LoggerWithContextFn,
@@ -43,7 +43,7 @@ async function handleStartAction(params: {
4343
userId: string
4444
logger: Logger
4545
trackEvent: TrackEventFn
46-
db: TestableDb
46+
db: DbOperations
4747
}) {
4848
const { data, userId, logger, trackEvent, db } = params
4949
const { agentId, ancestorRunIds } = data
@@ -105,7 +105,7 @@ async function handleFinishAction(params: {
105105
userId: string
106106
logger: Logger
107107
trackEvent: TrackEventFn
108-
db: TestableDb
108+
db: DbOperations
109109
}) {
110110
const { data, userId, logger, trackEvent, db } = params
111111
const {
@@ -174,7 +174,7 @@ export async function postAgentRuns(params: {
174174
logger: Logger
175175
loggerWithContext: LoggerWithContextFn
176176
trackEvent: TrackEventFn
177-
db: TestableDb
177+
db: DbOperations
178178
}) {
179179
const { req, getUserInfoFromApiKey, loggerWithContext, trackEvent, db } =
180180
params

0 commit comments

Comments
 (0)