CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
hook-runner.test.js450 linesDownload Raw Back to internals
1'use strict'2 3const { test } = require('node:test')4const { hookRunnerGenerator, onSendHookRunner } = require('../../lib/hooks')5 6test('hookRunner - Basic', t => {7  t.plan(9)8 9  const hookRunner = hookRunnerGenerator(iterator)10 11  hookRunner([fn1, fn2, fn3], 'a', 'b', done)12 13  function iterator (fn, a, b, done) {14    return fn(a, b, done)15  }16 17  function fn1 (a, b, done) {18    t.assert.strictEqual(a, 'a')19    t.assert.strictEqual(b, 'b')20    done()21  }22 23  function fn2 (a, b, done) {24    t.assert.strictEqual(a, 'a')25    t.assert.strictEqual(b, 'b')26    done()27  }28 29  function fn3 (a, b, done) {30    t.assert.strictEqual(a, 'a')31    t.assert.strictEqual(b, 'b')32    done()33  }34 35  function done (err, a, b) {36    t.assert.ifError(err)37    t.assert.strictEqual(a, 'a')38    t.assert.strictEqual(b, 'b')39  }40})41 42test('hookRunner - In case of error should skip to done', t => {43  t.plan(7)44 45  const hookRunner = hookRunnerGenerator(iterator)46 47  hookRunner([fn1, fn2, fn3], 'a', 'b', done)48 49  function iterator (fn, a, b, done) {50    return fn(a, b, done)51  }52 53  function fn1 (a, b, done) {54    t.assert.strictEqual(a, 'a')55    t.assert.strictEqual(b, 'b')56    done()57  }58 59  function fn2 (a, b, done) {60    t.assert.strictEqual(a, 'a')61    t.assert.strictEqual(b, 'b')62    done(new Error('kaboom'))63  }64 65  function fn3 () {66    t.assert.fail('We should not be here')67  }68 69  function done (err, a, b) {70    t.assert.strictEqual(err.message, 'kaboom')71    t.assert.strictEqual(a, 'a')72    t.assert.strictEqual(b, 'b')73  }74})75 76test('hookRunner - Should handle throw', t => {77  t.plan(7)78 79  const hookRunner = hookRunnerGenerator(iterator)80 81  hookRunner([fn1, fn2, fn3], 'a', 'b', done)82 83  function iterator (fn, a, b, done) {84    return fn(a, b, done)85  }86 87  function fn1 (a, b, done) {88    t.assert.strictEqual(a, 'a')89    t.assert.strictEqual(b, 'b')90    done()91  }92 93  function fn2 (a, b, done) {94    t.assert.strictEqual(a, 'a')95    t.assert.strictEqual(b, 'b')96    throw new Error('kaboom')97  }98 99  function fn3 () {100    t.assert.fail('We should not be here')101  }102 103  function done (err, a, b) {104    t.assert.strictEqual(err.message, 'kaboom')105    t.assert.strictEqual(a, 'a')106    t.assert.strictEqual(b, 'b')107  }108})109 110test('hookRunner - Should handle promises', t => {111  t.plan(9)112 113  const hookRunner = hookRunnerGenerator(iterator)114 115  hookRunner([fn1, fn2, fn3], 'a', 'b', done)116 117  function iterator (fn, a, b, done) {118    return fn(a, b, done)119  }120 121  function fn1 (a, b) {122    t.assert.strictEqual(a, 'a')123    t.assert.strictEqual(b, 'b')124    return Promise.resolve()125  }126 127  function fn2 (a, b) {128    t.assert.strictEqual(a, 'a')129    t.assert.strictEqual(b, 'b')130    return Promise.resolve()131  }132 133  function fn3 (a, b) {134    t.assert.strictEqual(a, 'a')135    t.assert.strictEqual(b, 'b')136    return Promise.resolve()137  }138 139  function done (err, a, b) {140    t.assert.ifError(err)141    t.assert.strictEqual(a, 'a')142    t.assert.strictEqual(b, 'b')143  }144})145 146test('hookRunner - In case of error should skip to done (with promises)', t => {147  t.plan(7)148 149  const hookRunner = hookRunnerGenerator(iterator)150 151  hookRunner([fn1, fn2, fn3], 'a', 'b', done)152 153  function iterator (fn, a, b, done) {154    return fn(a, b, done)155  }156 157  function fn1 (a, b) {158    t.assert.strictEqual(a, 'a')159    t.assert.strictEqual(b, 'b')160    return Promise.resolve()161  }162 163  function fn2 (a, b) {164    t.assert.strictEqual(a, 'a')165    t.assert.strictEqual(b, 'b')166    return Promise.reject(new Error('kaboom'))167  }168 169  function fn3 () {170    t.assert.fail('We should not be here')171  }172 173  function done (err, a, b) {174    t.assert.strictEqual(err.message, 'kaboom')175    t.assert.strictEqual(a, 'a')176    t.assert.strictEqual(b, 'b')177  }178})179 180test('hookRunner - Be able to exit before its natural end', t => {181  t.plan(4)182 183  const hookRunner = hookRunnerGenerator(iterator)184 185  let shouldStop = false186  hookRunner([fn1, fn2, fn3], 'a', 'b', done)187 188  function iterator (fn, a, b, done) {189    if (shouldStop) {190      return undefined191    }192    return fn(a, b, done)193  }194 195  function fn1 (a, b, done) {196    t.assert.strictEqual(a, 'a')197    t.assert.strictEqual(b, 'b')198    done()199  }200 201  function fn2 (a, b) {202    t.assert.strictEqual(a, 'a')203    t.assert.strictEqual(b, 'b')204    shouldStop = true205    return Promise.resolve()206  }207 208  function fn3 () {209    t.assert.fail('this should not be called')210  }211 212  function done () {213    t.assert.fail('this should not be called')214  }215})216 217test('hookRunner - Promises that resolve to a value do not change the state', t => {218  t.plan(5)219 220  const originalState = { a: 'a', b: 'b' }221 222  const hookRunner = hookRunnerGenerator(iterator)223 224  hookRunner([fn1, fn2, fn3], originalState, 'b', done)225 226  function iterator (fn, state, b, done) {227    return fn(state, b, done)228  }229 230  function fn1 (state, b, done) {231    t.assert.strictEqual(state, originalState)232    return Promise.resolve(null)233  }234 235  function fn2 (state, b, done) {236    t.assert.strictEqual(state, originalState)237    return Promise.resolve('string')238  }239 240  function fn3 (state, b, done) {241    t.assert.strictEqual(state, originalState)242    return Promise.resolve({ object: true })243  }244 245  function done (err, state, b) {246    t.assert.ifError(err)247    t.assert.strictEqual(state, originalState)248  }249})250 251test('onSendHookRunner - Basic', t => {252  t.plan(13)253 254  const originalRequest = { body: null }255  const originalReply = { request: originalRequest }256  const originalPayload = 'payload'257 258  onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, originalPayload, done)259 260  function fn1 (request, reply, payload, done) {261    t.assert.deepStrictEqual(request, originalRequest)262    t.assert.deepStrictEqual(reply, originalReply)263    t.assert.strictEqual(payload, originalPayload)264    done()265  }266 267  function fn2 (request, reply, payload, done) {268    t.assert.deepStrictEqual(request, originalRequest)269    t.assert.deepStrictEqual(reply, originalReply)270    t.assert.strictEqual(payload, originalPayload)271    done()272  }273 274  function fn3 (request, reply, payload, done) {275    t.assert.deepStrictEqual(request, originalRequest)276    t.assert.deepStrictEqual(reply, originalReply)277    t.assert.strictEqual(payload, originalPayload)278    done()279  }280 281  function done (err, request, reply, payload) {282    t.assert.ifError(err)283    t.assert.deepStrictEqual(request, originalRequest)284    t.assert.deepStrictEqual(reply, originalReply)285    t.assert.strictEqual(payload, originalPayload)286  }287})288 289test('onSendHookRunner - Can change the payload', t => {290  t.plan(7)291 292  const originalRequest = { body: null }293  const originalReply = { request: originalRequest }294  const v1 = { hello: 'world' }295  const v2 = { ciao: 'mondo' }296  const v3 = { winter: 'is coming' }297  const v4 = { winter: 'has come' }298 299  onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, v1, done)300 301  function fn1 (request, reply, payload, done) {302    t.assert.deepStrictEqual(payload, v1)303    done(null, v2)304  }305 306  function fn2 (request, reply, payload, done) {307    t.assert.deepStrictEqual(payload, v2)308    done(null, v3)309  }310 311  function fn3 (request, reply, payload, done) {312    t.assert.deepStrictEqual(payload, v3)313    done(null, v4)314  }315 316  function done (err, request, reply, payload) {317    t.assert.ifError(err)318    t.assert.deepStrictEqual(request, originalRequest)319    t.assert.deepStrictEqual(reply, originalReply)320    t.assert.deepStrictEqual(payload, v4)321  }322})323 324test('onSendHookRunner - In case of error should skip to done', t => {325  t.plan(6)326 327  const originalRequest = { body: null }328  const originalReply = { request: originalRequest }329  const v1 = { hello: 'world' }330  const v2 = { ciao: 'mondo' }331 332  onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, v1, done)333 334  function fn1 (request, reply, payload, done) {335    t.assert.deepStrictEqual(payload, v1)336    done(null, v2)337  }338 339  function fn2 (request, reply, payload, done) {340    t.assert.deepStrictEqual(payload, v2)341    done(new Error('kaboom'))342  }343 344  function fn3 () {345    t.assert.fail('We should not be here')346  }347 348  function done (err, request, reply, payload) {349    t.assert.strictEqual(err.message, 'kaboom')350    t.assert.deepStrictEqual(request, originalRequest)351    t.assert.deepStrictEqual(reply, originalReply)352    t.assert.deepStrictEqual(payload, v2)353  }354})355 356test('onSendHookRunner - Should handle promises', t => {357  t.plan(7)358 359  const originalRequest = { body: null }360  const originalReply = { request: originalRequest }361  const v1 = { hello: 'world' }362  const v2 = { ciao: 'mondo' }363  const v3 = { winter: 'is coming' }364  const v4 = { winter: 'has come' }365 366  onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, v1, done)367 368  function fn1 (request, reply, payload) {369    t.assert.deepStrictEqual(payload, v1)370    return Promise.resolve(v2)371  }372 373  function fn2 (request, reply, payload) {374    t.assert.deepStrictEqual(payload, v2)375    return Promise.resolve(v3)376  }377 378  function fn3 (request, reply, payload) {379    t.assert.deepStrictEqual(payload, v3)380    return Promise.resolve(v4)381  }382 383  function done (err, request, reply, payload) {384    t.assert.ifError(err)385    t.assert.deepStrictEqual(request, originalRequest)386    t.assert.deepStrictEqual(reply, originalReply)387    t.assert.deepStrictEqual(payload, v4)388  }389})390 391test('onSendHookRunner - In case of error should skip to done (with promises)', t => {392  t.plan(6)393 394  const originalRequest = { body: null }395  const originalReply = { request: originalRequest }396  const v1 = { hello: 'world' }397  const v2 = { ciao: 'mondo' }398 399  onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, v1, done)400 401  function fn1 (request, reply, payload) {402    t.assert.deepStrictEqual(payload, v1)403    return Promise.resolve(v2)404  }405 406  function fn2 (request, reply, payload) {407    t.assert.deepStrictEqual(payload, v2)408    return Promise.reject(new Error('kaboom'))409  }410 411  function fn3 () {412    t.assert.fail('We should not be here')413  }414 415  function done (err, request, reply, payload) {416    t.assert.strictEqual(err.message, 'kaboom')417    t.assert.deepStrictEqual(request, originalRequest)418    t.assert.deepStrictEqual(reply, originalReply)419    t.assert.deepStrictEqual(payload, v2)420  }421})422 423test('onSendHookRunner - Be able to exit before its natural end', t => {424  t.plan(2)425 426  const originalRequest = { body: null }427  const originalReply = { request: originalRequest }428  const v1 = { hello: 'world' }429  const v2 = { ciao: 'mondo' }430 431  onSendHookRunner([fn1, fn2, fn3], originalRequest, originalReply, v1, done)432 433  function fn1 (request, reply, payload, done) {434    t.assert.deepStrictEqual(payload, v1)435    done(null, v2)436  }437 438  function fn2 (request, reply, payload) {439    t.assert.deepStrictEqual(payload, v2)440  }441 442  function fn3 () {443    t.assert.fail('this should not be called')444  }445 446  function done () {447    t.assert.fail('this should not be called')448  }449})450