test(todo): cover validation branches

This commit is contained in:
瑞达 committed 2026-08-19 11:01:59 +08:00
1 parent 63fe34a239
commit 3f0e75ea0a
4 files changed
+35 -19

No files matched your search

-4
View File
@@ -19,7 +19,6 @@ import (
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/corecmd"
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/corecmd/contract"
apperrors "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/errors"
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/output"
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut"
todoshortcut "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut/todo"
@@ -77,9 +76,6 @@ var Assign = shortcut.Shortcut{
Execute: func(rt *shortcut.RuntimeContext) error {
name := strings.TrimSpace(rt.Str("to"))
task := strings.TrimSpace(rt.Str("task"))
if name == "" || task == "" {
return apperrors.NewValidation("--to 与 --task 均不能为空")
}
var dueMillis int64
if rt.Changed("due") {
var err error
-3
View File
@@ -84,9 +84,6 @@ var Remind = shortcut.Shortcut{
Tips: []string{`dws todo +remind --task "交周报" --at 2026-03-10T18:00:00+08:00`},
Execute: func(rt *shortcut.RuntimeContext) error {
task := strings.TrimSpace(rt.Str("task"))
if task == "" {
return apperrors.NewValidation("--task 不能为空")
}
var dueMillis int64
if rt.Changed("at") {
var err error
@@ -177,17 +177,32 @@ func TestCrossPlatformCoverageCalendarSmartReadCommands(t *testing.T) {
}
func TestCrossPlatformCoverageTodoSmartWriteBranches(t *testing.T) {
for name, args := range map[string][]string{
"assign-empty-task": {"todo", "+assign", "--task", " ", "--to", "fixture person", "--yes"},
"assign-bad-due": {"todo", "+assign", "--task", "task", "--to", "fixture person", "--due", "tomorrow", "--yes"},
"remind-empty-task": {"todo", "+remind", "--task", " ", "--yes"},
} {
t.Run(name, func(t *testing.T) {
if _, _, err := runCalendarSmartCLI(t, &calendarSmartTestCaller{}, args...); err == nil {
t.Fatal("invalid todo shortcut arguments accepted")
}
})
}
assignDry := &calendarSmartTestCaller{steps: map[string][]calendarSmartTestStep{"contact/search_contact_by_key_word": {{text: smartContact().text}}}}
if _, _, err := runCalendarSmartCLI(t, assignDry, "todo", "+assign", "--task", "task", "--to", "fixture person", "--due", smartCoverageEnd, "--dry-run", "--yes"); err != nil {
t.Fatal(err)
}
if _, _, err := runCalendarSmartCLI(t, &calendarSmartTestCaller{}, "todo", "+assign", "--task", "task", "--to", "fixture person", "--yes"); err == nil {
t.Fatal("assign contact resolution failure accepted")
}
for name, responses := range map[string]map[string][]calendarSmartTestStep{
"call": {"contact/search_contact_by_key_word": {{text: smartContact().text}}, "todo/create_personal_todo": {{err: errors.New("write")}}},
"verify": {"contact/search_contact_by_key_word": {{text: smartContact().text}}, "todo/create_personal_todo": {{text: `{"result":{}}`}}},
"success": {"contact/search_contact_by_key_word": {{text: smartContact().text}}, "todo/create_personal_todo": {{text: `{"success":true,"result":{"taskId":"task-placeholder"}}`}}, "todo/get_todo_detail": {{text: `{"success":true,"result":{"todoDetailModel":{"taskId":"task-placeholder","subject":"task"}}}`}}},
} {
t.Run("assign-"+name, func(t *testing.T) {
_, _, err := runCalendarSmartCLI(t, &calendarSmartTestCaller{steps: responses}, "todo", "+assign", "--task", "task", "--to", "fixture person", "--yes")
_, _, err := runCalendarSmartCLI(t, &calendarSmartTestCaller{steps: responses}, "todo", "+assign", "--task", "task", "--to", "fixture person", "--due", smartCoverageEnd, "--yes")
if name == "success" {
if err != nil {
t.Fatal(err)
@@ -232,6 +247,10 @@ func TestCrossPlatformCoverageTodoSmartWriteBranches(t *testing.T) {
if _, _, err := runCalendarSmartCLI(t, remindDry, "todo", "+remind", "--task", "task", "--at", smartCoverageEnd, "--dry-run", "--yes"); err != nil {
t.Fatal(err)
}
remindProfileFailure := &calendarSmartTestCaller{steps: map[string][]calendarSmartTestStep{"contact/get_current_user_profile": {{err: errors.New("profile")}}}}
if _, _, err := runCalendarSmartCLI(t, remindProfileFailure, "todo", "+remind", "--task", "task", "--yes"); err == nil {
t.Fatal("remind profile failure accepted")
}
remindSuccess := &calendarSmartTestCaller{steps: map[string][]calendarSmartTestStep{"contact/get_current_user_profile": {{text: `{"result":{"userId":"user-placeholder"}}`}}, "todo/create_personal_todo": {{text: `{"success":true,"result":{"taskId":"task-placeholder"}}`}}, "todo/get_todo_detail": {{text: `{"success":true,"result":{"todoDetailModel":{"taskId":"task-placeholder","subject":"task"}}}`}}}}
if _, _, err := runCalendarSmartCLI(t, remindSuccess, "todo", "+remind", "--task", "task", "--yes"); err != nil {
t.Fatal(err)
@@ -91,7 +91,7 @@ func TestCrossPlatformCoverageTodoCommonStrictBranches(t *testing.T) {
if _, err := requireTodoResponse(map[string]any{"result": map[string]any{}}, "todo/test"); err != nil {
t.Fatalf("response without optional success rejected: %v", err)
}
for _, response := range []map[string]any{{}, {"result": map[string]any{}}, {"success": false}} {
for _, response := range []map[string]any{{}, {"result": map[string]any{}}, {"success": "yes"}, {"success": false}} {
if err := requireTodoWriteReceipt(response, "todo/test"); err == nil {
t.Fatalf("bad receipt accepted: %#v", response)
}
@@ -431,11 +431,14 @@ func TestCrossPlatformCoverageTodoCommentAndReminder(t *testing.T) {
}
for name, args := range map[string][]string{
"none": {"--task-id", "task-1", "--yes"},
"both": {"--task-id", "task-1", "--clear", "--base-time", "dueTime", "--yes"},
"due-offset": {"--task-id", "task-1", "--base-time", "dueTime", "--yes"},
"custom-at": {"--task-id", "task-1", "--base-time", "customTime", "--yes"},
"custom-value": {"--task-id", "task-1", "--base-time", "customTime", "--at", "bad", "--yes"},
"none": {"--task-id", "task-1", "--yes"},
"both": {"--task-id", "task-1", "--clear", "--base-time", "dueTime", "--yes"},
"clear-offset": {"--task-id", "task-1", "--clear", "--due-date-offset", "-30", "--yes"},
"due-offset": {"--task-id", "task-1", "--base-time", "dueTime", "--yes"},
"due-at": {"--task-id", "task-1", "--base-time", "dueTime", "--due-date-offset", "-30", "--at", todoCoverageTime, "--yes"},
"custom-at": {"--task-id", "task-1", "--base-time", "customTime", "--yes"},
"custom-offset": {"--task-id", "task-1", "--base-time", "customTime", "--at", todoCoverageTime, "--due-date-offset", "-30", "--yes"},
"custom-value": {"--task-id", "task-1", "--base-time", "customTime", "--at", "bad", "--yes"},
} {
t.Run("reminder-"+name, func(t *testing.T) {
if err := runTodoCoverage(t, Reminder, &todoCoverageCaller{responses: map[string][]string{}}, args...); err == nil {
@@ -542,11 +545,12 @@ func TestCrossPlatformCoverageTodoVerificationHelpers(t *testing.T) {
func TestCrossPlatformCoverageTodoGetMyTasksAndListLeaves(t *testing.T) {
for name, args := range map[string][]string{
"page": {"--page", "0"},
"size": {"--size", "21"},
"priority": {"--priority", "11"},
"role": {"--role-types", "owner"},
"max": {"--all", "--max-pages", "0"},
"page": {"--page", "0"},
"size": {"--size", "21"},
"priority": {"--priority", "11"},
"role": {"--role-types", "owner"},
"max": {"--all", "--max-pages", "0"},
"max-alone": {"--max-pages", "1"},
} {
t.Run(name, func(t *testing.T) {
if err := runTodoCoverage(t, GetMyTasks, &todoCoverageCaller{responses: map[string][]string{}}, args...); err == nil {