package integrate import ( "os" "path/filepath" "strings" "testing" ) func TestTargetFile_PackageMain(t *testing.T) { src := "package main\n\nfunc DoSomething() {}\n" got := TargetFile(src, "/project") if !strings.Contains(got, "cmd/dendrite") { t.Errorf("expected cmd/dendrite for package main, got: %s", got) } if !strings.HasSuffix(got, "do_something.go") { t.Errorf("expected do_something.go, got: %s", got) } } func TestTargetFile_PackageLattice(t *testing.T) { src := "package lattice\n\ntype NodeCount struct{}\n" got := TargetFile(src, "/project") if !strings.Contains(got, "lattice") { t.Errorf("expected lattice dir, got: %s", got) } if !strings.HasSuffix(got, "node_count.go") { t.Errorf("expected node_count.go, got: %s", got) } } func TestTargetFile_ParseError(t *testing.T) { got := TargetFile("not go source", "/project") if !strings.HasSuffix(got, "generated.go") { t.Errorf("expected generated.go fallback, got: %s", got) } } func TestToSnake(t *testing.T) { tests := []struct { in, want string }{ {"CountNodes", "count_nodes"}, {"foo", "foo"}, {"A", "a"}, {"HTMLParser", "h_t_m_l_parser"}, } for _, tt := range tests { got := toSnake(tt.in) if got != tt.want { t.Errorf("toSnake(%q) = %q, want %q", tt.in, got, tt.want) } } } func TestCopyDir(t *testing.T) { // Create a minimal project. src := t.TempDir() os.WriteFile(filepath.Join(src, "go.mod"), []byte("module test\n\ngo 1.24\n"), 0o644) os.MkdirAll(filepath.Join(src, "pkg"), 0o755) os.WriteFile(filepath.Join(src, "pkg", "foo.go"), []byte("package pkg\n"), 0o644) os.WriteFile(filepath.Join(src, "pkg", "readme.txt"), []byte("skip me\n"), 0o644) os.MkdirAll(filepath.Join(src, ".git"), 0o755) os.WriteFile(filepath.Join(src, ".git", "config"), []byte("git config\n"), 0o644) dst := t.TempDir() if err := copyDir(src, dst); err != nil { t.Fatal(err) } // go.mod should be copied. if _, err := os.Stat(filepath.Join(dst, "go.mod")); err != nil { t.Error("go.mod not copied") } // .go file should be copied. if _, err := os.Stat(filepath.Join(dst, "pkg", "foo.go")); err != nil { t.Error("pkg/foo.go not copied") } // .txt should NOT be copied. if _, err := os.Stat(filepath.Join(dst, "pkg", "readme.txt")); err == nil { t.Error("readme.txt should not be copied") } // .git should NOT be copied. if _, err := os.Stat(filepath.Join(dst, ".git", "config")); err == nil { t.Error(".git should not be copied") } } func TestRollback(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "test.go") os.WriteFile(path, []byte("package test\n"), 0o644) plan := Plan{SourceFile: path} if err := Rollback(plan, dir); err != nil { t.Fatal(err) } if _, err := os.Stat(path); !os.IsNotExist(err) { t.Error("file should have been deleted") } }