@@ -6,23 +6,25 @@ import (
66 "os"
77 "path/filepath"
88 "testing"
9+ "time"
910
1011 "github.com/git-calendar/core/pkg/core"
1112 "github.com/git-calendar/core/pkg/filesystem"
13+ "github.com/google/uuid"
1214)
1315
14- const TestCalendarName = "test"
16+ const testCalendarName = "test"
1517
1618func TestCreateCalendar (t * testing.T ) {
1719 c := core .NewCore ()
1820
19- err := c .CreateCalendar (TestCalendarName , "" )
21+ err := c .CreateCalendar (testCalendarName , "" )
2022 if err != nil {
2123 t .Fatalf ("failed to init repo: %v" , err )
2224 }
2325
2426 t .Cleanup (func () {
25- _ = c .RemoveCalendar (TestCalendarName )
27+ _ = c .RemoveCalendar (testCalendarName )
2628 })
2729
2830 home , err := os .UserHomeDir ()
@@ -37,7 +39,7 @@ func TestCreateCalendar(t *testing.T) {
3739
3840 var found bool
3941 for _ , d := range dirs {
40- if d .Name () == TestCalendarName {
42+ if d .Name () == testCalendarName {
4143 found = true
4244 break
4345 }
@@ -50,13 +52,13 @@ func TestCreateCalendar(t *testing.T) {
5052func TestListCalendars (t * testing.T ) {
5153 c := core .NewCore ()
5254
53- err := c .CreateCalendar (TestCalendarName , "" )
55+ err := c .CreateCalendar (testCalendarName , "" )
5456 if err != nil {
5557 t .Fatalf ("failed to create calendar: %v" , err )
5658 }
5759
5860 t .Cleanup (func () {
59- _ = c .RemoveCalendar (TestCalendarName )
61+ _ = c .RemoveCalendar (testCalendarName )
6062 })
6163
6264 calendars , err := c .ListCalendars ()
@@ -66,7 +68,7 @@ func TestListCalendars(t *testing.T) {
6668
6769 var found bool
6870 for _ , calendar := range calendars {
69- if calendar .Name != TestCalendarName {
71+ if calendar .Name != testCalendarName {
7072 continue
7173 }
7274
@@ -82,25 +84,25 @@ func TestListCalendars(t *testing.T) {
8284 }
8385
8486 if ! found {
85- t .Errorf ("calendar %q not found in list" , TestCalendarName )
87+ t .Errorf ("calendar %q not found in list" , testCalendarName )
8688 }
8789}
8890
8991func TestListCalendars_WithRemote (t * testing.T ) {
9092 c := core .NewCore ()
9193
92- err := c .CreateCalendar (TestCalendarName , "" )
94+ err := c .CreateCalendar (testCalendarName , "" )
9395 if err != nil {
9496 t .Fatalf ("failed to create calendar: %v" , err )
9597 }
9698
9799 t .Cleanup (func () {
98- _ = c .RemoveCalendar (TestCalendarName )
100+ _ = c .RemoveCalendar (testCalendarName )
99101 })
100102
101103 remoteUrl := "https://github.com/git-calendar/calendar.git"
102104
103- err = c .UpdateRemote (TestCalendarName , mustParseUrl (remoteUrl ), false )
105+ err = c .UpdateRemote (testCalendarName , mustParseUrl (remoteUrl ), false )
104106 if err != nil {
105107 t .Fatalf ("failed to update remotes: %v" , err )
106108 }
@@ -112,7 +114,7 @@ func TestListCalendars_WithRemote(t *testing.T) {
112114
113115 var found bool
114116 for _ , calendar := range calendars {
115- if calendar .Name != TestCalendarName {
117+ if calendar .Name != testCalendarName {
116118 continue
117119 }
118120 rurl , err := calendar .RemoteURL ()
@@ -127,19 +129,22 @@ func TestListCalendars_WithRemote(t *testing.T) {
127129 }
128130
129131 if ! found {
130- t .Errorf ("calendar %q not found in list" , TestCalendarName )
132+ t .Errorf ("calendar %q not found in list" , testCalendarName )
131133 }
132134}
133135
134136func TestRemoveCalendar (t * testing.T ) {
135137 c := core .NewCore ()
136138
137- err := c .CreateCalendar (TestCalendarName , "" )
139+ err := c .CreateCalendar (testCalendarName , "" )
138140 if err != nil {
139141 t .Fatalf ("failed to create calendar: %v" , err )
140142 }
143+ if err := c .UpdateRemote (testCalendarName , mustParseUrl ("https://example.com/calendar.git" ), true ); err != nil {
144+ t .Fatalf ("failed to make calendar read-only: %v" , err )
145+ }
141146
142- err = c .RemoveCalendar (TestCalendarName )
147+ err = c .RemoveCalendar (testCalendarName )
143148 if err != nil {
144149 t .Fatalf ("failed to remove calendar: %v" , err )
145150 }
@@ -149,7 +154,7 @@ func TestRemoveCalendar(t *testing.T) {
149154 t .Errorf ("failed to get home dir: %v" , err )
150155 }
151156
152- calendarPath := filepath .Join (home , filesystem .DirName , TestCalendarName )
157+ calendarPath := filepath .Join (home , filesystem .DirName , testCalendarName )
153158
154159 _ , err = os .Stat (calendarPath )
155160 if err == nil {
@@ -159,13 +164,18 @@ func TestRemoveCalendar(t *testing.T) {
159164 if err != nil && ! os .IsNotExist (err ) {
160165 t .Errorf ("failed to check calendar directory: %v" , err )
161166 }
167+
168+ readonlyPath := filepath .Join (home , filesystem .DirName , testCalendarName + core .ReadonlyFileSuffix )
169+ if _ , err := os .Stat (readonlyPath ); ! os .IsNotExist (err ) {
170+ t .Errorf ("read-only marker still exists or could not be checked: %v" , err )
171+ }
162172}
163173
164174func TestRenameCalendar (t * testing.T ) {
165175 c := core .NewCore ()
166176
167- oldName := TestCalendarName + "_old"
168- newName := TestCalendarName + "_new"
177+ oldName := testCalendarName + "_old"
178+ newName := testCalendarName + "_new"
169179
170180 err := c .CreateCalendar (oldName , "" )
171181 if err != nil {
@@ -176,6 +186,18 @@ func TestRenameCalendar(t *testing.T) {
176186 _ = c .RemoveCalendar (oldName )
177187 })
178188
189+ start := time .Date (2026 , 1 , 1 , 9 , 0 , 0 , 0 , time .UTC )
190+ event , err := c .CreateEvent (core.Event {
191+ ID : uuid .New (),
192+ Title : "Renamed calendar event" ,
193+ From : start ,
194+ To : start .Add (time .Hour ),
195+ Calendar : oldName ,
196+ })
197+ if err != nil {
198+ t .Fatalf ("failed to create event: %v" , err )
199+ }
200+
179201 err = c .RenameCalendar (oldName , newName )
180202 if err != nil {
181203 t .Fatalf ("failed to rename calendar: %v" , err )
@@ -185,6 +207,14 @@ func TestRenameCalendar(t *testing.T) {
185207 _ = c .RemoveCalendar (newName )
186208 })
187209
210+ renamedEvent , err := c .GetEvent (event .ID )
211+ if err != nil {
212+ t .Fatalf ("failed to get event after rename: %v" , err )
213+ }
214+ if renamedEvent .Calendar != newName {
215+ t .Fatalf ("event calendar = %q, want %q" , renamedEvent .Calendar , newName )
216+ }
217+
188218 home , err := os .UserHomeDir ()
189219 if err != nil {
190220 t .Errorf ("failed to get home dir: %v" , err )
@@ -211,15 +241,15 @@ func TestRenameCalendar(t *testing.T) {
211241func TestRenameCalendar_SameName (t * testing.T ) {
212242 c := core .NewCore ()
213243
214- err := c .CreateCalendar (TestCalendarName , "" )
244+ err := c .CreateCalendar (testCalendarName , "" )
215245 if err != nil {
216246 t .Fatalf ("failed to create calendar: %v" , err )
217247 }
218248 t .Cleanup (func () {
219- _ = c .RemoveCalendar (TestCalendarName )
249+ _ = c .RemoveCalendar (testCalendarName )
220250 })
221251
222- err = c .RenameCalendar (TestCalendarName , TestCalendarName )
252+ err = c .RenameCalendar (testCalendarName , testCalendarName )
223253 if err != nil {
224254 t .Fatalf ("renaming calendar to same name should not fail: %v" , err )
225255 }
@@ -229,7 +259,7 @@ func TestRenameCalendar_SameName(t *testing.T) {
229259 t .Errorf ("failed to get home dir: %v" , err )
230260 }
231261
232- calendarPath := filepath .Join (home , filesystem .DirName , TestCalendarName )
262+ calendarPath := filepath .Join (home , filesystem .DirName , testCalendarName )
233263
234264 _ , err = os .Stat (calendarPath )
235265 if err != nil {
@@ -240,9 +270,9 @@ func TestRenameCalendar_SameName(t *testing.T) {
240270func TestRenameCalendar_MissingCalendar (t * testing.T ) {
241271 c := core .NewCore ()
242272
243- c .RemoveCalendar (TestCalendarName )
273+ c .RemoveCalendar (testCalendarName )
244274
245- err := c .RenameCalendar (TestCalendarName , "new-calendar" )
275+ err := c .RenameCalendar (testCalendarName , "new-calendar" )
246276 if err == nil {
247277 t .Fatal ("expected error when renaming missing calendar" )
248278 }
@@ -251,8 +281,8 @@ func TestRenameCalendar_MissingCalendar(t *testing.T) {
251281func TestRenameCalendar_AlreadyExists (t * testing.T ) {
252282 c := core .NewCore ()
253283
254- oldName := TestCalendarName + "_old"
255- newName := TestCalendarName + "_new"
284+ oldName := testCalendarName + "_old"
285+ newName := testCalendarName + "_new"
256286
257287 err := c .CreateCalendar (oldName , "" )
258288 if err != nil {
0 commit comments