@jeancodogno/specforce-kit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/.geminiignore +15 -0
  2. package/.github/workflows/ci.yml +100 -0
  3. package/.github/workflows/release.yml +51 -0
  4. package/.golangci.yml +21 -0
  5. package/.goreleaser.yaml +43 -0
  6. package/.rgignore +13 -0
  7. package/.specforce/config.yaml +32 -0
  8. package/.specforce/docs/.gitkeep +0 -0
  9. package/.specforce/docs/architecture.md +98 -0
  10. package/.specforce/docs/engineering.md +80 -0
  11. package/.specforce/docs/governance.md +30 -0
  12. package/.specforce/docs/memorial.md +44 -0
  13. package/.specforce/docs/principles.md +31 -0
  14. package/.specforce/docs/security.md +31 -0
  15. package/.specforce/docs/ui-ux.md +129 -0
  16. package/.specforce/specs/.gitkeep +0 -0
  17. package/AGENTS.md +36 -0
  18. package/CONTRIBUTING.md +81 -0
  19. package/LICENSE +21 -0
  20. package/Makefile +49 -0
  21. package/README.md +144 -0
  22. package/assets/logo-header.png +0 -0
  23. package/assets/logo.png +0 -0
  24. package/docs/artifacts.md +41 -0
  25. package/docs/cli.md +73 -0
  26. package/docs/configuration.md +50 -0
  27. package/docs/getting-started.md +110 -0
  28. package/docs/supported-tools.md +14 -0
  29. package/go.mod +35 -0
  30. package/go.sum +61 -0
  31. package/index.js +0 -0
  32. package/package.json +26 -0
  33. package/src/cmd/specforce/main.go +23 -0
  34. package/src/cmd/specforce/main_test.go +7 -0
  35. package/src/internal/agent/artifacts/constitution/architecture.yaml +70 -0
  36. package/src/internal/agent/artifacts/constitution/engineering.yaml +52 -0
  37. package/src/internal/agent/artifacts/constitution/governance.yaml +41 -0
  38. package/src/internal/agent/artifacts/constitution/memorial.yaml +55 -0
  39. package/src/internal/agent/artifacts/constitution/principles.yaml +54 -0
  40. package/src/internal/agent/artifacts/constitution/security.yaml +47 -0
  41. package/src/internal/agent/artifacts/constitution/ui-ux.yaml +158 -0
  42. package/src/internal/agent/artifacts/spec/design.yaml +50 -0
  43. package/src/internal/agent/artifacts/spec/requirements.yaml +48 -0
  44. package/src/internal/agent/artifacts/spec/tasks.yaml +55 -0
  45. package/src/internal/agent/compatibility_test.go +120 -0
  46. package/src/internal/agent/cwd_test.go +9 -0
  47. package/src/internal/agent/embed.go +19 -0
  48. package/src/internal/agent/integration_test.go +138 -0
  49. package/src/internal/agent/kit/agents/product-analyst.yaml +31 -0
  50. package/src/internal/agent/kit/agents/technical-developer.yaml +28 -0
  51. package/src/internal/agent/kit/agents/technical-project-planner.yaml +30 -0
  52. package/src/internal/agent/kit/agents/technical-qa-engineer.yaml +36 -0
  53. package/src/internal/agent/kit/agents/technical-solution-architect.yaml +32 -0
  54. package/src/internal/agent/kit/commands/archive.yaml +25 -0
  55. package/src/internal/agent/kit/commands/constitution.yaml +132 -0
  56. package/src/internal/agent/kit/commands/implement.yaml +96 -0
  57. package/src/internal/agent/kit/commands/spec.yaml +94 -0
  58. package/src/internal/agent/kit/instructions/archive.md +68 -0
  59. package/src/internal/agent/kit/kit.yaml +111 -0
  60. package/src/internal/agent/kit/skills/opportunity-framing/SKILL.yaml +56 -0
  61. package/src/internal/agent/kit/skills/pragmatic-product-owner/SKILL.yaml +58 -0
  62. package/src/internal/agent/kit/skills/pragmatic-product-owner/references/spec-artifacts.yaml +38 -0
  63. package/src/internal/agent/kit/skills/spec-clarification-interview/SKILL.yaml +52 -0
  64. package/src/internal/agent/kit/skills/task-atomic-decomposition/SKILL.yaml +137 -0
  65. package/src/internal/agent/kit/skills/task-atomic-decomposition/references/roadmap-patterns.yaml +103 -0
  66. package/src/internal/agent/kit_manifests_test.go +58 -0
  67. package/src/internal/agent/manifest.go +15 -0
  68. package/src/internal/agent/registry.go +69 -0
  69. package/src/internal/agent/registry_test.go +51 -0
  70. package/src/internal/agent/translator.go +334 -0
  71. package/src/internal/agent/translator_test.go +734 -0
  72. package/src/internal/cli/archive.go +91 -0
  73. package/src/internal/cli/archive_test.go +67 -0
  74. package/src/internal/cli/cli.go +259 -0
  75. package/src/internal/cli/cli_test.go +130 -0
  76. package/src/internal/cli/cobra/archive.go +26 -0
  77. package/src/internal/cli/cobra/console.go +21 -0
  78. package/src/internal/cli/cobra/constitution.go +44 -0
  79. package/src/internal/cli/cobra/dummy_test.go +7 -0
  80. package/src/internal/cli/cobra/implementation.go +50 -0
  81. package/src/internal/cli/cobra/init.go +23 -0
  82. package/src/internal/cli/cobra/install.go +21 -0
  83. package/src/internal/cli/cobra/root.go +47 -0
  84. package/src/internal/cli/cobra/spec.go +91 -0
  85. package/src/internal/cli/console.go +48 -0
  86. package/src/internal/cli/constitution.go +190 -0
  87. package/src/internal/cli/constitution_test.go +50 -0
  88. package/src/internal/cli/implementation.go +153 -0
  89. package/src/internal/cli/implementation_test.go +165 -0
  90. package/src/internal/cli/spec.go +369 -0
  91. package/src/internal/cli/spec_test.go +85 -0
  92. package/src/internal/constitution/registry.go +139 -0
  93. package/src/internal/constitution/registry_test.go +97 -0
  94. package/src/internal/constitution/status.go +70 -0
  95. package/src/internal/constitution/status_test.go +160 -0
  96. package/src/internal/core/blueprint.go +49 -0
  97. package/src/internal/core/blueprint_test.go +58 -0
  98. package/src/internal/core/config.go +124 -0
  99. package/src/internal/core/config_test.go +145 -0
  100. package/src/internal/core/constants.go +13 -0
  101. package/src/internal/core/errors.go +31 -0
  102. package/src/internal/core/errors_test.go +48 -0
  103. package/src/internal/core/hooks.go +89 -0
  104. package/src/internal/core/hooks_test.go +51 -0
  105. package/src/internal/core/kit.go +15 -0
  106. package/src/internal/core/kit_test.go +65 -0
  107. package/src/internal/core/path.go +115 -0
  108. package/src/internal/core/path_test.go +101 -0
  109. package/src/internal/core/ui.go +19 -0
  110. package/src/internal/installer/dependencies.go +23 -0
  111. package/src/internal/installer/install.go +89 -0
  112. package/src/internal/installer/installer.go +45 -0
  113. package/src/internal/installer/installer_test.go +96 -0
  114. package/src/internal/project/agents_md.go +108 -0
  115. package/src/internal/project/agents_md_test.go +101 -0
  116. package/src/internal/project/bootstrapper.go +64 -0
  117. package/src/internal/project/bootstrapper_test.go +89 -0
  118. package/src/internal/project/detector.go +35 -0
  119. package/src/internal/project/detector_test.go +96 -0
  120. package/src/internal/project/refresh_integration_test.go +86 -0
  121. package/src/internal/project/service.go +93 -0
  122. package/src/internal/project/service_test.go +135 -0
  123. package/src/internal/spec/archive.go +49 -0
  124. package/src/internal/spec/archive_test.go +55 -0
  125. package/src/internal/spec/discovery.go +53 -0
  126. package/src/internal/spec/discovery_test.go +60 -0
  127. package/src/internal/spec/implementation.go +296 -0
  128. package/src/internal/spec/implementation_test.go +253 -0
  129. package/src/internal/spec/registry.go +142 -0
  130. package/src/internal/spec/registry_test.go +105 -0
  131. package/src/internal/spec/scanner.go +219 -0
  132. package/src/internal/spec/scanner_test.go +243 -0
  133. package/src/internal/spec/service.go +157 -0
  134. package/src/internal/spec/service_test.go +238 -0
  135. package/src/internal/spec/status.go +94 -0
  136. package/src/internal/spec/status_test.go +146 -0
  137. package/src/internal/spec/tasks.go +103 -0
  138. package/src/internal/spec/tasks_test.go +137 -0
  139. package/src/internal/tui/components.go +132 -0
  140. package/src/internal/tui/console.go +247 -0
  141. package/src/internal/tui/console_test.go +19 -0
  142. package/src/internal/tui/constitution.go +36 -0
  143. package/src/internal/tui/constitution_test.go +32 -0
  144. package/src/internal/tui/implementation.go +151 -0
  145. package/src/internal/tui/logo.go +89 -0
  146. package/src/internal/tui/multiselect.go +199 -0
  147. package/src/internal/tui/progressbar.go +37 -0
  148. package/src/internal/tui/progressbar_test.go +30 -0
  149. package/src/internal/tui/spec.go +35 -0
  150. package/src/internal/tui/spinner.go +66 -0
  151. package/src/internal/tui/terminal.go +18 -0
  152. package/src/internal/tui/theme.go +125 -0
  153. package/src/internal/tui/ui.go +52 -0
@@ -0,0 +1,96 @@
1
+ package project
2
+
3
+ import (
4
+ "context"
5
+ "os"
6
+ "path/filepath"
7
+ "testing"
8
+ "testing/fstest"
9
+
10
+ "github.com/jeancodogno/specforce-kit/src/internal/agent"
11
+ )
12
+
13
+ func TestDetectExistingAgents(t *testing.T) {
14
+ ctx := context.Background()
15
+ tmpDir := t.TempDir()
16
+
17
+ // Mock kitFS
18
+ kitFS := fstest.MapFS{
19
+ "kit.yaml": &fstest.MapFile{Data: []byte(`
20
+ tools:
21
+ gemini:
22
+ name: "Gemini"
23
+ target: ".gemini/"
24
+ claude:
25
+ name: "Claude"
26
+ target: ".claude/"
27
+ `)},
28
+ "agents/gemini.yaml": &fstest.MapFile{Data: []byte("content: gemini")},
29
+ "agents/claude.yaml": &fstest.MapFile{Data: []byte("content: claude")},
30
+ }
31
+
32
+ reg := &agent.Registry{}
33
+ _ = reg.Initialize(kitFS)
34
+
35
+ t.Run("no existing agents", func(t *testing.T) {
36
+ got := DetectExistingAgents(ctx, tmpDir, reg)
37
+ if len(got) != 0 {
38
+ t.Errorf("expected no agents, got %v", got)
39
+ }
40
+ })
41
+
42
+ t.Run("existing agents", func(t *testing.T) {
43
+ if err := os.Mkdir(filepath.Join(tmpDir, ".gemini"), 0755); err != nil {
44
+ t.Fatal(err)
45
+ }
46
+ got := DetectExistingAgents(ctx, tmpDir, reg)
47
+ if len(got) != 1 || got[0] != "gemini" {
48
+ t.Errorf("expected [gemini], got %v", got)
49
+ }
50
+ })
51
+
52
+ t.Run("nil registry", func(t *testing.T) {
53
+ got := DetectExistingAgents(ctx, tmpDir, nil)
54
+ if len(got) != 0 {
55
+ t.Errorf("expected no agents for nil registry, got %v", got)
56
+ }
57
+ })
58
+
59
+ t.Run("cancelled context", func(t *testing.T) {
60
+ cancelCtx, cancel := context.WithCancel(ctx)
61
+ cancel()
62
+ got := DetectExistingAgents(cancelCtx, tmpDir, reg)
63
+ if len(got) != 0 {
64
+ t.Errorf("expected no agents for cancelled context, got %v", got)
65
+ }
66
+ })
67
+ }
68
+
69
+ func TestIsInitialized(t *testing.T) {
70
+ tmpDir := t.TempDir()
71
+
72
+ t.Run("not initialized", func(t *testing.T) {
73
+ if IsInitialized(tmpDir) {
74
+ t.Fatal("expected IsInitialized to be false")
75
+ }
76
+ })
77
+
78
+ t.Run("is initialized", func(t *testing.T) {
79
+ if err := os.Mkdir(filepath.Join(tmpDir, ".specforce"), 0755); err != nil {
80
+ t.Fatal(err)
81
+ }
82
+ if !IsInitialized(tmpDir) {
83
+ t.Fatal("expected IsInitialized to be true")
84
+ }
85
+ })
86
+
87
+ t.Run("is file not dir", func(t *testing.T) {
88
+ anotherTmp := t.TempDir()
89
+ if err := os.WriteFile(filepath.Join(anotherTmp, ".specforce"), []byte("not a dir"), 0644); err != nil {
90
+ t.Fatal(err)
91
+ }
92
+ if IsInitialized(anotherTmp) {
93
+ t.Fatal("expected IsInitialized to be false when .specforce is a file")
94
+ }
95
+ })
96
+ }
@@ -0,0 +1,86 @@
1
+ package project_test
2
+
3
+ import (
4
+ "context"
5
+ "os"
6
+ "path/filepath"
7
+ "strings"
8
+ "testing"
9
+ "testing/fstest"
10
+
11
+ "github.com/jeancodogno/specforce-kit/src/internal/project"
12
+ )
13
+
14
+ func createMockKitFS() fstest.MapFS {
15
+ return fstest.MapFS{
16
+ "kit.yaml": &fstest.MapFile{Data: []byte(`
17
+ tools:
18
+ gemini:
19
+ name: "Gemini"
20
+ target: ".gemini/"
21
+ mappings:
22
+ agents: { path: "agents", ext: ".md" }
23
+ skills: { path: "skills", ext: ".md" }
24
+ commands: { path: "commands", ext: ".md" }
25
+ `)},
26
+ "agents/coder.yaml": &fstest.MapFile{Data: []byte("content: kit-agent-content")},
27
+ "skills/tdd.yaml": &fstest.MapFile{Data: []byte("content: kit-skill-content")},
28
+ "commands/run.yaml": &fstest.MapFile{Data: []byte("content: kit-command-content")},
29
+ }
30
+ }
31
+
32
+ func TestService_UpdateTools_RefreshesAllCategories(t *testing.T) {
33
+ kitFS := createMockKitFS()
34
+ artifactsFS := fstest.MapFS{}
35
+ tmpDir := t.TempDir()
36
+
37
+ svc := project.NewService(kitFS, artifactsFS, tmpDir)
38
+ ui := &mockUI{}
39
+
40
+ // 1. Initial Installation
41
+ err := svc.UpdateTools(context.Background(), ui, []string{"gemini"})
42
+ if err != nil {
43
+ t.Fatalf("Initial UpdateTools failed: %v", err)
44
+ }
45
+
46
+ // Paths
47
+ agentPath := filepath.Join(tmpDir, ".gemini/agents/coder.md")
48
+ skillPath := filepath.Join(tmpDir, ".gemini/skills/tdd.md")
49
+ commandPath := filepath.Join(tmpDir, ".gemini/commands/run.md")
50
+
51
+ // 2. Modify files manually (simulate user customization or old version)
52
+ filesToModify := []string{agentPath, skillPath, commandPath}
53
+ for _, p := range filesToModify {
54
+ if err := os.WriteFile(p, []byte("user-modified-content"), 0644); err != nil {
55
+ t.Fatalf("failed to modify file %s: %v", p, err)
56
+ }
57
+ }
58
+
59
+ // 3. Run Update
60
+ err = svc.UpdateTools(context.Background(), ui, []string{"gemini"})
61
+ if err != nil {
62
+ t.Fatalf("Second UpdateTools failed: %v", err)
63
+ }
64
+
65
+ // 4. Verify Refresh
66
+ tests := []struct {
67
+ path string
68
+ expected string
69
+ }{
70
+ {agentPath, "kit-agent-content"},
71
+ {skillPath, "kit-skill-content"},
72
+ {commandPath, "kit-command-content"},
73
+ }
74
+
75
+ for _, tt := range tests {
76
+ content, err := os.ReadFile(tt.path)
77
+ if err != nil {
78
+ t.Errorf("failed to read file %s: %v", tt.path, err)
79
+ continue
80
+ }
81
+ // Note: AdaptArtifacts might inject headers, so we check if it contains the kit content
82
+ if !strings.Contains(string(content), tt.expected) {
83
+ t.Errorf("file %s was not refreshed. Got: %s, Expected to contain: %s", tt.path, string(content), tt.expected)
84
+ }
85
+ }
86
+ }
@@ -0,0 +1,93 @@
1
+ package project
2
+
3
+ import (
4
+ "context"
5
+ "errors"
6
+ "fmt"
7
+ "io/fs"
8
+
9
+ "github.com/jeancodogno/specforce-kit/src/internal/agent"
10
+ "github.com/jeancodogno/specforce-kit/src/internal/core"
11
+ "github.com/jeancodogno/specforce-kit/src/internal/installer"
12
+ )
13
+
14
+ // InitConfig contains project root, selected agents, and options for project initialization.
15
+ type InitConfig struct {
16
+ ProjectRoot string
17
+ SelectedAgents []string
18
+ }
19
+
20
+ // Service orchestrates project-related domain logic.
21
+ type Service struct {
22
+ kitFS fs.FS
23
+ artifactsFS fs.FS
24
+ projectRoot string
25
+ config *core.ProjectConfig
26
+ }
27
+
28
+ // NewService creates a new instance of the project service.
29
+ func NewService(kitFS, artifactsFS fs.FS, projectRoot string) *Service {
30
+ return &Service{
31
+ kitFS: kitFS,
32
+ artifactsFS: artifactsFS,
33
+ projectRoot: projectRoot,
34
+ }
35
+ }
36
+
37
+ // GetConfig returns the project-specific configuration.
38
+ func (s *Service) GetConfig(ctx context.Context) (*core.ProjectConfig, error) {
39
+ if s.config == nil {
40
+ s.config = core.LoadConfig(s.projectRoot)
41
+ }
42
+ return s.config, nil
43
+ }
44
+
45
+ // InitializeProject orchestrates the project initialization flow.
46
+ func (s *Service) InitializeProject(ctx context.Context, ui core.UI, config InitConfig) error {
47
+ if err := BootstrapProject(ctx, config.ProjectRoot, s.kitFS, s.artifactsFS, ui); err != nil {
48
+ if !errors.Is(err, core.ErrProjectAlreadyInitialized) {
49
+ return err
50
+ }
51
+ }
52
+
53
+ for _, a := range config.SelectedAgents {
54
+ if err := agent.AdaptArtifacts(ctx, config.ProjectRoot, s.kitFS, a, ui, installer.Options{}); err != nil {
55
+ return fmt.Errorf("failed to adapt artifacts for %s: %w", a, err)
56
+ }
57
+ }
58
+
59
+ return nil
60
+ }
61
+
62
+ // UpdateTools refreshes agent tools and instructions while preserving the .specforce/ directory.
63
+ func (s *Service) UpdateTools(ctx context.Context, ui core.UI, selectedAgents []string) error {
64
+ if ui != nil {
65
+ ui.SubTask("Updating agent tools and instructions...")
66
+ }
67
+
68
+ opts := installer.Options{ToolsOnly: true}
69
+
70
+ // Iterate through selected tools and update them
71
+ for _, agentID := range selectedAgents {
72
+ if err := ctx.Err(); err != nil {
73
+ return err
74
+ }
75
+
76
+ if err := agent.AdaptArtifacts(ctx, s.projectRoot, s.kitFS, agentID, ui, opts); err != nil {
77
+ // We log but continue if one agent fails to update
78
+ if ui != nil {
79
+ ui.Warn(fmt.Sprintf("Failed to update tools for %s: %v", agentID, err))
80
+ }
81
+ }
82
+ }
83
+
84
+ if ui != nil {
85
+ ui.Success("Agent tools and instructions updated successfully.")
86
+ }
87
+
88
+ if err := EnsureAgentsMD(s.projectRoot, ui); err != nil {
89
+ return fmt.Errorf("failed to update AGENTS.md: %w", err)
90
+ }
91
+
92
+ return nil
93
+ }
@@ -0,0 +1,135 @@
1
+ package project_test
2
+
3
+ import (
4
+ "context"
5
+ "testing"
6
+ "testing/fstest"
7
+
8
+ "github.com/jeancodogno/specforce-kit/src/internal/core"
9
+ "github.com/jeancodogno/specforce-kit/src/internal/project"
10
+ )
11
+
12
+ var _ core.UI = (*mockUI)(nil)
13
+
14
+ type mockUI struct {
15
+ subtasks []string
16
+ warnings []string
17
+ }
18
+
19
+ func (m *mockUI) Log(msg string) {}
20
+ func (m *mockUI) Success(msg string) {}
21
+ func (m *mockUI) Warn(msg string) { m.warnings = append(m.warnings, msg) }
22
+ func (m *mockUI) Error(msg string) {}
23
+ func (m *mockUI) SubTask(msg string) {
24
+ m.subtasks = append(m.subtasks, msg)
25
+ }
26
+ func (m *mockUI) StartSpinner(msg string) {}
27
+ func (m *mockUI) StopSpinner() {}
28
+ func (m *mockUI) Confirm(msg string) bool { return true }
29
+
30
+ func TestService_InitializeProject(t *testing.T) {
31
+ kitFS := fstest.MapFS{
32
+ "kit.yaml": &fstest.MapFile{Data: []byte(`
33
+ tools:
34
+ test-agent:
35
+ name: "Test Agent"
36
+ target: ".test-agent"
37
+ `)},
38
+ "agents/test-agent.yaml": &fstest.MapFile{Data: []byte("content: hello")},
39
+ }
40
+ tmpDir := t.TempDir()
41
+ artifactsFS := fstest.MapFS{}
42
+
43
+ svc := project.NewService(kitFS, artifactsFS, tmpDir)
44
+ ui := &mockUI{}
45
+
46
+ t.Run("success", func(t *testing.T) {
47
+ config := project.InitConfig{
48
+ ProjectRoot: tmpDir,
49
+ SelectedAgents: []string{"test-agent"},
50
+ }
51
+ err := svc.InitializeProject(context.Background(), ui, config)
52
+ if err != nil {
53
+ t.Fatalf("InitializeProject failed: %v", err)
54
+ }
55
+ })
56
+
57
+ t.Run("agent not found", func(t *testing.T) {
58
+ config := project.InitConfig{
59
+ ProjectRoot: t.TempDir(),
60
+ SelectedAgents: []string{"non-existent"},
61
+ }
62
+ err := svc.InitializeProject(context.Background(), ui, config)
63
+ if err == nil {
64
+ t.Fatal("expected error for non-existent agent")
65
+ }
66
+ })
67
+ }
68
+
69
+ func TestService_GetConfig(t *testing.T) {
70
+ kitFS := fstest.MapFS{}
71
+ artifactsFS := fstest.MapFS{}
72
+ tmpDir := t.TempDir()
73
+
74
+ svc := project.NewService(kitFS, artifactsFS, tmpDir)
75
+
76
+ // Initially, it should return an empty config
77
+ conf, err := svc.GetConfig(context.Background())
78
+ if err != nil {
79
+ t.Fatalf("GetConfig failed: %v", err)
80
+ }
81
+ if conf == nil || len(conf.Instructions) != 0 {
82
+ t.Errorf("Expected empty config, got %v", conf)
83
+ }
84
+ }
85
+
86
+ func TestService_UpdateTools(t *testing.T) {
87
+ kitFS := fstest.MapFS{
88
+ "kit.yaml": &fstest.MapFile{Data: []byte(`
89
+ tools:
90
+ gemini:
91
+ name: "Gemini"
92
+ target: ".gemini/"
93
+ `)},
94
+ "agents/gemini.yaml": &fstest.MapFile{Data: []byte("content: gemini-content")},
95
+ }
96
+ artifactsFS := fstest.MapFS{}
97
+ tmpDir := t.TempDir()
98
+
99
+ svc := project.NewService(kitFS, artifactsFS, tmpDir)
100
+ ui := &mockUI{}
101
+
102
+ t.Run("success", func(t *testing.T) {
103
+ err := svc.UpdateTools(context.Background(), ui, []string{"gemini"})
104
+ if err != nil {
105
+ t.Fatalf("UpdateTools failed: %v", err)
106
+ }
107
+ })
108
+
109
+ t.Run("agent update failure", func(t *testing.T) {
110
+ uiWithWarnings := &mockUI{}
111
+ err := svc.UpdateTools(context.Background(), uiWithWarnings, []string{"non-existent"})
112
+ if err != nil {
113
+ t.Fatalf("UpdateTools should not return error if one agent fails: %v", err)
114
+ }
115
+ if len(uiWithWarnings.warnings) == 0 {
116
+ t.Fatal("expected warning for failed agent update")
117
+ }
118
+ })
119
+
120
+ t.Run("cancelled context", func(t *testing.T) {
121
+ ctx, cancel := context.WithCancel(context.Background())
122
+ cancel()
123
+ err := svc.UpdateTools(ctx, ui, []string{"gemini"})
124
+ if err == nil {
125
+ t.Fatal("expected error for cancelled context")
126
+ }
127
+ })
128
+
129
+ t.Run("nil ui", func(t *testing.T) {
130
+ err := svc.UpdateTools(context.Background(), nil, []string{"gemini"})
131
+ if err != nil {
132
+ t.Fatalf("UpdateTools failed with nil UI: %v", err)
133
+ }
134
+ })
135
+ }
@@ -0,0 +1,49 @@
1
+ package spec
2
+
3
+ import (
4
+ "context"
5
+ "fmt"
6
+ "os"
7
+ "path/filepath"
8
+ )
9
+
10
+ // ArchiveSpec moves a specification from the specs directory to the archive directory.
11
+ func ArchiveSpec(ctx context.Context, basePath, slug string) error {
12
+ if err := ctx.Err(); err != nil {
13
+ return err
14
+ }
15
+
16
+ specsDir := filepath.Join(basePath, ".specforce", "specs", slug)
17
+ archiveDir := filepath.Join(basePath, ".specforce", "archive", slug)
18
+
19
+ // Check if source exists
20
+ srcInfo, err := os.Stat(specsDir)
21
+ if os.IsNotExist(err) {
22
+ return fmt.Errorf("spec not found: %s", slug)
23
+ }
24
+ if !srcInfo.IsDir() {
25
+ return fmt.Errorf("spec path is not a directory: %s", specsDir)
26
+ }
27
+
28
+ // Check if destination already exists
29
+ _, err = os.Stat(archiveDir)
30
+ if err == nil {
31
+ return fmt.Errorf("archive already exists: %s", slug)
32
+ }
33
+ if !os.IsNotExist(err) {
34
+ return fmt.Errorf("failed to check archive destination: %w", err)
35
+ }
36
+
37
+ // Ensure archive parent directory exists
38
+ if err := os.MkdirAll(filepath.Dir(archiveDir), 0750); err != nil {
39
+ return fmt.Errorf("failed to create archive base directory: %w", err)
40
+ }
41
+
42
+ // Perform atomic move
43
+ if err := os.Rename(specsDir, archiveDir); err != nil {
44
+ // Fallback for cross-partition move or other rename failures could be implemented here
45
+ return fmt.Errorf("failed to archive spec: %w", err)
46
+ }
47
+
48
+ return nil
49
+ }
@@ -0,0 +1,55 @@
1
+ package spec
2
+
3
+ import (
4
+ "context"
5
+ "os"
6
+ "path/filepath"
7
+ "testing"
8
+ )
9
+
10
+ func TestArchiveSpec(t *testing.T) {
11
+ tempDir, err := os.MkdirTemp("", "specforce-test-*")
12
+ if err != nil {
13
+ t.Fatalf("failed to create temp dir: %v", err)
14
+ }
15
+ defer func() { _ = os.RemoveAll(tempDir) }()
16
+
17
+ slug := "0001-test"
18
+ specsPath := filepath.Join(tempDir, ".specforce", "specs", slug)
19
+ archivePath := filepath.Join(tempDir, ".specforce", "archive", slug)
20
+
21
+ if err := os.MkdirAll(specsPath, 0755); err != nil {
22
+ t.Fatalf("failed to create specs path: %v", err)
23
+ }
24
+ if err := os.WriteFile(filepath.Join(specsPath, "requirements.md"), []byte("test content"), 0644); err != nil {
25
+ t.Fatalf("failed to create test file: %v", err)
26
+ }
27
+
28
+ // Happy Path
29
+ if err := ArchiveSpec(context.Background(), tempDir, slug); err != nil {
30
+ t.Errorf("expected no error, got %v", err)
31
+ }
32
+
33
+ if _, err := os.Stat(archivePath); os.IsNotExist(err) {
34
+ t.Errorf("expected archive path to exist")
35
+ }
36
+ if _, err := os.Stat(specsPath); !os.IsNotExist(err) {
37
+ t.Errorf("expected source path to be removed")
38
+ }
39
+
40
+ // Error Case: Spec not found
41
+ err = ArchiveSpec(context.Background(), tempDir, "9999-missing")
42
+ if err == nil || err.Error() != "spec not found: 9999-missing" {
43
+ t.Errorf("expected 'spec not found' error, got %v", err)
44
+ }
45
+
46
+ // Error Case: Archive already exists
47
+ // Re-create source
48
+ if err := os.MkdirAll(specsPath, 0755); err != nil {
49
+ t.Fatalf("failed to recreate specs path: %v", err)
50
+ }
51
+ err = ArchiveSpec(context.Background(), tempDir, slug)
52
+ if err == nil || err.Error() != "archive already exists: 0001-test" {
53
+ t.Errorf("expected 'archive already exists' error, got %v", err)
54
+ }
55
+ }
@@ -0,0 +1,53 @@
1
+ package spec
2
+
3
+ import (
4
+ "context"
5
+ "os"
6
+ "path/filepath"
7
+ )
8
+
9
+ // SpecInfo contains basic information about a specification.
10
+ type SpecInfo struct {
11
+ Slug string `json:"slug"`
12
+ }
13
+
14
+ // ListActiveSpecs returns a list of all non-archived specs.
15
+ func ListActiveSpecs(ctx context.Context, projectRoot string) ([]SpecInfo, error) {
16
+ if err := ctx.Err(); err != nil {
17
+ return nil, err
18
+ }
19
+
20
+ specsDir := filepath.Join(projectRoot, ".specforce", "specs")
21
+ entries, err := os.ReadDir(specsDir)
22
+ if err != nil {
23
+ if os.IsNotExist(err) {
24
+ return []SpecInfo{}, nil
25
+ }
26
+ return nil, err
27
+ }
28
+
29
+ var specs []SpecInfo
30
+ for _, entry := range entries {
31
+ // Ignore hidden directories and non-directories
32
+ if entry.IsDir() && entry.Name()[0] != '.' {
33
+ specs = append(specs, SpecInfo{Slug: entry.Name()})
34
+ }
35
+ }
36
+
37
+ return specs, nil
38
+ }
39
+
40
+ // SpecExists checks if a spec slug already exists in either specs or archive.
41
+ func SpecExists(projectRoot string, slug string) (bool, string) {
42
+ specsDir := filepath.Join(projectRoot, ".specforce", "specs", slug)
43
+ if _, err := os.Stat(specsDir); err == nil {
44
+ return true, "active"
45
+ }
46
+
47
+ archiveDir := filepath.Join(projectRoot, ".specforce", "archive", slug)
48
+ if _, err := os.Stat(archiveDir); err == nil {
49
+ return true, "archived"
50
+ }
51
+
52
+ return false, ""
53
+ }
@@ -0,0 +1,60 @@
1
+ package spec
2
+
3
+ import (
4
+ "context"
5
+ "os"
6
+ "path/filepath"
7
+ "testing"
8
+ )
9
+
10
+ func TestDiscovery(t *testing.T) {
11
+ // Setup temp project root
12
+ if err := os.MkdirAll(filepath.Join("testdata", "discovery"), 0755); err != nil {
13
+ t.Fatal(err)
14
+ }
15
+ defer func() { _ = os.RemoveAll("testdata") }()
16
+
17
+ projectRoot := filepath.Join("testdata", "discovery")
18
+ specsDir := filepath.Join(projectRoot, ".specforce", "specs")
19
+ archiveDir := filepath.Join(projectRoot, ".specforce", "archive")
20
+
21
+ // Create active spec
22
+ if err := os.MkdirAll(filepath.Join(specsDir, "active-spec"), 0755); err != nil {
23
+ t.Fatal(err)
24
+ }
25
+ // Create archived spec
26
+ if err := os.MkdirAll(filepath.Join(archiveDir, "archived-spec"), 0755); err != nil {
27
+ t.Fatal(err)
28
+ }
29
+
30
+ t.Run("SpecExists active", func(t *testing.T) {
31
+ exists, status := SpecExists(projectRoot, "active-spec")
32
+ if !exists || status != "active" {
33
+ t.Errorf("expected active-spec to exist and be active, got %v, %s", exists, status)
34
+ }
35
+ })
36
+
37
+ t.Run("SpecExists archived", func(t *testing.T) {
38
+ exists, status := SpecExists(projectRoot, "archived-spec")
39
+ if !exists || status != "archived" {
40
+ t.Errorf("expected archived-spec to exist and be archived, got %v, %s", exists, status)
41
+ }
42
+ })
43
+
44
+ t.Run("SpecExists none", func(t *testing.T) {
45
+ exists, status := SpecExists(projectRoot, "none")
46
+ if exists {
47
+ t.Errorf("expected none to not exist, got %v, %s", exists, status)
48
+ }
49
+ })
50
+
51
+ t.Run("ListActiveSpecs", func(t *testing.T) {
52
+ specs, err := ListActiveSpecs(context.Background(), projectRoot)
53
+ if err != nil {
54
+ t.Fatal(err)
55
+ }
56
+ if len(specs) != 1 || specs[0].Slug != "active-spec" {
57
+ t.Errorf("expected 1 active spec (active-spec), got %d: %+v", len(specs), specs)
58
+ }
59
+ })
60
+ }