-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathblock_task_card_test.go
More file actions
155 lines (138 loc) · 3.75 KB
/
block_task_card_test.go
File metadata and controls
155 lines (138 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package slack
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewTaskCardBlock(t *testing.T) {
block := NewTaskCardBlock("task-1", "Search the web", TaskCardBlockOptionBlockID("block-1"))
assert.Equal(t, MBTTaskCard, block.BlockType())
assert.Equal(t, "task_card", string(block.Type))
assert.Equal(t, "block-1", block.ID())
assert.Equal(t, "task-1", block.TaskID)
assert.Equal(t, "Search the web", block.Title)
}
func TestTaskCardBlockChainableMethods(t *testing.T) {
details := &RichTextBlock{
Type: MBTRichText,
Elements: []RichTextElement{
&RichTextSection{
Type: RTESection,
Elements: []RichTextSectionElement{
&RichTextSectionTextElement{Type: RTSEText, Text: "Searching..."},
},
},
},
}
output := &RichTextBlock{
Type: MBTRichText,
Elements: []RichTextElement{
&RichTextSection{
Type: RTESection,
Elements: []RichTextSectionElement{
&RichTextSectionTextElement{Type: RTSEText, Text: "Found 3 results"},
},
},
},
}
block := NewTaskCardBlock("task-1", "Search the web").
WithStatus(TaskCardStatusComplete).
WithDetails(details).
WithOutput(output).
WithSources(
NewTaskCardSource("https://example.com", "Example"),
NewTaskCardSource("https://other.com", "Other"),
)
assert.Equal(t, TaskCardStatusComplete, block.Status)
assert.Equal(t, details, block.Details)
assert.Equal(t, output, block.Output)
assert.Len(t, block.Sources, 2)
assert.Equal(t, "url", block.Sources[0].Type)
assert.Equal(t, "https://example.com", block.Sources[0].URL)
assert.Equal(t, "Example", block.Sources[0].Text)
}
func TestTaskCardBlockJSONRoundTrip(t *testing.T) {
payload := `{
"type": "task_card",
"block_id": "block-1",
"task_id": "task-1",
"title": "Search the web",
"status": "in_progress",
"details": {
"type": "rich_text",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "Searching for results"
}
]
}
]
},
"output": {
"type": "rich_text",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "Found 3 results"
}
]
}
]
},
"sources": [
{
"type": "url",
"url": "https://example.com",
"text": "Example"
}
]
}`
var block TaskCardBlock
err := json.Unmarshal([]byte(payload), &block)
require.NoError(t, err)
assert.Equal(t, MBTTaskCard, block.BlockType())
assert.Equal(t, "block-1", block.ID())
assert.Equal(t, "task-1", block.TaskID)
assert.Equal(t, "Search the web", block.Title)
assert.Equal(t, TaskCardStatusInProgress, block.Status)
require.NotNil(t, block.Details)
require.NotNil(t, block.Output)
require.Len(t, block.Sources, 1)
assert.Equal(t, "https://example.com", block.Sources[0].URL)
marshalled, err := json.Marshal(block)
require.NoError(t, err)
var expected, actual map[string]interface{}
err = json.Unmarshal([]byte(payload), &expected)
require.NoError(t, err)
err = json.Unmarshal(marshalled, &actual)
require.NoError(t, err)
assert.Equal(t, expected, actual)
}
func TestTaskCardBlockUnmarshalViaBlocks(t *testing.T) {
payload := `[
{
"type": "task_card",
"task_id": "task-1",
"title": "Analyze data",
"status": "complete"
}
]`
var blocks Blocks
err := json.Unmarshal([]byte(payload), &blocks)
require.NoError(t, err)
require.Len(t, blocks.BlockSet, 1)
taskCard, ok := blocks.BlockSet[0].(*TaskCardBlock)
require.True(t, ok, "expected *TaskCardBlock, got %T", blocks.BlockSet[0])
assert.Equal(t, MBTTaskCard, taskCard.BlockType())
assert.Equal(t, "task-1", taskCard.TaskID)
assert.Equal(t, "Analyze data", taskCard.Title)
assert.Equal(t, TaskCardStatusComplete, taskCard.Status)
}