<commit_before>'use strict'

/**
 * Test dependencies
 * @ignore
 */
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')

/**
 * Assertions
 * @ignore
 */
chai.use(chaiAsPromised)
chai.should()
const expect = chai.expect

/**
 * Code Under Test
 * @ignore
 */
const { JWKSet, JWK } = require('../src')

/**
 * Test Data
 * @ignore
 */
const { RSAPrivateJWK, RSAPublicJWK } = require('./keys')
const RSAKeyGenDescriptor = {
  alg: 'RS256',
  modulusLength: 1024, // low for testing
  kid: 'abc',
  key_ops: ['sign', 'verify']
}

/**
 * Tests
 * @ignore
 */
describe('JWKSet', () => {

  describe('generateKeys', () => {

    describe('static', () => {
      it('should return a promise', () => {
        return JWKSet.generateKeys(RSAKeyGenDescriptor).should.be.fulfilled
      })

      it('should resolve a JWKSet', () => {
        return JWKSet.generateKeys(RSAKeyGenDescriptor).should.eventually.be.a.instanceOf(JWKSet)
      })

      it('should generate and import', () => {
        return JWKSet.generateKeys(RSAKeyGenDescriptor).then(jwks => {
          jwks.keys.length.should.equal(2)
          jwks.keys[0].should.be.instanceOf(JWK)
        })
      })

      it('should generate multiple keys', () => {
        return JWKSet.generateKeys([RSAKeyGenDescriptor, RSAKeyGenDescriptor]).then(jwks => {
          jwks.keys.length.should.equal(4)
          jwks.keys[3].should.be.instanceOf(JWK)
        })
      })

      it('should allow mixed input', () => {
        return JWKSet.generateKeys([RSAKeyGenDescriptor, 'RS256']).then(jwks => {
          jwks.keys.length.should.equal(4)
          jwks.keys[3].should.be.instanceOf(JWK)
        })
      })
    })

    describe('member', () => {
      let jwks

      beforeEach(() => {
        jwks = new JWKSet()
      })

      it('should return a promise', () => {
        return jwks.generateKeys('RS256').should.be.fulfilled
      })

      it('should return newly generated keys', () => {
        return jwks.generateKeys(['RS256', 'RS256'])
          .then(generated => {
            generated.length.should.equal(2)
            jwks.keys.length.should.equal(4)
          })
      })
    })
  })

  describe('importKeys', () => {

    describe('static', () => {

      it('should return a promise', () => {
        return JWKSet.importKeys(RSAPublicJWK).should.be.fulfilled
      })

      it('should resolve a JWKSet', () => {
        return JWKSet.importKeys(RSAPublicJWK).should.eventually.be.a.instanceOf(JWKSet)
      })

      it('should import keys', () => {
        return JWKSet.importKeys(RSAPublicJWK).then(jwks => {
          jwks.keys[0].should.be.instanceOf(JWK)
        })
      })

      it('should import multiple keys', () => {
        return JWKSet.importKeys([RSAPublicJWK, RSAPublicJWK]).then(jwks => {
          jwks.keys[1].should.be.instanceOf(JWK)
        })
      })
    })

    describe('member', () => {
      let jwks

      beforeEach(() => {
        jwks = new JWKSet()
      })

      it('should return a promise', () => {
        return jwks.importKeys(RSAPublicJWK).should.be.fulfilled
      })

      it('should return newly imported keys', () => {
        return jwks.importKeys([RSAPublicJWK, RSAPrivateJWK])
          .then(imported => {
            imported.length.should.equal(2)
            jwks.keys.length.should.equal(2)
          })
      })
    })
  })
})<commit_msg>test(JWKSet): wip tests<commit_after>'use strict'

/**
 * Test dependencies
 * @ignore
 */
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
const sinon = require('sinon')
const sinonChai = require('sinon-chai')

/**
 * Assertions
 * @ignore
 */
chai.use(chaiAsPromised)
chai.should()
chai.use(sinonChai)
const expect = chai.expect

/**
 * Code Under Test
 * @ignore
 */
const { JWA } = require('@trust/jwa')
const { JWKSet, JWK } = require('../src')

/**
 * Test Data
 * @ignore
 */
const { ECPrivateJWK, ECPublicJWK } = require('./keys')
const ECDSAKeyGenDescriptor = {
  alg: 'ES256',
  key_ops: ['sign', 'verify']
}

/**
 * Tests
 * @ignore
 */
