rdev/internal/domain/component_test.go
jordan 8282d60c69 feat: implement composable monorepo template system with component architecture
Adds the composable monorepo template system that generates project skeletons
with pluggable components (service, worker, app-react, app-astro, cli).

Key changes:
- Monorepo skeleton templates with shared pkg/, scripts/, and git hooks
- Component templates (service, worker, app-react, app-astro, cli) with
  Dockerfiles, CI steps, and component.yaml manifests
- Component domain model with validation and dependency resolution
- Component handler endpoints for CRUD and composition
- Template provider extended with BuildComposableProject and component assembly
- Deployer extended with composable project deployment support
- Handler timeout constants (TimeoutFastLookup through TimeoutLongRunning)
- envutil package for centralized env var reads with defaults
- api.DecodeJSON helper for standardized request body decoding
- Standardized response helpers (WriteBadRequest, WriteNotFound, etc.)
- Replaced fullstack-app cookbook with composable-app cookbook
- Hardened handler timeouts, logging, and error responses across all handlers

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 19:11:42 -07:00

177 lines
4.5 KiB
Go

package domain
import "testing"
func TestIsValidComponentType(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{"service", "service", true},
{"worker", "worker", true},
{"app-astro", "app-astro", true},
{"app-react", "app-react", true},
{"cli", "cli", true},
{"invalid", "invalid", false},
{"empty", "", false},
{"uppercase", "SERVICE", false},
{"partial", "serv", false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := IsValidComponentType(tc.input)
if result != tc.expected {
t.Errorf("IsValidComponentType(%q) = %v, want %v", tc.input, result, tc.expected)
}
})
}
}
func TestComponentType_DestDir(t *testing.T) {
tests := []struct {
name string
componentType ComponentType
expected string
}{
{"service", ComponentTypeService, "services"},
{"worker", ComponentTypeWorker, "workers"},
{"app-astro", ComponentTypeAppAstro, "apps"},
{"app-react", ComponentTypeAppReact, "apps"},
{"cli", ComponentTypeCLI, "cli"},
{"unknown", ComponentType("unknown"), ""},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := tc.componentType.DestDir()
if result != tc.expected {
t.Errorf("%s.DestDir() = %q, want %q", tc.componentType, result, tc.expected)
}
})
}
}
func TestComponentType_StartingPort(t *testing.T) {
tests := []struct {
name string
componentType ComponentType
expected int
}{
{"service", ComponentTypeService, 8001},
{"worker", ComponentTypeWorker, 0},
{"app-astro", ComponentTypeAppAstro, 3001},
{"app-react", ComponentTypeAppReact, 3001},
{"cli", ComponentTypeCLI, 0},
{"unknown", ComponentType("unknown"), 0},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := tc.componentType.StartingPort()
if result != tc.expected {
t.Errorf("%s.StartingPort() = %d, want %d", tc.componentType, result, tc.expected)
}
})
}
}
func TestComponentType_NeedsPort(t *testing.T) {
tests := []struct {
name string
componentType ComponentType
expected bool
}{
{"service", ComponentTypeService, true},
{"worker", ComponentTypeWorker, false},
{"app-astro", ComponentTypeAppAstro, true},
{"app-react", ComponentTypeAppReact, true},
{"cli", ComponentTypeCLI, false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := tc.componentType.NeedsPort()
if result != tc.expected {
t.Errorf("%s.NeedsPort() = %v, want %v", tc.componentType, result, tc.expected)
}
})
}
}
func TestComponentType_IsGoComponent(t *testing.T) {
tests := []struct {
name string
componentType ComponentType
expected bool
}{
{"service", ComponentTypeService, true},
{"worker", ComponentTypeWorker, true},
{"app-astro", ComponentTypeAppAstro, false},
{"app-react", ComponentTypeAppReact, false},
{"cli", ComponentTypeCLI, true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := tc.componentType.IsGoComponent()
if result != tc.expected {
t.Errorf("%s.IsGoComponent() = %v, want %v", tc.componentType, result, tc.expected)
}
})
}
}
func TestValidateComponentName(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
}{
{"valid simple", "auth", false},
{"valid with dash", "auth-api", false},
{"valid with numbers", "api2", false},
{"valid complex", "user-service-v2", false},
{"empty", "", true},
{"starts with number", "2api", true},
{"starts with dash", "-api", true},
{"uppercase", "Auth", true},
{"mixed case", "authApi", true},
{"underscore", "auth_api", true},
{"space", "auth api", true},
{"special char", "auth@api", true},
{"too long", "a" + string(make([]byte, 63)), true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := ValidateComponentName(tc.input)
if (err != nil) != tc.wantErr {
t.Errorf("ValidateComponentName(%q) error = %v, wantErr %v", tc.input, err, tc.wantErr)
}
})
}
}
func TestValidComponentTypes(t *testing.T) {
// Ensure all valid types are in the slice
expected := []ComponentType{
ComponentTypeService,
ComponentTypeWorker,
ComponentTypeAppAstro,
ComponentTypeAppReact,
ComponentTypeCLI,
}
if len(ValidComponentTypes) != len(expected) {
t.Errorf("ValidComponentTypes has %d types, want %d", len(ValidComponentTypes), len(expected))
}
for i, ct := range ValidComponentTypes {
if ct != expected[i] {
t.Errorf("ValidComponentTypes[%d] = %s, want %s", i, ct, expected[i])
}
}
}