|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { collectDeliveredMediaUrls } from "./delivery-evidence.js"; |
| 3 | + |
| 4 | +describe("collectDeliveredMediaUrls attachment recursion", () => { |
| 5 | +it("collects media URLs across nested attachments", () => { |
| 6 | +const urls = collectDeliveredMediaUrls({ |
| 7 | +payloads: [ |
| 8 | +{ |
| 9 | +url: "https://example.com/root.png", |
| 10 | +attachments: [ |
| 11 | +{ mediaUrl: "https://example.com/child.png" }, |
| 12 | +{ attachments: [{ filePath: "/tmp/grandchild.jpg" }] }, |
| 13 | +], |
| 14 | +}, |
| 15 | +], |
| 16 | +}); |
| 17 | +expect(urls.toSorted()).toEqual([ |
| 18 | +"/tmp/grandchild.jpg", |
| 19 | +"https://example.com/child.png", |
| 20 | +"https://example.com/root.png", |
| 21 | +]); |
| 22 | +}); |
| 23 | + |
| 24 | +it("does not overflow the stack on a self-referential attachments cycle", () => { |
| 25 | +// Payloads arrive as in-process `unknown` objects; a malformed self-referential |
| 26 | +// attachments chain previously recursed until the stack overflowed. |
| 27 | +const cyclic: Record<string, unknown> = { url: "https://example.com/loop.png" }; |
| 28 | +cyclic.attachments = [cyclic]; |
| 29 | + |
| 30 | +let urls: string[] = []; |
| 31 | +expect(() => { |
| 32 | +urls = collectDeliveredMediaUrls({ payloads: [cyclic] }); |
| 33 | +}).not.toThrow(); |
| 34 | +expect(urls).toEqual(["https://example.com/loop.png"]); |
| 35 | +}); |
| 36 | + |
| 37 | +it("does not overflow on a mutual attachments cycle", () => { |
| 38 | +const a: Record<string, unknown> = { mediaUrl: "https://example.com/a.png" }; |
| 39 | +const b: Record<string, unknown> = { mediaUrl: "https://example.com/b.png" }; |
| 40 | +a.attachments = [b]; |
| 41 | +b.attachments = [a]; |
| 42 | + |
| 43 | +const urls = collectDeliveredMediaUrls({ payloads: [a] }); |
| 44 | +expect(urls.toSorted()).toEqual(["https://example.com/a.png", "https://example.com/b.png"]); |
| 45 | +}); |
| 46 | +}); |