|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { resurfaceHiddenDmMessage } from "./hiddenDmResurfaceAction.ts"; |
| 5 | + |
| 6 | +const SELF = "1".repeat(64); |
| 7 | +const ALICE = "2".repeat(64); |
| 8 | +const BOB = "3".repeat(64); |
| 9 | + |
| 10 | +function event() { |
| 11 | + return { |
| 12 | + id: "event-1", |
| 13 | + kind: 40002, |
| 14 | + pubkey: ALICE, |
| 15 | + content: "hello", |
| 16 | + created_at: 10, |
| 17 | + // Message p tags are intentionally incomplete for this group DM. |
| 18 | + tags: [ |
| 19 | + ["h", "hidden-dm"], |
| 20 | + ["p", SELF], |
| 21 | + ], |
| 22 | + sig: "", |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function member(pubkey) { |
| 27 | + return { |
| 28 | + pubkey, |
| 29 | + role: "member", |
| 30 | + isAgent: false, |
| 31 | + joinedAt: "", |
| 32 | + displayName: null, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +test("reopens the source hidden group DM from authoritative membership", async () => { |
| 37 | + const inputs = []; |
| 38 | + assert.equal( |
| 39 | + await resurfaceHiddenDmMessage({ |
| 40 | + event: event(), |
| 41 | + expectedRelayUrl: "wss://relay.example", |
| 42 | + expectedSignerPubkey: SELF, |
| 43 | + fetchHiddenDmIds: async () => new Set(["hidden-dm"]), |
| 44 | + fetchMembers: async () => [member(SELF), member(ALICE), member(BOB)], |
| 45 | + isCurrent: () => true, |
| 46 | + reopen: async (input) => { |
| 47 | + inputs.push(input); |
| 48 | + return { id: "hidden-dm" }; |
| 49 | + }, |
| 50 | + }), |
| 51 | + true, |
| 52 | + ); |
| 53 | + assert.deepEqual(inputs, [ |
| 54 | + { |
| 55 | + pubkeys: [ALICE, BOB], |
| 56 | + expectedRelayUrl: "wss://relay.example", |
| 57 | + expectedSignerPubkey: SELF, |
| 58 | + }, |
| 59 | + ]); |
| 60 | +}); |
| 61 | + |
| 62 | +test("a suspended old-community read cannot reopen a DM", async () => { |
| 63 | + let current = true; |
| 64 | + let resume; |
| 65 | + const members = new Promise((resolve) => { |
| 66 | + resume = resolve; |
| 67 | + }); |
| 68 | + let reopenCount = 0; |
| 69 | + const result = resurfaceHiddenDmMessage({ |
| 70 | + event: event(), |
| 71 | + expectedRelayUrl: "wss://old.example", |
| 72 | + expectedSignerPubkey: SELF, |
| 73 | + fetchHiddenDmIds: async () => new Set(["hidden-dm"]), |
| 74 | + fetchMembers: async () => members, |
| 75 | + isCurrent: () => current, |
| 76 | + reopen: async () => { |
| 77 | + reopenCount += 1; |
| 78 | + return { id: "hidden-dm" }; |
| 79 | + }, |
| 80 | + }); |
| 81 | + await Promise.resolve(); |
| 82 | + current = false; |
| 83 | + resume([member(SELF), member(ALICE)]); |
| 84 | + assert.equal(await result, false); |
| 85 | + assert.equal(reopenCount, 0); |
| 86 | +}); |
| 87 | + |
| 88 | +test("rejects a reopen result for any channel other than the source", async () => { |
| 89 | + await assert.rejects( |
| 90 | + resurfaceHiddenDmMessage({ |
| 91 | + event: event(), |
| 92 | + expectedRelayUrl: "wss://relay.example", |
| 93 | + expectedSignerPubkey: SELF, |
| 94 | + fetchHiddenDmIds: async () => new Set(["hidden-dm"]), |
| 95 | + fetchMembers: async () => [member(SELF), member(ALICE)], |
| 96 | + isCurrent: () => true, |
| 97 | + reopen: async () => ({ id: "alternate-dm" }), |
| 98 | + }), |
| 99 | + /different DM conversation/, |
| 100 | + ); |
| 101 | +}); |
0 commit comments