|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | +import React from "react"; |
| 4 | +import { render, waitFor, cleanup, act } from "@testing-library/react"; |
| 5 | +import { |
| 6 | + CommunityOnboardingProvider, |
| 7 | + useCommunityOnboarding, |
| 8 | + startCommunityOnboarding, |
| 9 | +} from "./communityOnboarding.tsx"; |
| 10 | +import { useClaimInvite } from "./useClaimInvite.ts"; |
| 11 | + |
| 12 | +let controls; |
| 13 | +function Claim() { |
| 14 | + controls = useCommunityOnboarding(); |
| 15 | + useClaimInvite(); |
| 16 | + return null; |
| 17 | +} |
| 18 | +const policy = { |
| 19 | + version: "v1", |
| 20 | + terms_markdown: "Terms", |
| 21 | + age_attestation_required: false, |
| 22 | +}; |
| 23 | +const tauri = { |
| 24 | + invoke: async (command) => { |
| 25 | + if (command === "sign_event") |
| 26 | + return JSON.stringify({ id: "signed", tags: [] }); |
| 27 | + throw new Error(`Unexpected command ${command}`); |
| 28 | + }, |
| 29 | +}; |
| 30 | +window.__TAURI_INTERNALS__ = tauri; |
| 31 | +globalThis.__TAURI_INTERNALS__ = tauri; |
| 32 | + |
| 33 | +for (const configured of [true, false]) { |
| 34 | + test(`receipt-less invite ${configured ? "waits for native consent" : "claims a policy-free relay"}`, async () => { |
| 35 | + localStorage.clear(); |
| 36 | + const requests = []; |
| 37 | + const original = globalThis.fetch; |
| 38 | + globalThis.fetch = async (url) => { |
| 39 | + requests.push(String(url)); |
| 40 | + if (String(url).endsWith("/api/join-policy")) |
| 41 | + return Response.json(configured ? { policy } : {}); |
| 42 | + return Response.json({ |
| 43 | + status: "joined", |
| 44 | + community_id: "id", |
| 45 | + host: "relay.example", |
| 46 | + role: "member", |
| 47 | + }); |
| 48 | + }; |
| 49 | + try { |
| 50 | + startCommunityOnboarding({ |
| 51 | + source: "deep-link-join", |
| 52 | + relayUrl: "wss://relay.example", |
| 53 | + inviteCode: "code", |
| 54 | + }); |
| 55 | + render( |
| 56 | + React.createElement( |
| 57 | + CommunityOnboardingProvider, |
| 58 | + null, |
| 59 | + React.createElement(Claim), |
| 60 | + ), |
| 61 | + ); |
| 62 | + await waitFor(() => |
| 63 | + assert.equal( |
| 64 | + controls.transaction.stage, |
| 65 | + configured ? "invite-consent" : "connecting", |
| 66 | + ), |
| 67 | + ); |
| 68 | + assert.equal( |
| 69 | + requests.filter((url) => url.endsWith("/claim")).length, |
| 70 | + configured ? 0 : 1, |
| 71 | + ); |
| 72 | + } finally { |
| 73 | + cleanup(); |
| 74 | + globalThis.fetch = original; |
| 75 | + } |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +test("rejected receipt returns to native consent and clears the stale proof", async () => { |
| 80 | + localStorage.clear(); |
| 81 | + const original = globalThis.fetch; |
| 82 | + globalThis.fetch = async () => |
| 83 | + Response.json({ error: "join_policy_required" }, { status: 403 }); |
| 84 | + try { |
| 85 | + startCommunityOnboarding({ |
| 86 | + source: "deep-link-join", |
| 87 | + relayUrl: "wss://relay.example", |
| 88 | + inviteCode: "code", |
| 89 | + policyReceipt: "expired", |
| 90 | + }); |
| 91 | + render( |
| 92 | + React.createElement( |
| 93 | + CommunityOnboardingProvider, |
| 94 | + null, |
| 95 | + React.createElement(Claim), |
| 96 | + ), |
| 97 | + ); |
| 98 | + await waitFor(() => |
| 99 | + assert.equal(controls.transaction.stage, "invite-consent"), |
| 100 | + ); |
| 101 | + assert.equal(controls.transaction.policyReceipt, undefined); |
| 102 | + assert.equal(controls.transaction.error, undefined); |
| 103 | + } finally { |
| 104 | + cleanup(); |
| 105 | + globalThis.fetch = original; |
| 106 | + } |
| 107 | +}); |
| 108 | + |
| 109 | +test("policy lookup failure never claims and can be retried", async () => { |
| 110 | + localStorage.clear(); |
| 111 | + const original = globalThis.fetch; |
| 112 | + let failed = true; |
| 113 | + let claims = 0; |
| 114 | + globalThis.fetch = async (url) => { |
| 115 | + if (String(url).endsWith("/api/join-policy")) { |
| 116 | + if (failed) throw new Error("offline"); |
| 117 | + return Response.json({ policy }); |
| 118 | + } |
| 119 | + claims++; |
| 120 | + throw new Error("unexpected claim"); |
| 121 | + }; |
| 122 | + try { |
| 123 | + startCommunityOnboarding({ |
| 124 | + source: "deep-link-join", |
| 125 | + relayUrl: "wss://relay.example", |
| 126 | + inviteCode: "code", |
| 127 | + }); |
| 128 | + render( |
| 129 | + React.createElement( |
| 130 | + CommunityOnboardingProvider, |
| 131 | + null, |
| 132 | + React.createElement(Claim), |
| 133 | + ), |
| 134 | + ); |
| 135 | + await waitFor(() => assert.equal(controls.transaction.error, "offline")); |
| 136 | + failed = false; |
| 137 | + await act(async () => controls.update({ error: undefined })); |
| 138 | + await waitFor(() => |
| 139 | + assert.equal(controls.transaction.stage, "invite-consent"), |
| 140 | + ); |
| 141 | + assert.equal(claims, 0); |
| 142 | + } finally { |
| 143 | + cleanup(); |
| 144 | + globalThis.fetch = original; |
| 145 | + } |
| 146 | +}); |
| 147 | + |
| 148 | +test("cancelled policy discovery cannot claim after its response arrives", async () => { |
| 149 | + localStorage.clear(); |
| 150 | + const original = globalThis.fetch; |
| 151 | + let resolvePolicy; |
| 152 | + let started; |
| 153 | + const lookupStarted = new Promise((resolve) => { |
| 154 | + started = resolve; |
| 155 | + }); |
| 156 | + const response = new Promise((resolve) => { |
| 157 | + resolvePolicy = resolve; |
| 158 | + }); |
| 159 | + let claims = 0; |
| 160 | + globalThis.fetch = async (url) => { |
| 161 | + if (String(url).endsWith("/api/join-policy")) { |
| 162 | + started(); |
| 163 | + return response; |
| 164 | + } |
| 165 | + claims++; |
| 166 | + throw new Error("unexpected claim"); |
| 167 | + }; |
| 168 | + try { |
| 169 | + startCommunityOnboarding({ |
| 170 | + source: "deep-link-join", |
| 171 | + relayUrl: "wss://relay.example", |
| 172 | + inviteCode: "code", |
| 173 | + }); |
| 174 | + render( |
| 175 | + React.createElement( |
| 176 | + CommunityOnboardingProvider, |
| 177 | + null, |
| 178 | + React.createElement(Claim), |
| 179 | + ), |
| 180 | + ); |
| 181 | + await lookupStarted; |
| 182 | + await act(async () => controls.clear()); |
| 183 | + await act(async () => resolvePolicy(Response.json({}))); |
| 184 | + assert.equal(controls.transaction, null); |
| 185 | + assert.equal(claims, 0); |
| 186 | + } finally { |
| 187 | + cleanup(); |
| 188 | + globalThis.fetch = original; |
| 189 | + } |
| 190 | +}); |
0 commit comments