app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

new-listing-readiness.test.ts (8537B)


      1 import { expect, it } from 'vitest';
      2 import {
      3   createIdentitySession,
      4   connectIdentity,
      5   probeIdentityMessaging,
      6   disconnectIdentity
      7 } from '../../src/lib/runtime/identity-session.ts';
      8 import {
      9   createInboxSetupView,
     10   checkInboxSetupView,
     11   unlockInboxSetupView,
     12   stopInboxSetupView,
     13   closeInboxSetupView
     14 } from '../../src/lib/messaging/inbox-setup-view.ts';
     15 import {
     16   makeSetupFixture,
     17   fixturePolicy,
     18   inbox
     19 } from '../e2e/harness/inbox-setup.ts';
     20 import {
     21   newListingReadinessSnapshot,
     22   captureNewListingReadiness,
     23   newListingReadinessOwnership,
     24   closeNewListingReadiness,
     25   type NewListingReadiness
     26 } from '../../src/lib/publishing/new-listing-readiness.ts';
     27 import type { ObserveInboxAccess } from '../../src/lib/messaging/readiness.ts';
     28 
     29 // HC_TEST_ONLY_QUALIFICATION_PORT: exercises the original owner-bound gate,
     30 // not real extension/relay access, retention or launch qualification.
     31 async function fixture(access = true) {
     32   const f = await makeSetupFixture([['relay', inbox]]);
     33   let currentAccess = true;
     34   const observeAccess: ObserveInboxAccess = (context) => ({
     35     ...context,
     36     readTargets: context.readTargets.slice(),
     37     archiveTargets: context.archiveTargets.slice(),
     38     receive: 'qualified_exercised',
     39     archive: 'qualified_exercised',
     40     current: () => currentAccess
     41   });
     42   const setup = createInboxSetupView({
     43     identity: f.identity,
     44     policy: fixturePolicy(),
     45     lookup: () => Promise.resolve(f.resolve().resolver),
     46     ...(access ? { observeAccess } : {})
     47   });
     48   if (!setup) throw Error('original setup unavailable');
     49   await checkInboxSetupView(setup);
     50   await unlockInboxSetupView(setup);
     51   return {
     52     ...f,
     53     setup,
     54     retireAccess: () => {
     55       currentAccess = false;
     56     },
     57     close() {
     58       closeInboxSetupView(setup);
     59       f.close();
     60     }
     61   };
     62 }
     63 const review = 'reviewed_new_listing_inbox';
     64 it('guest can draft but cannot review a new reply-capable listing', () => {
     65   const identity = createIdentitySession();
     66   expect(newListingReadinessSnapshot(identity, undefined)).toMatchObject({
     67     draft: true,
     68     readyToReview: false,
     69     preferenceWrite: 'not_requested'
     70   });
     71   expect(
     72     captureNewListingReadiness(identity, undefined, review)
     73   ).toBeUndefined();
     74 });
     75 it('actual signing-only identity cannot advertise new messaging-ready publication', async () => {
     76   const f = await makeSetupFixture();
     77   try {
     78     disconnectIdentity(f.identity);
     79     const provider = window.nostr as {
     80       getPublicKey(): Promise<string>;
     81       signEvent(...args: unknown[]): Promise<never>;
     82     };
     83     Object.defineProperty(window, 'nostr', {
     84       configurable: true,
     85       value: {
     86         getPublicKey: () => provider.getPublicKey(),
     87         signEvent: (...args: Parameters<typeof provider.signEvent>) =>
     88           provider.signEvent(...args)
     89       }
     90     });
     91     await connectIdentity(f.identity);
     92     const before = f.counts();
     93     expect(
     94       captureNewListingReadiness(f.identity, undefined, review)
     95     ).toBeUndefined();
     96     expect(
     97       newListingReadinessSnapshot(f.identity, undefined).readyToReview
     98     ).toBe(false);
     99     expect(f.counts()).toEqual(before);
    100   } finally {
    101     f.close();
    102   }
    103 });
    104 it('compatible signed configuration without exercised qualified access remains blocked', async () => {
    105   const f = await fixture(false);
    106   try {
    107     const before = f.counts();
    108     expect(newListingReadinessSnapshot(f.identity, f.setup)).toMatchObject({
    109       draft: true,
    110       readyToReview: false,
    111       preferenceWrite: 'not_requested'
    112     });
    113     expect(
    114       captureNewListingReadiness(f.identity, f.setup, review)
    115     ).toBeUndefined();
    116     expect(f.counts()).toEqual(before);
    117   } finally {
    118     f.close();
    119   }
    120 });
    121 it('original qualified compatible inbox permits explicit review without another preference write or signature', async () => {
    122   const f = await fixture();
    123   try {
    124     const before = f.counts();
    125     expect(newListingReadinessSnapshot(f.identity, f.setup).readyToReview).toBe(
    126       true
    127     );
    128     const proof = captureNewListingReadiness(f.identity, f.setup, review);
    129     const owned = newListingReadinessOwnership(proof);
    130     expect(owned?.owner).toBe(f.owner);
    131     expect(owned?.identity).toBe(f.identity);
    132     expect(owned?.current()).toBe(true);
    133     expect(f.counts()).toEqual(before);
    134     expect(f.counts().signs).toBe(0);
    135   } finally {
    136     f.close();
    137   }
    138 });
    139 for (const mode of ['missing', 'partial', 'unsupported'] as const)
    140   it(
    141     'actual signed/bounded own inbox cannot admit new review: ' + mode,
    142     async () => {
    143       const f = await makeSetupFixture([
    144         ['relay', mode === 'unsupported' ? '' : inbox]
    145       ]);
    146       const resolver = f.resolve(
    147         mode === 'missing' ? null : f.event,
    148         mode !== 'partial'
    149       ).resolver;
    150       const setup = createInboxSetupView({
    151         identity: f.identity,
    152         policy: fixturePolicy(),
    153         lookup: () => Promise.resolve(resolver)
    154       });
    155       try {
    156         await checkInboxSetupView(setup);
    157         await unlockInboxSetupView(setup);
    158         const before = f.counts();
    159         expect(
    160           captureNewListingReadiness(f.identity, setup, review)
    161         ).toBeUndefined();
    162         expect(
    163           newListingReadinessSnapshot(f.identity, setup).readyToReview
    164         ).toBe(false);
    165         expect(f.counts()).toEqual(before);
    166       } finally {
    167         closeInboxSetupView(setup);
    168         f.close();
    169       }
    170     }
    171   );
    172 it('copied setup and readiness tokens cannot borrow genuine owner authority', async () => {
    173   const f = await fixture();
    174   try {
    175     const proof = captureNewListingReadiness(f.identity, f.setup, review)!;
    176     expect(newListingReadinessOwnership(proof)?.current()).toBe(true);
    177     expect(
    178       captureNewListingReadiness({ ...f.identity }, f.setup, review)
    179     ).toBeUndefined();
    180     expect(
    181       captureNewListingReadiness(f.identity, { ...f.setup }, review)
    182     ).toBeUndefined();
    183     expect(newListingReadinessOwnership({ ...proof })).toBeUndefined();
    184     expect(
    185       newListingReadinessOwnership({} as NewListingReadiness)
    186     ).toBeUndefined();
    187     expect(newListingReadinessOwnership(proof)?.current()).toBe(true);
    188   } finally {
    189     f.close();
    190   }
    191 });
    192 it('another original identity cannot borrow the seller setup even with the same displayed observations', async () => {
    193   const f = await fixture(),
    194     other = createIdentitySession();
    195   try {
    196     await connectIdentity(other);
    197     await probeIdentityMessaging(other, 'reviewed_self_copy');
    198     expect(captureNewListingReadiness(other, f.setup, review)).toBeUndefined();
    199     expect(
    200       captureNewListingReadiness(f.identity, f.setup, review)
    201     ).toBeDefined();
    202   } finally {
    203     disconnectIdentity(other);
    204     f.close();
    205   }
    206 });
    207 it('retired actual exercised access revokes held proof without signing or publishing', async () => {
    208   const f = await fixture();
    209   try {
    210     const proof = captureNewListingReadiness(f.identity, f.setup, review)!;
    211     const held = newListingReadinessOwnership(proof)!;
    212     expect(held.current()).toBe(true);
    213     f.retireAccess();
    214     expect(held.current()).toBe(false);
    215     expect(newListingReadinessOwnership(proof)).toBeUndefined();
    216     expect(f.counts().signs).toBe(0);
    217   } finally {
    218     f.close();
    219   }
    220 });
    221 for (const invalidate of ['stop', 'close', 'disconnect', 'revoke'] as const)
    222   it(
    223     'original lifecycle invalidation revokes a new-listing review: ' +
    224       invalidate,
    225     async () => {
    226       const f = await fixture();
    227       try {
    228         const proof = captureNewListingReadiness(f.identity, f.setup, review)!;
    229         const held = newListingReadinessOwnership(proof)!;
    230         expect(held.current()).toBe(true);
    231         if (invalidate === 'stop') stopInboxSetupView(f.setup);
    232         if (invalidate === 'close') closeInboxSetupView(f.setup);
    233         if (invalidate === 'disconnect') disconnectIdentity(f.identity);
    234         if (invalidate === 'revoke') closeNewListingReadiness(proof);
    235         expect(held.current()).toBe(false);
    236         expect(newListingReadinessOwnership(proof)).toBeUndefined();
    237         expect(f.counts().signs).toBe(0);
    238       } finally {
    239         f.close();
    240       }
    241     }
    242   );
    243 it('unreviewed readiness observations cannot silently create a review capability', async () => {
    244   const f = await fixture();
    245   try {
    246     const before = f.counts();
    247     expect(
    248       captureNewListingReadiness(f.identity, f.setup, undefined)
    249     ).toBeUndefined();
    250     expect(
    251       captureNewListingReadiness(f.identity, f.setup, true)
    252     ).toBeUndefined();
    253     expect(f.counts()).toEqual(before);
    254   } finally {
    255     f.close();
    256   }
    257 });