@@ -3,8 +3,11 @@ import test from "node:test";
33
44import {
55 clearCommunityOnboardingTransaction ,
6+ isTransactionStillConnecting ,
67 loadCommunityOnboardingTransaction ,
78 markCommunityOnboardingComplete ,
9+ resolveProfileCheckAction ,
10+ shouldSkipCommunityOnboarding ,
811 startCommunityOnboarding ,
912 updateCommunityOnboardingTransaction ,
1013 updateCurrentCommunityOnboardingTransaction ,
@@ -147,3 +150,195 @@ test("completion is scoped by relay and pubkey and preserves legacy gate", () =>
147150 ) ;
148151 assert . equal ( storage . getItem ( "buzz-onboarding-complete.v1:pubkey" ) , "true" ) ;
149152} ) ;
153+
154+ // ── shouldSkipCommunityOnboarding ────────────────────────────────────────────
155+
156+ /** Minimal Profile stub — only the fields the helper inspects. */
157+ function makeProfile ( hasProfileEvent , overrides = { } ) {
158+ return {
159+ pubkey : "aabbcc" ,
160+ displayName : null ,
161+ avatarUrl : null ,
162+ about : null ,
163+ nip05Handle : null ,
164+ ownerPubkey : null ,
165+ hasProfileEvent,
166+ ...overrides ,
167+ } ;
168+ }
169+
170+ test ( "shouldSkipCommunityOnboarding_hasProfileEvent_returnsTrue" , ( ) => {
171+ assert . equal (
172+ shouldSkipCommunityOnboarding ( makeProfile ( true ) ) ,
173+ true ,
174+ "existing kind:0 ⇒ skip onboarding" ,
175+ ) ;
176+ } ) ;
177+
178+ test ( "shouldSkipCommunityOnboarding_noProfileEvent_returnsFalse" , ( ) => {
179+ assert . equal (
180+ shouldSkipCommunityOnboarding ( makeProfile ( false ) ) ,
181+ false ,
182+ "no kind:0 ⇒ show profile step" ,
183+ ) ;
184+ } ) ;
185+
186+ test ( "shouldSkipCommunityOnboarding_fetchError_returnsFalse" , ( ) => {
187+ // fetch error is represented as null — must never block onboarding
188+ assert . equal (
189+ shouldSkipCommunityOnboarding ( null ) ,
190+ false ,
191+ "fetch error ⇒ show profile step (safe fallback)" ,
192+ ) ;
193+ } ) ;
194+
195+ // ── resolveProfileCheckAction — async orchestration ──────────────────────────
196+
197+ /**
198+ * Returns a fake scheduleTimeout that captures registered callbacks so tests
199+ * can fire or skip them manually without real timers.
200+ */
201+ function makeScheduler ( ) {
202+ const callbacks = [ ] ;
203+ return {
204+ schedule : ( fn , _ms ) => callbacks . push ( fn ) ,
205+ fireTimeout : ( ) => {
206+ const fn = callbacks . shift ( ) ;
207+ if ( fn ) fn ( ) ;
208+ } ,
209+ pendingCount : ( ) => callbacks . length ,
210+ } ;
211+ }
212+
213+ test ( "resolveProfileCheckAction_hasProfileEvent_returnsSkipWithProfile" , async ( ) => {
214+ const profile = makeProfile ( true , { pubkey : "aabbcc" } ) ;
215+ const result = await resolveProfileCheckAction (
216+ ( ) => Promise . resolve ( profile ) ,
217+ 10_000 ,
218+ makeScheduler ( ) . schedule ,
219+ ) ;
220+ assert . equal ( result . action , "skip" ) ;
221+ assert . equal ( result . profile . pubkey , "aabbcc" ) ;
222+ } ) ;
223+
224+ test ( "resolveProfileCheckAction_noProfileEvent_returnsShowProfile" , async ( ) => {
225+ const result = await resolveProfileCheckAction (
226+ ( ) => Promise . resolve ( makeProfile ( false ) ) ,
227+ 10_000 ,
228+ makeScheduler ( ) . schedule ,
229+ ) ;
230+ assert . equal ( result . action , "show-profile" ) ;
231+ } ) ;
232+
233+ test ( "resolveProfileCheckAction_fetchRejects_returnsShowProfile" , async ( ) => {
234+ const result = await resolveProfileCheckAction (
235+ ( ) => Promise . reject ( new Error ( "network error" ) ) ,
236+ 10_000 ,
237+ makeScheduler ( ) . schedule ,
238+ ) ;
239+ assert . equal ( result . action , "show-profile" ) ;
240+ } ) ;
241+
242+ test ( "resolveProfileCheckAction_timeout_returnsShowProfile" , async ( ) => {
243+ // Fetch never settles; scheduler fires the timeout immediately.
244+ const scheduler = makeScheduler ( ) ;
245+ const result = await resolveProfileCheckAction (
246+ ( ) => new Promise ( ( ) => { } ) , // hangs forever
247+ 10_000 ,
248+ ( fn , ms ) => {
249+ scheduler . schedule ( fn , ms ) ;
250+ // Fire synchronously so the test does not wait for a real timer.
251+ scheduler . fireTimeout ( ) ;
252+ } ,
253+ ) ;
254+ assert . equal (
255+ result . action ,
256+ "show-profile" ,
257+ "timeout ⇒ show-profile (never strands onboarding)" ,
258+ ) ;
259+ } ) ;
260+
261+ test ( "resolveProfileCheckAction_lateSuccessAfterTimeout_doesNotSkip" , async ( ) => {
262+ // Fetch resolves AFTER the timeout has already fired.
263+ // resolveProfileCheckAction must return show-profile from the timeout path,
264+ // and the late fetch result must have no effect.
265+ let resolveFetch ;
266+ const fetchPromise = new Promise ( ( resolve ) => {
267+ resolveFetch = resolve ;
268+ } ) ;
269+
270+ const scheduler = makeScheduler ( ) ;
271+ const resultPromise = resolveProfileCheckAction (
272+ ( ) => fetchPromise ,
273+ 10_000 ,
274+ scheduler . schedule ,
275+ ) ;
276+
277+ // Fire the timeout — race settles with the timeout rejection.
278+ scheduler . fireTimeout ( ) ;
279+
280+ // Now resolve the fetch with a kind:0 profile.
281+ resolveFetch ( makeProfile ( true ) ) ;
282+
283+ const result = await resultPromise ;
284+ assert . equal (
285+ result . action ,
286+ "show-profile" ,
287+ "late success after timeout must not complete onboarding" ,
288+ ) ;
289+ } ) ;
290+
291+ // ── isTransactionStillConnecting — stale-transaction guard ───────────────────
292+
293+ /**
294+ * Builds a minimal transaction stub for testing the guard predicate.
295+ * Only `id` and `stage` are inspected by isTransactionStillConnecting.
296+ */
297+ function makeTransaction ( id , stage ) {
298+ return {
299+ id,
300+ stage,
301+ source : "add-community" ,
302+ relayUrl : "wss://relay.example" ,
303+ communityName : "test" ,
304+ createdAt : new Date ( ) . toISOString ( ) ,
305+ updatedAt : new Date ( ) . toISOString ( ) ,
306+ } ;
307+ }
308+
309+ test ( "isTransactionStillConnecting_matchingIdAndStage_returnsTrue" , ( ) => {
310+ assert . equal (
311+ isTransactionStillConnecting ( makeTransaction ( "tx-a" , "connecting" ) , "tx-a" ) ,
312+ true ,
313+ "same id + connecting stage → guard passes" ,
314+ ) ;
315+ } ) ;
316+
317+ test ( "isTransactionStillConnecting_replacedTransaction_returnsFalse" , ( ) => {
318+ // Transaction B replaced A while the fetch was in flight.
319+ // The callback for A must not clear B.
320+ assert . equal (
321+ isTransactionStillConnecting ( makeTransaction ( "tx-b" , "connecting" ) , "tx-a" ) ,
322+ false ,
323+ "different id (replacement) → guard rejects stale success" ,
324+ ) ;
325+ } ) ;
326+
327+ test ( "isTransactionStillConnecting_cancelWithNoReplacement_returnsFalse" , ( ) => {
328+ // User cancelled without starting a new transaction; ref is null.
329+ assert . equal (
330+ isTransactionStillConnecting ( null , "tx-a" ) ,
331+ false ,
332+ "null ref (cancel without replacement) → guard rejects stale success" ,
333+ ) ;
334+ } ) ;
335+
336+ test ( "isTransactionStillConnecting_stagePastConnecting_returnsFalse" , ( ) => {
337+ // Fallback already advanced the transaction to 'profile' before the skip
338+ // result arrived — double-completion must not occur.
339+ assert . equal (
340+ isTransactionStillConnecting ( makeTransaction ( "tx-a" , "profile" ) , "tx-a" ) ,
341+ false ,
342+ "stage advanced past connecting → guard rejects late action" ,
343+ ) ;
344+ } ) ;
0 commit comments