MCPcopy Index your code
hub / github.com/coder/coder / TestScanDeltaDetectsContentChanges

Function TestScanDeltaDetectsContentChanges

agent/agentgit/agentgit_test.go:325–387  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

323}
324
325func TestScanDeltaDetectsContentChanges(t *testing.T) {
326 t.Parallel()
327
328 repoDir := initTestRepo(t)
329 logger := slogtest.Make(t, nil)
330
331 h := agentgit.NewHandler(logger)
332
333 // Modify a committed file.
334 readmePath := filepath.Join(repoDir, "README.md")
335 require.NoError(t, os.WriteFile(readmePath, []byte("# Edit 1\n"), 0o600))
336
337 h.Subscribe([]string{readmePath})
338 ctx := context.Background()
339
340 // First scan — returns the initial dirty state.
341 msg1 := h.Scan(ctx)
342 require.NotNil(t, msg1)
343 require.Len(t, msg1.Repositories, 1)
344
345 require.Contains(t, msg1.Repositories[0].UnifiedDiff, "README.md")
346
347 // Second scan with no changes: heartbeat, no repositories.
348 msg2 := h.Scan(ctx)
349 require.NotNil(t, msg2, "heartbeat should fire even with no delta")
350 require.Empty(t, msg2.Repositories)
351
352 // Now modify the SAME file further (still "Modified" status, but
353 // different content).
354 require.NoError(t, os.WriteFile(readmePath, []byte("# Edit 2\nMore lines\nEven more\n"), 0o600))
355
356 // Third scan — should detect the content change even though the
357 // status is still "Modified".
358 msg3 := h.Scan(ctx)
359 require.NotNil(t, msg3, "content change in already-dirty file should emit delta")
360 require.Len(t, msg3.Repositories, 1)
361
362 require.Contains(t, msg3.Repositories[0].UnifiedDiff, "README.md")
363
364 // Also test an untracked (unstaged) file — its status is "Added"
365 // throughout, but further edits should still emit deltas.
366 untrackedPath := filepath.Join(repoDir, "untracked.go")
367 require.NoError(t, os.WriteFile(untrackedPath, []byte("package main\n"), 0o600))
368
369 h.Subscribe([]string{untrackedPath})
370 msg4 := h.Scan(ctx)
371 require.NotNil(t, msg4)
372
373 require.Contains(t, msg4.Repositories[0].UnifiedDiff, "untracked.go")
374
375 // No changes: heartbeat, no repositories.
376 msg5 := h.Scan(ctx)
377 require.NotNil(t, msg5, "heartbeat should fire even with no delta")
378 require.Empty(t, msg5.Repositories)
379
380 // Modify the untracked file further.
381 require.NoError(t, os.WriteFile(untrackedPath, []byte("package main\n\nfunc init() {}\n"), 0o600))
382

Callers

nothing calls this directly

Calls 8

SubscribeMethod · 0.95
ScanMethod · 0.95
NewHandlerFunction · 0.92
initTestRepoFunction · 0.85
WriteFileMethod · 0.65
LenMethod · 0.45
ContainsMethod · 0.45
EmptyMethod · 0.45

Tested by

no test coverage detected