|
1 | 1 | /** |
2 | 2 | * CLI Draft List Tests |
3 | 3 | * |
4 | | - * Tests the `superhuman draft list` command output with source column. |
| 4 | + * Tests the `superhuman draft list` command output with source column, |
| 5 | + * JSON output, and --to / --subject filtering. |
5 | 6 | */ |
6 | 7 |
|
7 | 8 | import { describe, it, expect, mock, beforeEach, afterEach } from "bun:test"; |
@@ -110,4 +111,174 @@ describe("superhuman draft list", () => { |
110 | 111 | expect(exitCode).toBe(0); |
111 | 112 | expect(stdout).toContain("draft"); |
112 | 113 | }); |
| 114 | + |
| 115 | + it("should filter drafts by --to recipient", () => { |
| 116 | + const drafts: Draft[] = [ |
| 117 | + { |
| 118 | + id: "draft-1", |
| 119 | + subject: "Meeting Follow-up", |
| 120 | + from: "user@example.com", |
| 121 | + to: ["jon@example.com"], |
| 122 | + preview: "Hi Jon...", |
| 123 | + timestamp: "2026-02-08T14:30:00Z", |
| 124 | + source: "gmail", |
| 125 | + }, |
| 126 | + { |
| 127 | + id: "draft-2", |
| 128 | + subject: "Project Update", |
| 129 | + from: "user@example.com", |
| 130 | + to: ["alice@example.com"], |
| 131 | + preview: "Hi Alice...", |
| 132 | + timestamp: "2026-02-08T15:00:00Z", |
| 133 | + source: "gmail", |
| 134 | + }, |
| 135 | + ]; |
| 136 | + |
| 137 | + const filterTo = "jon@example.com".toLowerCase(); |
| 138 | + const filtered = drafts.filter((d) => |
| 139 | + d.to.some((recipient) => recipient.toLowerCase().includes(filterTo)) |
| 140 | + ); |
| 141 | + |
| 142 | + expect(filtered).toHaveLength(1); |
| 143 | + expect(filtered[0].id).toBe("draft-1"); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should filter drafts by --subject substring", () => { |
| 147 | + const drafts: Draft[] = [ |
| 148 | + { |
| 149 | + id: "draft-1", |
| 150 | + subject: "Meeting Follow-up", |
| 151 | + from: "user@example.com", |
| 152 | + to: ["jon@example.com"], |
| 153 | + preview: "Hi Jon...", |
| 154 | + timestamp: "2026-02-08T14:30:00Z", |
| 155 | + source: "gmail", |
| 156 | + }, |
| 157 | + { |
| 158 | + id: "draft-2", |
| 159 | + subject: "Project Update", |
| 160 | + from: "user@example.com", |
| 161 | + to: ["alice@example.com"], |
| 162 | + preview: "Hi Alice...", |
| 163 | + timestamp: "2026-02-08T15:00:00Z", |
| 164 | + source: "gmail", |
| 165 | + }, |
| 166 | + ]; |
| 167 | + |
| 168 | + const filterSubject = "meeting".toLowerCase(); |
| 169 | + const filtered = drafts.filter((d) => |
| 170 | + (d.subject || "").toLowerCase().includes(filterSubject) |
| 171 | + ); |
| 172 | + |
| 173 | + expect(filtered).toHaveLength(1); |
| 174 | + expect(filtered[0].id).toBe("draft-1"); |
| 175 | + expect(filtered[0].subject).toBe("Meeting Follow-up"); |
| 176 | + }); |
| 177 | + |
| 178 | + it("should return empty array when no drafts match --to filter", () => { |
| 179 | + const drafts: Draft[] = [ |
| 180 | + { |
| 181 | + id: "draft-1", |
| 182 | + subject: "Test", |
| 183 | + from: "user@example.com", |
| 184 | + to: ["jon@example.com"], |
| 185 | + preview: "Preview", |
| 186 | + timestamp: "2026-02-08T14:30:00Z", |
| 187 | + source: "gmail", |
| 188 | + }, |
| 189 | + ]; |
| 190 | + |
| 191 | + const filterTo = "nobody@example.com".toLowerCase(); |
| 192 | + const filtered = drafts.filter((d) => |
| 193 | + d.to.some((recipient) => recipient.toLowerCase().includes(filterTo)) |
| 194 | + ); |
| 195 | + |
| 196 | + expect(filtered).toHaveLength(0); |
| 197 | + }); |
| 198 | + |
| 199 | + it("should return all drafts when no filter is applied", () => { |
| 200 | + const drafts: Draft[] = [ |
| 201 | + { |
| 202 | + id: "draft-1", |
| 203 | + subject: "First", |
| 204 | + from: "user@example.com", |
| 205 | + to: ["a@example.com"], |
| 206 | + preview: "Preview 1", |
| 207 | + timestamp: "2026-02-08T14:30:00Z", |
| 208 | + source: "gmail", |
| 209 | + }, |
| 210 | + { |
| 211 | + id: "draft-2", |
| 212 | + subject: "Second", |
| 213 | + from: "user@example.com", |
| 214 | + to: ["b@example.com"], |
| 215 | + preview: "Preview 2", |
| 216 | + timestamp: "2026-02-08T15:00:00Z", |
| 217 | + source: "outlook", |
| 218 | + }, |
| 219 | + ]; |
| 220 | + |
| 221 | + // No filters applied |
| 222 | + const filterTo = ""; |
| 223 | + const filterSubject = ""; |
| 224 | + let filtered = drafts; |
| 225 | + if (filterTo) { |
| 226 | + filtered = filtered.filter((d) => |
| 227 | + d.to.some((recipient) => recipient.toLowerCase().includes(filterTo)) |
| 228 | + ); |
| 229 | + } |
| 230 | + if (filterSubject) { |
| 231 | + filtered = filtered.filter((d) => |
| 232 | + (d.subject || "").toLowerCase().includes(filterSubject) |
| 233 | + ); |
| 234 | + } |
| 235 | + |
| 236 | + expect(filtered).toHaveLength(2); |
| 237 | + }); |
| 238 | + |
| 239 | + it("should produce valid JSON output for drafts", () => { |
| 240 | + const drafts: Draft[] = [ |
| 241 | + { |
| 242 | + id: "draft-abc123", |
| 243 | + subject: "Meeting Follow-up", |
| 244 | + from: "user@example.com", |
| 245 | + to: ["jon@example.com"], |
| 246 | + preview: "Hi Jon, following up on our conversation...", |
| 247 | + timestamp: "2026-02-08T14:30:00Z", |
| 248 | + source: "gmail", |
| 249 | + }, |
| 250 | + ]; |
| 251 | + |
| 252 | + const jsonStr = JSON.stringify(drafts, null, 2); |
| 253 | + const parsed = JSON.parse(jsonStr) as typeof drafts; |
| 254 | + |
| 255 | + expect(Array.isArray(parsed)).toBe(true); |
| 256 | + expect(parsed).toHaveLength(1); |
| 257 | + expect(parsed[0].id).toBe("draft-abc123"); |
| 258 | + expect(parsed[0].subject).toBe("Meeting Follow-up"); |
| 259 | + expect(parsed[0].to).toEqual(["jon@example.com"]); |
| 260 | + expect(parsed[0].source).toBe("gmail"); |
| 261 | + }); |
| 262 | + |
| 263 | + it("should match --to filter case-insensitively", () => { |
| 264 | + const drafts: Draft[] = [ |
| 265 | + { |
| 266 | + id: "draft-1", |
| 267 | + subject: "Hello", |
| 268 | + from: "user@example.com", |
| 269 | + to: ["Jon@Example.COM"], |
| 270 | + preview: "Hi", |
| 271 | + timestamp: "2026-02-08T14:30:00Z", |
| 272 | + source: "gmail", |
| 273 | + }, |
| 274 | + ]; |
| 275 | + |
| 276 | + // Filter using lowercase |
| 277 | + const filterTo = "jon@example.com"; |
| 278 | + const filtered = drafts.filter((d) => |
| 279 | + d.to.some((recipient) => recipient.toLowerCase().includes(filterTo)) |
| 280 | + ); |
| 281 | + |
| 282 | + expect(filtered).toHaveLength(1); |
| 283 | + }); |
113 | 284 | }); |
0 commit comments