1+ import {
2+ clearMockedModules ,
3+ mockModule ,
4+ } from '@codebuff/common/testing/mock-modules'
5+ import { getStubProjectFileContext } from '@codebuff/common/util/file'
16import {
27 describe ,
38 expect ,
@@ -6,12 +11,9 @@ import {
611 afterEach ,
712 spyOn ,
813 mock ,
14+ beforeAll ,
15+ afterAll ,
916} from 'bun:test'
10- import {
11- clearMockedModules ,
12- mockModule ,
13- } from '@codebuff/common/testing/mock-modules'
14- import { getStubProjectFileContext } from '@codebuff/common/util/file'
1517
1618import {
1719 getAgentTemplate ,
@@ -20,53 +22,8 @@ import {
2022} from '../templates/agent-registry'
2123
2224import type { AgentTemplate } from '../templates/types'
23- import type { ProjectFileContext } from '@codebuff/common/util/file'
2425import type { DynamicAgentTemplate } from '@codebuff/common/types/dynamic-agent-template'
25-
26- // Mock the database module
27- mockModule ( '@codebuff/common/db' , ( ) => ( {
28- default : {
29- select : ( ) => ( {
30- from : ( ) => ( {
31- where : ( ) => ( {
32- orderBy : ( ) => ( {
33- limit : ( ) => Promise . resolve ( [ ] ) ,
34- } ) ,
35- then : ( fn : ( rows : any [ ] ) => any ) => fn ( [ ] ) ,
36- } ) ,
37- } ) ,
38- } ) ,
39- } ,
40- } ) )
41-
42- // Mock the schema module
43- mockModule ( '@codebuff/common/db/schema' , ( ) => ( {
44- agentConfig : {
45- id : 'id' ,
46- publisher_id : 'publisher_id' ,
47- version : 'version' ,
48- major : 'major' ,
49- minor : 'minor' ,
50- patch : 'patch' ,
51- data : 'data' ,
52- } ,
53- } ) )
54-
55- // Mock drizzle-orm
56- mockModule ( 'drizzle-orm' , ( ) => ( {
57- and : ( ...args : any [ ] ) => ( { type : 'and' , args } ) ,
58- desc : ( field : any ) => ( { type : 'desc' , field } ) ,
59- eq : ( field : any , value : any ) => ( { type : 'eq' , field, value } ) ,
60- } ) )
61-
62- // Mock logger
63- mockModule ( '../util/logger' , ( ) => ( {
64- logger : {
65- debug : ( ) => { } ,
66- error : ( ) => { } ,
67- warn : ( ) => { } ,
68- } ,
69- } ) )
26+ import type { ProjectFileContext } from '@codebuff/common/util/file'
7027
7128// Create mock static templates that will be used by the agent registry
7229const mockStaticTemplates : Record < string , AgentTemplate > = {
@@ -100,16 +57,63 @@ const mockStaticTemplates: Record<string, AgentTemplate> = {
10057 } ,
10158}
10259
103- // Mock static agent templates
104- mockModule ( '@codebuff/backend/templates/agent-list' , ( ) => ( {
105- agentTemplates : mockStaticTemplates ,
106- } ) )
107-
10860// We'll spy on the validation functions instead of mocking the entire module
10961
11062describe ( 'Agent Registry' , ( ) => {
11163 let mockFileContext : ProjectFileContext
11264
65+ beforeAll ( ( ) => {
66+ // Mock the database module
67+ mockModule ( '@codebuff/common/db' , ( ) => ( {
68+ default : {
69+ select : ( ) => ( {
70+ from : ( ) => ( {
71+ where : ( ) => ( {
72+ orderBy : ( ) => ( {
73+ limit : ( ) => Promise . resolve ( [ ] ) ,
74+ } ) ,
75+ then : ( fn : ( rows : any [ ] ) => any ) => fn ( [ ] ) ,
76+ } ) ,
77+ } ) ,
78+ } ) ,
79+ } ,
80+ } ) )
81+
82+ // Mock the schema module
83+ mockModule ( '@codebuff/common/db/schema' , ( ) => ( {
84+ agentConfig : {
85+ id : 'id' ,
86+ publisher_id : 'publisher_id' ,
87+ version : 'version' ,
88+ major : 'major' ,
89+ minor : 'minor' ,
90+ patch : 'patch' ,
91+ data : 'data' ,
92+ } ,
93+ } ) )
94+
95+ // Mock drizzle-orm
96+ mockModule ( 'drizzle-orm' , ( ) => ( {
97+ and : ( ...args : any [ ] ) => ( { type : 'and' , args } ) ,
98+ desc : ( field : any ) => ( { type : 'desc' , field } ) ,
99+ eq : ( field : any , value : any ) => ( { type : 'eq' , field, value } ) ,
100+ } ) )
101+
102+ // Mock logger
103+ mockModule ( '../util/logger' , ( ) => ( {
104+ logger : {
105+ debug : ( ) => { } ,
106+ error : ( ) => { } ,
107+ warn : ( ) => { } ,
108+ } ,
109+ } ) )
110+
111+ // Mock static agent templates
112+ mockModule ( '@codebuff/backend/templates/agent-list' , ( ) => ( {
113+ agentTemplates : mockStaticTemplates ,
114+ } ) )
115+ } )
116+
113117 beforeEach ( async ( ) => {
114118 // Clear cache before each test
115119 clearDatabaseCache ( )
@@ -168,6 +172,9 @@ describe('Agent Registry', () => {
168172
169173 afterEach ( ( ) => {
170174 mock . restore ( )
175+ } )
176+
177+ afterAll ( ( ) => {
171178 clearMockedModules ( )
172179 } )
173180
0 commit comments