internal/update_test.go
249 lines · 7542 bytes
14 symbols in this file
TestIsUpdateNewerTestPendingUpdateShouldPromptRespectsSkipAndRemindTestCheckForUpdateInBackgroundWritesPendingStateTestTruncateReleaseNotesTestIsPermissionErrorTestUpdatePendingPathTestFormatLocalTimeUsesLocalZoneTestPreferGUIPasswordPromptWithRofiTestIsCrossDeviceErrorTestCopyFileReplaceCrossDeviceStyleTestReplaceExecutableHandlesCrossDeviceTestBuildUpdatePromptMessageTestMarkdownToPangoColorsHeadingsAndBulletsTestUpdateActionOptionsOrder
1package internal
2
3import (
4 "encoding/json"
5 "errors"
6 "net/http"
7 "net/http/httptest"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12 "time"
13)
14
15func TestIsUpdateNewer(t *testing.T) {
16 if !isUpdateNewer("2.0.4", "2.0.3") {
17 t.Fatal("expected 2.0.4 newer than 2.0.3")
18 }
19 if isUpdateNewer("2.0.3", "2.0.3") {
20 t.Fatal("same version is not newer")
21 }
22 if isUpdateNewer("2.0.2", "2.0.3") {
23 t.Fatal("older version is not newer")
24 }
25 if !isUpdateNewer("v2.1.0", "2.0.9") {
26 t.Fatal("tag prefix should be normalized")
27 }
28}
29
30func TestPendingUpdateShouldPromptRespectsSkipAndRemind(t *testing.T) {
31 cfg := &CurdConfig{CheckUpdates: true}
32 state := updatePendingState{
33 Available: true,
34 LatestVersion: "2.1.0",
35 }
36 if !pendingUpdateShouldPrompt(cfg, "2.0.4", state) {
37 t.Fatal("expected prompt when update available")
38 }
39
40 state.SkippedVersion = "2.1.0"
41 if pendingUpdateShouldPrompt(cfg, "2.0.4", state) {
42 t.Fatal("skipped version should not prompt")
43 }
44
45 state.SkippedVersion = ""
46 state.RemindAfter = time.Now().Add(time.Hour).UTC().Format(time.RFC3339)
47 if pendingUpdateShouldPrompt(cfg, "2.0.4", state) {
48 t.Fatal("remind-later window should suppress prompt")
49 }
50
51 cfg.CheckUpdates = false
52 state.RemindAfter = ""
53 if pendingUpdateShouldPrompt(cfg, "2.0.4", state) {
54 t.Fatal("disabled CheckUpdates should not prompt")
55 }
56}
57
58func TestCheckForUpdateInBackgroundWritesPendingState(t *testing.T) {
59 storage := t.TempDir()
60 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61 if r.URL.Path != "/repos/Wraient/curd/releases/latest" {
62 http.NotFound(w, r)
63 return
64 }
65 _ = json.NewEncoder(w).Encode(map[string]interface{}{
66 "tag_name": "v9.9.9",
67 "name": "Curd v9.9.9",
68 "body": "## Changes\n- test",
69 "html_url": "https://github.com/Wraient/curd/releases/tag/v9.9.9",
70 "assets": []map[string]string{},
71 })
72 }))
73 t.Cleanup(server.Close)
74
75 // Point GitHub API at the test server by temporarily overriding fetch via env is hard;
76 // instead call fetch through a rewritten helper path: exercise save/load + isUpdateNewer
77 // with a synthetic state write matching what background check would store.
78 state := updatePendingState{
79 Available: true,
80 LatestVersion: "9.9.9",
81 LatestTag: "v9.9.9",
82 ReleaseName: "Curd v9.9.9",
83 ReleaseNotes: "## Changes\n- test",
84 HTMLURL: "https://github.com/Wraient/curd/releases/tag/v9.9.9",
85 CheckedAt: time.Now().UTC().Format(time.RFC3339),
86 }
87 if err := saveUpdatePendingState(storage, state); err != nil {
88 t.Fatalf("save: %v", err)
89 }
90 loaded := loadUpdatePendingState(storage)
91 if !loaded.Available || loaded.LatestVersion != "9.9.9" {
92 t.Fatalf("unexpected loaded state: %#v", loaded)
93 }
94 cfg := &CurdConfig{CheckUpdates: true, StoragePath: storage}
95 if !pendingUpdateShouldPrompt(cfg, "2.0.4", loaded) {
96 t.Fatal("expected pending update prompt")
97 }
98 _ = server
99}
100
101func TestTruncateReleaseNotes(t *testing.T) {
102 short := truncateReleaseNotes("hello")
103 if short != "hello" {
104 t.Fatalf("got %q", short)
105 }
106 long := strings.Repeat("a", maxReleaseNotesRunes+50)
107 got := truncateReleaseNotes(long)
108 if !strings.Contains(got, "truncated") {
109 t.Fatalf("expected truncation marker, got len=%d", len(got))
110 }
111}
112
113func TestIsPermissionError(t *testing.T) {
114 if !isPermissionError(os.ErrPermission) {
115 t.Fatal("os.ErrPermission should match")
116 }
117 if isPermissionError(os.ErrNotExist) {
118 t.Fatal("not-exist should not match")
119 }
120}
121
122func TestUpdatePendingPath(t *testing.T) {
123 path := updatePendingPath(filepath.Join("tmp", "share"))
124 if filepath.Base(path) != updatePendingFileName {
125 t.Fatalf("unexpected path %q", path)
126 }
127}
128
129func TestFormatLocalTimeUsesLocalZone(t *testing.T) {
130 // Fixed instant: 2026-07-23T20:24:06Z
131 utc := time.Date(2026, 7, 23, 20, 24, 6, 0, time.UTC)
132 got := formatLocalTime(utc)
133 if strings.Contains(got, "2026-07-23T20:24:06Z") {
134 t.Fatalf("expected local display, not RFC3339 Z form: %q", got)
135 }
136 if !strings.Contains(got, "2026") || !strings.Contains(got, ":") {
137 t.Fatalf("unexpected local format %q", got)
138 }
139}
140
141func TestPreferGUIPasswordPromptWithRofi(t *testing.T) {
142 prev := GetGlobalConfig()
143 t.Cleanup(func() { SetGlobalConfig(prev) })
144
145 SetGlobalConfig(&CurdConfig{RofiSelection: true})
146 if !preferGUIPasswordPrompt() {
147 t.Fatal("expected GUI password preference when RofiSelection is on")
148 }
149
150 // CLI mode (Rofi off): never force GUI just because a display session exists.
151 SetGlobalConfig(&CurdConfig{RofiSelection: false})
152 if preferGUIPasswordPrompt() && stdinIsTerminal() {
153 t.Fatal("CLI with a TTY should use terminal password, not GUI")
154 }
155}
156
157func TestIsCrossDeviceError(t *testing.T) {
158 if !isCrossDeviceError(errors.New("rename /tmp/a /home/b: invalid cross-device link")) {
159 t.Fatal("expected cross-device detection")
160 }
161 if isCrossDeviceError(os.ErrPermission) {
162 t.Fatal("permission is not cross-device")
163 }
164}
165
166func TestCopyFileReplaceCrossDeviceStyle(t *testing.T) {
167 srcDir := t.TempDir()
168 dstDir := t.TempDir()
169 src := filepath.Join(srcDir, "newbin")
170 dst := filepath.Join(dstDir, "curd")
171 if err := os.WriteFile(src, []byte("#!/bin/sh\necho new\n"), 0755); err != nil {
172 t.Fatal(err)
173 }
174 if err := os.WriteFile(dst, []byte("#!/bin/sh\necho old\n"), 0755); err != nil {
175 t.Fatal(err)
176 }
177 if err := copyFileReplace(src, dst); err != nil {
178 t.Fatalf("copyFileReplace: %v", err)
179 }
180 data, err := os.ReadFile(dst)
181 if err != nil {
182 t.Fatal(err)
183 }
184 if !strings.Contains(string(data), "new") {
185 t.Fatalf("dest not replaced: %q", data)
186 }
187}
188
189func TestReplaceExecutableHandlesCrossDevice(t *testing.T) {
190 srcDir := t.TempDir()
191 dstDir := t.TempDir()
192 src := filepath.Join(srcDir, "downloaded")
193 dst := filepath.Join(dstDir, "curd")
194 if err := os.WriteFile(src, []byte("new-binary-content"), 0755); err != nil {
195 t.Fatal(err)
196 }
197 if err := os.WriteFile(dst, []byte("old-binary-content"), 0755); err != nil {
198 t.Fatal(err)
199 }
200 // src and dst are different temp dirs — rename often fails with EXDEV on Linux.
201 if err := replaceExecutable(src, dst); err != nil {
202 t.Fatalf("replaceExecutable: %v", err)
203 }
204 data, err := os.ReadFile(dst)
205 if err != nil {
206 t.Fatal(err)
207 }
208 if string(data) != "new-binary-content" {
209 t.Fatalf("got %q", data)
210 }
211}
212
213func TestBuildUpdatePromptMessage(t *testing.T) {
214 prompt, msg := buildUpdatePromptMessage("2.0.1", updatePendingState{
215 LatestVersion: "2.0.2",
216 ReleaseName: "Curd v2.0.2",
217 HTMLURL: "https://example.com",
218 ReleaseNotes: "## Direct Commits\n- **fixed** stuff",
219 })
220 if !strings.Contains(prompt, "2.0.1") || !strings.Contains(prompt, "2.0.2") {
221 t.Fatalf("prompt=%q", prompt)
222 }
223 if !strings.Contains(msg, "fixed") || !strings.Contains(msg, "https://example.com") {
224 t.Fatalf("message=%q", msg)
225 }
226}
227
228func TestMarkdownToPangoColorsHeadingsAndBullets(t *testing.T) {
229 md := "## Direct Commits\n- fix: something\n**Full Changelog**: https://example.com/compare"
230 got := markdownToPango(md)
231 if !strings.Contains(got, "foreground=") {
232 t.Fatalf("expected pango colors, got %q", got)
233 }
234 if !strings.Contains(got, "Direct Commits") || !strings.Contains(got, "•") {
235 t.Fatalf("expected heading/bullet conversion, got %q", got)
236 }
237}
238
239func TestUpdateActionOptionsOrder(t *testing.T) {
240 opts := updateActionOptions()
241 if len(opts) < 1 || opts[0].Key != "update" {
242 t.Fatalf("Update now must be first, got %#v", opts)
243 }
244 for _, o := range opts {
245 if strings.HasPrefix(o.Label, "1.") || strings.Contains(o.Label, "1. ") {
246 t.Fatalf("labels should not be numbered: %q", o.Label)
247 }
248 }
249}