coh-content-db 2.0.0-rc.1 → 2.0.0-rc.3

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.
@@ -1,42 +1,143 @@
1
1
  import { CohContentDatabase } from '../../main'
2
- import { serverGroupDataFixture } from '../api/server-group-data.fixture'
2
+ import { contentBundleFixture } from '../api/content-bundle.fixture'
3
+ import { archetypeDataFixture } from '../api/archetype-data.fixture'
4
+ import { badgeDataFixture } from '../api/badge-data.fixture'
5
+ import { gameMapDataFixture } from '../api/game-map-data.fixture'
3
6
 
4
7
  describe(CohContentDatabase.name, () => {
5
- test('should be instantiable', () => {
6
- expect(new CohContentDatabase()).not.toBeNull()
8
+ test('should load a basic bundle', () => {
9
+ new CohContentDatabase(contentBundleFixture.create())
7
10
  })
8
11
 
9
- describe(CohContentDatabase.prototype.loadServerGroupData.name, () => {
10
- test('should load an empty server group', () => {
11
- const database = new CohContentDatabase()
12
- database.loadServerGroupData(serverGroupDataFixture.create())
12
+ describe('servers', () => {
13
+ test(`should accept an undefined field`, () => {
14
+ const data = contentBundleFixture
15
+ .omit('servers')
16
+ .create()
17
+ expect(() => new CohContentDatabase(data).archetypes).toHaveLength(0)
18
+ })
19
+
20
+ test(`should load values from bundle`, () => {
21
+ const data = contentBundleFixture
22
+ .create({ servers: ['Foo', 'Bar'] })
23
+ expect(new CohContentDatabase(data).servers).toStrictEqual(['Foo', 'Bar'])
24
+ })
25
+ })
26
+
27
+ describe('archetypes', () => {
28
+ test(`should throw an error on duplicate key`, () => {
29
+ const data = contentBundleFixture.create({
30
+ archetypes: [
31
+ archetypeDataFixture.create({ key: 'foo' }),
32
+ archetypeDataFixture.create({ key: 'foo' }),
33
+ ],
34
+ })
35
+ expect(() => new CohContentDatabase(data)).toThrow('Duplicate archetype key [foo]')
36
+ })
37
+
38
+ test(`should accept an undefined field`, () => {
39
+ const data = contentBundleFixture
40
+ .omit('archetypes')
41
+ .create()
42
+ expect(() => new CohContentDatabase(data).archetypes).toHaveLength(0)
43
+ })
44
+
45
+ test(`should load data from bundle`, () => {
46
+ const data = contentBundleFixture
47
+ .create({ archetypes: [archetypeDataFixture.create({ key: 'foo' })] })
48
+ expect(() => new CohContentDatabase(data).getArchetype('foo')).not.toBeUndefined()
49
+ })
50
+ })
51
+
52
+ describe('badges', () => {
53
+ test(`should throw an error on duplicate key`, () => {
54
+ const data = contentBundleFixture.create({
55
+ badges: [
56
+ badgeDataFixture.create({ key: 'foo' }),
57
+ badgeDataFixture.create({ key: 'foo' }),
58
+ ],
59
+ })
60
+ expect(() => new CohContentDatabase(data)).toThrow('Duplicate badge key [foo]')
61
+ })
62
+
63
+ test(`should accept an undefined field`, () => {
64
+ const data = contentBundleFixture
65
+ .omit('badges')
66
+ .create()
67
+ expect(() => new CohContentDatabase(data).badges).toHaveLength(0)
68
+ })
69
+ })
70
+
71
+ describe('maps', () => {
72
+ test(`should throw an error on duplicate map`, () => {
73
+ const data = contentBundleFixture.create({
74
+ maps: [
75
+ gameMapDataFixture.create({ key: 'foo' }),
76
+ gameMapDataFixture.create({ key: 'foo' }),
77
+ ],
78
+ })
79
+ expect(() => new CohContentDatabase(data)).toThrow('Duplicate map key [foo]')
80
+ })
81
+
82
+ test(`should accept an undefined field`, () => {
83
+ const data = contentBundleFixture
84
+ .omit('maps')
85
+ .create()
86
+ expect(() => new CohContentDatabase(data).maps).toHaveLength(0)
13
87
  })
14
88
  })
15
89
 
16
- describe(CohContentDatabase.prototype.listServerGroups.name, () => {
17
- test('should load two server groups', () => {
18
- const database = new CohContentDatabase()
19
- database.loadServerGroupData(serverGroupDataFixture.create({ key: 'sg1' }))
20
- database.loadServerGroupData(serverGroupDataFixture.create({ key: 'sg2' }))
90
+ describe('getArchetype', () => {
91
+ test(`should retrieve archetype from the index`, () => {
92
+ const data = contentBundleFixture.create({
93
+ archetypes: [archetypeDataFixture.create({ key: 'foo' })],
94
+ })
21
95
 
22
- const sgs = database.listServerGroups()
96
+ expect(new CohContentDatabase(data).getArchetype('foo')).not.toBeUndefined()
97
+ })
23
98
 
24
- expect(sgs).toHaveLength(2)
99
+ test(`should throw error for unknown archetype`, () => {
100
+ const data = contentBundleFixture.create({
101
+ archetypes: [],
102
+ })
25
103
 
26
- for (const entry of sgs) {
27
- expect(['sg1', 'sg2']).toContain(entry.key)
28
- }
104
+ expect(() => new CohContentDatabase(data).getArchetype('foo')).toThrow('Unknown archetype key [foo]')
29
105
  })
30
106
  })
31
107
 
32
- describe(CohContentDatabase.prototype.getServerGroup.name, () => {
33
- test('should load a server group by key', () => {
34
- const database = new CohContentDatabase()
35
- database.loadServerGroupData(serverGroupDataFixture.create({ key: 'sg1' }))
108
+ describe('getMap', () => {
109
+ test(`should retrieve map from the index`, () => {
110
+ const data = contentBundleFixture.create({
111
+ maps: [gameMapDataFixture.create({ key: 'foo' })],
112
+ })
113
+
114
+ expect(new CohContentDatabase(data).getMap('foo')).not.toBeUndefined()
115
+ })
116
+
117
+ test(`should throw error for unknown map`, () => {
118
+ const data = contentBundleFixture.create({
119
+ maps: [],
120
+ })
121
+
122
+ expect(() => new CohContentDatabase(data).getMap('foo')).toThrow('Unknown map key [foo]')
123
+ })
124
+ })
125
+
126
+ describe('getBadge', () => {
127
+ test(`should retrieve badge from the index`, () => {
128
+ const data = contentBundleFixture.create({
129
+ badges: [badgeDataFixture.create({ key: 'foo' })],
130
+ })
131
+
132
+ expect(new CohContentDatabase(data).getBadge('foo')).not.toBeUndefined()
133
+ })
134
+
135
+ test(`should throw error for unknown badge`, () => {
136
+ const data = contentBundleFixture.create({
137
+ badges: [],
138
+ })
36
139
 
37
- const sg = database.getServerGroup('sg1')
38
- expect(sg).not.toBeNull()
39
- expect(sg?.key).toEqual('sg1')
140
+ expect(() => new CohContentDatabase(data).getBadge('foo')).toThrow('Unknown badge key [foo]')
40
141
  })
41
142
  })
42
143
  })
@@ -1,112 +0,0 @@
1
- import { Archetype } from './archetype'
2
- import { Badge } from './badge'
3
- import { ServerGroupData } from '../api/server-group-data'
4
- import { Key } from './key'
5
- import { Change } from '../api/change'
6
- import { GameMap } from './game-map'
7
- import { Link } from '../api/link'
8
-
9
- export class ServerGroup {
10
- readonly #archetypeIndex: Record<string, Archetype> = {}
11
- readonly #mapIndex: Record<string, GameMap> = {}
12
- readonly #badgeIndex: Record<string, Badge> = {}
13
-
14
- /**
15
- * The database key for this server group.
16
- */
17
- readonly key: string
18
-
19
- /**
20
- * Name of the server group.
21
- */
22
- readonly name: string
23
-
24
- /**
25
- * Description of the server group.
26
- *
27
- * Supports {@link https://www.markdownguide.org/|Markdown} format.
28
- */
29
- readonly description?: string
30
-
31
- /**
32
- * Repository where the db content package is maintained.
33
- */
34
- readonly repository?: string
35
-
36
- /**
37
- * List of external links for this Server Group. Wiki, forums, etc.
38
- */
39
- readonly links?: Link[]
40
-
41
- /**
42
- * List of the game server names in this server group.
43
- * Torchbearer, Excelsior, etc.
44
- */
45
- readonly servers: string[]
46
-
47
- /**
48
- * List of archetypes available in this server group.
49
- */
50
- readonly archetypes: Archetype[]
51
-
52
- /**
53
- * List of game maps supported by this server group.
54
- */
55
- readonly maps: GameMap[]
56
-
57
- /**
58
- * List of badges available on this server group.
59
- */
60
- readonly badges: Badge[]
61
-
62
- /**
63
- * Change log for this data package.
64
- */
65
- readonly changelog?: Change[]
66
-
67
- constructor(data: ServerGroupData) {
68
- this.key = new Key(data.key).value
69
- this.name = data.name
70
- this.description = data.description
71
- this.repository = data.repository
72
- this.links = data.links
73
- this.servers = data.servers ?? []
74
- this.archetypes = data.archetypes?.map((data) => {
75
- if (this.#archetypeIndex[data.key] !== undefined) throw new Error(`Duplicate archetype key [${data.key}]`)
76
- const archetype = new Archetype(data)
77
- this.#archetypeIndex[archetype.key] = archetype
78
- return archetype
79
- }) ?? []
80
- this.maps = data.maps?.map((data) => {
81
- if (this.#mapIndex[data.key] !== undefined) throw new Error(`Duplicate map key [${data.key}]`)
82
- const map = new GameMap(data)
83
- this.#mapIndex[map.key] = map
84
- return map
85
- }) ?? []
86
- this.badges = data.badges?.map((data) => {
87
- if (this.#badgeIndex[data.key] !== undefined) throw new Error(`Duplicate badge key [${data.key}]`)
88
- const badge = new Badge(data)
89
- this.#badgeIndex[badge.key] = badge
90
- return badge
91
- }) ?? []
92
- this.changelog = data.changelog
93
- }
94
-
95
- getArchetype(key: string): Archetype {
96
- const result = this.#archetypeIndex[key]
97
- if (result === undefined) throw new Error(`Unknown archetype key [${key}]`)
98
- return result
99
- }
100
-
101
- getMap(key: string): GameMap {
102
- const result = this.#mapIndex[key]
103
- if (result === undefined) throw new Error(`Unknown map key [${key}]`)
104
- return result
105
- }
106
-
107
- getBadge(key: string): Badge {
108
- const result = this.#badgeIndex[key]
109
- if (result === undefined) throw new Error(`Unknown badge key [${key}]`)
110
- return result
111
- }
112
- }
@@ -1,124 +0,0 @@
1
- import { ServerGroup } from '../../main'
2
- import { serverGroupDataFixture } from '../api/server-group-data.fixture'
3
- import { badgeDataFixture } from '../api/badge-data.fixture'
4
- import { gameMapDataFixture } from '../api/game-map-data.fixture'
5
- import { archetypeDataFixture } from '../api/archetype-data.fixture'
6
-
7
- describe(ServerGroup.name, () => {
8
- describe('Constructor', () => {
9
- test(`should accept the test fixture`, () => {
10
- new ServerGroup(serverGroupDataFixture.create())
11
- })
12
- })
13
-
14
- describe('archetypes', () => {
15
- test(`should throw an error on duplicate key`, () => {
16
- const data = serverGroupDataFixture.create({
17
- archetypes: [
18
- archetypeDataFixture.create({ key: 'foo' }),
19
- archetypeDataFixture.create({ key: 'foo' }),
20
- ],
21
- })
22
- expect(() => new ServerGroup(data)).toThrow('Duplicate archetype key [foo]')
23
- })
24
-
25
- test(`should accept an empty list`, () => {
26
- const data = serverGroupDataFixture
27
- .omit('archetypes')
28
- .create()
29
- expect(() => new ServerGroup(data).archetypes).toHaveLength(0)
30
- })
31
- })
32
-
33
- describe('badges', () => {
34
- test(`should throw an error on duplicate key`, () => {
35
- const data = serverGroupDataFixture.create({
36
- badges: [
37
- badgeDataFixture.create({ key: 'foo' }),
38
- badgeDataFixture.create({ key: 'foo' }),
39
- ],
40
- })
41
- expect(() => new ServerGroup(data)).toThrow('Duplicate badge key [foo]')
42
- })
43
-
44
- test(`should accept an empty list`, () => {
45
- const data = serverGroupDataFixture
46
- .omit('badges')
47
- .create()
48
- expect(() => new ServerGroup(data).badges).toHaveLength(0)
49
- })
50
- })
51
-
52
- describe('maps', () => {
53
- test(`should throw an error on duplicate map`, () => {
54
- const data = serverGroupDataFixture.create({
55
- maps: [
56
- gameMapDataFixture.create({ key: 'foo' }),
57
- gameMapDataFixture.create({ key: 'foo' }),
58
- ],
59
- })
60
- expect(() => new ServerGroup(data)).toThrow('Duplicate map key [foo]')
61
- })
62
-
63
- test(`should accept an empty list`, () => {
64
- const data = serverGroupDataFixture
65
- .omit('maps')
66
- .create()
67
- expect(() => new ServerGroup(data).maps).toHaveLength(0)
68
- })
69
- })
70
-
71
- describe('getArchetype', () => {
72
- test(`should retrieve archetype from the index`, () => {
73
- const data = serverGroupDataFixture.create({
74
- archetypes: [archetypeDataFixture.create({ key: 'foo' })],
75
- })
76
-
77
- expect(new ServerGroup(data).getArchetype('foo')).not.toBeNull()
78
- })
79
-
80
- test(`should throw error for unknown archetype`, () => {
81
- const data = serverGroupDataFixture.create({
82
- archetypes: [],
83
- })
84
-
85
- expect(() => new ServerGroup(data).getArchetype('foo')).toThrow('Unknown archetype key [foo]')
86
- })
87
- })
88
-
89
- describe('getMap', () => {
90
- test(`should retrieve map from the index`, () => {
91
- const data = serverGroupDataFixture.create({
92
- maps: [gameMapDataFixture.create({ key: 'foo' })],
93
- })
94
-
95
- expect(new ServerGroup(data).getMap('foo')).not.toBeNull()
96
- })
97
-
98
- test(`should throw error for unknown map`, () => {
99
- const data = serverGroupDataFixture.create({
100
- maps: [],
101
- })
102
-
103
- expect(() => new ServerGroup(data).getMap('foo')).toThrow('Unknown map key [foo]')
104
- })
105
- })
106
-
107
- describe('getBadge', () => {
108
- test(`should retrieve badge from the index`, () => {
109
- const data = serverGroupDataFixture.create({
110
- badges: [badgeDataFixture.create({ key: 'foo' })],
111
- })
112
-
113
- expect(new ServerGroup(data).getBadge('foo')).not.toBeNull()
114
- })
115
-
116
- test(`should throw error for unknown badge`, () => {
117
- const data = serverGroupDataFixture.create({
118
- badges: [],
119
- })
120
-
121
- expect(() => new ServerGroup(data).getBadge('foo')).toThrow('Unknown badge key [foo]')
122
- })
123
- })
124
- })