@@ -169,3 +169,157 @@ describe('BuildService failure boundaries', () => {
169169 expect ( recordDeployFailure ) . not . toHaveBeenCalled ( ) ;
170170 } ) ;
171171} ) ;
172+
173+ describe ( 'BuildService queue fingerprinting' , ( ) => {
174+ let buildService : BuildService ;
175+ let mockBuildQuery : any ;
176+ let mockBuildQueueAdd : jest . Mock ;
177+ let mockResolveQueueAdd : jest . Mock ;
178+
179+ const createMockBuild = ( overrides : any = { } ) =>
180+ ( {
181+ id : 1 ,
182+ enableFullYaml : true ,
183+ commentRuntimeEnv : { FEATURE_FLAG : 'on' } ,
184+ commentInitEnv : { } ,
185+ pullRequest : { latestCommit : 'abcdef123456' } ,
186+ deploys : [
187+ {
188+ id : 11 ,
189+ uuid : 'api-deploy' ,
190+ githubRepositoryId : 100 ,
191+ branchName : 'feature-branch' ,
192+ active : true ,
193+ publicUrl : 'https://example.test/api' ,
194+ env : { API_URL : 'https://api.test' } ,
195+ initEnv : { INIT_MODE : 'warm' } ,
196+ deployable : { name : 'api' , commentBranchName : null } ,
197+ service : { name : 'api' } ,
198+ } ,
199+ {
200+ id : 22 ,
201+ uuid : 'worker-deploy' ,
202+ githubRepositoryId : 200 ,
203+ branchName : 'feature-branch' ,
204+ active : true ,
205+ publicUrl : 'https://example.test/worker' ,
206+ env : { QUEUE : 'jobs' } ,
207+ initEnv : { } ,
208+ deployable : { name : 'worker' , commentBranchName : 'worker-override' } ,
209+ service : { name : 'worker' } ,
210+ } ,
211+ ] ,
212+ $fetchGraph : jest . fn ( ) . mockResolvedValue ( undefined ) ,
213+ ...overrides ,
214+ } as any ) ;
215+
216+ beforeEach ( ( ) => {
217+ jest . clearAllMocks ( ) ;
218+
219+ mockBuildQueueAdd = jest . fn ( ) . mockResolvedValue ( undefined ) ;
220+ mockResolveQueueAdd = jest . fn ( ) . mockResolvedValue ( undefined ) ;
221+
222+ mockBuildQuery = {
223+ findOne : jest . fn ( ) . mockReturnThis ( ) ,
224+ withGraphFetched : jest . fn ( ) ,
225+ } ;
226+
227+ const queueManager = {
228+ registerQueue : jest . fn ( ( ) => ( {
229+ add : jest . fn ( ) ,
230+ process : jest . fn ( ) ,
231+ on : jest . fn ( ) ,
232+ } ) ) ,
233+ } ;
234+
235+ buildService = new BuildService (
236+ {
237+ models : {
238+ Build : {
239+ query : jest . fn ( ( ) => mockBuildQuery ) ,
240+ } ,
241+ } ,
242+ services : { } ,
243+ } as any ,
244+ { } as any ,
245+ { } as any ,
246+ queueManager as any
247+ ) ;
248+ ( buildService as any ) . buildQueue = { add : mockBuildQueueAdd } ;
249+ ( buildService as any ) . resolveAndDeployBuildQueue = { add : mockResolveQueueAdd } ;
250+ } ) ;
251+
252+ test ( 'changes fingerprint when comment runtime env changes' , async ( ) => {
253+ const baseBuild = createMockBuild ( ) ;
254+ const changedBuild = createMockBuild ( {
255+ commentRuntimeEnv : { FEATURE_FLAG : 'off' } ,
256+ } ) ;
257+
258+ const baseFingerprint = await buildService . computeBuildRequestFingerprint ( baseBuild ) ;
259+ const changedFingerprint = await buildService . computeBuildRequestFingerprint ( changedBuild ) ;
260+
261+ expect ( baseFingerprint ) . not . toEqual ( changedFingerprint ) ;
262+ } ) ;
263+
264+ test ( 'changes fingerprint when repository filter changes' , async ( ) => {
265+ const build = createMockBuild ( ) ;
266+
267+ const apiFingerprint = await buildService . computeBuildRequestFingerprint ( build , 100 ) ;
268+ const workerFingerprint = await buildService . computeBuildRequestFingerprint ( build , 200 ) ;
269+
270+ expect ( apiFingerprint ) . not . toEqual ( workerFingerprint ) ;
271+ } ) ;
272+
273+ test ( 'enqueues resolve queue with deduplication derived from the current build fingerprint' , async ( ) => {
274+ const build = createMockBuild ( ) ;
275+ mockBuildQuery . withGraphFetched . mockResolvedValue ( build ) ;
276+
277+ const expectedFingerprint = await buildService . computeBuildRequestFingerprint ( build , 100 ) ;
278+
279+ await buildService . enqueueResolveAndDeployBuild ( {
280+ buildId : 1 ,
281+ githubRepositoryId : 100 ,
282+ correlationId : 'corr-1' ,
283+ } ) ;
284+
285+ expect ( mockResolveQueueAdd ) . toHaveBeenCalledWith (
286+ 'resolve-deploy' ,
287+ expect . objectContaining ( {
288+ buildId : 1 ,
289+ githubRepositoryId : 100 ,
290+ correlationId : 'corr-1' ,
291+ } ) ,
292+ expect . objectContaining ( {
293+ deduplication : {
294+ id : `resolve:1:${ expectedFingerprint } ` ,
295+ ttl : 30000 ,
296+ } ,
297+ } )
298+ ) ;
299+ } ) ;
300+
301+ test ( 'enqueues build queue with a deterministic job id derived from the current build fingerprint' , async ( ) => {
302+ const build = createMockBuild ( ) ;
303+ mockBuildQuery . withGraphFetched . mockResolvedValue ( build ) ;
304+
305+ const expectedFingerprint = await buildService . computeBuildRequestFingerprint ( build , 100 ) ;
306+
307+ await buildService . enqueueBuildJob ( {
308+ buildId : 1 ,
309+ githubRepositoryId : 100 ,
310+ correlationId : 'corr-2' ,
311+ } ) ;
312+
313+ expect ( mockBuildQueueAdd ) . toHaveBeenCalledWith (
314+ 'build' ,
315+ expect . objectContaining ( {
316+ buildId : 1 ,
317+ githubRepositoryId : 100 ,
318+ correlationId : 'corr-2' ,
319+ } ) ,
320+ expect . objectContaining ( {
321+ jobId : `build:1:${ expectedFingerprint } ` ,
322+ } )
323+ ) ;
324+ } ) ;
325+ } ) ;
0 commit comments