package command_test import ( "testing" "time" "github.com/google/go-cmp/cmp" "go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/plan/command" "go-mod.ewintr.nl/planner/plan/storage/memory" ) func TestAddParse(t *testing.T) { t.Parallel() cmd := command.AddCmd{} for _, tc := range []struct { name string args []string expAS *command.ArgSet expErr bool }{ { name: "empty", expErr: true, }, { name: "title missing", args: []string{"add", "-on", "2024-11-09"}, expErr: true, }, { name: "date missing", args: []string{"add", "some", "title"}, expErr: true, }, { name: "minimal", args: []string{"add", "title", "-on", "2024-11-09"}, expAS: &command.ArgSet{ Main: "title", Flags: map[string]string{ command.FlagOn: "2024-11-09", command.FlagFor: "24h", }, }, }, { name: "start", args: []string{"add", "title", "-on", "2024-11-09", "-at", "12:00"}, expAS: &command.ArgSet{ Main: "title", Flags: map[string]string{ command.FlagOn: "2024-11-09", command.FlagAt: "12:00", }, }, }, { name: "start and duration", args: []string{"add", "title", "-on", "2024-11-10", "-at", "12:00", "-for", "1h"}, expAS: &command.ArgSet{ Main: "title", Flags: map[string]string{ command.FlagOn: "2024-11-10", command.FlagAt: "12:00", command.FlagFor: "1h", }, }, }, { name: "start without duration", args: []string{"add", "title", "-on", "2024-11-10", "-for", "1h"}, expErr: true, }, } { t.Run(tc.name, func(t *testing.T) { actAS, actErr := cmd.Parse(tc.args) if tc.expErr != (actErr != nil) { t.Errorf("exp nil, got %v", actErr) } if tc.expErr { return } if diff := cmp.Diff(tc.expAS, actAS); diff != "" { t.Errorf("(exp +, got -)\n%s", diff) } }) } } func TestAdd(t *testing.T) { t.Parallel() oneHour, err := time.ParseDuration("1h") if err != nil { t.Errorf("exp nil, got %v", err) } oneDay, err := time.ParseDuration("24h") if err != nil { t.Errorf("exp nil, got %v", err) } for _, tc := range []struct { name string args *command.ArgSet expEvent item.Event expErr bool }{ { name: "time, but no duration", args: &command.ArgSet{ Main: "event", Flags: map[string]string{ command.FlagOn: "2024-10-01", command.FlagAt: "9:00", }, }, expEvent: item.Event{ ID: "a", EventBody: item.EventBody{ Title: "event", Start: time.Date(2024, 10, 1, 9, 0, 0, 0, time.UTC), }, }, }, { name: "no time, no duration", args: &command.ArgSet{ Main: "event", Flags: map[string]string{ command.FlagOn: "2024-10-01", command.FlagFor: "24h", }, }, expEvent: item.Event{ ID: "a", EventBody: item.EventBody{ Title: "event", Start: time.Date(2024, 10, 1, 0, 0, 0, 0, time.UTC), Duration: oneDay, }, }, }, { name: "full", args: &command.ArgSet{ Main: "event", Flags: map[string]string{ command.FlagOn: "2024-10-01", command.FlagAt: "9:00", command.FlagFor: "1h", }, }, expEvent: item.Event{ ID: "a", EventBody: item.EventBody{ Title: "event", Start: time.Date(2024, 10, 1, 9, 0, 0, 0, time.UTC), Duration: oneHour, }, }, }, } { t.Run(tc.name, func(t *testing.T) { eventRepo := memory.NewEvent() localRepo := memory.NewLocalID() syncRepo := memory.NewSync() cmd := command.NewAddCmd(localRepo, eventRepo, syncRepo) actErr := cmd.Do(tc.args) != nil if tc.expErr != actErr { t.Errorf("exp %v, got %v", tc.expErr, actErr) } if tc.expErr { return } actEvents, err := eventRepo.FindAll() if err != nil { t.Errorf("exp nil, got %v", err) } if len(actEvents) != 1 { t.Errorf("exp 1, got %d", len(actEvents)) } actLocalIDs, err := localRepo.FindAll() if err != nil { t.Errorf("exp nil, got %v", err) } if len(actLocalIDs) != 1 { t.Errorf("exp 1, got %v", len(actLocalIDs)) } if _, ok := actLocalIDs[actEvents[0].ID]; !ok { t.Errorf("exp true, got %v", ok) } if actEvents[0].ID == "" { t.Errorf("exp string not te be empty") } tc.expEvent.ID = actEvents[0].ID if diff := cmp.Diff(tc.expEvent, actEvents[0]); diff != "" { t.Errorf("(exp +, got -)\n%s", diff) } updated, err := syncRepo.FindAll() if err != nil { t.Errorf("exp nil, got %v", err) } if len(updated) != 1 { t.Errorf("exp 1, got %v", len(updated)) } }) } }