describe('JWKSet', () => {

  describe('constructor', () => {
    let meta = 'abc'

    describe('with object input', () => {

      it('should assign metadata', () => {
        let jwkset = new JWKSet({ meta })
        jwkset.meta.should.equal(meta)
      })

      it('should create keys array if not present', () => {
        let jwkset = new JWKSet({})
        jwkset.keys.should.be.instanceOf(Array)
      })

      it('should not override the keys array if provided', () => {
        let jwkset = new JWKSet({ keys: [meta] })
        jwkset.keys[0].should.equal(meta)
      })
    })

    describe('with array input', () => {

      it('should treat the array as `keys`', () => {
        let keys = [meta]
        let jwkset = new JWKSet(keys)
        jwkset.keys.should.equal(keys)
        jwkset.keys[0].should.equal(meta)
      })
    })

    describe('with no input', () => {

      it('should create keys array if not present', () => {
        let jwkset = new JWKSet({})
        jwkset.keys.should.be.instanceOf(Array)
      })
    })
  })

  describe('generateKeys', () => {

    describe('static', () => {

      beforeEach(() => {
        sinon.stub(JWKSet.prototype, 'generateKeys').resolves()
      })

      afterEach(() => {
        JWKSet.prototype.generateKeys.restore()
      })

      it('should return a promise', () => {
        return JWKSet.generateKeys().should.be.fulfilled
      })

      it('should resolve a JWKSet', () => {
        return JWKSet.generateKeys().should.eventually.be.a.instanceOf(JWKSet)
      })

      it('should defer to member generateKeys', () => {
        return JWKSet.generateKeys(ECDSAKeyGenDescriptor).then(jwks => {
          jwks.generateKeys.should.be.calledWith(ECDSAKeyGenDescriptor)
        })
      })

      it('should reject if generateKeys rejects', () => {
        JWKSet.prototype.generateKeys.restore()
        sinon.stub(JWKSet.prototype, 'generateKeys').rejects()
        return JWKSet.generateKeys().should.be.rejected
      })
    })

    describe('member', () => {
      let jwks

      beforeEach(() => {
        jwks = new JWKSet()
      })

      it('should return a promise', () => {
        return jwks.generateKeys('ES256').should.be.fulfilled
      })

      it('should reject on invalid input', () => {
        return jwks.generateKeys(null).should.be.rejected
      })

      describe('with JWA alg string input', () => {

        it('should resolve the new keypair', () => {
          return jwks.generateKeys('ES256').then(keys => keys.length.should.equal(2))
        })

        it('should reject with invalid alg', () => {
          return jwks.generateKeys('ES255').should.be.rejected
        })
      })

      describe('with array input', () => {

        it('should resolve an array of new keypairs', () => {
          return jwks.generateKeys(['ES256', 'ES256']).then(keys => {
            keys.length.should.equal(2)
            expect(Array.isArray(keys[0])).to.be.true
          })
        })

        it('should reject if one entry fails', () => {
          return jwks.generateKeys(['ES256', 'ES255']).should.be.rejected
        })
      })

      describe('with key descriptor object input', () => {

        it('should resolve the new keypair', () => {
          return jwks.generateKeys(ECDSAKeyGenDescriptor).should.be.fulfilled
        })

        it('should reject if `alg` is not present', () => {
          return jwks.generateKeys({ key_ops: ['sign', 'verify'] }).should.be.rejected
        })

        it('should use default value if `key_ops` is not present', () => {
          return jwks.generateKeys({ alg: 'ES256' }).should.be.fulfilled
        })
      })
    })
  })

  describe('importKeys', () => {

    describe('static', () => {

      beforeEach(() => {
        sinon.stub(JWKSet.prototype, 'importKeys').resolves()
      })

      afterEach(() => {
        JWKSet.prototype.importKeys.restore()
      })

      it('should return a promise', () => {
        return JWKSet.importKeys().should.be.fulfilled
      })

      it('should resolve a JWKSet', () => {
        return JWKSet.importKeys().should.eventually.be.a.instanceOf(JWKSet)
      })

      it('should defer to member importKeys', () => {
        return JWKSet.importKeys(ECDSAKeyGenDescriptor).then(jwks => {
          jwks.importKeys.should.be.calledWith(ECDSAKeyGenDescriptor)
        })
      })

      it('should reject if importKeys rejects', () => {
        JWKSet.prototype.importKeys.restore()
        sinon.stub(JWKSet.prototype, 'importKeys').rejects()
        return JWKSet.importKeys().should.be.rejected
      })
    })

    describe('member', () => {
      let jwks

      beforeEach(() => {
        jwks = new JWKSet()
      })

      it('should return a promise', () => {
        return jwks.importKeys(ECPrivateJWK).should.be.fulfilled
      })

      it('should reject on invalid input', () => {
        return jwks.importKeys(null).should.be.rejected
      })
    })
  })

  describe('filter', () => {

  })

  describe('find', () => {

  })

  describe('exportKeys', () => {

  })
})
{'fraction_non_alphanumeric': 0.16766663030435258, 'fraction_numerical': 0.006872477364459474, 'mean_word_length': 2.3241479332849893, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1831118', 'n_tokens_mistral': 3104, 'n_tokens_neox': 2915, 'n_words': 497}