coh-content-db 2.0.0-rc.10 → 2.0.0-rc.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,7 +53,7 @@ describe(BadgeIndex.name, () => {
53
53
  new Badge(badgeDataFixture.create({ key: 'miss-1', name: [{ value: 'Bar 4' }] })),
54
54
  ])
55
55
 
56
- const result = index.search({ query: { str: 'Foo', on: { name: true } } })
56
+ const result = index.search({ query: { str: 'Foo', fields: ['name'] } })
57
57
  const keys = result.items.map(x => x.key)
58
58
  expect(keys).toStrictEqual(['match-1', 'match-2', 'match-3'])
59
59
  })
@@ -67,7 +67,7 @@ describe(BadgeIndex.name, () => {
67
67
  new Badge(badgeDataFixture.create({ key: 'miss-2', badgeText: undefined })),
68
68
  ])
69
69
 
70
- const result = index.search({ query: { str: 'Foo', on: { badgeText: true } } })
70
+ const result = index.search({ query: { str: 'Foo', fields: ['badge-text'] } })
71
71
  const keys = result.items.map(x => x.key)
72
72
  expect(keys).toStrictEqual(['match-1', 'match-2', 'match-3'])
73
73
  })
@@ -80,7 +80,7 @@ describe(BadgeIndex.name, () => {
80
80
  new Badge(badgeDataFixture.create({ key: 'miss-2', acquisition: undefined })),
81
81
  ])
82
82
 
83
- const result = index.search({ query: { str: 'Foo', on: { acquisition: true } } })
83
+ const result = index.search({ query: { str: 'Foo', fields: ['acquisition'] } })
84
84
  const keys = result.items.map(x => x.key)
85
85
  expect(keys).toStrictEqual(['match-1', 'match-2'])
86
86
  })
@@ -93,7 +93,7 @@ describe(BadgeIndex.name, () => {
93
93
  new Badge(badgeDataFixture.create({ key: 'miss-2', effect: undefined })),
94
94
  ])
95
95
 
96
- const result = index.search({ query: { str: 'Foo', on: { effect: true } } })
96
+ const result = index.search({ query: { str: 'Foo', fields: ['effect'] } })
97
97
  const keys = result.items.map(x => x.key)
98
98
  expect(keys).toStrictEqual(['match-1', 'match-2'])
99
99
  })
@@ -106,7 +106,7 @@ describe(BadgeIndex.name, () => {
106
106
  new Badge(badgeDataFixture.create({ key: 'miss-2', notes: undefined })),
107
107
  ])
108
108
 
109
- const result = index.search({ query: { str: 'Foo', on: { notes: true } } })
109
+ const result = index.search({ query: { str: 'Foo', fields: ['notes'] } })
110
110
  const keys = result.items.map(x => x.key)
111
111
  expect(keys).toStrictEqual(['match-1', 'match-2'])
112
112
  })
@@ -119,7 +119,22 @@ describe(BadgeIndex.name, () => {
119
119
  new Badge(badgeDataFixture.create({ key: 'miss-2', setTitleId: undefined })),
120
120
  ])
121
121
 
122
- const result = index.search({ query: { str: '123', on: { setTitle: true } } })
122
+ const result = index.search({ query: { str: '123', fields: ['set-title-id'] } })
123
+
124
+ expect(result.items).toHaveLength(2)
125
+ const keys = result.items.map(x => x.key)
126
+ expect(keys).toStrictEqual(['match-1', 'match-2'])
127
+ })
128
+
129
+ test(`should match on multiple fields`, () => {
130
+ const index = new BadgeIndex([
131
+ new Badge(badgeDataFixture.create({ key: 'match-1', name: 'foo' })),
132
+ new Badge(badgeDataFixture.create({ key: 'match-2', notes: 'foo' })),
133
+ new Badge(badgeDataFixture.create({ key: 'miss-1', name: 'bar' })),
134
+ new Badge(badgeDataFixture.create({ key: 'miss-2', notes: 'bar' })),
135
+ ])
136
+
137
+ const result = index.search({ query: { str: 'foo', fields: ['name', 'notes'] } })
123
138
 
124
139
  expect(result.items).toHaveLength(2)
125
140
  const keys = result.items.map(x => x.key)
@@ -133,7 +148,7 @@ describe(BadgeIndex.name, () => {
133
148
  new Badge(badgeDataFixture.create({ key: 'miss-1', acquisition: 'Bar 1' })),
134
149
  ])
135
150
 
136
- const result = index.search({ query: { str: 'Fo', on: { acquisition: true } } })
151
+ const result = index.search({ query: { str: 'Fo', fields: ['acquisition'] } })
137
152
  const keys = result.items.map(x => x.key)
138
153
  expect(keys).toStrictEqual(['match-1', 'match-2'])
139
154
  })
@@ -145,7 +160,7 @@ describe(BadgeIndex.name, () => {
145
160
  new Badge(badgeDataFixture.create({ key: 'miss-1', acquisition: 'Bar 1' })),
146
161
  ])
