summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/all-package-metadata-entry-stream-unit.js
blob: 4e916229cd852dc08f077d9f44195f65223c1314 (plain)
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
'use strict'

const common = require('../common-tap.js')
const getStream = require('get-stream')
const mkdirp = require('mkdirp')
const mr = require('npm-registry-mock')
const npm = require('../../')
const path = require('path')
const rimraf = require('rimraf')
const Tacks = require('tacks')
const test = require('tap').test

const {File} = Tacks

const _createEntryStream = require('../../lib/search/all-package-metadata.js')._createEntryStream

const PKG_DIR = path.resolve(__dirname, 'create-entry-update-stream')
const CACHE_DIR = path.resolve(PKG_DIR, 'cache')

let server

function setup () {
  mkdirp.sync(CACHE_DIR)
}

function cleanup () {
  rimraf.sync(PKG_DIR)
}

test('setup', t => {
  cleanup()
  mr({port: common.port, throwOnUnmatched: true}, (err, s) => {
    t.ifError(err, 'registry mocked successfully')
    npm.load({ cache: CACHE_DIR, registry: common.registry }, err => {
      t.ifError(err, 'npm loaded successfully')
      server = s
      t.pass('all set up')
      t.done()
    })
  })
})

test('createEntryStream full request', t => {
  setup()
  const cachePath = path.join(CACHE_DIR, '.cache.json')
  const dataTime = +(new Date())
  server.get('/-/all').once().reply(200, {
    '_updated': dataTime,
    'bar': { name: 'bar', version: '1.0.0' },
    'foo': { name: 'foo', version: '1.0.0' }
  }, {
    date: 1234 // should never be used.
  })
  return _createEntryStream(cachePath, 600, {
    registry: common.registry
  }).then(({
    entryStream: stream,
    latest,
    newEntries
  }) => {
    t.equals(latest, dataTime, '`latest` correctly extracted')
    t.ok(newEntries, 'new entries need to be written to cache')
    t.ok(stream, 'returned a stream')
    return getStream.array(stream)
  }).then(results => {
    t.deepEquals(results, [{
      name: 'bar',
      version: '1.0.0'
    }, {
      name: 'foo',
      version: '1.0.0'
    }])
    server.done()
    cleanup()
  })
})

test('createEntryStream cache only', function (t) {
  setup()
  const now = Date.now()
  const cacheTime = now - 100000
  const cachePath = path.join(CACHE_DIR, '.cache.json')
  const fixture = new Tacks(File({
    '_updated': cacheTime,
    bar: { name: 'bar', version: '1.0.0' },
    cool: { name: 'cool', version: '1.0.0' },
    foo: { name: 'foo', version: '2.0.0' },
    other: { name: 'other', version: '1.0.0' }
  }))
  fixture.create(cachePath)
  return _createEntryStream(cachePath, 600, {
    registry: common.registry
  }).then(({
    entryStream: stream,
    latest,
    newEntries
  }) => {
    t.equals(latest, cacheTime, '`latest` is cache time')
    t.ok(stream, 'returned a stream')
    t.notOk(newEntries, 'cache only means no need to write to cache')
    return getStream.array(stream)
  }).then(results => {
    t.deepEquals(
      results.map(function (pkg) { return pkg.name }),
      ['bar', 'cool', 'foo', 'other'],
      'packages deduped and sorted'
    )
    server.done()
    cleanup()
  })
})

test('createEntryStream merged stream', function (t) {
  setup()
  const now = Date.now()
  const cacheTime = now - 6000000
  server.get('/-/all/since?stale=update_after&startkey=' + cacheTime).once().reply(200, {
    'bar': { name: 'bar', version: '2.0.0' },
    'car': { name: 'car', version: '1.0.0' },
    'foo': { name: 'foo', version: '1.0.0' }
  }, {
    date: (new Date(now)).toISOString()
  })
  const cachePath = path.join(CACHE_DIR, '.cache.json')
  const fixture = new Tacks(File({
    '_updated': cacheTime,
    bar: { name: 'bar', version: '1.0.0' },
    cool: { name: 'cool', version: '1.0.0' },
    foo: { name: 'foo', version: '2.0.0' },
    other: { name: 'other', version: '1.0.0' }
  }))
  fixture.create(cachePath)
  return _createEntryStream(cachePath, 600, {
    registry: common.registry
  }).then(({
    entryStream: stream,
    latest,
    newEntries
  }) => {
    t.equals(latest, now, '`latest` correctly extracted from header')
    t.ok(stream, 'returned a stream')
    t.ok(newEntries, 'cache update means entries should be written')
    return getStream.array(stream)
  }).then(results => {
    t.deepEquals(
      results.map(function (pkg) { return pkg.name }),
      ['bar', 'car', 'cool', 'foo', 'other'],
      'packages deduped and sorted'
    )
    t.deepEquals(results[0], {
      name: 'bar',
      version: '2.0.0'
    }, 'update stream version wins on dedupe')
    t.deepEquals(results[3], {
      name: 'foo',
      version: '1.0.0'
    }, 'update stream version wins on dedupe even when the newer one has a lower semver.')
    server.done()
    cleanup()
  })
})

test('createEntryStream no sources', function (t) {
  setup()
  const cachePath = path.join(CACHE_DIR, '.cache.json')
  server.get('/-/all').once().reply(404, {})
  return _createEntryStream(cachePath, 600, {
    registry: common.registry
  }).then(({
    entryStream: stream,
    latest,
    newEntries
  }) => {
    throw new Error('should not succeed')
  }, err => {
    t.ok(err, 'no sources, got an error')
    t.match(err.message, /No search sources available/, 'useful error message')
  }).then(() => {
    server.done()
    cleanup()
  })
})

test('cleanup', function (t) {
  cleanup()
  server.close()
  t.pass('all done')
  t.done()
})