Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/internal/utils_test.go
2649 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
})
22
23
when("GetConfigHome()", func() {
24
it("Uses the default value if OPENAI_CONFIG_HOME is not set", func() {
25
configHome, err := internal.GetConfigHome()
26
27
Expect(err).NotTo(HaveOccurred())
28
Expect(configHome).To(ContainSubstring(".chatgpt-cli")) // Assuming default location is ~/.chatgpt-cli
29
})
30
31
it("Overwrites the default when OPENAI_CONFIG_HOME is set", func() {
32
customConfigHome := "/custom/config/path"
33
Expect(os.Setenv("OPENAI_CONFIG_HOME", customConfigHome)).To(Succeed())
34
35
configHome, err := internal.GetConfigHome()
36
37
Expect(err).NotTo(HaveOccurred())
38
Expect(configHome).To(Equal(customConfigHome))
39
})
40
})
41
42
when("GetDataHome()", func() {
43
it("Uses the default value if OPENAI_DATA_HOME is not set", func() {
44
dataHome, err := internal.GetDataHome()
45
46
Expect(err).NotTo(HaveOccurred())
47
Expect(dataHome).To(ContainSubstring(".chatgpt-cli/history")) // Assuming default location is ~/.local/share/chatgpt-cli
48
})
49
50
it("Overwrites the default when OPENAI_DATA_HOME is set", func() {
51
customDataHome := "/custom/data/path"
52
Expect(os.Setenv("OPENAI_DATA_HOME", customDataHome)).To(Succeed())
53
54
dataHome, err := internal.GetDataHome()
55
56
Expect(err).NotTo(HaveOccurred())
57
Expect(dataHome).To(Equal(customDataHome))
58
})
59
})
60
61
when("GenerateUniqueSlug()", func() {
62
it("Has the expected length", func() {
63
prefix := "123"
64
result := internal.GenerateUniqueSlug(prefix)
65
Expect(result).To(HaveLen(len(prefix) + internal.SlugPostfixLength))
66
})
67
})
68
}
69
70