|
| 1 | +import assert from "node:assert"; |
| 2 | + |
| 3 | +it("maintains connections for the event even if other handlers closed it", async () => { |
| 4 | + await using alice = await sessions.acquire({ waitForWs: true }); |
| 5 | + |
| 6 | + using subscription1 = alice.ws.hashtag.subscribe({ tag: "test" }); |
| 7 | + using subscription2 = alice.ws.hashtag.subscribe({ tag: "test" }); |
| 8 | + |
| 9 | + const promise1 = subscription1.values().take(1).toArray(); |
| 10 | + const promise2 = subscription2.values().take(2).toArray(); |
| 11 | + |
| 12 | + // Dispatch event for subscription1 to establish connection |
| 13 | + const status1 = await alice.rest.v1.statuses.create({ |
| 14 | + status: "#test", |
| 15 | + visibility: "public", |
| 16 | + }); |
| 17 | + await promise1; |
| 18 | + subscription1.unsubscribe(); |
| 19 | + |
| 20 | + // subscription1 is now closed, so status2 will only be dispatched to subscription2 |
| 21 | + const status2 = await alice.rest.v1.statuses.create({ |
| 22 | + status: "#test", |
| 23 | + visibility: "public", |
| 24 | + }); |
| 25 | + |
| 26 | + try { |
| 27 | + const [e1, e2] = await promise2; |
| 28 | + assert(e1.event === "update"); |
| 29 | + expect(e1.payload.id).toBe(status1.id); |
| 30 | + |
| 31 | + assert(e2.event === "update"); |
| 32 | + expect(e2.payload.id).toBe(status2.id); |
| 33 | + } finally { |
| 34 | + await alice.rest.v1.statuses.$select(status1.id).remove(); |
| 35 | + await alice.rest.v1.statuses.$select(status2.id).remove(); |
| 36 | + } |
| 37 | +}); |
| 38 | + |
| 39 | +it("maintains connections for the event if unsubscribe called twice", async () => { |
| 40 | + await using alice = await sessions.acquire({ waitForWs: true }); |
| 41 | + |
| 42 | + using subscription1 = alice.ws.hashtag.subscribe({ tag: "test" }); |
| 43 | + using subscription2 = alice.ws.hashtag.subscribe({ tag: "test" }); |
| 44 | + |
| 45 | + const promise1 = subscription1.values().take(1).toArray(); |
| 46 | + const promise2 = subscription2.values().take(2).toArray(); |
| 47 | + |
| 48 | + const status1 = await alice.rest.v1.statuses.create({ |
| 49 | + status: "#test", |
| 50 | + visibility: "public", |
| 51 | + }); |
| 52 | + await promise1; |
| 53 | + subscription1.unsubscribe(); |
| 54 | + subscription1.unsubscribe(); |
| 55 | + subscription1.unsubscribe(); |
| 56 | + subscription1.unsubscribe(); |
| 57 | + |
| 58 | + const status2 = await alice.rest.v1.statuses.create({ |
| 59 | + status: "#test", |
| 60 | + visibility: "public", |
| 61 | + }); |
| 62 | + |
| 63 | + try { |
| 64 | + const [e1, e2] = await promise2; |
| 65 | + assert(e1.event === "update"); |
| 66 | + expect(e1.payload.id).toBe(status1.id); |
| 67 | + |
| 68 | + assert(e2.event === "update"); |
| 69 | + expect(e2.payload.id).toBe(status2.id); |
| 70 | + } finally { |
| 71 | + await alice.rest.v1.statuses.$select(status1.id).remove(); |
| 72 | + await alice.rest.v1.statuses.$select(status2.id).remove(); |
| 73 | + } |
| 74 | +}); |
| 75 | + |
| 76 | +it("maintains connections for the event if another handler called unsubscribe before connection established", async () => { |
| 77 | + await using alice = await sessions.acquire({ waitForWs: true }); |
| 78 | + |
| 79 | + using subscription1 = alice.ws.hashtag.subscribe({ tag: "test" }); |
| 80 | + using subscription2 = alice.ws.hashtag.subscribe({ tag: "test" }); |
| 81 | + |
| 82 | + subscription1.unsubscribe(); |
| 83 | + |
| 84 | + const promise2 = subscription2.values().take(1).toArray(); |
| 85 | + |
| 86 | + const status1 = await alice.rest.v1.statuses.create({ |
| 87 | + status: "#test", |
| 88 | + visibility: "public", |
| 89 | + }); |
| 90 | + |
| 91 | + try { |
| 92 | + const [e1] = await promise2; |
| 93 | + assert(e1.event === "update"); |
| 94 | + expect(e1.payload.id).toBe(status1.id); |
| 95 | + } finally { |
| 96 | + await alice.rest.v1.statuses.$select(status1.id).remove(); |
| 97 | + } |
| 98 | +}); |
0 commit comments