147
162
 
148
- const result = index.search({ query: { str: 'foo', on: { acquisition: true } } })
163
+ const result = index.search({ query: { str: 'foo', fields: ['acquisition'] } })
149
164
  const keys = result.items.map(x => x.key)
150
165
  expect(keys).toStrictEqual(['match-1', 'match-2'])
151
166
  })
@@ -162,6 +177,19 @@ describe(BadgeIndex.name, () => {
162
177
  const keys = result.items.map(x => x.key)
163
178
  expect(keys).toStrictEqual(['match-1'])
164
179
  })
180
+
181
+ test(`should return everything if no query fields are set`, () => {
182
+ const index = new BadgeIndex([
183
+ new Badge(badgeDataFixture.create({ key: 'match-1', name: [{ value: 'Foo 1' }] })),
184
+ new Badge(badgeDataFixture.create({ key: 'match-2', acquisition: 'Foo 2' })),
185
+ new Badge(badgeDataFixture.create({ key: 'match-3', name: [{ value: 'Bar 1' }] })),
186
+ ])
187
+
188
+ const result = index.search({ query: { str: 'no-hit', fields: [] } })
189
+
190
+ const keys = result.items.map(x => x.key)
191
+ expect(keys).toStrictEqual(['match-1', 'match-2', 'match-3'])
192
+ })
165
193
  })
166
194
 
167
195
  describe('filter', () => {
@@ -514,6 +542,30 @@ describe(BadgeIndex.name, () => {
514
542
  const keys = result.items.map(x => x.key)
515
543
  expect(keys).toStrictEqual(['badge-5', 'badge-1', 'badge-3', 'badge-2', 'badge-4'])
516
544
  })
545
+
546
+ test(`should sort by release date`, () => {
547
+ const index = new BadgeIndex([
548
+ new Badge(badgeDataFixture.create({ key: 'badge-1', releaseDate: '2025-02-03' })),
549
+ new Badge(badgeDataFixture.create({ key: 'badge-2', releaseDate: '2025-02-03' })),
550
+ new Badge(badgeDataFixture.create({ key: 'badge-3', releaseDate: '2020-01-01' })),
551
+ ])
552
+
553
+ const result = index.search({ sort: { by: 'release-date' } })
554
+ const keys = result.items.map(x => x.key)
555
+ expect(keys).toStrictEqual(['badge-3', 'badge-1', 'badge-2'])
556
+ })
557
+
558
+ test(`should sort by release date descending`, () => {
559
+ const index = new BadgeIndex([
560
+ new Badge(badgeDataFixture.create({ key: 'badge-1', releaseDate: '2025-02-03' })),
561
+ new Badge(badgeDataFixture.create({ key: 'badge-2', releaseDate: '2020-01-01' })),
562
+ new Badge(badgeDataFixture.create({ key: 'badge-3', releaseDate: '2025-02-03' })),
563
+ ])
564
+
565
+ const result = index.search({ sort: { by: 'release-date', dir: 'desc' } })
566
+ const keys = result.items.map(x => x.key)
567
+ expect(keys).toStrictEqual(['badge-1', 'badge-3', 'badge-2'])
568
+ })
517
569
  })
518
570
  })
519
571
  })
@@ -1,4 +1,4 @@
1
- import { Badge, compareByDefaultName, compareByZoneKey } from '../../main'
1
+ import { Badge, compareByDefaultName, compareByReleaseDate, compareByZoneKey } from '../../main'
2
2
  import { badgeDataFixture } from '../api/badge-data.fixture'
3
3
  import { badgeRequirementDataFixture } from '../api/badge-requirement-data.fixture'
4
4
 
@@ -28,6 +28,18 @@ describe(Badge.name, () => {
28
28
  const badge = new Badge(badgeDataFixture.create({ name: [{ value: 'foo' }] }))
29
29
  expect(badge.name.default).toEqual({ value: 'foo' })
30
30
  })
31
+
32
+ test('should accept a string', () => {
33
+ const badge = new Badge(badgeDataFixture.create({ name: 'foo' }))
34
+ expect(badge.name.default).toEqual({ value: 'foo' })
35
+ })
36
+ })
37
+
38
+ describe('releaseDate', () => {
39
+ test('should be set from the data', () => {
40
+ const badge = new Badge(badgeDataFixture.create({ releaseDate: '2025-08-08' }))
41
+ expect(badge.releaseDate).toEqual(new Date('2025-08-08'))
42
+ })
31
43
  })
32
44
 
