fix(setup): harden AX contracts
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
07942b5d27
commit
07f255fca1
6 changed files with 202 additions and 25 deletions
|
|
@ -151,7 +151,7 @@ func renderConfig(comment string, sections []configSection) (string, error) {
|
|||
builder.WriteString(":\n")
|
||||
|
||||
for _, value := range section.Values {
|
||||
scalar, err := yaml.Marshal(value.Value)
|
||||
scalar, err := marshalConfigValue(value.Value)
|
||||
if err != nil {
|
||||
return "", core.E("setup.renderConfig", core.Concat("marshal ", section.Key, ".", value.Key), err)
|
||||
}
|
||||
|
|
@ -159,7 +159,7 @@ func renderConfig(comment string, sections []configSection) (string, error) {
|
|||
builder.WriteString(" ")
|
||||
builder.WriteString(value.Key)
|
||||
builder.WriteString(": ")
|
||||
builder.WriteString(core.Trim(string(scalar)))
|
||||
builder.WriteString(scalar)
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
|
||||
|
|
@ -171,7 +171,22 @@ func renderConfig(comment string, sections []configSection) (string, error) {
|
|||
return builder.String(), nil
|
||||
}
|
||||
|
||||
func marshalConfigValue(value any) (scalar string, err error) {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
err = core.E("setup.marshalConfigValue", core.Sprint(recovered), nil)
|
||||
}
|
||||
}()
|
||||
|
||||
data, err := yaml.Marshal(value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return core.Trim(string(data)), nil
|
||||
}
|
||||
|
||||
func parseGitRemote(remote string) string {
|
||||
remote = core.Trim(remote)
|
||||
if remote == "" {
|
||||
return ""
|
||||
}
|
||||
|
|
@ -204,6 +219,12 @@ func parseGitRemote(remote string) string {
|
|||
}
|
||||
|
||||
func trimRemotePath(remote string) string {
|
||||
trimmed := core.TrimPrefix(remote, "/")
|
||||
trimmed := core.Trim(remote)
|
||||
for core.HasPrefix(trimmed, "/") {
|
||||
trimmed = core.TrimPrefix(trimmed, "/")
|
||||
}
|
||||
for core.HasSuffix(trimmed, "/") {
|
||||
trimmed = core.TrimSuffix(trimmed, "/")
|
||||
}
|
||||
return core.TrimSuffix(trimmed, ".git")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,12 +25,34 @@ func TestConfig_GenerateBuildConfig_Bad_Unknown(t *testing.T) {
|
|||
assert.NotEmpty(t, config)
|
||||
}
|
||||
|
||||
func TestConfig_GenerateBuildConfig_Ugly_WailsNestedPath(t *testing.T) {
|
||||
config, err := GenerateBuildConfig("/tmp/workspaces/team-console", TypeWails)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, config, "name: team-console")
|
||||
assert.Contains(t, config, "type: wails")
|
||||
assert.Contains(t, config, "main: ./cmd/team-console")
|
||||
}
|
||||
|
||||
func TestConfig_GenerateTestConfig_Good_Go(t *testing.T) {
|
||||
config, err := GenerateTestConfig(TypeGo)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, config, "go test")
|
||||
}
|
||||
|
||||
func TestConfig_GenerateTestConfig_Bad_Unknown(t *testing.T) {
|
||||
config, err := GenerateTestConfig(TypeUnknown)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, config, "# Test configuration")
|
||||
assert.NotContains(t, config, "commands:")
|
||||
}
|
||||
|
||||
func TestConfig_GenerateTestConfig_Ugly_WailsUsesGoSuite(t *testing.T) {
|
||||
config, err := GenerateTestConfig(TypeWails)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, config, "go test ./...")
|
||||
assert.Contains(t, config, "go test -race ./...")
|
||||
}
|
||||
|
||||
func TestConfig_ParseGitRemote_Good_CommonFormats(t *testing.T) {
|
||||
tests := map[string]string{
|
||||
"https://github.com/dAppCore/go-io.git": "dAppCore/go-io",
|
||||
|
|
@ -51,11 +73,24 @@ func TestConfig_ParseGitRemote_Bad_Empty(t *testing.T) {
|
|||
assert.Equal(t, "", parseGitRemote("origin"))
|
||||
}
|
||||
|
||||
func TestConfig_ParseGitRemote_Ugly_WhitespaceAndTrailingSlash(t *testing.T) {
|
||||
remote := " https://github.com/dAppCore/go-io.git/ "
|
||||
assert.Equal(t, "dAppCore/go-io", parseGitRemote(remote))
|
||||
}
|
||||
|
||||
func TestConfig_TrimRemotePath_Good(t *testing.T) {
|
||||
assert.Equal(t, "core/go-io", trimRemotePath("/core/go-io.git"))
|
||||
}
|
||||
|
||||
func TestConfig_RenderConfig_Good(t *testing.T) {
|
||||
func TestConfig_TrimRemotePath_Bad_Empty(t *testing.T) {
|
||||
assert.Equal(t, "", trimRemotePath(""))
|
||||
}
|
||||
|
||||
func TestConfig_TrimRemotePath_Ugly_RepeatedSlashes(t *testing.T) {
|
||||
assert.Equal(t, "core/go-io", trimRemotePath("///core/go-io.git///"))
|
||||
}
|
||||
|
||||
func TestConfig_RenderConfig_Good_SingleSection(t *testing.T) {
|
||||
sections := []configSection{
|
||||
{Key: "project", Values: []configValue{{Key: "name", Value: "test"}}},
|
||||
}
|
||||
|
|
@ -63,3 +98,18 @@ func TestConfig_RenderConfig_Good(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.Contains(t, result, "name: test")
|
||||
}
|
||||
|
||||
func TestConfig_RenderConfig_Bad_UnsupportedValue(t *testing.T) {
|
||||
sections := []configSection{
|
||||
{Key: "project", Values: []configValue{{Key: "name", Value: func() {}}}},
|
||||
}
|
||||
result, err := renderConfig("Test", sections)
|
||||
assert.Error(t, err)
|
||||
assert.Empty(t, result)
|
||||
}
|
||||
|
||||
func TestConfig_RenderConfig_Ugly_EmptySections(t *testing.T) {
|
||||
result, err := renderConfig("", nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "", result)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,6 +39,13 @@ func TestDetect_Detect_Bad_Unknown(t *testing.T) {
|
|||
assert.Equal(t, TypeUnknown, Detect(dir))
|
||||
}
|
||||
|
||||
func TestDetect_Detect_Ugly_WailsWinsOverGo(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
require.True(t, fs.WriteMode(core.JoinPath(dir, "go.mod"), "module test\n", 0644).OK)
|
||||
require.True(t, fs.WriteMode(core.JoinPath(dir, "wails.json"), `{}`, 0644).OK)
|
||||
assert.Equal(t, TypeWails, Detect(dir))
|
||||
}
|
||||
|
||||
func TestDetect_DetectAll_Good_Polyglot(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
require.True(t, fs.WriteMode(core.JoinPath(dir, "go.mod"), "module test\n", 0644).OK)
|
||||
|
|
@ -54,7 +61,24 @@ func TestDetect_DetectAll_Bad_Empty(t *testing.T) {
|
|||
assert.Empty(t, DetectAll(dir))
|
||||
}
|
||||
|
||||
func TestDetect_AbsolutePath_Ugly_Empty(t *testing.T) {
|
||||
result := absolutePath("")
|
||||
assert.NotEmpty(t, result)
|
||||
func TestDetect_DetectAll_Ugly_StableOrder(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
require.True(t, fs.WriteMode(core.JoinPath(dir, "go.mod"), "module test\n", 0644).OK)
|
||||
require.True(t, fs.WriteMode(core.JoinPath(dir, "composer.json"), "{}", 0644).OK)
|
||||
require.True(t, fs.WriteMode(core.JoinPath(dir, "package.json"), `{"name":"test"}`, 0644).OK)
|
||||
require.True(t, fs.WriteMode(core.JoinPath(dir, "wails.json"), `{}`, 0644).OK)
|
||||
assert.Equal(t, []ProjectType{TypeGo, TypePHP, TypeNode, TypeWails}, DetectAll(dir))
|
||||
}
|
||||
|
||||
func TestDetect_AbsolutePath_Good_ExplicitPath(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
assert.Equal(t, core.Path(dir), absolutePath(dir))
|
||||
}
|
||||
|
||||
func TestDetect_AbsolutePath_Bad_EmptyUsesDirCWD(t *testing.T) {
|
||||
assert.Equal(t, core.Env("DIR_CWD"), absolutePath(""))
|
||||
}
|
||||
|
||||
func TestDetect_AbsolutePath_Ugly_RelativeSegments(t *testing.T) {
|
||||
assert.Equal(t, core.Path("./repo/../repo"), absolutePath("./repo/../repo"))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,10 @@ import (
|
|||
"context"
|
||||
"testing"
|
||||
|
||||
"dappco.re/go/agent/pkg/agentic"
|
||||
core "dappco.re/go/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestService_Register_Good(t *testing.T) {
|
||||
|
|
@ -25,10 +27,36 @@ func TestService_OnStartup_Good(t *testing.T) {
|
|||
assert.True(t, result.OK)
|
||||
}
|
||||
|
||||
func TestService_DetectGitRemote_Good_NonGitDir(t *testing.T) {
|
||||
func TestService_OnStartup_Bad_CancelledContext(t *testing.T) {
|
||||
c := core.New()
|
||||
svc := &Service{ServiceRuntime: core.NewServiceRuntime(c, SetupOptions{})}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
result := svc.OnStartup(ctx)
|
||||
assert.True(t, result.OK)
|
||||
}
|
||||
|
||||
func TestService_OnStartup_Ugly_NilRuntime(t *testing.T) {
|
||||
result := (&Service{}).OnStartup(context.Background())
|
||||
assert.True(t, result.OK)
|
||||
}
|
||||
|
||||
func TestService_DetectGitRemote_Good_GitOrigin(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
c := core.New()
|
||||
require.True(t, agentic.ProcessRegister(c).OK)
|
||||
svc := &Service{ServiceRuntime: core.NewServiceRuntime(c, SetupOptions{})}
|
||||
|
||||
require.True(t, c.Process().RunIn(context.Background(), dir, "git", "init").OK)
|
||||
require.True(t, c.Process().RunIn(context.Background(), dir, "git", "remote", "add", "origin", "git@forge.lthn.ai:core/agent.git").OK)
|
||||
|
||||
assert.Equal(t, "core/agent", svc.DetectGitRemote(dir))
|
||||
}
|
||||
|
||||
func TestService_DetectGitRemote_Bad_NonGitDir(t *testing.T) {
|
||||
c := core.New()
|
||||
svc := &Service{ServiceRuntime: core.NewServiceRuntime(c, SetupOptions{})}
|
||||
// Non-git dir returns empty
|
||||
remote := svc.DetectGitRemote(t.TempDir())
|
||||
assert.Equal(t, "", remote)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,14 +35,26 @@ func (s *Service) Run(opts Options) error {
|
|||
core.Print(nil, "Also: %v (polyglot)", allTypes)
|
||||
}
|
||||
|
||||
var tmplName string
|
||||
if opts.Template != "" {
|
||||
var err error
|
||||
tmplName, err = resolveTemplateName(opts.Template, projType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !templateExists(tmplName) {
|
||||
return core.E("setup.Run", core.Concat("template not found: ", tmplName), nil)
|
||||
}
|
||||
}
|
||||
|
||||
// Generate .core/ config files
|
||||
if err := setupCoreDir(opts, projType); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Scaffold from dir template if requested
|
||||
if opts.Template != "" {
|
||||
return s.scaffoldTemplate(opts, projType)
|
||||
if tmplName != "" {
|
||||
return s.scaffoldTemplate(opts, projType, tmplName)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -84,12 +96,7 @@ func setupCoreDir(opts Options, projType ProjectType) error {
|
|||
}
|
||||
|
||||
// scaffoldTemplate extracts a dir template into the target path.
|
||||
func (s *Service) scaffoldTemplate(opts Options, projType ProjectType) error {
|
||||
tmplName, err := resolveTemplateName(opts.Template, projType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) scaffoldTemplate(opts Options, projType ProjectType, tmplName string) error {
|
||||
core.Print(nil, "Template: %s", tmplName)
|
||||
|
||||
data := &lib.WorkspaceData{
|
||||
|
|
@ -105,10 +112,6 @@ func (s *Service) scaffoldTemplate(opts Options, projType ProjectType) error {
|
|||
TestCmd: defaultTestCommand(projType),
|
||||
}
|
||||
|
||||
if !templateExists(tmplName) {
|
||||
return core.E("setup.scaffoldTemplate", core.Concat("template not found: ", tmplName), nil)
|
||||
}
|
||||
|
||||
if opts.DryRun {
|
||||
core.Print(nil, "Would extract workspace/%s to %s", tmplName, opts.Path)
|
||||
core.Print(nil, " Template found: %s", tmplName)
|
||||
|
|
|
|||
|
|
@ -43,6 +43,25 @@ func TestSetup_Run_Good_TemplateAlias(t *testing.T) {
|
|||
assert.Contains(t, prompt.Value.(string), "This workspace was scaffolded by pkg/setup.")
|
||||
}
|
||||
|
||||
func TestSetup_Run_Bad_MissingTemplateDoesNotWrite(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
require.True(t, fs.WriteMode(core.JoinPath(dir, "go.mod"), "module example.com/test\n", 0644).OK)
|
||||
|
||||
err := newSetupService().Run(Options{Path: dir, Template: "missing-template"})
|
||||
require.Error(t, err)
|
||||
assert.False(t, fs.Exists(core.JoinPath(dir, ".core")))
|
||||
}
|
||||
|
||||
func TestSetup_Run_Ugly_DryRunDoesNotWrite(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
require.True(t, fs.WriteMode(core.JoinPath(dir, "go.mod"), "module example.com/test\n", 0644).OK)
|
||||
|
||||
err := newSetupService().Run(Options{Path: dir, Template: "agent", DryRun: true})
|
||||
require.NoError(t, err)
|
||||
assert.False(t, fs.Exists(core.JoinPath(dir, ".core")))
|
||||
assert.False(t, fs.Exists(core.JoinPath(dir, "PROMPT.md")))
|
||||
}
|
||||
|
||||
func TestSetup_ResolveTemplateName_Good_Auto(t *testing.T) {
|
||||
name, err := resolveTemplateName("auto", TypeGo)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -54,6 +73,12 @@ func TestSetup_ResolveTemplateName_Bad_Empty(t *testing.T) {
|
|||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestSetup_ResolveTemplateName_Ugly_ConventionsAlias(t *testing.T) {
|
||||
name, err := resolveTemplateName("conventions", TypeGo)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "review", name)
|
||||
}
|
||||
|
||||
func TestSetup_TemplateExists_Good_Default(t *testing.T) {
|
||||
assert.True(t, templateExists("default"))
|
||||
}
|
||||
|
|
@ -62,22 +87,38 @@ func TestSetup_TemplateExists_Bad_Missing(t *testing.T) {
|
|||
assert.False(t, templateExists("missing-template"))
|
||||
}
|
||||
|
||||
func TestSetup_DefaultBuildCommand_Good(t *testing.T) {
|
||||
func TestSetup_TemplateExists_Ugly_Review(t *testing.T) {
|
||||
assert.True(t, templateExists("review"))
|
||||
}
|
||||
|
||||
func TestSetup_DefaultBuildCommand_Good_KnownTypes(t *testing.T) {
|
||||
assert.Equal(t, "go build ./...", defaultBuildCommand(TypeGo))
|
||||
assert.Equal(t, "go build ./...", defaultBuildCommand(TypeWails))
|
||||
assert.Equal(t, "composer test", defaultBuildCommand(TypePHP))
|
||||
assert.Equal(t, "npm run build", defaultBuildCommand(TypeNode))
|
||||
}
|
||||
|
||||
func TestSetup_DefaultBuildCommand_Bad_Unknown(t *testing.T) {
|
||||
assert.Equal(t, "make build", defaultBuildCommand(TypeUnknown))
|
||||
}
|
||||
|
||||
func TestSetup_DefaultTestCommand_Good(t *testing.T) {
|
||||
func TestSetup_DefaultBuildCommand_Ugly_WailsMatchesGo(t *testing.T) {
|
||||
assert.Equal(t, defaultBuildCommand(TypeGo), defaultBuildCommand(TypeWails))
|
||||
}
|
||||
|
||||
func TestSetup_DefaultTestCommand_Good_KnownTypes(t *testing.T) {
|
||||
assert.Equal(t, "go test ./...", defaultTestCommand(TypeGo))
|
||||
assert.Equal(t, "go test ./...", defaultTestCommand(TypeWails))
|
||||
assert.Equal(t, "composer test", defaultTestCommand(TypePHP))
|
||||
assert.Equal(t, "npm test", defaultTestCommand(TypeNode))
|
||||
}
|
||||
|
||||
func TestSetup_DefaultTestCommand_Bad_Unknown(t *testing.T) {
|
||||
assert.Equal(t, "make test", defaultTestCommand(TypeUnknown))
|
||||
}
|
||||
|
||||
func TestSetup_DefaultTestCommand_Ugly_WailsMatchesGo(t *testing.T) {
|
||||
assert.Equal(t, defaultTestCommand(TypeGo), defaultTestCommand(TypeWails))
|
||||
}
|
||||
|
||||
func TestSetup_FormatFlow_Good(t *testing.T) {
|
||||
goFlow := formatFlow(TypeGo)
|
||||
assert.Contains(t, goFlow, "go build ./...")
|
||||
|
|
@ -90,3 +131,13 @@ func TestSetup_FormatFlow_Good(t *testing.T) {
|
|||
assert.Contains(t, nodeFlow, "npm run build")
|
||||
assert.Contains(t, nodeFlow, "npm test")
|
||||
}
|
||||
|
||||
func TestSetup_FormatFlow_Bad_Unknown(t *testing.T) {
|
||||
flow := formatFlow(TypeUnknown)
|
||||
assert.Contains(t, flow, "make build")
|
||||
assert.Contains(t, flow, "make test")
|
||||
}
|
||||
|
||||
func TestSetup_FormatFlow_Ugly_Wails(t *testing.T) {
|
||||
assert.Equal(t, formatFlow(TypeGo), formatFlow(TypeWails))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue