refactor: better project structure (#35)

This project is starting to grow, and I've seen flaws in the initial
project structure design
I think I'll restructure the entire project directory for better
scalability and also cleaner
This commit is contained in:
VERSE
2026-07-11 16:15:05 +07:00
committed by GitHub
parent 94c8af7b1d
commit 1e04949242
52 changed files with 339 additions and 445 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/spf13/cobra"
"github.com/versenilvis/iris/config"
"github.com/versenilvis/iris/internal/config"
)
var ConfigCmd = &cobra.Command{
+32
View File
@@ -0,0 +1,32 @@
package root
import (
"bytes"
"os"
"path/filepath"
"testing"
)
func TestConfigCommands(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "iris-config-cmd-test")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
t.Setenv("XDG_CONFIG_HOME", tmpDir)
ConfigInitCmd.Run(ConfigInitCmd, []string{})
configPath := filepath.Join(tmpDir, "iris", "config.toml")
if _, err := os.Stat(configPath); err != nil {
t.Errorf("expected config file to be created at %s, but it was not", configPath)
}
buf := new(bytes.Buffer)
ConfigShowCmd.SetOut(buf)
ConfigShowCmd.Run(ConfigShowCmd, []string{})
if buf.Len() == 0 {
t.Errorf("expected show command to output configuration")
}
}
+1 -1
View File
@@ -11,7 +11,7 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/versenilvis/iris/config"
"github.com/versenilvis/iris/internal/config"
)
// startRescueShell starts a fallback shell if the application crashes to keep the terminal open
+89
View File
@@ -0,0 +1,89 @@
package root
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
)
func TestWriteCrashLog(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "iris-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
t.Setenv("HOME", tmpDir)
t.Setenv("XDG_CACHE_HOME", filepath.Join(tmpDir, ".cache"))
testErr := "test panic message"
WriteCrashLog(testErr)
dir := filepath.Join(tmpDir, ".cache", "iris", "crashes")
files, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("failed to read crashes dir: %v", err)
}
if len(files) != 1 {
t.Fatalf("expected exactly 1 crash log file, got %d", len(files))
}
logPath := filepath.Join(dir, files[0].Name())
data, err := os.ReadFile(logPath)
if err != nil {
t.Fatalf("failed to read crash log: %v", err)
}
content := string(data)
if !strings.Contains(content, "=== IRIS CRASH ") {
t.Errorf("expected header in log, got: %s", content)
}
if !strings.Contains(content, "panic: test panic message") {
t.Errorf("expected panic message in log, got: %s", content)
}
if !strings.Contains(content, "version:") {
t.Errorf("expected version in log, got: %s", content)
}
}
func TestCrashLogCommand(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "iris-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
t.Setenv("HOME", tmpDir)
t.Setenv("XDG_CACHE_HOME", filepath.Join(tmpDir, ".cache"))
var buf bytes.Buffer
CrashCmd.SetOut(&buf)
CrashCmd.SetArgs([]string{})
ClearLog = false
CrashCmd.Run(CrashCmd, []string{})
if !strings.Contains(buf.String(), "no crash log found") {
t.Errorf("expected 'no crash log found', got: %q", buf.String())
}
WriteCrashLog("mock error")
buf.Reset()
CrashCmd.Run(CrashCmd, []string{})
if !strings.Contains(buf.String(), "crash_") || !strings.Contains(buf.String(), ".log") {
t.Errorf("expected crash log path, got: %q", buf.String())
}
buf.Reset()
ClearLog = true
CrashCmd.Run(CrashCmd, []string{})
if !strings.Contains(buf.String(), "crash log cleared") {
t.Errorf("expected 'crash log cleared', got: %q", buf.String())
}
dir := filepath.Join(tmpDir, ".cache", "iris", "crashes")
if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Errorf("expected crashes directory to be deleted, but it exists")
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"strings"
"github.com/spf13/cobra"
"github.com/versenilvis/iris/config"
"github.com/versenilvis/iris/internal/config"
)
var initCmd = &cobra.Command{
+2 -2
View File
@@ -15,8 +15,8 @@ import (
"github.com/spf13/cobra"
_ "github.com/versenilvis/iris/commands"
"github.com/versenilvis/iris/config"
"github.com/versenilvis/iris/logger"
"github.com/versenilvis/iris/internal/config"
"github.com/versenilvis/iris/internal/logger"
"golang.org/x/term"
)
+3 -3
View File
@@ -4,10 +4,10 @@ import (
"strings"
"sync"
"github.com/versenilvis/iris/ai"
"github.com/versenilvis/iris/config"
"github.com/versenilvis/iris/integration"
"github.com/versenilvis/iris/logger"
"github.com/versenilvis/iris/internal/ai"
"github.com/versenilvis/iris/internal/config"
"github.com/versenilvis/iris/internal/logger"
"github.com/versenilvis/iris/spec"
)
+49
View File
@@ -0,0 +1,49 @@
package root
import (
"testing"
"github.com/versenilvis/iris/spec"
)
func TestMergeResults(t *testing.T) {
t.Run("Dedup exact match", func(t *testing.T) {
// Mock history items that might conflict with specs
res := MergeResults("git", "spec")
seen := make(map[string]bool)
for _, r := range res {
if seen[r.Cmd] {
t.Errorf("Duplicate suggestion found: %q", r.Cmd)
}
seen[r.Cmd] = true
}
})
t.Run("Limit 100", func(t *testing.T) {
res := MergeResults("a", "history")
if len(res) > 100 {
t.Errorf("Expected max 100 suggestions, got %d", len(res))
}
})
t.Run("AI Suggestion Promotion", func(t *testing.T) {
aiSugg := &spec.Suggestion{
Cmd: "git commit -m \"fix(auth): login bug\"",
Desc: "AI suggestion",
Source: "ai",
Confidence: 85,
}
SetCurrentAISuggestion(aiSugg)
defer SetCurrentAISuggestion(nil)
res := MergeResults("git c", "history")
if len(res) == 0 {
t.Fatalf("Expected suggestions, got 0")
}
if res[0].Cmd != aiSugg.Cmd {
t.Errorf("Expected AI suggestion at index 0, got %q (confidence %d)", res[0].Cmd, res[0].Confidence)
}
})
}
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"strings"
"github.com/spf13/cobra"
"github.com/versenilvis/iris/config"
"github.com/versenilvis/iris/internal/config"
)
func init() {
+1 -1
View File
@@ -13,7 +13,7 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/versenilvis/iris/config"
"github.com/versenilvis/iris/internal/config"
)
// updateResult is passed from the async checker to the main loop
+64
View File
@@ -0,0 +1,64 @@
package root
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/versenilvis/iris/internal/config"
)
func TestIsNewer(t *testing.T) {
tests := []struct {
current string
latest string
want bool
}{
{"v1.0.0", "v1.0.1", true},
{"v1.0.1", "v1.0.0", false},
{"v1.0.0", "v1.0.0", false},
{"v1.2.3", "v1.2.4", true},
{"v1.2.0", "v1.1.9", false},
{"dev", "v1.0.0", false}, // dev never updates
{"v1.0.0", "dev", false},
{"", "v1.0.0", false},
{"v1.0.0", "v1.1.0-nightly.8cb1f47", false}, // nightly never triggers update
{"v1.1.0-nightly.abc", "v1.2.0", true}, // but if you are on nightly, you can update to stable
}
for _, tt := range tests {
if got := IsNewer(tt.current, tt.latest); got != tt.want {
t.Errorf("IsNewer(%q, %q) = %v; want %v", tt.current, tt.latest, got, tt.want)
}
}
}
func TestUpdateState(t *testing.T) {
// Use a temporary directory for the state file
tmpDir, err := os.MkdirTemp("", "iris-test-*")
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.RemoveAll(tmpDir) }()
t.Setenv("HOME", tmpDir)
t.Setenv("XDG_DATA_HOME", filepath.Join(tmpDir, ".local", "share"))
state := config.LoadState()
state.Updater.SeenVersion = "v1.0.0"
state.Updater.LastCheckTime = time.Unix(123456789, 0)
err = config.SaveState(state)
if err != nil {
t.Fatalf("failed to save state: %v", err)
}
loaded := config.LoadState()
if loaded.Updater.SeenVersion != state.Updater.SeenVersion {
t.Errorf("Expected SeenVersion %q, got %q", state.Updater.SeenVersion, loaded.Updater.SeenVersion)
}
if loaded.Updater.LastCheckTime.Unix() != state.Updater.LastCheckTime.Unix() {
t.Errorf("Expected LastCheck %v, got %v", state.Updater.LastCheckTime, loaded.Updater.LastCheckTime)
}
}
+3 -3
View File
@@ -17,11 +17,11 @@ import (
"time"
"github.com/creack/pty"
"github.com/versenilvis/iris/ai"
"github.com/versenilvis/iris/config"
"github.com/versenilvis/iris/integration"
"github.com/versenilvis/iris/integration/shell"
"github.com/versenilvis/iris/logger"
"github.com/versenilvis/iris/internal/ai"
"github.com/versenilvis/iris/internal/config"
"github.com/versenilvis/iris/internal/logger"
"github.com/versenilvis/iris/spec"
"golang.org/x/sys/unix"
"golang.org/x/term"