Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .claude/docs/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ dominate package wall time.
rename the file to `*_internal_test.go` instead. A directive plus a
justification comment is strictly worse than the established naming
convention, and the repo standardizes on the latter.
- **Do not add `export_test.go`.** First rework the test so that it needs
only the exported API. As a last resort, move a test that needs an
unexported symbol to a `*_internal_test.go` file. The existing
`export_test.go` files are legacy: do not extend them.

## RFC Protocol Testing

Expand Down
6 changes: 6 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ linters:
checks:
- all
- -SA1019
testpackage:
skip-regexp: _internal_test\.go$
usetesting:
context-background: false
context-todo: false
Expand All @@ -190,6 +192,10 @@ linters:
- linters:
- exhaustruct
path: ^scripts/
# Legacy export_test.go shims. Do not add to this list.
- linters:
- testpackage
path: ^coderd/(|aibridged/|x/chatd/|x/chatd/chatstate/|x/chatd/mcpclient/)export_test\.go$
- linters:
- ALL
path: scripts/rules.go
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Docs use `pnpm run format-docs` and `pnpm run lint-docs`. Frontend commands live
- **Public API:** add the required Swagger annotations for new public HTTP endpoints.
- **Transactions:** keep `InTx` work on the transaction handle. Prefer explicit database-to-SDK converters.
- **Concurrent tests:** call `t.Parallel()`, use unique identifiers, and do not use `time.Sleep` to mask timing problems.
- **Go test packages:** default to `package foo_test` and test through the exported API. Only as a last resort, put a test that needs unexported symbols in a `*_internal_test.go` file with `package foo`. Never add an `export_test.go` shim or a `//nolint:testpackage` directive.
- **Frontend:** reuse shared UI primitives. Prefer Vitest and `userEvent` tests that assert the non-visual outcome of the interaction (callback, request, state); extend existing coverage instead of adding a new test when equivalent coverage already exists. Use Storybook stories only for visual components that should be covered by the visual regression tool, which Pixel screenshots in CI.
- **GitHub Actions:** set top-level `permissions: {}` and grant only required permissions per job.

Expand Down
7 changes: 0 additions & 7 deletions agent/agentcontext/export_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions agent/agentcontext/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ func (m *Manager) Run(ctx context.Context) error {
}
}

