Files
archie/cmd/archied/codesearch_test.go
T
sam 446dc6b3de feat(indexing): port tau's codesearch workspace index (4we)
First slice of the tau cherry-pick strategy: copy-and-mutate rather than
extracting tau to pkg/. Exporting would force designing metrics, session
and logger seams upfront, before archie-core knows which of them it wants
differently — a cost paid for reuse not yet proven. ai-sdk earned library
status by stabilising across two consumers; nothing in tau/internal has.

indexing is the cheapest high-value lift: 512 LoC, one file, and its only
coupling to tau was three calls to process-global config path helpers.
Nothing else in tau imports it.

Mutations from upstream:
  - path helpers become an explicit indexing.Config. Two archied instances
    share a host (archie and winter), so they must not share one index.
  - the child-process helper is archied's own workspace-codesearch
    subcommand, dispatched before flag parsing so it touches no config,
    store or NATS.
  - deferred db.Close calls explicitly discarded; archie-core runs errcheck.

The child process is not incidental: google/codesearch calls fatal exits on
a corrupt index and leaks mmapped regions. Verified end to end against this
repo — 2.2MB indexed, correct hits for BuildSystemPrompt, [] for no match,
and a deliberately corrupted index returns "corrupt index" with exit 1
instead of taking the process down.

Every lifted file carries a provenance header naming its tau path and
commit (4893818d0c76), so refreshing from upstream is a diff rather than
archaeology.
2026-07-28 01:37:19 +10:00

86 lines
2.8 KiB
Go

package main
import (
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
func TestRunCodesearchHelper(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "a.txt"), []byte("alpha needle omega\n"), 0o600); err != nil {
t.Fatalf("write fixture: %v", err)
}
if err := os.WriteFile(filepath.Join(root, "b.txt"), []byte("nothing here\n"), 0o600); err != nil {
t.Fatalf("write fixture: %v", err)
}
indexPath := filepath.Join(t.TempDir(), "workspace.csearch")
t.Run("build writes an index sidecar", func(t *testing.T) {
var out strings.Builder
code := runCodesearchHelper([]string{"build", "--root", root, "--index", indexPath}, &out)
if code != 0 {
t.Fatalf("build exit code = %d, want 0", code)
}
if _, err := os.Stat(indexPath); err != nil {
t.Fatalf("index sidecar not written: %v", err)
}
})
t.Run("candidates emits the JSON contract the manager decodes", func(t *testing.T) {
var out strings.Builder
code := runCodesearchHelper([]string{
"candidates", "--index", indexPath, "--pattern", "needle",
}, &out)
if code != 0 {
t.Fatalf("candidates exit code = %d, want 0", code)
}
var files []string
if err := json.Unmarshal([]byte(out.String()), &files); err != nil {
t.Fatalf("decode candidates output %q: %v", out.String(), err)
}
if !slices.ContainsFunc(files, func(p string) bool {
return strings.HasSuffix(p, "a.txt")
}) {
t.Errorf("candidates = %v, want the file containing the needle", files)
}
})
invalid := []struct {
name string
args []string
}{
{name: "no subcommand", args: nil},
{name: "unknown subcommand", args: []string{"frobnicate"}},
{name: "build without root", args: []string{"build", "--index", indexPath}},
{name: "build without index", args: []string{"build", "--root", root}},
{name: "candidates without index", args: []string{"candidates", "--pattern", "x"}},
{name: "candidates without pattern", args: []string{"candidates", "--index", indexPath}},
}
for _, tc := range invalid {
t.Run(tc.name+" is rejected", func(t *testing.T) {
var out strings.Builder
if code := runCodesearchHelper(tc.args, &out); code == 0 {
t.Errorf("exit code = 0, want non-zero for %v", tc.args)
}
})
}
}
// The manager shells out to argv[0] with this exact subcommand name, so a
// rename on either side silently disables indexed search.
func TestCodesearchHelperSubcommandIsRecognised(t *testing.T) {
if !isCodesearchHelperArgs([]string{codesearchHelperCommand, "build"}) {
t.Errorf("isCodesearchHelperArgs() did not recognise %q", codesearchHelperCommand)
}
if isCodesearchHelperArgs([]string{"-once"}) {
t.Error("isCodesearchHelperArgs() claimed a normal daemon flag")
}
if isCodesearchHelperArgs(nil) {
t.Error("isCodesearchHelperArgs() claimed an empty argument list")
}
}