File size: 9,729 Bytes
23ac194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
'use strict'

const { test } = require('node:test')
const proxyquire = require('proxyquire')
const fp = require('../plugin')
const Fastify = require('fastify')

const pkg = require('../package.json')

test('fastify-plugin is a function', (t) => {
  t.plan(1)
  t.assert.ok(typeof fp === 'function')
})

test('should return the function with the skip-override Symbol', (t) => {
  t.plan(1)

  function plugin (fastify, opts, next) {
    next()
  }

  fp(plugin)
  t.assert.ok(plugin[Symbol.for('skip-override')])
})

test('should support "default" function from babel module', (t) => {
  t.plan(1)

  const plugin = {
    default: () => { }
  }

  try {
    fp(plugin)
    t.assert.ok(true)
  } catch (e) {
    t.assert.strictEqual(e.message, 'fastify-plugin expects a function, instead got a \'object\'')
  }
})

test('should throw if the plugin is not a function', (t) => {
  t.plan(1)

  try {
    fp('plugin')
    t.assert.fail()
  } catch (e) {
    t.assert.strictEqual(e.message, 'fastify-plugin expects a function, instead got a \'string\'')
  }
})

test('should check the fastify version', (t) => {
  t.plan(1)

  function plugin (fastify, opts, next) {
    next()
  }

  try {
    fp(plugin, { fastify: '>=0.10.0' })
    t.assert.ok(true)
  } catch (e) {
    t.assert.fail()
  }
})

test('should check the fastify version', (t) => {
  t.plan(1)

  function plugin (fastify, opts, next) {
    next()
  }

  try {
    fp(plugin, '>=0.10.0')
    t.assert.ok(true)
  } catch (e) {
    t.assert.fail()
  }
})

test('the options object should be an object', (t) => {
  t.plan(2)

  try {
    fp(() => { }, null)
    t.assert.fail()
  } catch (e) {
    t.assert.strictEqual(e.message, 'The options object should be an object')
  }

  try {
    fp(() => { }, [])
    t.assert.fail()
  } catch (e) {
    t.assert.strictEqual(e.message, 'The options object should be an object')
  }
})

test('should throw if the version number is not a string', (t) => {
  t.plan(1)

  try {
    fp(() => { }, { fastify: 12 })
    t.assert.fail()
  } catch (e) {
    t.assert.strictEqual(e.message, 'fastify-plugin expects a version string, instead got \'number\'')
  }
})

test('Should accept an option object', (t) => {
  t.plan(2)

  const opts = { hello: 'world' }

  function plugin (fastify, opts, next) {
    next()
  }

  fp(plugin, opts)

  t.assert.ok(plugin[Symbol.for('skip-override')], 'skip-override symbol should be present')
  t.assert.deepStrictEqual(plugin[Symbol.for('plugin-meta')], opts, 'plugin-meta should match opts')
})

test('Should accept an option object and checks the version', (t) => {
  t.plan(2)

  const opts = { hello: 'world', fastify: '>=0.10.0' }

  function plugin (fastify, opts, next) {
    next()
  }

  fp(plugin, opts)
  t.assert.ok(plugin[Symbol.for('skip-override')])
  t.assert.deepStrictEqual(plugin[Symbol.for('plugin-meta')], opts)
})

test('should set anonymous function name to file it was called from with a counter', (t) => {
  const fp = proxyquire('../plugin.js', { stubs: {} })

  const fn = fp((fastify, opts, next) => {
    next()
  })

  t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, 'test-auto-0')
  t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], 'test-auto-0')

  const fn2 = fp((fastify, opts, next) => {
    next()
  })

  t.assert.strictEqual(fn2[Symbol.for('plugin-meta')].name, 'test-auto-1')
  t.assert.strictEqual(fn2[Symbol.for('fastify.display-name')], 'test-auto-1')
})

test('should set function name if Error.stackTraceLimit is set to 0', (t) => {
  const stackTraceLimit = Error.stackTraceLimit = 0

  const fp = proxyquire('../plugin.js', { stubs: {} })

  const fn = fp((fastify, opts, next) => {
    next()
  })

  t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, 'test-auto-0')
  t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], 'test-auto-0')

  const fn2 = fp((fastify, opts, next) => {
    next()
  })

  t.assert.strictEqual(fn2[Symbol.for('plugin-meta')].name, 'test-auto-1')
  t.assert.strictEqual(fn2[Symbol.for('fastify.display-name')], 'test-auto-1')

  Error.stackTraceLimit = stackTraceLimit
})

test('should set display-name to meta name', (t) => {
  t.plan(2)

  const functionName = 'superDuperSpecialFunction'

  const fn = fp((fastify, opts, next) => next(), {
    name: functionName
  })

  t.assert.strictEqual(fn[Symbol.for('plugin-meta')].name, functionName)
  t.assert.strictEqual(fn[Symbol.for('fastify.display-name')], functionName)
})

test('should preserve fastify version in meta', (t) => {
  t.plan(1)

  const opts = { hello: 'world', fastify: '>=0.10.0' }

  const fn = fp((fastify, opts, next) => next(), opts)

  t.assert.strictEqual(fn[Symbol.for('plugin-meta')].fastify, '>=0.10.0')
})

