Files
Local-Cursor/internal/cursor/localize_error_test.go

71 lines
2.1 KiB
Go
Raw Normal View History

package cursor
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
func TestCommandErrorIncludesOutput(t *testing.T) {
err := commandError("汉化包执行失败", "汉化未完成:测试错误", errors.New("exit status 1"))
if err == nil || !strings.Contains(err.Error(), "exit status 1") || !strings.Contains(err.Error(), "汉化未完成:测试错误") {
t.Fatalf("command output missing from error: %v", err)
}
}
func TestCommandOutputDetailLimitsLength(t *testing.T) {
detail := commandOutputDetail(strings.Repeat("汉", 4100))
if !strings.HasPrefix(detail, "…") {
t.Fatal("expected truncation marker")
}
if got := len([]rune(detail)); got != 4001 {
t.Fatalf("unexpected detail length: %d", got)
}
}
func TestOverrideEnvironmentRestoresValues(t *testing.T) {
t.Run("existing", func(t *testing.T) {
key := "LOCALIZE_TEST_REGISTRY_EXISTING"
t.Setenv(key, "https://mirror.example.com")
restore := overrideEnvironment([]string{key}, officialNPMRegistry)
if got := os.Getenv(key); got != officialNPMRegistry {
t.Fatalf("override not applied: %s", got)
}
restore()
if got := os.Getenv(key); got != "https://mirror.example.com" {
t.Fatalf("original value not restored: %s", got)
}
})
t.Run("missing", func(t *testing.T) {
key := "LOCALIZE_TEST_REGISTRY_MISSING"
t.Setenv(key, "placeholder")
if err := os.Unsetenv(key); err != nil {
t.Fatal(err)
}
restore := overrideEnvironment([]string{key}, officialNPMRegistry)
restore()
if _, exists := os.LookupEnv(key); exists {
t.Fatal("missing environment variable was not removed")
}
})
}
func TestFindNodeCLI(t *testing.T) {
nodeDir := t.TempDir()
binDir := filepath.Join(nodeDir, "node_modules", "npm", "bin")
if err := os.MkdirAll(binDir, 0o755); err != nil {
t.Fatal(err)
}
npxCLI := filepath.Join(binDir, "npx-cli.js")
if err := os.WriteFile(npxCLI, []byte("// test\n"), 0o644); err != nil {
t.Fatal(err)
}
nodePath := filepath.Join(nodeDir, "node.exe")
if got := findNodeCLI(nodePath, "npx-cli.js"); got != npxCLI {
t.Fatalf("unexpected npx CLI path: %s", got)
}
}