Skip to content

Commit c611780

Browse files
committed
refactor(testing): move TestableDb interface to mock-db.ts
TestableDb is a testing-specific interface, so it belongs with the mock utilities.
1 parent 4bb1046 commit c611780

File tree

5 files changed

+51
-58
lines changed

5 files changed

+51
-58
lines changed

common/src/testing/mock-db.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
1-
import type {
2-
TestableDb,
3-
TestableDbWhereResult,
4-
} from '@codebuff/common/types/contracts/database'
51
import type { Logger } from '@codebuff/common/types/contracts/logger'
62

3+
// ============================================================================
4+
// Testable Database Interface
5+
// ============================================================================
6+
7+
/* eslint-disable @typescript-eslint/no-explicit-any */
8+
/**
9+
* Minimal database interface for dependency injection in API routes.
10+
* Both the real CodebuffPgDatabase and test mocks can satisfy this interface.
11+
*
12+
* This allows tests to provide mock implementations without type casting.
13+
* Uses `any` for table/column parameters to be compatible with Drizzle ORM's
14+
* specific table types while remaining flexible for mocks.
15+
*/
16+
export interface TestableDb {
17+
insert: (table: any) => {
18+
values: (data: any) => PromiseLike<any>
19+
}
20+
update: (table: any) => {
21+
set: (data: any) => {
22+
where: (condition: any) => PromiseLike<any>
23+
}
24+
}
25+
select: (columns?: any) => {
26+
from: (table: any) => {
27+
where: (condition: any) => TestableDbWhereResult
28+
}
29+
}
30+
}
31+
32+
/**
33+
* Result type for where() that supports multiple query patterns:
34+
* - .limit(n) for simple queries
35+
* - .orderBy(...).limit(n) for sorted queries
36+
* - .then() for promise-like resolution
37+
*/
38+
export interface TestableDbWhereResult {
39+
then: <TResult = any[]>(
40+
onfulfilled?: ((value: any[]) => TResult | PromiseLike<TResult>) | null | undefined,
41+
) => PromiseLike<TResult>
42+
limit: (n: number) => PromiseLike<any[]>
43+
orderBy: (...columns: any[]) => {
44+
limit: (n: number) => PromiseLike<any[]>
45+
}
46+
}
47+
/* eslint-enable @typescript-eslint/no-explicit-any */
48+
749
// Compatibility layer: use bun:test mock when available, otherwise identity function
850
// This allows the utilities to work in both Bun and Jest environments
951
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-explicit-any */

common/src/types/contracts/database.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -93,48 +93,3 @@ export type AddAgentStepFn = (params: {
9393

9494
export type DatabaseAgentCache = Map<string, AgentTemplate | null>
9595

96-
// ============================================================================
97-
// Testable Database Interface
98-
// ============================================================================
99-
100-
/* eslint-disable @typescript-eslint/no-explicit-any */
101-
/**
102-
* Minimal database interface for dependency injection in API routes.
103-
* Both the real CodebuffPgDatabase and test mocks can satisfy this interface.
104-
*
105-
* This allows tests to provide mock implementations without type casting.
106-
* Uses `any` for table/column parameters to be compatible with Drizzle ORM's
107-
* specific table types while remaining flexible for mocks.
108-
*/
109-
export interface TestableDb {
110-
insert: (table: any) => {
111-
values: (data: any) => PromiseLike<any>
112-
}
113-
update: (table: any) => {
114-
set: (data: any) => {
115-
where: (condition: any) => PromiseLike<any>
116-
}
117-
}
118-
select: (columns?: any) => {
119-
from: (table: any) => {
120-
where: (condition: any) => TestableDbWhereResult
121-
}
122-
}
123-
}
124-
125-
/**
126-
* Result type for where() that supports multiple query patterns:
127-
* - .limit(n) for simple queries
128-
* - .orderBy(...).limit(n) for sorted queries
129-
* - .then() for promise-like resolution
130-
*/
131-
export interface TestableDbWhereResult {
132-
then: <TResult = any[]>(
133-
onfulfilled?: ((value: any[]) => TResult | PromiseLike<TResult>) | null | undefined,
134-
) => PromiseLike<TResult>
135-
limit: (n: number) => PromiseLike<any[]>
136-
orderBy: (...columns: any[]) => {
137-
limit: (n: number) => PromiseLike<any[]>
138-
}
139-
}
140-
/* eslint-enable @typescript-eslint/no-explicit-any */

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

Lines changed: 1 addition & 1 deletion
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/types/contracts/database'
5+
import type { TestableDb } from '@codebuff/common/testing/mock-db'
66

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

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import { NextResponse } from 'next/server'
77
import { z } from 'zod'
88

99
import type { TrackEventFn } from '@codebuff/common/types/contracts/analytics'
10-
import type {
11-
GetUserInfoFromApiKeyFn,
12-
TestableDb,
13-
} from '@codebuff/common/types/contracts/database'
10+
import type { GetUserInfoFromApiKeyFn } from '@codebuff/common/types/contracts/database'
11+
import type { TestableDb } from '@codebuff/common/testing/mock-db'
1412
import type {
1513
Logger,
1614
LoggerWithContextFn,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import { NextResponse } from 'next/server'
77
import { z } from 'zod'
88

99
import type { TrackEventFn } from '@codebuff/common/types/contracts/analytics'
10-
import type {
11-
GetUserInfoFromApiKeyFn,
12-
TestableDb,
13-
} from '@codebuff/common/types/contracts/database'
10+
import type { GetUserInfoFromApiKeyFn } from '@codebuff/common/types/contracts/database'
11+
import type { TestableDb } from '@codebuff/common/testing/mock-db'
1412
import type {
1513
Logger,
1614
LoggerWithContextFn,

0 commit comments

Comments
 (0)