33
45
  describe('morality', () => {
@@ -55,6 +67,11 @@ describe(Badge.name, () => {
55
67
  expect(badge.badgeText.default).toEqual({ value: 'foo' })
56
68
  })
57
69
 
70
+ test('should accept a string', () => {
71
+ const badge = new Badge(badgeDataFixture.create({ badgeText: 'foo' }))
72
+ expect(badge.badgeText.default).toEqual({ value: 'foo' })
73
+ })
74
+
58
75
  test('should be optional', () => {
59
76
  const badge = new Badge(badgeDataFixture.omit('badgeText').create())
60
77
  expect(badge.badgeText.default).toBeUndefined()
@@ -79,6 +96,11 @@ describe(Badge.name, () => {
79
96
  expect(badge.icon.default).toEqual({ value: 'foo' })
80
97
  })
81
98
 
99
+ test('should accept a string', () => {
100
+ const badge = new Badge(badgeDataFixture.create({ icon: 'foo' }))
101
+ expect(badge.icon.default).toEqual({ value: 'foo' })
102
+ })
103
+
82
104
  test('should be optional', () => {
83
105
  const badge = new Badge(badgeDataFixture.omit('icon').create())
84
106
  expect(badge.icon.default).toBeUndefined()
@@ -159,7 +181,19 @@ describe(Badge.name, () => {
159
181
  badgeRequirementDataFixture.create({ key: 'foo' }),
160
182
  ],
161
183
  })
162
- expect(() => new Badge(data)).toThrow('Duplicate badge requirement key [badge:foo]')
184
+ expect(() => new Badge(data)).toThrow('Duplicate key [foo]')
185
+ })
186
+
187
+ test(`should return requirement list`, () => {
188
+ const data = badgeDataFixture.create({
189
+ requirements: [
190
+ badgeRequirementDataFixture.create({ key: 'foo' }),
191
+ badgeRequirementDataFixture.create({ key: 'bar' }),
192
+ ],
193
+ })
194
+ const badge = new Badge(data)
195
+
196
+ expect(badge.requirements.map(x => x.key)).toStrictEqual(['foo', 'bar'])
163
197
  })
164
198
  })
165
199
 
@@ -172,12 +206,12 @@ describe(Badge.name, () => {
172
206
  expect(new Badge(data).getRequirement('foo')).not.toBeUndefined()
173
207
  })
174
208
 
175
- test(`should throw error for unknown requirement`, () => {
209
+ test(`should return undefined for unknown requirement`, () => {
176
210
  const data = badgeDataFixture.create({
177
211
  requirements: [],
178
212
  })
179
213
 
180
- expect(() => new Badge(data).getRequirement('foo')).toThrow('Unknown badge requirement key [foo]')
214
+ expect(new Badge(data).getRequirement('foo')).toBeUndefined()
181
215
  })
182
216
  })
183
217
 
@@ -334,4 +368,35 @@ describe(Badge.name, () => {
334
368
  expect([badgeB, badgeA].sort(compareByZoneKey)).toStrictEqual([badgeA, badgeB])
335
369
  })
336
370
  })
371
+
372
+ describe(compareByReleaseDate.name, () => {
373
+ test(`should compare two badges by releaseDate`, () => {
374
+ const badgeA = new Badge(badgeDataFixture.create({ releaseDate: '2024-01-01' }))
375
+ const badgeB = new Badge(badgeDataFixture.create({ releaseDate: '2025-01-01' }))
376
+ expect(compareByReleaseDate(badgeA, badgeB)).toBeLessThan(0)
377
+ expect([badgeB, badgeA].sort(compareByReleaseDate)).toStrictEqual([badgeA, badgeB])
378
+ })
379
+
380
+ test(`should return 0 for equal releaseDates`, () => {
381
+ const badgeA = new Badge(badgeDataFixture.create({ releaseDate: '2025-01-01' }))
382
+ const badgeB = new Badge(badgeDataFixture.create({ releaseDate: '2025-01-01' }))
383
+ expect(compareByReleaseDate(badgeA, badgeB)).toEqual(0)
384
+ })
385
+
386
+ test(`should equate two undefined values`, () => {
387
+ const badgeA = undefined
388
+ const badgeB = undefined
389
+ expect(compareByReleaseDate(badgeA, badgeB)).toEqual(0)
390
+ })
391
+
392
+ test(`should compare undefined value as higher`, () => {
393
+ const badgeA = undefined
394
+ const badgeB = new Badge(badgeDataFixture.create({ releaseDate: '2025-01-01' }))
395
+ expect(compareByReleaseDate(badgeA, badgeB)).toBeGreaterThan(0)
396
+ expect([badgeA, badgeB].sort(compareByReleaseDate)).toStrictEqual([badgeB, badgeA])
397
+
398
+ expect(compareByReleaseDate(badgeB, badgeA)).toBeLessThan(0)
399
+ expect([badgeB, badgeA].sort(compareByReleaseDate)).toStrictEqual([badgeB, badgeA])
400
+ })
401
+ })
337
402
  })
@@ -274,7 +274,7 @@ describe(CohContentDatabase.name, () => {
274
274
  }),
275
275
  )
276
276
 
277
- const result = database.searchBadges({ query: { str: 'oo' } })
277
+ const result = database.searchBadges({ query: { str: 'oo', fields: ['name'] } })
278
278
  expect(result.totalItems).toBe(1)
279
279
  expect(result.items.map(x => x.key)).toStrictEqual(['foo'])
280
280
  })