-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add initial automations and overdue labels #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,153 @@ | ||||||||
| // SPDX-FileCopyrightText: 2025 Daniel Morris <daniel@honestempire.com> | ||||||||
| // SPDX-License-Identifier: MIT | ||||||||
|
|
||||||||
| package automation | ||||||||
|
|
||||||||
| import ( | ||||||||
| "fmt" | ||||||||
| "time" | ||||||||
|
|
||||||||
| "github.com/unfunco/t/internal/list" | ||||||||
| "github.com/unfunco/t/internal/model" | ||||||||
| "github.com/unfunco/t/internal/storage" | ||||||||
| ) | ||||||||
|
|
||||||||
| const day = 24 * time.Hour | ||||||||
|
|
||||||||
| // Sync loads all default lists, applies scheduled automations, persists any | ||||||||
| // changes, and returns the resulting lists keyed by their ID. | ||||||||
| func Sync(store storage.Storage, now time.Time) (map[list.ID]*model.TodoList, error) { | ||||||||
| defs := list.Default() | ||||||||
| lists := make(map[list.ID]*model.TodoList, len(defs)) | ||||||||
|
|
||||||||
| for _, def := range defs { | ||||||||
| l, err := store.LoadList(def) | ||||||||
| if err != nil { | ||||||||
| return nil, fmt.Errorf("load %s list: %w", def.Name, err) | ||||||||
| } | ||||||||
|
|
||||||||
| lists[def.ID] = l | ||||||||
| } | ||||||||
|
|
||||||||
| todayStart := startOfDay(now) | ||||||||
| changed := ensureDueDates(lists[list.TodayID], list.TodayID) | ||||||||
|
|
||||||||
| if ensureDueDates(lists[list.TomorrowID], list.TomorrowID) { | ||||||||
| changed = true | ||||||||
| } | ||||||||
|
|
||||||||
| if moveTomorrowTodos(lists[list.TomorrowID], lists[list.TodayID], todayStart) { | ||||||||
| changed = true | ||||||||
| } | ||||||||
|
|
||||||||
| if changed { | ||||||||
| for _, def := range defs { | ||||||||
| l := lists[def.ID] | ||||||||
| if l == nil { | ||||||||
| continue | ||||||||
| } | ||||||||
| if err := store.SaveList(def, l); err != nil { | ||||||||
| return nil, fmt.Errorf("save %s list: %w", def.Name, err) | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return lists, nil | ||||||||
| } | ||||||||
|
|
||||||||
| func ensureDueDates(todoList *model.TodoList, id list.ID) bool { | ||||||||
| if todoList == nil { | ||||||||
| return false | ||||||||
| } | ||||||||
|
|
||||||||
| changed := false | ||||||||
| for i := range todoList.Todos { | ||||||||
| todo := &todoList.Todos[i] | ||||||||
|
|
||||||||
| switch id { | ||||||||
| case list.TodayID: | ||||||||
| if applyDueDate(todo, list.DefaultDueDate(list.TodayID, todo.CreatedAt)) { | ||||||||
| changed = true | ||||||||
| } | ||||||||
| case list.TomorrowID: | ||||||||
| if applyDueDate(todo, list.DefaultDueDate(list.TomorrowID, todo.CreatedAt)) { | ||||||||
| changed = true | ||||||||
| } | ||||||||
| default: | ||||||||
| if todo.DueDate != nil { | ||||||||
| normalized := startOfDay(*todo.DueDate) | ||||||||
| if !todo.DueDate.Equal(normalized) { | ||||||||
| todo.SetDueDate(&normalized) | ||||||||
| changed = true | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return changed | ||||||||
| } | ||||||||
|
|
||||||||
| func moveTomorrowTodos(tomorrowList, todayList *model.TodoList, todayStart time.Time) bool { | ||||||||
| if tomorrowList == nil || todayList == nil { | ||||||||
| return false | ||||||||
| } | ||||||||
|
|
||||||||
| var remaining []model.Todo | ||||||||
| changed := false | ||||||||
|
|
||||||||
| for _, todo := range tomorrowList.Todos { | ||||||||
| if todo.Completed { | ||||||||
| remaining = append(remaining, todo) | ||||||||
| continue | ||||||||
| } | ||||||||
|
|
||||||||
| if todo.DueDate == nil { | ||||||||
| remaining = append(remaining, todo) | ||||||||
| continue | ||||||||
| } | ||||||||
|
|
||||||||
| due := startOfDay(*todo.DueDate) | ||||||||
| if due.After(todayStart) { | ||||||||
| remaining = append(remaining, todo) | ||||||||
| continue | ||||||||
| } | ||||||||
|
|
||||||||
|
||||||||
| todo.SetDueDate(list.DefaultDueDate(list.TodayID, todayStart)) |
unfunco marked this conversation as resolved.
Show resolved
Hide resolved
unfunco marked this conversation as resolved.
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // SPDX-FileCopyrightText: 2025 Daniel Morris <daniel@honestempire.com> | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package automation | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/unfunco/t/internal/list" | ||
| "github.com/unfunco/t/internal/model" | ||
| ) | ||
|
|
||
| func TestSyncMovesDueTomorrowTodos(t *testing.T) { | ||
| now := time.Date(2025, time.January, 2, 9, 0, 0, 0, time.UTC) | ||
| yesterday := now.Add(-day) | ||
|
|
||
| due := list.DefaultDueDate(list.TomorrowID, yesterday) | ||
| if due == nil { | ||
| t.Fatalf("expected tomorrow list to have a due date") | ||
| } | ||
|
|
||
| store := newMemoryStorage(map[list.ID]*model.TodoList{ | ||
| list.TodayID: { | ||
| Name: list.Today().Name, | ||
| }, | ||
| list.TomorrowID: { | ||
| Name: list.Tomorrow().Name, | ||
| Todos: []model.Todo{ | ||
| { | ||
| ID: "1", | ||
| Title: "Move me", | ||
| CreatedAt: yesterday, | ||
| DueDate: due, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| lists, err := Sync(store, now) | ||
| if err != nil { | ||
| t.Fatalf("Sync returned error: %v", err) | ||
| } | ||
|
|
||
| if got := len(lists[list.TomorrowID].Todos); got != 0 { | ||
| t.Fatalf("expected tomorrow list to be empty, got %d todos", got) | ||
| } | ||
|
|
||
| if got := len(lists[list.TodayID].Todos); got != 1 { | ||
| t.Fatalf("expected today list to have 1 todo, got %d", got) | ||
| } | ||
|
|
||
| todo := lists[list.TodayID].Todos[0] | ||
| wantDue := list.DefaultDueDate(list.TodayID, now) | ||
| if todo.DueDate == nil || wantDue == nil || !todo.DueDate.Equal(*wantDue) { | ||
| t.Fatalf("expected due date %v, got %v", wantDue, todo.DueDate) | ||
| } | ||
| } | ||
|
|
||
| func TestSyncLeavesFutureTomorrowTodos(t *testing.T) { | ||
| now := time.Date(2025, time.January, 1, 9, 0, 0, 0, time.UTC) | ||
|
|
||
| due := list.DefaultDueDate(list.TomorrowID, now) | ||
| store := newMemoryStorage(map[list.ID]*model.TodoList{ | ||
| list.TomorrowID: { | ||
| Name: list.Tomorrow().Name, | ||
| Todos: []model.Todo{ | ||
| { | ||
| ID: "1", | ||
| Title: "Too early", | ||
| CreatedAt: now, | ||
| DueDate: due, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| lists, err := Sync(store, now) | ||
| if err != nil { | ||
| t.Fatalf("Sync returned error: %v", err) | ||
| } | ||
|
|
||
| if got := len(lists[list.TomorrowID].Todos); got != 1 { | ||
| t.Fatalf("expected tomorrow list to keep todo, got %d", got) | ||
| } | ||
| } | ||
|
|
||
| func TestSyncBackfillsMissingDueDates(t *testing.T) { | ||
| now := time.Date(2025, time.January, 3, 9, 0, 0, 0, time.UTC) | ||
| created := now.Add(-2 * day) | ||
|
|
||
| store := newMemoryStorage(map[list.ID]*model.TodoList{ | ||
| list.TodayID: { | ||
| Name: list.Today().Name, | ||
| Todos: []model.Todo{ | ||
| { | ||
| ID: "a", | ||
| Title: "Missing due date", | ||
| CreatedAt: created, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| lists, err := Sync(store, now) | ||
| if err != nil { | ||
| t.Fatalf("Sync returned error: %v", err) | ||
| } | ||
|
|
||
| todos := lists[list.TodayID].Todos | ||
| if len(todos) != 1 { | ||
| t.Fatalf("expected 1 todo after sync, got %d", len(todos)) | ||
| } | ||
|
|
||
| want := list.DefaultDueDate(list.TodayID, created) | ||
| if want == nil { | ||
| t.Fatalf("expected today list to produce a due date") | ||
| } | ||
|
|
||
| if todos[0].DueDate == nil || !todos[0].DueDate.Equal(*want) { | ||
| t.Fatalf("expected due date %v, got %v", want, todos[0].DueDate) | ||
| } | ||
| } | ||
|
|
||
| type memoryStorage struct { | ||
| lists map[list.ID]*model.TodoList | ||
| } | ||
|
|
||
| func newMemoryStorage(initial map[list.ID]*model.TodoList) *memoryStorage { | ||
| lists := make(map[list.ID]*model.TodoList, len(initial)) | ||
| for id, todoList := range initial { | ||
| lists[id] = todoList | ||
| } | ||
|
|
||
| return &memoryStorage{lists: lists} | ||
| } | ||
|
|
||
| func (m *memoryStorage) LoadList(def list.Definition) (*model.TodoList, error) { | ||
| if l, ok := m.lists[def.ID]; ok { | ||
| return l, nil | ||
| } | ||
|
|
||
| l := &model.TodoList{Name: def.Name} | ||
| m.lists[def.ID] = l | ||
|
|
||
| return l, nil | ||
| } | ||
|
|
||
| func (m *memoryStorage) SaveList(def list.Definition, todoList *model.TodoList) error { | ||
| m.lists[def.ID] = todoList | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.