// started returns a channel that is closed once Run has
// Started returns a channel that is closed once Run has
// claimed the running flag. Tests use it to coordinate with
// the watcher loop without polling; a closed channel never
// blocks, so this is safe to call repeatedly.
func (m *Manager) started() <-chan struct{} {
func (m *Manager) Started() <-chan struct{} {
return m.runStartedCh
}

Expand Down
2 changes: 1 addition & 1 deletion agent/agentcontext/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func TestManager_RunOnce(t *testing.T) {
// second call rejects with a deterministic error rather than
// racing the scheduler.
select {
case <-agentcontext.ManagerStarted(m):
case <-m.Started():
case <-ctx.Done():
t.Fatalf("manager never started: %v", ctx.Err())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package filefinder_test
package filefinder

import (
"context"
Expand All @@ -14,7 +14,6 @@ import (

"cdr.dev/slog/v3"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/agent/filefinder"
)

var (
Expand Down Expand Up @@ -76,11 +75,11 @@ func generateFileTree(t testing.TB, root string, n int, seed int64) {

// buildIndex walks root and returns a populated Index, the same
// way Engine.AddRoot does but without starting a watcher.
func buildIndex(t testing.TB, root string) *filefinder.Index {
func buildIndex(t testing.TB, root string) *Index {
t.Helper()
absRoot, err := filepath.Abs(root)
require.NoError(t, err)
idx, err := filefinder.BuildTestIndex(absRoot)
idx, err := walkRoot(absRoot)
require.NoError(t, err)
return idx
}
Expand Down Expand Up @@ -147,14 +146,14 @@ func BenchmarkSearch_ByScale(b *testing.B) {
generateFileTree(b, dir, sc.n, 42)
idx := buildIndex(b, dir)
snap := idx.Snapshot()
opts := filefinder.DefaultSearchOptions()
opts := DefaultSearchOptions()

for _, q := range queries {
b.Run(q.name, func(b *testing.B) {
p := filefinder.NewQueryPlanForTest(q.query)
p := newQueryPlan(q.query)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = filefinder.SearchSnapshotForTest(p, snap, opts.MaxCandidates)
_ = searchSnapshot(p, snap, opts.MaxCandidates)
}
})
}
Expand All @@ -168,11 +167,11 @@ func BenchmarkSearch_ConcurrentReads(b *testing.B) {

logger := slogtest.Make(b, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelError)
ctx := context.Background()
eng := filefinder.NewEngine(logger)
eng := NewEngine(logger)
require.NoError(b, eng.AddRoot(ctx, dir))
b.Cleanup(func() { _ = eng.Close() })

opts := filefinder.DefaultSearchOptions()
opts := DefaultSearchOptions()
goroutines := []int{1, 4, 16, 64}

for _, g := range goroutines {
Expand Down Expand Up @@ -223,12 +222,12 @@ func BenchmarkDeltaUpdate(b *testing.B) {
idx.Add(fmt.Sprintf("injected/extra/file_%d.go", i), 0)
}
snap := idx.Snapshot()
plan := filefinder.NewQueryPlanForTest("handler")
opts := filefinder.DefaultSearchOptions()
plan := newQueryPlan("handler")
opts := DefaultSearchOptions()

b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = filefinder.SearchSnapshotForTest(plan, snap, opts.MaxCandidates)
_ = searchSnapshot(plan, snap, opts.MaxCandidates)
}
})
}
Expand Down Expand Up @@ -288,8 +287,8 @@ func BenchmarkSearch_ConcurrentReads_Throughput(b *testing.B) {
snap := idx.Snapshot()

goroutines := []int{1, 4, 16, 64}
plan := filefinder.NewQueryPlanForTest("handler.go")
maxCands := filefinder.DefaultSearchOptions().MaxCandidates
plan := newQueryPlan("handler.go")
maxCands := DefaultSearchOptions().MaxCandidates

for _, g := range goroutines {
b.Run(fmt.Sprintf("goroutines_%d", g), func(b *testing.B) {
Expand All @@ -302,7 +301,7 @@ func BenchmarkSearch_ConcurrentReads_Throughput(b *testing.B) {
for gi := 0; gi < g; gi++ {
wg.Go(func() {
for j := 0; j < perGoroutine; j++ {
_ = filefinder.SearchSnapshotForTest(plan, snap, maxCands)
_ = searchSnapshot(plan, snap, maxCands)
}
})
}
Expand Down
63 changes: 63 additions & 0 deletions agent/filefinder/delta_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package filefinder

import (
"testing"
)

func TestIndex_AddOverwrite(t *testing.T) {
t.Parallel()
idx := NewIndex()
idx.Add("foo/bar.go", uint16(FlagFile))
idx.Add("foo/bar.go", uint16(FlagDir)) // overwrite
if idx.Len() != 1 {
t.Fatalf("expected 1 after overwrite, got %d", idx.Len())
}
// The old entry should be tombstoned.
if !idx.deleted[0] {
t.Fatal("expected old entry to be deleted")
}
if idx.deleted[1] {
t.Fatal("expected new entry to be live")
}
}

func TestIndex_Snapshot(t *testing.T) {
t.Parallel()
idx := NewIndex()
idx.Add("foo/bar.go", 0)
idx.Add("foo/baz.go", 0)

snap := idx.Snapshot()
if len(snap.docs) != 2 {
t.Fatalf("expected snapshot count 2, got %d", len(snap.docs))
}

// Adding more docs after snapshot doesn't affect it.
idx.Add("foo/qux.go", 0)
if len(snap.docs) != 2 {
t.Fatal("snapshot count should not change after new adds")
}
}

func TestIndex_TrigramIndex(t *testing.T) {
t.Parallel()
idx := NewIndex()
idx.Add("handler.go", 0)

// "handler.go" should produce trigrams for "handler.go".
// Check that at least one trigram exists.
if len(idx.byGram) == 0 {
t.Fatal("expected non-empty trigram index")
}
}

func TestIndex_PrefixIndex(t *testing.T) {
t.Parallel()
idx := NewIndex()
idx.Add("handler.go", 0)

// basename is "handler.go", first byte is 'h'
if len(idx.byPrefix1['h']) == 0 {
t.Fatal("expected prefix1['h'] to be non-empty")
}
}
58 changes: 0 additions & 58 deletions agent/filefinder/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,64 +43,6 @@ func TestIndex_Remove(t *testing.T) {
}
}

func TestIndex_AddOverwrite(t *testing.T) {
t.Parallel()
idx := filefinder.NewIndex()
idx.Add("foo/bar.go", uint16(filefinder.FlagFile))
idx.Add("foo/bar.go", uint16(filefinder.FlagDir)) // overwrite
if idx.Len() != 1 {
t.Fatalf("expected 1 after overwrite, got %d", idx.Len())
}
// The old entry should be tombstoned.
if !filefinder.IndexIsDeleted(idx, 0) {
t.Fatal("expected old entry to be deleted")
}
if filefinder.IndexIsDeleted(idx, 1) {
t.Fatal("expected new entry to be live")
}
}

func TestIndex_Snapshot(t *testing.T) {
t.Parallel()
idx := filefinder.NewIndex()
idx.Add("foo/bar.go", 0)
idx.Add("foo/baz.go", 0)

snap := idx.Snapshot()
if filefinder.SnapshotCount(snap) != 2 {
t.Fatalf("expected snapshot count 2, got %d", filefinder.SnapshotCount(snap))
}

// Adding more docs after snapshot doesn't affect it.
idx.Add("foo/qux.go", 0)
if filefinder.SnapshotCount(snap) != 2 {
t.Fatal("snapshot count should not change after new adds")
}
}

func TestIndex_TrigramIndex(t *testing.T) {
t.Parallel()
idx := filefinder.NewIndex()
idx.Add("handler.go", 0)

// "handler.go" should produce trigrams for "handler.go".
// Check that at least one trigram exists.
if filefinder.IndexByGramLen(idx) == 0 {
t.Fatal("expected non-empty trigram index")
}
}

func TestIndex_PrefixIndex(t *testing.T) {
t.Parallel()
idx := filefinder.NewIndex()
idx.Add("handler.go", 0)

// basename is "handler.go", first byte is 'h'
if filefinder.IndexByPrefix1Len(idx, 'h') == 0 {
t.Fatal("expected prefix1['h'] to be non-empty")
}
}

func TestIndex_RemoveNonexistent(t *testing.T) {
t.Parallel()
idx := filefinder.NewIndex()
Expand Down
5 changes: 3 additions & 2 deletions agent/filefinder/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ func TestEngine_AddRootIdempotent(t *testing.T) {
require.NoError(t, eng.AddRoot(ctx, dir))
require.NoError(t, eng.AddRoot(ctx, dir))

snapLen := filefinder.EngineSnapLen(eng)
require.Equal(t, 1, snapLen, "expected exactly one root after duplicate add")
results, err := eng.Search(ctx, "file", filefinder.DefaultSearchOptions())
require.NoError(t, err)
require.Len(t, results, 1, "expected exactly one root after duplicate add")
}

func TestEngine_RemoveRoot(t *testing.T) {
Expand Down
85 changes: 0 additions & 85 deletions agent/filefinder/export_test.go

This file was deleted.

Loading
Loading