test('should check fastify dependency graph - plugin', async (t) => {
  t.plan(1)
  const fastify = Fastify()

  fastify.register(fp((fastify, opts, next) => next(), {
    fastify: '5.x',
    name: 'plugin1-name'
  }))

  fastify.register(fp((fastify, opts, next) => next(), {
    fastify: '5.x',
    name: 'test',
    dependencies: ['plugin1-name', 'plugin2-name']
  }))

  await t.assert.rejects(fastify.ready(), { message: "The dependency 'plugin2-name' of plugin 'test' is not registered" })
})

test('should check fastify dependency graph - decorate', async (t) => {
  t.plan(1)
  const fastify = Fastify()

  fastify.decorate('plugin1', fp((fastify, opts, next) => next(), {
    fastify: '5.x',
    name: 'plugin1-name'
  }))

  fastify.register(fp((fastify, opts, next) => next(), {
    fastify: '5.x',
    name: 'test',
    decorators: { fastify: ['plugin1', 'plugin2'] }
  }))

  await t.assert.rejects(fastify.ready(), { message: "The decorator 'plugin2' required by 'test' is not present in Fastify" })
})

test('should check fastify dependency graph - decorateReply', async (t) => {
  t.plan(1)
  const fastify = Fastify()

  fastify.decorateReply('plugin1', fp((fastify, opts, next) => next(), {
    fastify: '5.x',
    name: 'plugin1-name'
  }))

  fastify.register(fp((fastify, opts, next) => next(), {
    fastify: '5.x',
    name: 'test',
    decorators: { reply: ['plugin1', 'plugin2'] }
  }))

  await t.assert.rejects(fastify.ready(), { message: "The decorator 'plugin2' required by 'test' is not present in Reply" })
})

test('should accept an option to encapsulate', async (t) => {
  t.plan(3)

  const fastify = Fastify()

  fastify.register(fp((fastify, opts, next) => {
    fastify.decorate('accessible', true)
    next()
  }, {
    name: 'accessible-plugin'
  }))

  fastify.register(fp((fastify, opts, next) => {
    fastify.decorate('alsoAccessible', true)
    next()
  }, {
    name: 'accessible-plugin2',
    encapsulate: false
  }))

  fastify.register(fp((fastify, opts, next) => {
    fastify.decorate('encapsulated', true)
    next()
  }, {
    name: 'encapsulated-plugin',
    encapsulate: true
  }))

  await fastify.ready()

  t.assert.ok(fastify.hasDecorator('accessible'))
  t.assert.ok(fastify.hasDecorator('alsoAccessible'))
  t.assert.ok(!fastify.hasDecorator('encapsulated'))
})

test('should check dependencies when encapsulated', async (t) => {
  t.plan(1)
  const fastify = Fastify()

  fastify.register(fp((fastify, opts, next) => next(), {
    name: 'test',
    dependencies: ['missing-dependency-name'],
    encapsulate: true
  }))

  await t.assert.rejects(fastify.ready(), { message: "The dependency 'missing-dependency-name' of plugin 'test' is not registered" })
})

test(
  'should check version when encapsulated',
  { skip: /\d-.+/.test(pkg.devDependencies.fastify) },
  async (t) => {
    t.plan(1)
    const fastify = Fastify()

    fastify.register(fp((fastify, opts, next) => next(), {
      name: 'test',
      fastify: '<=2.10.0',
      encapsulate: true
    }))

    await t.assert.rejects(fastify.ready(), { message: /fastify-plugin: test - expected '<=2.10.0' fastify version, '\d.\d+.\d+' is installed/ })
  }
)

test('should check decorators when encapsulated', async (t) => {
  t.plan(1)
  const fastify = Fastify()

  fastify.decorate('plugin1', 'foo')

  fastify.register(fp((fastify, opts, next) => next(), {
    fastify: '5.x',
    name: 'test',
    encapsulate: true,
    decorators: { fastify: ['plugin1', 'plugin2'] }
  }))

  await t.assert.rejects(fastify.ready(), { message: "The decorator 'plugin2' required by 'test' is not present in Fastify" })
})

test('plugin name when encapsulated', async (t) => {
  t.plan(6)
  const fastify = Fastify()

  fastify.register(function plugin (instance, opts, next) {
    next()
  })

  fastify.register(fp(getFn('hello'), {
    fastify: '5.x',
    name: 'hello',
    encapsulate: true
  }))

  fastify.register(function plugin (fastify, opts, next) {
    fastify.register(fp(getFn('deep'), {
      fastify: '5.x',
      name: 'deep',
      encapsulate: true
    }))

    fastify.register(fp(function genericPlugin (fastify, opts, next) {
      t.assert.strictEqual(fastify.pluginName, 'deep-deep', 'should be deep-deep')

      fastify.register(fp(getFn('deep-deep-deep'), {
        fastify: '5.x',
        name: 'deep-deep-deep',
        encapsulate: true
      }))

      fastify.register(fp(getFn('deep-deep -> not-encapsulated-2'), {
        fastify: '5.x',
        name: 'not-encapsulated-2'
      }))

      next()
    }, {
      fastify: '5.x',
      name: 'deep-deep',
      encapsulate: true
    }))

    fastify.register(fp(getFn('plugin -> not-encapsulated'), {
      fastify: '5.x',
      name: 'not-encapsulated'
    }))

    next()
  })

  await fastify.ready()

  function getFn (expectedName) {
    return function genericPlugin (fastify, opts, next) {
      t.assert.strictEqual(fastify.pluginName, expectedName, `should be ${expectedName}`)
      next()
    }
  }
})