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() aDateStr := "2024-11-02" aDate := time.Date(2024, 11, 9, 0, 0, 0, 0, time.UTC) aTimeStr := "12:00" aTime := time.Date(0, 0, 0, 12, 0, 0, 0, time.UTC) aDayStr := "24h" aDay := time.Duration(24) * time.Hour flagOn := &command.FlagDate{ Name: command.FlagOn, Value: aDate, } flagAt := &command.FlagTime{ Name: command.FlagAt, Value: aTime, } flagFor := &command.FlagDuration{ Name: command.FlagFor, Value: aDay, } cmd := command.AddCmd{} for _, tc := range []struct { name string main []string flags map[string]command.Flag expErr bool }{ { name: "empty", expErr: true, }, { name: "title missing", main: []string{"add"}, flags: map[string]command.Flag{ command.FlagOn: flagOn, }, expErr: true, }, { name: "date missing", main: []string{"add", "some", "title"}, expErr: true, }, { name: "minimal", main: []string{"add", "title"}, flags: map[string]command.Flag{ command.FlagOn: flagOn, }, }, { name: "start", main: []string{"add", "title"}, flags: map[string]command.Flag{ command.FlagOn: flagOn, command.FlagAt: flagAt, }, }, // { // 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) { actErr := cmd.Parse(tc.main, tc.flags) 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)) } }) } }