@@ -214,3 +214,69 @@ func TestExtractBaseBranchFromHeadRef(t *testing.T) {
214214 })
215215 }
216216}
217+
218+ func TestFindUnprocessedGithubMergeIntents (t * testing.T ) {
219+ require .NoError (t , db .Clear (IntentCollection ))
220+ t .Cleanup (func () {
221+ assert .NoError (t , db .Clear (IntentCollection ))
222+ })
223+
224+ recent := time .Now ().UTC ().Round (time .Millisecond )
225+ old := recent .Add (- 2 * time .Hour )
226+ tooOld := recent .Add (- 10 * time .Hour )
227+ require .NoError (t , db .Insert (t .Context (), IntentCollection , githubMergeIntent {
228+ DocumentID : "recent" ,
229+ IntentType : GithubMergeIntentType ,
230+ HeadSHA : "recent_sha" ,
231+ CreatedAt : recent ,
232+ }))
233+ require .NoError (t , db .Insert (t .Context (), IntentCollection , githubMergeIntent {
234+ DocumentID : "old" ,
235+ IntentType : GithubMergeIntentType ,
236+ HeadSHA : "old_sha" ,
237+ CreatedAt : old ,
238+ }))
239+ require .NoError (t , db .Insert (t .Context (), IntentCollection , githubMergeIntent {
240+ DocumentID : "too_old" ,
241+ IntentType : GithubMergeIntentType ,
242+ HeadSHA : "too_old_sha" ,
243+ CreatedAt : tooOld ,
244+ }))
245+ require .NoError (t , db .Insert (t .Context (), IntentCollection , githubMergeIntent {
246+ DocumentID : "processed" ,
247+ IntentType : GithubMergeIntentType ,
248+ HeadSHA : "processed_sha" ,
249+ CreatedAt : recent ,
250+ Processed : true ,
251+ }))
252+ require .NoError (t , db .Insert (t .Context (), IntentCollection , githubIntent {
253+ DocumentID : "pr" ,
254+ IntentType : GithubIntentType ,
255+ HeadHash : "pr_sha" ,
256+ CreatedAt : recent ,
257+ }))
258+
259+ intents , err := FindUnprocessedGithubMergeIntents (t .Context (), recent .Add (- 6 * time .Hour ))
260+ require .NoError (t , err )
261+ assert .ElementsMatch (t , []UnprocessedMergeIntent {
262+ {ID : "recent" , HeadSHA : "recent_sha" , CreatedAt : recent },
263+ {ID : "old" , HeadSHA : "old_sha" , CreatedAt : old },
264+ }, intents )
265+ }
266+
267+ func TestRemoveIntent (t * testing.T ) {
268+ require .NoError (t , db .Clear (IntentCollection ))
269+ t .Cleanup (func () {
270+ assert .NoError (t , db .Clear (IntentCollection ))
271+ })
272+
273+ require .NoError (t , db .Insert (t .Context (), IntentCollection , githubMergeIntent {DocumentID : "remove" , IntentType : GithubMergeIntentType }))
274+ require .NoError (t , db .Insert (t .Context (), IntentCollection , githubMergeIntent {DocumentID : "keep" , IntentType : GithubMergeIntentType }))
275+
276+ require .NoError (t , RemoveIntent (t .Context (), "remove" ))
277+
278+ var intents []githubMergeIntent
279+ require .NoError (t , db .FindAllQ (t .Context (), IntentCollection , db .Query (bson.M {}), & intents ))
280+ require .Len (t , intents , 1 )
281+ assert .Equal (t , "keep" , intents [0 ].DocumentID )
282+ }
0 commit comments