@@ -206,6 +206,69 @@ func TestDetachDeleteStatsTracking(t *testing.T) {
206206 assert .Equal (t , int64 (1 ), nc , "After flush: 1 node (nodeB)" )
207207 assert .Equal (t , int64 (0 ), ec , "After flush: 0 edges" )
208208 })
209+
210+ t .Run ("delete_nonexistent_node_does_not_affect_count" , func (t * testing.T ) {
211+ // This tests the critical fix: deleting a non-existent node should NOT
212+ // decrement the count and cause it to go negative
213+ engine := NewMemoryEngine ()
214+ asyncConfig := & AsyncEngineConfig {
215+ FlushInterval : 100 * time .Millisecond ,
216+ }
217+ asyncEngine := NewAsyncEngine (engine , asyncConfig )
218+ defer asyncEngine .Close ()
219+
220+ // Initial count should be 0
221+ count , err := asyncEngine .NodeCount ()
222+ require .NoError (t , err )
223+ assert .Equal (t , int64 (0 ), count , "Initial count should be 0" )
224+
225+ // Try to delete a non-existent node
226+ err = asyncEngine .DeleteNode ("nonexistent_node_id" )
227+ assert .Equal (t , ErrNotFound , err , "Deleting non-existent node should return ErrNotFound" )
228+
229+ // Count should still be 0, not -1
230+ count , err = asyncEngine .NodeCount ()
231+ require .NoError (t , err )
232+ assert .Equal (t , int64 (0 ), count , "Count should remain 0 after deleting non-existent node" )
233+
234+ // Flush and verify
235+ require .NoError (t , asyncEngine .Flush ())
236+
237+ count , err = asyncEngine .NodeCount ()
238+ require .NoError (t , err )
239+ assert .Equal (t , int64 (0 ), count , "Count should remain 0 after flush" )
240+ })
241+
242+ t .Run ("delete_nonexistent_edge_does_not_affect_count" , func (t * testing.T ) {
243+ // Similar test for edges
244+ engine := NewMemoryEngine ()
245+ asyncConfig := & AsyncEngineConfig {
246+ FlushInterval : 100 * time .Millisecond ,
247+ }
248+ asyncEngine := NewAsyncEngine (engine , asyncConfig )
249+ defer asyncEngine .Close ()
250+
251+ // Initial count should be 0
252+ count , err := asyncEngine .EdgeCount ()
253+ require .NoError (t , err )
254+ assert .Equal (t , int64 (0 ), count , "Initial edge count should be 0" )
255+
256+ // Try to delete a non-existent edge
257+ err = asyncEngine .DeleteEdge ("nonexistent_edge_id" )
258+ assert .Equal (t , ErrNotFound , err , "Deleting non-existent edge should return ErrNotFound" )
259+
260+ // Count should still be 0
261+ count , err = asyncEngine .EdgeCount ()
262+ require .NoError (t , err )
263+ assert .Equal (t , int64 (0 ), count , "Edge count should remain 0 after deleting non-existent edge" )
264+
265+ // Flush and verify
266+ require .NoError (t , asyncEngine .Flush ())
267+
268+ count , err = asyncEngine .EdgeCount ()
269+ require .NoError (t , err )
270+ assert .Equal (t , int64 (0 ), count , "Edge count should remain 0 after flush" )
271+ })
209272}
210273
211274// TestBadgerEngineDetachDeleteStats tests the underlying BadgerEngine directly
0 commit comments