Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/internal/utils_test.go
3434 views
1
package internal_test
2
3
import (
4
"github.com/kardolus/chatgpt-cli/internal"
5
. "github.com/onsi/gomega"
6
"github.com/sclevine/spec"
7
"github.com/sclevine/spec/report"
8
"os"
9
"testing"
10
)
11
12
func TestUnitUtils(t *testing.T) {
13
spec.Run(t, "Testing the Utils", testUtils, spec.Report(report.Terminal{}))
14
}
15
16
func testUtils(t *testing.T, when spec.G, it spec.S) {
17
it.Before(func() {
18
RegisterTestingT(t)
19
Expect(os.Unsetenv(internal.ConfigHomeEnv)).To(Succeed())
20
Expect(os.Unsetenv(internal.DataHomeEnv)).To(Succeed())
21
Expect(os.Unsetenv(internal.CacheHomeEnv)).To(Succeed())
22
})
23
24
when("GetConfigHome()", func() {
25
it("Uses the default value if OPENAI_CONFIG_HOME is not set", func() {
26
configHome, err := internal.GetConfigHome()
27
28
Expect(err).NotTo(HaveOccurred())
29
Expect(configHome).To(ContainSubstring(".chatgpt-cli")) // Assuming default location is ~/.chatgpt-cli
30
})
31
32
it("Overwrites the default when OPENAI_CONFIG_HOME is set", func() {
33
customConfigHome := "/custom/config/path"
34
Expect(os.Setenv("OPENAI_CONFIG_HOME", customConfigHome)).To(Succeed())
35
36
configHome, err := internal.GetConfigHome()
37
38
Expect(err).NotTo(HaveOccurred())
39
Expect(configHome).To(Equal(customConfigHome))
40
})
41
})
42
43
when("GetDataHome()", func() {
44
it("Uses the default value if OPENAI_DATA_HOME is not set", func() {
45
dataHome, err := internal.GetDataHome()
46
47
Expect(err).NotTo(HaveOccurred())
48
Expect(dataHome).To(ContainSubstring(".chatgpt-cli/history")) // Assuming default location is ~/.local/share/chatgpt-cli
49
})
50
51
it("Overwrites the default when OPENAI_DATA_HOME is set", func() {
52
customDataHome := "/custom/data/path"
53
Expect(os.Setenv("OPENAI_DATA_HOME", customDataHome)).To(Succeed())
54
55
dataHome, err := internal.GetDataHome()
56
57
Expect(err).NotTo(HaveOccurred())
58
Expect(dataHome).To(Equal(customDataHome))
59
})
60
})
61
62
when("GetCacheHome()", func() {
63
it("Uses the default value if OPENAI_CACHE_HOME is not set", func() {
64
cacheHome, err := internal.GetCacheHome()
65
66
Expect(err).NotTo(HaveOccurred())
67
Expect(cacheHome).To(ContainSubstring(".chatgpt-cli/cache")) // Assuming default location is ~/.local/share/chatgpt-cli
68
})
69
70
it("Overwrites the default when OPENAI_CACHE_HOME is set", func() {
71
cacheDataHome := "/custom/cache/path"
72
Expect(os.Setenv("OPENAI_CACHE_HOME", cacheDataHome)).To(Succeed())
73
74
cacheHome, err := internal.GetCacheHome()
75
76
Expect(err).NotTo(HaveOccurred())
77
Expect(cacheHome).To(Equal(cacheDataHome))
78
})
79
})
80
81
when("GenerateUniqueSlug()", func() {
82
it("Has the expected length", func() {
83
prefix := "123"
84
result := internal.GenerateUniqueSlug(prefix)
85
Expect(result).To(HaveLen(len(prefix) + internal.SlugPostfixLength))
86
})
87
})
88
}
89
90