app

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

view-context.test.ts (2322B)


      1 import { describe, it, expect } from 'vitest';
      2 import {
      3   createIdentityViewContext,
      4   identityViewSnapshot,
      5   mountIdentityView,
      6   connectIdentityView,
      7   probeIdentityViewMessaging,
      8   disconnectIdentityView,
      9   closeIdentityView,
     10   subscribeIdentityView,
     11   identityViewMatchesOwner,
     12   type IdentityViewContext
     13 } from '../../src/lib/runtime/view-context.ts';
     14 describe('genuine per-client identity view context', () => {
     15   it('constructs separate inert disconnected contexts without browser acquisition', () => {
     16     const a = createIdentityViewContext(),
     17       b = createIdentityViewContext();
     18     expect(a).not.toBe(b);
     19     expect(Object.keys(a)).toEqual([]);
     20     expect(identityViewSnapshot(a)).toEqual({
     21       mounted: false,
     22       identity: { state: 'guest', reason: 'disconnected' }
     23     });
     24     disconnectIdentityView(a);
     25     expect(identityViewSnapshot(b).identity).toEqual({
     26       state: 'guest',
     27       reason: 'disconnected'
     28     });
     29   });
     30   it('rejects forged tokens and does not infer authorization from a public key', async () => {
     31     const fake = {} as IdentityViewContext;
     32     expect(mountIdentityView(fake)).toBe(false);
     33     expect(identityViewMatchesOwner(fake, 'a'.repeat(64))).toBe(false);
     34     expect((await connectIdentityView(fake)).identity.state).toBe('guest');
     35   });
     36   it('SSR cannot mount, connect or invoke a reviewed messaging action', async () => {
     37     const c = createIdentityViewContext();
     38     expect(mountIdentityView(c)).toBe(false);
     39     expect((await connectIdentityView(c)).identity.state).toBe('guest');
     40     expect(
     41       (await probeIdentityViewMessaging(c, 'reviewed_self_copy')).identity.state
     42     ).toBe('guest');
     43   });
     44   it('snapshot mutation and one subscriber cannot change another context', () => {
     45     const a = createIdentityViewContext(),
     46       b = createIdentityViewContext();
     47     const events: string[] = [];
     48     const off = subscribeIdentityView(a, (state) =>
     49       events.push(state.identity.state)
     50     );
     51     const copy = identityViewSnapshot(a);
     52     (copy as { mounted: boolean }).mounted = true;
     53     expect(identityViewSnapshot(a).mounted).toBe(false);
     54     closeIdentityView(a);
     55     off();
     56     expect(events.length).toBeGreaterThan(0);
     57     expect(identityViewSnapshot(b).mounted).toBe(false);
     58     expect(identityViewMatchesOwner(b, 'a'.repeat(64))).toBe(false);
     59   });
     60 });