|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestMakeResponse(t *testing.T) { |
| 11 | + // Test with empty votes |
| 12 | + t.Run("EmptyVotes", func(t *testing.T) { |
| 13 | + votes := []map[string]string{} |
| 14 | + result := makeResponse(votes) |
| 15 | + |
| 16 | + // Verify the structure |
| 17 | + assert.Equal(t, "formattedData", result.CadenceResponseType) |
| 18 | + assert.Equal(t, "blocks", result.Format) |
| 19 | + assert.Len(t, result.Blocks, 5) |
| 20 | + |
| 21 | + // Verify JSON serialization matches expected output |
| 22 | + resultJSON, err := json.Marshal(result) |
| 23 | + assert.NoError(t, err) |
| 24 | + |
| 25 | + expectedJSON := `{ |
| 26 | + "cadenceResponseType": "formattedData", |
| 27 | + "format": "blocks", |
| 28 | + "blocks": [ |
| 29 | + { |
| 30 | + "type": "section", |
| 31 | + "format": "text/markdown", |
| 32 | + "componentOptions": { |
| 33 | + "text": "## Lunch options\nWe're voting on where to order lunch today. Select the option you want to vote for." |
| 34 | + } |
| 35 | + }, |
| 36 | + { |
| 37 | + "type": "divider" |
| 38 | + }, |
| 39 | + { |
| 40 | + "type": "section", |
| 41 | + "format": "text/markdown", |
| 42 | + "componentOptions": { |
| 43 | + "text": "| lunch order vote | meal | requests |\n|-------|-------|-------|\n| No votes yet |\n" |
| 44 | + } |
| 45 | + }, |
| 46 | + { |
| 47 | + "type": "section", |
| 48 | + "format": "text/markdown", |
| 49 | + "componentOptions": { |
| 50 | + "text": "| Picture | Description |\n|---|----|\n|  | Farmhouse - Red Thai Curry: (Thai: แกง, romanized: kaeng, pronounced [kɛ̄ːŋ]) is a dish in Thai cuisine made from curry paste, coconut milk or water, meat, seafood, vegetables or fruit, and herbs. Curries in Thailand mainly differ from the Indian subcontinent in their use of ingredients such as fresh rhizomes, herbs, and aromatic leaves rather than a mix of dried spices. |\n|  | Ler Ros: Lemongrass Tofu Bahn Mi: In Vietnamese cuisine, bánh mì, bánh mỳ or banh mi is a sandwich consisting of a baguette filled with various ingredients, most commonly including a protein such as pâté, chicken, or pork, and vegetables such as lettuce, cilantro, and cucumber. |\n|  | Ethiopian Wat: Wat is a traditional Ethiopian dish made from a mixture of spices, vegetables, and legumes. It is typically served with injera, a sourdough flatbread that is used to scoop up the food. |\n\n\n\n(source wikipedia)" |
| 51 | + } |
| 52 | + }, |
| 53 | + { |
| 54 | + "type": "actions", |
| 55 | + "elements": [ |
| 56 | + { |
| 57 | + "type": "button", |
| 58 | + "componentOptions": { |
| 59 | + "type": "plain_text", |
| 60 | + "text": "Farmhouse" |
| 61 | + }, |
| 62 | + "action": { |
| 63 | + "type": "signal", |
| 64 | + "signal_name": "lunch_order", |
| 65 | + "signal_value": { |
| 66 | + "location": "farmhouse - red thai curry", |
| 67 | + "requests": "spicy" |
| 68 | + } |
| 69 | + } |
| 70 | + }, |
| 71 | + { |
| 72 | + "type": "button", |
| 73 | + "componentOptions": { |
| 74 | + "type": "plain_text", |
| 75 | + "text": "Ethiopian" |
| 76 | + }, |
| 77 | + "action": { |
| 78 | + "type": "signal", |
| 79 | + "signal_name": "no_lunch_order_walk_in_person", |
| 80 | + "workflow_id": "in-person-order-workflow" |
| 81 | + } |
| 82 | + }, |
| 83 | + { |
| 84 | + "type": "button", |
| 85 | + "componentOptions": { |
| 86 | + "type": "plain_text", |
| 87 | + "text": "Ler Ros" |
| 88 | + }, |
| 89 | + "action": { |
| 90 | + "type": "signal", |
| 91 | + "signal_name": "lunch_order", |
| 92 | + "signal_value": { |
| 93 | + "location": "Ler Ros", |
| 94 | + "meal": "tofo Bahn Mi" |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + ] |
| 99 | + } |
| 100 | + ] |
| 101 | +}` |
| 102 | + |
| 103 | + assert.JSONEq(t, expectedJSON, string(resultJSON)) |
| 104 | + }) |
| 105 | + |
| 106 | + // Test with some votes |
| 107 | + t.Run("WithVotes", func(t *testing.T) { |
| 108 | + votes := []map[string]string{ |
| 109 | + {"location": "farmhouse - red thai curry", "requests": "spicy"}, |
| 110 | + {"location": "Ler Ros", "meal": "tofo Bahn Mi"}, |
| 111 | + } |
| 112 | + result := makeResponse(votes) |
| 113 | + |
| 114 | + // Verify the structure |
| 115 | + assert.Equal(t, "formattedData", result.CadenceResponseType) |
| 116 | + assert.Equal(t, "blocks", result.Format) |
| 117 | + assert.Len(t, result.Blocks, 5) |
| 118 | + |
| 119 | + // Verify JSON serialization matches expected output |
| 120 | + resultJSON, err := json.Marshal(result) |
| 121 | + assert.NoError(t, err) |
| 122 | + |
| 123 | + expectedJSON := `{ |
| 124 | + "cadenceResponseType": "formattedData", |
| 125 | + "format": "blocks", |
| 126 | + "blocks": [ |
| 127 | + { |
| 128 | + "type": "section", |
| 129 | + "format": "text/markdown", |
| 130 | + "componentOptions": { |
| 131 | + "text": "## Lunch options\nWe're voting on where to order lunch today. Select the option you want to vote for." |
| 132 | + } |
| 133 | + }, |
| 134 | + { |
| 135 | + "type": "divider" |
| 136 | + }, |
| 137 | + { |
| 138 | + "type": "section", |
| 139 | + "format": "text/markdown", |
| 140 | + "componentOptions": { |
| 141 | + "text": "| lunch order vote | meal | requests |\n|-------|-------|-------|\n| farmhouse - red thai curry | | spicy |\n| Ler Ros | tofo Bahn Mi | |\n" |
| 142 | + } |
| 143 | + }, |
| 144 | + { |
| 145 | + "type": "section", |
| 146 | + "format": "text/markdown", |
| 147 | + "componentOptions": { |
| 148 | + "text": "| Picture | Description |\n|---|----|\n|  | Farmhouse - Red Thai Curry: (Thai: แกง, romanized: kaeng, pronounced [kɛ̄ːŋ]) is a dish in Thai cuisine made from curry paste, coconut milk or water, meat, seafood, vegetables or fruit, and herbs. Curries in Thailand mainly differ from the Indian subcontinent in their use of ingredients such as fresh rhizomes, herbs, and aromatic leaves rather than a mix of dried spices. |\n|  | Ler Ros: Lemongrass Tofu Bahn Mi: In Vietnamese cuisine, bánh mì, bánh mỳ or banh mi is a sandwich consisting of a baguette filled with various ingredients, most commonly including a protein such as pâté, chicken, or pork, and vegetables such as lettuce, cilantro, and cucumber. |\n|  | Ethiopian Wat: Wat is a traditional Ethiopian dish made from a mixture of spices, vegetables, and legumes. It is typically served with injera, a sourdough flatbread that is used to scoop up the food. |\n\n\n\n(source wikipedia)" |
| 149 | + } |
| 150 | + }, |
| 151 | + { |
| 152 | + "type": "actions", |
| 153 | + "elements": [ |
| 154 | + { |
| 155 | + "type": "button", |
| 156 | + "componentOptions": { |
| 157 | + "type": "plain_text", |
| 158 | + "text": "Farmhouse" |
| 159 | + }, |
| 160 | + "action": { |
| 161 | + "type": "signal", |
| 162 | + "signal_name": "lunch_order", |
| 163 | + "signal_value": { |
| 164 | + "location": "farmhouse - red thai curry", |
| 165 | + "requests": "spicy" |
| 166 | + } |
| 167 | + } |
| 168 | + }, |
| 169 | + { |
| 170 | + "type": "button", |
| 171 | + "componentOptions": { |
| 172 | + "type": "plain_text", |
| 173 | + "text": "Ethiopian" |
| 174 | + }, |
| 175 | + "action": { |
| 176 | + "type": "signal", |
| 177 | + "signal_name": "no_lunch_order_walk_in_person", |
| 178 | + "workflow_id": "in-person-order-workflow" |
| 179 | + } |
| 180 | + }, |
| 181 | + { |
| 182 | + "type": "button", |
| 183 | + "componentOptions": { |
| 184 | + "type": "plain_text", |
| 185 | + "text": "Ler Ros" |
| 186 | + }, |
| 187 | + "action": { |
| 188 | + "type": "signal", |
| 189 | + "signal_name": "lunch_order", |
| 190 | + "signal_value": { |
| 191 | + "location": "Ler Ros", |
| 192 | + "meal": "tofo Bahn Mi" |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + ] |
| 197 | + } |
| 198 | + ] |
| 199 | +}` |
| 200 | + |
| 201 | + assert.JSONEq(t, expectedJSON, string(resultJSON)) |
| 202 | + }) |
| 203 | +} |
0 commit comments