@toa.io/norm 1.0.0-alpha.3 → 1.0.0-alpha.300

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.
Files changed (98) hide show
  1. package/CHANGELOG.md +124 -0
  2. package/package.json +9 -7
  3. package/src/.component/.expand/bridge.js +2 -6
  4. package/src/.component/.expand/entity.js +2 -9
  5. package/src/.component/.expand/events.js +2 -6
  6. package/src/.component/.expand/extensions.js +5 -8
  7. package/src/.component/.expand/index.js +7 -17
  8. package/src/.component/.expand/operations.js +2 -9
  9. package/src/.component/.expand/receivers.js +4 -7
  10. package/src/.component/.expand/version.js +55 -0
  11. package/src/.component/.normalize/entity.js +7 -0
  12. package/src/.component/.normalize/events.js +16 -4
  13. package/src/.component/.normalize/index.js +4 -9
  14. package/src/.component/.normalize/operations.js +10 -6
  15. package/src/.component/.normalize/receivers.js +4 -5
  16. package/src/.component/collapse.js +60 -9
  17. package/src/.component/defaults.js +16 -25
  18. package/src/.component/dependencies.js +14 -10
  19. package/src/.component/dereference.js +20 -18
  20. package/src/.component/expand.js +6 -9
  21. package/src/.component/extensions.js +9 -10
  22. package/src/.component/index.js +10 -21
  23. package/src/.component/merge.js +25 -13
  24. package/src/.component/migrations.js +57 -0
  25. package/src/.component/normalize.js +10 -6
  26. package/src/.component/schema.yaml +139 -59
  27. package/src/.component/validate.js +37 -17
  28. package/src/.context/.dependencies/connectors.js +11 -10
  29. package/src/.context/.dependencies/describe.js +4 -5
  30. package/src/.context/.dependencies/extensions.js +93 -20
  31. package/src/.context/.dependencies/index.js +3 -9
  32. package/src/.context/.dependencies/resolve.js +44 -7
  33. package/src/.context/complete.js +1 -5
  34. package/src/.context/converge.js +31 -0
  35. package/src/.context/dependencies.js +43 -6
  36. package/src/.context/dereference.js +11 -6
  37. package/src/.context/evict.js +38 -0
  38. package/src/.context/expand.js +11 -6
  39. package/src/.context/index.js +8 -15
  40. package/src/.context/normalize.js +9 -7
  41. package/src/.context/schema.yaml +179 -11
  42. package/src/.context/validate.js +8 -14
  43. package/src/component.js +37 -18
  44. package/src/context.js +33 -14
  45. package/src/definition.js +56 -0
  46. package/src/entity.js +15 -0
  47. package/src/index.js +6 -9
  48. package/src/manifest.js +154 -0
  49. package/src/shortcuts.js +9 -14
  50. package/test/component/collapse.fixtures.js +30 -39
  51. package/test/component/collapse.test.js +90 -10
  52. package/test/component/dependencies.test.js +16 -14
  53. package/test/component/dereference.fixtures.js +15 -24
  54. package/test/component/dereference.test.js +18 -8
  55. package/test/component/dummies/extension/index.js +1 -5
  56. package/test/component/dummies/migrations/conflicting/migrations/0001-one.yaml +2 -0
  57. package/test/component/dummies/migrations/conflicting/migrations/0001-one.yml +2 -0
  58. package/test/component/dummies/migrations/malformed/migrations/0001-one.yaml +1 -0
  59. package/test/component/dummies/migrations/ordered/migrations/0001-first.yml +4 -0
  60. package/test/component/dummies/migrations/ordered/migrations/0002-second.yaml +4 -0
  61. package/test/component/dummies/migrations/ordered/migrations/0010-third.json +1 -0
  62. package/test/component/dummies/migrations/stateless/migrations/0001-one.yaml +2 -0
  63. package/test/component/expand.fixtures.js +13 -12
  64. package/test/component/expand.test.js +17 -13
  65. package/test/component/migrations.test.js +62 -0
  66. package/test/component/normalize.fixtures.js +3 -7
  67. package/test/component/normalize.test.js +86 -28
  68. package/test/component/validate.fixtures.js +15 -13
  69. package/test/component/validate.test.js +181 -107
  70. package/test/context/complete.fixtures.js +2 -8
  71. package/test/context/complete.test.js +14 -8
  72. package/test/context/converge.test.js +57 -0
  73. package/test/context/dereference.fixtures.js +3 -9
  74. package/test/context/dereference.test.js +18 -7
  75. package/test/context/evict.fixtures.js +39 -0
  76. package/test/context/evict.test.js +72 -0
  77. package/test/context/expand.fixtures.js +1 -5
  78. package/test/context/expand.test.js +36 -8
  79. package/test/context/normalize.fixtures.js +2 -7
  80. package/test/context/normalize.test.js +33 -9
  81. package/test/context/validate.fixtures.js +1 -6
  82. package/test/context/validate.test.js +143 -34
  83. package/test/definition.test.js +35 -0
  84. package/test/dummies/bare/index.js +1 -0
  85. package/test/dummies/defined/definition.js +1 -0
  86. package/test/dummies/defined/index.js +1 -0
  87. package/test/dummies/defined/package.json +1 -0
  88. package/test/manifest.test.js +126 -0
  89. package/test/shortcuts.fixtures.js +4 -9
  90. package/test/shortcuts.test.js +17 -15
  91. package/types/component.d.ts +24 -5
  92. package/types/context/declaration.d.ts +33 -3
  93. package/types/context.d.ts +19 -13
  94. package/types/entity.d.ts +7 -0
  95. package/types/index.d.ts +12 -3
  96. package/src/.component/.expand/properties.js +0 -14
  97. package/src/.context/.dependencies/load.js +0 -36
  98. package/test/context/dependencies.load.test.js +0 -24
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @param id {string}
3
+ * @return {toa.norm.component.Declaration}
4
+ */
5
+ const component = (id) => {
6
+ const [namespace, name] = id.split('.')
7
+
8
+ return {
9
+ namespace,
10
+ name,
11
+ version: '0.0.0',
12
+ locator: {
13
+ namespace,
14
+ name,
15
+ id,
16
+ label: `${namespace}-${name}`
17
+ }
18
+ }
19
+ }
20
+
21
+ export const context = {
22
+ name: 'test',
23
+ description: 'context fixture',
24
+ version: '0.0.0',
25
+ runtime: '0.0.0',
26
+ registry: 'localhost:5000',
27
+ components: [component('a.b'), component('b.a'), component('d.c')],
28
+ compositions: [
29
+ {
30
+ name: 'foo',
31
+ components: ['a.b', 'b.a'],
32
+ services: ['@toa.io/extensions.exposition', '@toa.io/extensions.realtime']
33
+ },
34
+ {
35
+ name: 'bar',
36
+ components: ['d.c']
37
+ }
38
+ ]
39
+ }
@@ -0,0 +1,72 @@
1
+ import { it, beforeEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+
4
+ import clone from 'clone-deep'
5
+
6
+ import { evict } from '../../src/.context/index.js'
7
+ import * as fixtures from './evict.fixtures.js'
8
+
9
+ /** @type {toa.norm.Context} */
10
+ let context
11
+
12
+ beforeEach(() => {
13
+ context = clone(fixtures.context)
14
+ })
15
+
16
+ it('should keep a context that evicts nothing', () => {
17
+ evict(context)
18
+
19
+ assert.deepStrictEqual(context, fixtures.context)
20
+ })
21
+
22
+ it('should mark an evicted component and keep it', () => {
23
+ context.evicted = { components: ['a.b'] }
24
+
25
+ evict(context)
26
+
27
+ assert.deepStrictEqual(
28
+ context.components.map((component) => [component.locator.id, component.evicted === true]),
29
+ [
30
+ ['a.b', true],
31
+ ['b.a', false],
32
+ ['d.c', false]
33
+ ]
34
+ )
35
+ })
36
+
37
+ it('should keep an evicted component in a composition that lists it', () => {
38
+ context.evicted = { components: ['a.b', 'd.c'] }
39
+
40
+ evict(context)
41
+
42
+ assert.deepStrictEqual(context.compositions[0].components, ['a.b', 'b.a'])
43
+ assert.deepStrictEqual(context.compositions[1].components, ['d.c'])
44
+ })
45
+
46
+ it('should refuse an unknown component', () => {
47
+ context.evicted = { components: ['d.a'] }
48
+
49
+ assert.throws(() => evict(context), {
50
+ message: "'evicted' names an unknown component 'd.a'."
51
+ })
52
+ })
53
+
54
+ it('should drop an evicted service from a composition that runs it', () => {
55
+ context.evicted = { services: ['@toa.io/extensions.exposition'] }
56
+
57
+ evict(context)
58
+
59
+ assert.deepStrictEqual(context.compositions[0].services, [
60
+ '@toa.io/extensions.realtime'
61
+ ])
62
+ })
63
+
64
+ it('should leave no services where every one is evicted', () => {
65
+ context.evicted = {
66
+ services: ['@toa.io/extensions.exposition', '@toa.io/extensions.realtime']
67
+ }
68
+
69
+ evict(context)
70
+
71
+ assert.strictEqual('services' in context.compositions[0], false)
72
+ })
@@ -1,5 +1 @@
1
- 'use strict'
2
-
3
- const { context } = require('./validate.fixtures')
4
-
5
- exports.context = context
1
+ export { context } from './validate.fixtures.js'
@@ -1,10 +1,11 @@
1
- 'use strict'
1
+ import { describe, it, beforeEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
2
3
 
3
- const clone = require('clone-deep')
4
- const { generate } = require('randomstring')
4
+ import clone from 'clone-deep'
5
+ import { generate } from 'randomstring'
5
6
 
6
- const { expand } = require('../../src/.context')
7
- const fixtures = require('./expand.fixtures')
7
+ import { expand } from '../../src/.context/index.js'
8
+ import * as fixtures from './expand.fixtures.js'
8
9
 
9
10
  /** @type {toa.norm.context.Declaration | object} */
10
11
  let context
@@ -17,7 +18,7 @@ describe('annotations', () => {
17
18
  it('should not throw without annotations', () => {
18
19
  delete context.annotations
19
20
 
20
- expect(() => expand(context)).not.toThrow()
21
+ assert.doesNotThrow(() => expand(context))
21
22
  })
22
23
 
23
24
  it('should expand known annotations', () => {
@@ -28,7 +29,7 @@ describe('annotations', () => {
28
29
 
29
30
  expand(context)
30
31
 
31
- expect(context.annotations).toStrictEqual(fixtures.context.annotations)
32
+ assert.deepStrictEqual(context.annotations, fixtures.context.annotations)
32
33
  })
33
34
 
34
35
  it('should recognize annotations', () => {
@@ -36,6 +37,33 @@ describe('annotations', () => {
36
37
 
37
38
  expand(context)
38
39
 
39
- expect(context.annotations['@toa.io/storages.mongodb']).toBeDefined()
40
+ assert.notStrictEqual(context.annotations['@toa.io/storages.mongodb'], undefined)
41
+ })
42
+ })
43
+
44
+ describe('composition services', () => {
45
+ it('should resolve shortcuts', () => {
46
+ context.compositions[0].services = ['exposition', 'configuration']
47
+
48
+ expand(context)
49
+
50
+ assert.deepStrictEqual(context.compositions[0].services, [
51
+ '@toa.io/extensions.exposition',
52
+ '@toa.io/extensions.configuration'
53
+ ])
54
+ })
55
+
56
+ it('should leave a package reference alone', () => {
57
+ context.compositions[0].services = ['@acme/extension']
58
+
59
+ expand(context)
60
+
61
+ assert.deepStrictEqual(context.compositions[0].services, ['@acme/extension'])
62
+ })
63
+
64
+ it('should not throw without compositions', () => {
65
+ delete context.compositions
66
+
67
+ assert.doesNotThrow(() => expand(context))
40
68
  })
41
69
  })
@@ -1,15 +1,12 @@
1
- 'use strict'
1
+ import { generate } from 'randomstring'
2
2
 
3
- const { generate } = require('randomstring')
4
-
5
- const context = {
3
+ export const context = {
6
4
  runtime: {
7
5
  version: '0.0.0'
8
6
  },
9
7
  name: 'test',
10
8
  description: 'context fixture',
11
9
  version: '0.0.0',
12
- packages: 'namespaces/**/*',
13
10
  registry: 'localhost:5000',
14
11
  compositions: [
15
12
  {
@@ -28,5 +25,3 @@ const context = {
28
25
  }
29
26
  }
30
27
  }
31
-
32
- exports.context = context
@@ -1,10 +1,12 @@
1
- 'use strict'
1
+ import * as definitions from '@toa.io/definitions'
2
+ import { it, beforeEach } from 'node:test'
3
+ import assert from 'node:assert/strict'
2
4
 
3
- const clone = require('clone-deep')
4
- const { generate } = require('randomstring')
5
+ import clone from 'clone-deep'
6
+ import { generate } from 'randomstring'
5
7
 
6
- const fixtures = require('./normalize.fixtures')
7
- const { normalize } = require('../../src/.context')
8
+ import * as fixtures from './normalize.fixtures.js'
9
+ import { normalize } from '../../src/.context/index.js'
8
10
 
9
11
  let context
10
12
 
@@ -17,8 +19,8 @@ it('should resolve local version', () => {
17
19
 
18
20
  normalize(context)
19
21
 
20
- expect(context.runtime).not.toEqual('.')
21
- expect(context.runtime.version).toEqual(require('@toa.io/runtime').version)
22
+ assert.notDeepStrictEqual(context.runtime, '.')
23
+ assert.deepStrictEqual(context.runtime.version, definitions.version)
22
24
  })
23
25
 
24
26
  it('should expand registry', () => {
@@ -28,7 +30,29 @@ it('should expand registry', () => {
28
30
 
29
31
  normalize(context)
30
32
 
31
- expect(context.registry).toEqual({
32
- base
33
+ assert.deepStrictEqual(context.registry, {
34
+ base,
35
+ platforms: ['linux/amd64', 'linux/arm/v7', 'linux/arm64']
33
36
  })
34
37
  })
38
+
39
+ it('should set default platforms', () => {
40
+ context.registry = { base: generate() }
41
+
42
+ normalize(context)
43
+
44
+ assert.deepStrictEqual(context.registry.platforms, [
45
+ 'linux/amd64',
46
+ 'linux/arm/v7',
47
+ 'linux/arm64'
48
+ ])
49
+ })
50
+
51
+ it('should keep platforms declared as none', () => {
52
+ // `platforms: ~` in the context: the runner's own platform, not the default list
53
+ context.registry = { base: generate(), platforms: null }
54
+
55
+ normalize(context)
56
+
57
+ assert.strictEqual(context.registry.platforms, null)
58
+ })
@@ -1,13 +1,10 @@
1
- 'use strict'
2
-
3
- const context = {
1
+ export const context = {
4
2
  runtime: {
5
3
  version: '0.0.0'
6
4
  },
7
5
  name: 'test',
8
6
  description: 'context fixture',
9
7
  version: '0.0.0',
10
- packages: 'namespaces/**/*',
11
8
  build: {
12
9
  command: 'echo test'
13
10
  },
@@ -37,5 +34,3 @@ const context = {
37
34
  }
38
35
  }
39
36
  }
40
-
41
- exports.context = context
@@ -1,14 +1,15 @@
1
- 'use strict'
1
+ import { describe, it, before, beforeEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
2
3
 
3
- const clone = require('clone-deep')
4
+ import clone from 'clone-deep'
4
5
 
5
- const { validate } = require('../../src/.context')
6
- const fixtures = require('./validate.fixtures')
6
+ import { validate } from '../../src/.context/index.js'
7
+ import * as fixtures from './validate.fixtures.js'
7
8
 
8
9
  let context
9
10
 
10
- beforeAll(() => {
11
- expect(() => validate(fixtures.context)).not.toThrow()
11
+ before(() => {
12
+ assert.doesNotThrow(() => validate(fixtures.context))
12
13
  })
13
14
 
14
15
  beforeEach(() => {
@@ -18,23 +19,32 @@ beforeEach(() => {
18
19
  describe('runtime', () => {
19
20
  it('should require', () => {
20
21
  delete context.runtime
21
- expect(() => validate(context)).toThrow(/required/)
22
+ assert.throws(
23
+ () => validate(context),
24
+ (error) => /required/.test(error.message)
25
+ )
22
26
  })
23
27
 
24
28
  it('should require registry to match uri format', () => {
25
29
  context.runtime.registry = 'not-a-uri'
26
- expect(() => validate(context)).toThrow(/must match format/)
30
+ assert.throws(
31
+ () => validate(context),
32
+ (error) => /must match format/.test(error.message)
33
+ )
27
34
 
28
35
  context.runtime.registry = 'http://localhost'
29
- expect(() => validate(context)).not.toThrow()
36
+ assert.doesNotThrow(() => validate(context))
30
37
  })
31
38
 
32
39
  it('should require proxy to match uri format', () => {
33
40
  context.runtime.proxy = 'not-a-uri'
34
- expect(() => validate(context)).toThrow(/must match format/)
41
+ assert.throws(
42
+ () => validate(context),
43
+ (error) => /must match format/.test(error.message)
44
+ )
35
45
 
36
46
  context.runtime.proxy = 'http://localhost'
37
- expect(() => validate(context)).not.toThrow()
47
+ assert.doesNotThrow(() => validate(context))
38
48
  })
39
49
  })
40
50
 
@@ -42,42 +52,141 @@ describe('registry', () => {
42
52
  it('should require', () => {
43
53
  delete context.registry
44
54
 
45
- expect(() => validate(context)).toThrow(/required property 'registry'/)
55
+ assert.throws(
56
+ () => validate(context),
57
+ (error) => /required property 'registry'/.test(error.message)
58
+ )
46
59
  })
60
+ })
61
+
62
+ it('should require name as label', () => {
63
+ delete context.name
64
+ assert.throws(
65
+ () => validate(context),
66
+ (error) => /required/.test(error.message)
67
+ )
68
+
69
+ context.name = 'foo bar'
70
+ assert.throws(
71
+ () => validate(context),
72
+ (error) => /pattern/.test(error.message)
73
+ )
74
+
75
+ context.name = 'foo-bar'
76
+ assert.doesNotThrow(() => validate(context))
77
+ })
78
+
79
+ it('should require registry url', () => {
80
+ delete context.registry
81
+ assert.throws(
82
+ () => validate(context),
83
+ (error) => /required/.test(error.message)
84
+ )
85
+ })
86
+
87
+ it('should allow mono replicas and resources', () => {
88
+ context.mono = {
89
+ replicas: 2,
90
+ resources: {
91
+ cpu: ['200m', '2'],
92
+ memory: ['256Mi', '2Gi']
93
+ }
94
+ }
95
+
96
+ assert.doesNotThrow(() => validate(context))
97
+ })
47
98
 
48
- it('should require base', () => {
49
- delete context.registry.base
99
+ describe('compositions', () => {
100
+ it('should allow services', () => {
101
+ context.compositions[0].services = ['@toa.io/extensions.exposition']
50
102
 
51
- expect(() => validate(context)).toThrow(/required property 'base'/)
103
+ assert.doesNotThrow(() => validate(context))
52
104
  })
53
105
 
54
- it('should set default platforms', () => {
55
- delete context.registry.platforms
106
+ it('should require services to be a non-empty array of strings', () => {
107
+ context.compositions[0].services = []
108
+ assert.throws(
109
+ () => validate(context),
110
+ (error) => /fewer than 1 items/.test(error.message)
111
+ )
112
+
113
+ context.compositions[0].services = 'exposition'
114
+ assert.throws(
115
+ () => validate(context),
116
+ (error) => /must be array/.test(error.message)
117
+ )
118
+ })
56
119
 
57
- validate(context)
120
+ it('should reject an unknown property', () => {
121
+ context.compositions[0].compoments = ['a.b']
58
122
 
59
- expect(context.registry.platforms).toBeInstanceOf(Array)
60
- expect(context.registry.platforms).toStrictEqual(['linux/amd64', 'linux/arm/v7', 'linux/arm64'])
123
+ assert.throws(
124
+ () => validate(context),
125
+ (error) => /must NOT have additional properties/.test(error.message)
126
+ )
61
127
  })
62
128
  })
63
129
 
64
- it('should require name as label', () => {
65
- delete context.name
66
- expect(() => validate(context)).toThrow(/required/)
130
+ describe('evicted', () => {
131
+ it('should allow components and services', () => {
132
+ context.evicted = {
133
+ components: ['a.b'],
134
+ services: ['@toa.io/extensions.exposition']
135
+ }
67
136
 
68
- context.name = 'foo bar'
69
- expect(() => validate(context)).toThrow(/pattern/)
137
+ assert.doesNotThrow(() => validate(context))
138
+ })
70
139
 
71
- context.name = 'foo-bar'
72
- expect(() => validate(context)).not.toThrow(/pattern/)
73
- })
140
+ it('should require a component to be named as one', () => {
141
+ context.evicted = { components: ['a'] }
142
+
143
+ assert.throws(
144
+ () => validate(context),
145
+ (error) => /must match pattern/.test(error.message)
146
+ )
147
+ })
74
148
 
75
- it('should require packages location', () => {
76
- delete context.packages
77
- expect(() => validate(context)).toThrow(/required/)
149
+ it('should reject an empty declaration', () => {
150
+ context.evicted = {}
151
+
152
+ assert.throws(
153
+ () => validate(context),
154
+ (error) => /fewer than 1 properties/.test(error.message)
155
+ )
156
+ })
157
+
158
+ it('should reject an unknown property', () => {
159
+ context.evicted = { compoments: ['a.b'] }
160
+
161
+ assert.throws(
162
+ () => validate(context),
163
+ (error) => /must NOT have additional properties/.test(error.message)
164
+ )
165
+ })
78
166
  })
79
167
 
80
- it('should require registry url', () => {
81
- delete context.registry
82
- expect(() => validate(context)).toThrow(/required/)
168
+ describe('addressed', () => {
169
+ it('should take the timeout an addressed call waits by default', () => {
170
+ context.addressed = { timeout: 5000 }
171
+
172
+ assert.doesNotThrow(() => validate(context))
173
+ })
174
+
175
+ it('should take a timeout of a millisecond at least', () => {
176
+ context.addressed = { timeout: 0 }
177
+
178
+ assert.throws(
179
+ () => validate(context),
180
+ (error) => /must be >= 1/.test(error.message)
181
+ )
182
+ })
183
+
184
+ it('should take nothing else', () => {
185
+ context.addressed = { interval: 5000 }
186
+
187
+ assert.throws(
188
+ () => validate(context),
189
+ (error) => /must NOT have additional properties/.test(error.message)
190
+ )
191
+ })
83
192
  })
@@ -0,0 +1,35 @@
1
+ import { it } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { join } from 'node:path'
4
+
5
+ import { definition } from '../src/definition.js'
6
+
7
+ it('reads a definition.js where a package ships one', async () => {
8
+ const { name, module } = await definition(join(import.meta.dirname, 'dummies/defined'))
9
+
10
+ assert.equal(name, 'dummies.defined')
11
+ assert.equal(module.declares, true)
12
+ assert.equal(module.runs, undefined)
13
+ })
14
+
15
+ it('reads the entry where a package ships no definition.js', async () => {
16
+ const path = join(import.meta.dirname, '../')
17
+ const { name, module } = await definition(path)
18
+
19
+ assert.equal(name, '@toa.io/norm')
20
+ assert.ok(module.context instanceof Function)
21
+ })
22
+
23
+ it('names a directory without package.json by its reference', async () => {
24
+ const path = join(import.meta.dirname, 'dummies/bare')
25
+ const { name, module } = await definition(path)
26
+
27
+ assert.equal(name, path)
28
+ assert.equal(module.declares, true)
29
+ })
30
+
31
+ it('loads a definition once', async () => {
32
+ const path = join(import.meta.dirname, 'dummies/bare')
33
+
34
+ assert.equal(await definition(path), await definition(path))
35
+ })
@@ -0,0 +1 @@
1
+ export const declares = true
@@ -0,0 +1 @@
1
+ export const declares = true
@@ -0,0 +1 @@
1
+ export const runs = true
@@ -0,0 +1 @@
1
+ { "name": "dummies.defined", "type": "module", "main": "index.js" }