/ test / select.spec.js
select.spec.js
  1  import { fact, same, Collection } from './lib.js'
  2  import proofsDB from './proofs.db.js'
  3  
  4  const UCAN = fact({
  5    the: 'ucan',
  6    cid: String,
  7    issuer: String,
  8    audience: String,
  9    capabilities: Object,
 10  })
 11  
 12  const Capability = fact({
 13    the: 'capability',
 14    can: String,
 15    with: String,
 16  })
 17  
 18  /**
 19   * @type {import('entail').Suite}
 20   */
 21  export const testSelector = {
 22    'nested selection': async (assert) => {
 23      const Delegation = fact({
 24        this: Object,
 25        cid: String,
 26        can: String,
 27        subject: String,
 28      })
 29        .with({ capabilities: Object, capability: Object })
 30        .where(
 31          ({ this: ucan, cid, capabilities, capability, subject, can, _ }) => [
 32            UCAN.match({ this: ucan, cid, capabilities }),
 33            Collection({ this: capabilities, of: capability }),
 34            Capability({ this: capability, can, with: subject }),
 35            Delegation.claim({ this: ucan, cid, can, subject }),
 36          ]
 37        )
 38  
 39      const Permission = fact({
 40        space: String,
 41        upload: String,
 42        store: String,
 43      }).where(({ space, upload, store }) => [
 44        Delegation.match({ subject: space, can: 'upload/add', cid: upload }),
 45        Delegation.match({ subject: space, can: 'store/add', cid: store }),
 46        Permission.claim({ space, upload, store }),
 47      ])
 48  
 49      const result = await Permission().query({ from: proofsDB })
 50      assert.deepEqual(result, [
 51        Permission.assert({
 52          space: 'did:key:zAlice',
 53          upload: 'bafy...upload',
 54          store: 'bafy...store',
 55        }),
 56      ])
 57    },
 58    'deeply nested selection': async (assert) => {
 59      const Delegation = fact({
 60        this: Object,
 61        cid: String,
 62        can: String,
 63        subject: String,
 64      })
 65        .with({ capabilities: Object, capability: Object })
 66        .where(
 67          ({ this: ucan, cid, capabilities, capability, subject, can, _ }) => [
 68            UCAN.match({ this: ucan, cid, capabilities }),
 69            Collection({ this: capabilities, of: capability }),
 70            Capability({ this: capability, can, with: subject }),
 71            Delegation.claim({ this: ucan, cid, can, subject }),
 72          ]
 73        )
 74  
 75      const Upload = Delegation.where((delegation) => [
 76        Delegation(delegation),
 77        same({ this: delegation.can, as: 'upload/add' }),
 78      ])
 79  
 80      const Store = Delegation.where((delegation) => [
 81        Delegation(delegation),
 82        same({ this: delegation.can, as: 'store/add' }),
 83      ])
 84  
 85      const Permission = fact({
 86        upload: String,
 87        store: String,
 88        space: String,
 89      }).where(({ upload, store, space }) => [
 90        Upload.match({ subject: space, cid: upload }),
 91        Store.match({ subject: space, cid: store }),
 92        Permission.claim({ space, store, upload }),
 93      ])
 94  
 95      assert.deepEqual(await Permission().query({ from: proofsDB }), [
 96        Permission.assert({
 97          space: 'did:key:zAlice',
 98          upload: 'bafy...upload',
 99          store: 'bafy...store',
100        }),
101      ])
102    },
103  }