package main import ( "path/filepath" "testing" ) // ─── checkReinventedBackend ──────────────────────────────────────────────── func TestCheckReinventedBackend_TimestamptzDuplicate(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "util.go", `package util func timestamptz(t time.Time) pgtype.Timestamptz { return pgtype.Timestamptz{Time: t, Valid: true} } `) vs := checkReinventedBackend(dir) found := false for _, v := range vs { if v.rule == "dup-timestamptz" { found = true } } if !found { t.Fatalf("expected dup-timestamptz violation, got %#v", vs) } } func TestCheckReinventedBackend_ToPgTimestamptzDuplicate(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "util.go", `package util func toPgTimestamptz(t time.Time) pgtype.Timestamptz { return pgtype.Timestamptz{Time: t, Valid: true} } `) vs := checkReinventedBackend(dir) found := false for _, v := range vs { if v.rule == "dup-toPgTimestamptz" { found = true } } if !found { t.Fatalf("expected dup-toPgTimestamptz violation, got %#v", vs) } } func TestCheckReinventedBackend_TimestampFromTimeDuplicate(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "util.go", `package util func timestampFromTime(t time.Time) *timestamppb.Timestamp { return timestamppb.New(t) } `) vs := checkReinventedBackend(dir) found := false for _, v := range vs { if v.rule == "dup-timestampFromTime" { found = true } } if !found { t.Fatalf("expected dup-timestampFromTime violation, got %#v", vs) } } func TestCheckReinventedBackend_TestFile_Skipped(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "util_test.go", `package util func timestamptz(t time.Time) pgtype.Timestamptz { return pgtype.Timestamptz{Time: t, Valid: true} } `) vs := checkReinventedBackend(dir) for _, v := range vs { if v.rule == "dup-timestamptz" { t.Fatal("test files should be skipped for dup-timestamptz check") } } } func TestCheckReinventedBackend_TypeAssertionWithHelpersImport_Flagged(t *testing.T) { // A file that imports internal/helpers AND does m["key"].(string) should be flagged dir := t.TempDir() writeFile(t, dir, "service.go", `package service import "git.dev.alexdunmow.com/block/cms/internal/helpers" func render(m map[string]interface{}) string { _ = helpers.GetStringOr(m, "key", "") return m["title"].(string) } `) vs := checkReinventedBackend(dir) found := false for _, v := range vs { if v.rule == "use-helper-accessor" { found = true } } if !found { t.Fatalf("expected use-helper-accessor violation for m[\"key\"].(string) with helpers imported, got %#v", vs) } } func TestCheckReinventedBackend_TypeAssertionWithoutHelpersImport_NotFlagged(t *testing.T) { // A file that does NOT import helpers should NOT be flagged for type assertions dir := t.TempDir() writeFile(t, dir, "service.go", `package service func render(m map[string]interface{}) string { return m["title"].(string) } `) vs := checkReinventedBackend(dir) for _, v := range vs { if v.rule == "use-helper-accessor" { t.Fatal("files without helpers import should NOT trigger use-helper-accessor") } } } func TestCheckReinventedBackend_HelpersDirSelf_NotFlagged(t *testing.T) { // The helpers package itself is allowed to define these functions dir := t.TempDir() helpersDir := filepath.Join(dir, "internal", "helpers") writeTestFile(t, filepath.Join(helpersDir, "map.go"), `package helpers import "git.dev.alexdunmow.com/block/cms/internal/helpers" func GetStringOr(m map[string]interface{}, key, def string) string { if v, ok := m[key]; ok { return v.(string) } return def } `, 0644) vs := checkReinventedBackend(dir) for _, v := range vs { if v.rule == "use-helper-accessor" { t.Fatal("internal/helpers/ itself should be exempt from use-helper-accessor check") } } } func TestCheckReinventedBackend_CleanFile_NoViolation(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "service.go", `package service func doWork() string { return "hello" } `) vs := checkReinventedBackend(dir) if len(vs) != 0 { t.Fatalf("expected no violations for clean Go file, got %d: %#v", len(vs), vs) } } func TestCheckReinventedBackend_VendorDir_Skipped(t *testing.T) { dir := t.TempDir() vendorDir := filepath.Join(dir, "vendor", "somelib") writeTestFile(t, filepath.Join(vendorDir, "util.go"), `package somelib func timestamptz(t interface{}) interface{} { return t } `, 0644) vs := checkReinventedBackend(dir) for _, v := range vs { if v.rule == "dup-timestamptz" { t.Fatal("vendor/ files should be skipped") } } } func TestCheckReinventedBackend_BoolTypeAssertion_Flagged(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "service.go", `package service import "git.dev.alexdunmow.com/block/cms/internal/helpers" func check(m map[string]interface{}) bool { _ = helpers.GetBoolOr(m, "enabled", false) return m["active"].(bool) } `) vs := checkReinventedBackend(dir) found := false for _, v := range vs { if v.rule == "use-helper-accessor" { found = true } } if !found { t.Fatalf("expected use-helper-accessor for m[\"key\"].(bool) with helpers imported, got %#v", vs) } } func TestCheckReinventedBackend_NonGoFile_Skipped(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "util.ts", `// TypeScript: function timestamptz() {} function timestamptz(t: Date) { return t } `) vs := checkReinventedBackend(dir) if len(vs) != 0 { t.Fatalf("expected no violations for .ts file (backend check only scans .go), got %d", len(vs)) } } // ─── checkReinventedFrontend ────────────────────────────────────────────── func TestCheckReinventedFrontend_ClsxDirectly_Flagged(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "comp.tsx", `import { clsx } from 'clsx' export function Comp({ active }: { active: boolean }) { return
} `) vs := checkReinventedFrontend(dir) found := false for _, v := range vs { if v.rule == "use-cn" { found = true } } if !found { t.Fatalf("expected use-cn violation for clsx() usage, got %#v", vs) } } func TestCheckReinventedFrontend_ClassnamesDirectly_Flagged(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "comp.tsx", `import classnames from 'classnames' export function Comp({ a }: { a: boolean }) { return } `) vs := checkReinventedFrontend(dir) found := false for _, v := range vs { if v.rule == "use-cn" { found = true } } if !found { t.Fatalf("expected use-cn violation for classnames() usage, got %#v", vs) } } func TestCheckReinventedFrontend_CustomDebounce_Flagged(t *testing.T) { dir := t.TempDir() writeFile(t, dir, "utils-extra.ts", `// local debounce — do not commit function debounce