1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5package lunatask_test
6
7import (
8 "errors"
9 "testing"
10
11 lunatask "git.secluded.site/go-lunatask"
12)
13
14func TestParseDeepLink(t *testing.T) {
15 t.Parallel()
16
17 tests := []struct {
18 name string
19 input string
20 wantResource lunatask.Resource
21 wantUUID string
22 wantErr error
23 }{
24 // Valid deep links
25 {"task_link", "lunatask://tasks/abc-123", lunatask.ResourceTask, "abc-123", nil},
26 {"area_link", "lunatask://areas/def-456", lunatask.ResourceArea, "def-456", nil},
27 {"goal_link", "lunatask://goals/ghi-789", lunatask.ResourceGoal, "ghi-789", nil},
28 {"note_link", "lunatask://notes/jkl-012", lunatask.ResourceNote, "jkl-012", nil},
29 {"person_link", "lunatask://people/mno-345", lunatask.ResourcePerson, "mno-345", nil},
30 {"notebook_link", "lunatask://notebooks/pqr-678", lunatask.ResourceNotebook, "pqr-678", nil},
31
32 // Plain UUIDs
33 {"plain_uuid", "abc-123-def-456", "", "abc-123-def-456", nil},
34 {"uuid_only", "12345678-1234-1234-1234-123456789012", "", "12345678-1234-1234-1234-123456789012", nil},
35
36 // Invalid inputs
37 {"empty", "", "", "", lunatask.ErrInvalidDeepLink},
38 {"invalid_resource", "lunatask://invalid/abc-123", "", "", lunatask.ErrInvalidResource},
39 {"missing_uuid", "lunatask://tasks/", "", "", lunatask.ErrInvalidDeepLink},
40 {"missing_resource", "lunatask:///abc-123", "", "", lunatask.ErrInvalidDeepLink},
41 {"malformed", "lunatask://tasks", "", "", lunatask.ErrInvalidDeepLink},
42 }
43
44 for _, testCase := range tests {
45 t.Run(testCase.name, func(t *testing.T) {
46 t.Parallel()
47
48 resource, uuid, err := lunatask.ParseDeepLink(testCase.input)
49
50 if testCase.wantErr != nil {
51 if !errors.Is(err, testCase.wantErr) {
52 t.Errorf("ParseDeepLink(%q) error = %v, want %v", testCase.input, err, testCase.wantErr)
53 }
54
55 return
56 }
57
58 if err != nil {
59 t.Errorf("ParseDeepLink(%q) unexpected error = %v", testCase.input, err)
60
61 return
62 }
63
64 if resource != testCase.wantResource {
65 t.Errorf("ParseDeepLink(%q) resource = %q, want %q", testCase.input, resource, testCase.wantResource)
66 }
67
68 if uuid != testCase.wantUUID {
69 t.Errorf("ParseDeepLink(%q) uuid = %q, want %q", testCase.input, uuid, testCase.wantUUID)
70 }
71 })
72 }
73}
74
75func TestBuildDeepLink(t *testing.T) {
76 t.Parallel()
77
78 tests := []struct {
79 name string
80 resource lunatask.Resource
81 id string
82 want string
83 wantErr error
84 }{
85 // Valid builds
86 {"task", lunatask.ResourceTask, "abc-123", "lunatask://tasks/abc-123", nil},
87 {"area", lunatask.ResourceArea, "def-456", "lunatask://areas/def-456", nil},
88 {"goal", lunatask.ResourceGoal, "ghi-789", "lunatask://goals/ghi-789", nil},
89 {"note", lunatask.ResourceNote, "jkl-012", "lunatask://notes/jkl-012", nil},
90 {"person", lunatask.ResourcePerson, "mno-345", "lunatask://people/mno-345", nil},
91 {"notebook", lunatask.ResourceNotebook, "pqr-678", "lunatask://notebooks/pqr-678", nil},
92
93 // Invalid inputs
94 {"empty_id", lunatask.ResourceTask, "", "", lunatask.ErrInvalidUUID},
95 {"invalid_resource", lunatask.Resource("invalid"), "abc-123", "", lunatask.ErrInvalidResource},
96 }
97
98 for _, testCase := range tests {
99 t.Run(testCase.name, func(t *testing.T) {
100 t.Parallel()
101
102 got, err := lunatask.BuildDeepLink(testCase.resource, testCase.id)
103
104 if testCase.wantErr != nil {
105 if !errors.Is(err, testCase.wantErr) {
106 t.Errorf("BuildDeepLink(%q, %q) error = %v, want %v", testCase.resource, testCase.id, err, testCase.wantErr)
107 }
108
109 return
110 }
111
112 if err != nil {
113 t.Errorf("BuildDeepLink(%q, %q) unexpected error = %v", testCase.resource, testCase.id, err)
114
115 return
116 }
117
118 if got != testCase.want {
119 t.Errorf("BuildDeepLink(%q, %q) = %q, want %q", testCase.resource, testCase.id, got, testCase.want)
120 }
121 })
122 }
123}