import { describe, it, beforeEach } from 'node:test' import assert from 'node:assert/strict' import clone from 'clone-deep' import { validate } from '../../src/.component/index.js' import * as fixtures from './validate.fixtures.js' let manifest beforeEach(() => { manifest = clone(fixtures.ok) }) it('should be ok', async () => { await assert.doesNotReject(validate(manifest)) }) it('should provide error', async () => { manifest.foo = 'bar' await assert.rejects(validate(manifest)) }) it('should not have additional properties', async () => { manifest.foo = 'bar' await assert.rejects(validate(manifest)) }) describe('namespace', () => { it('should match token pattern', async () => { manifest.namespace = '1' await assert.rejects(validate(manifest), (error) => /must match pattern/.test(error.message) ) manifest.namespace = 'foo_' await assert.rejects(validate(manifest), (error) => /must match pattern/.test(error.message) ) manifest.namespace = 'foo_bar' await assert.rejects(validate(manifest), (error) => /must match pattern/.test(error.message) ) manifest.namespace = 'foo-' await assert.rejects(validate(manifest), (error) => /must match pattern/.test(error.message) ) manifest.namespace = 'foo-bar' await assert.rejects(validate(manifest), (error) => /must match pattern/.test(error.message) ) manifest.namespace = 'FooBar12' await assert.doesNotReject(validate(manifest)) }) it("should forbid 'system' namespace", async () => { manifest.namespace = 'system' await assert.rejects(validate(manifest), (error) => /must NOT be valid/.test(error.message) ) }) }) describe('name', () => { it('should be optional', async () => { delete manifest.name await assert.doesNotReject(validate(manifest)) }) }) describe('entity', () => { it('should be optional', async () => { delete manifest.entity await assert.doesNotReject(validate(manifest)) }) it('should be object', async () => { manifest.entity = 'foo' await assert.rejects(validate(manifest), (error) => /must be object/.test(error.message) ) }) it('should not have additional properties', async () => { manifest.entity.foo = 'bar' await assert.rejects(validate(manifest)) }) describe('properties', () => { it('should be required', async () => { delete manifest.entity.properties await assert.rejects(validate(manifest)) }) it('should be JSON schemas', async () => { manifest.entity.properties.foo = 'bar' await assert.rejects(validate(manifest)) }) it('should have names matching token pattern', async () => { manifest.entity.properties._foo = { type: 'string' } await assert.rejects(validate(manifest), (error) => /pattern/.test(error.message)) }) it('should allow own id', async () => { manifest.entity.properties.id = { type: 'string', pattern: '^[a-fA-F0-9]+$' } await assert.doesNotReject(validate(manifest)) }) it('should not declare TRAILERS', async () => { manifest.entity.properties.TRAILERS = { type: 'object' } await assert.rejects(validate(manifest), (error) => /System property 'TRAILERS' cannot be declared/.test(error.message) ) }) it('should not declare DISCARD', async () => { manifest.entity.properties.DISCARD = { type: 'boolean' } await assert.rejects(validate(manifest), (error) => /System property 'DISCARD' cannot be declared/.test(error.message) ) }) }) describe('required', () => { it('should name declared properties', async () => { manifest.entity.required = ['name'] await assert.doesNotReject(validate(manifest)) manifest.entity.required = ['nope'] await assert.rejects(validate(manifest), (error) => /requires property 'nope'/.test(error.message) ) }) }) describe('blank', () => { it('should name declared properties', async () => { manifest.entity.blank = { name: 'whatever' } await assert.doesNotReject(validate(manifest)) manifest.entity.blank = { nope: 1 } await assert.rejects(validate(manifest), (error) => /names property 'nope'/.test(error.message) ) }) it('should not name a system property', async () => { manifest.entity.blank = { VERSION: 1 } await assert.rejects(validate(manifest), (error) => /must NOT be valid/.test(error.message) ) }) it('should not name TRAILERS', async () => { manifest.entity.blank = { TRAILERS: {} } await assert.rejects(validate(manifest), (error) => /must NOT be valid/.test(error.message) ) }) it('should not name DISCARD', async () => { manifest.entity.blank = { DISCARD: true } await assert.rejects(validate(manifest), (error) => /must NOT be valid/.test(error.message) ) }) }) }) describe('bindings', () => { it('should be array of unique strings', async () => { manifest.bindings = 'oops' await assert.rejects(validate(manifest), (error) => /must be array/.test(error.message) ) manifest.bindings = ['oops', 'oops'] await assert.rejects(validate(manifest), (error) => /duplicate items/.test(error.message) ) manifest.bindings = ['oops', {}] await assert.rejects(validate(manifest), (error) => /must be string/.test(error.message) ) }) it('should forbid explicit loop', async () => { manifest.bindings = ['@toa.io/bindings.loop'] await assert.rejects(validate(manifest), (error) => /must NOT be valid/.test(error.message) ) }) }) describe('operations', () => { it('should be object', async () => { manifest.operations.get = 'bar' await assert.rejects(validate(manifest), (error) => /must be object/.test(error.message) ) }) it('should not have additional properties', async () => { manifest.operations.get.foo = 'bar' await assert.rejects(validate(manifest)) }) it('should have type (transition or observation)', async () => { delete manifest.operations.get.type await assert.rejects(validate(manifest)) manifest.operations.get.type = 'foo' await assert.rejects(validate(manifest), (error) => /one of the allowed values/.test(error.message) ) }) it('should forbid explicit loop', async () => { manifest.operations.get.bindings = ['@toa.io/bindings.loop'] await assert.rejects(validate(manifest), (error) => /must NOT be valid/.test(error.message) ) }) it('should forbid query: false for observations', async () => { manifest.operations.get.query = false await assert.rejects(validate(manifest), (error) => /must NOT be valid/.test(error.message) ) }) for (const [_, operation] of [['computation', 'compute']]) it(`should set query: false for ${_}`, async () => { validate(manifest) assert.strictEqual(manifest.operations[operation].query, false) }) describe('scope', () => { it('should have scope', async () => { delete manifest.operations.get.scope await assert.rejects(validate(manifest), (error) => /required property/.test(error.message) ) }) it('should allow only entity or set for observations', async () => { manifest.operations.get.scope = 'changeset' await assert.rejects(validate(manifest), (error) => /allowed values/.test(error.message) ) }) it('should allow only entity for transitions', async () => { manifest.operations.add.scope = 'changeset' await assert.rejects(validate(manifest), (error) => /allowed values/.test(error.message) ) manifest.operations.add.scope = 'set' await assert.rejects(validate(manifest), (error) => /allowed values/.test(error.message) ) }) it('should refuse the names the scopes had before `entry` and `entries`', async () => { for (const scope of ['object', 'objects']) { manifest.operations.get.scope = scope await assert.rejects(validate(manifest), (error) => /allowed values/.test(error.message) ) } }) it('should allow only changeset for assignments', async () => { manifest.operations.set.scope = 'changeset' await assert.doesNotReject(validate(manifest)) manifest.operations.set.scope = 'set' await assert.rejects(validate(manifest), (error) => /allowed values/.test(error.message) ) }) }) describe('once', () => { it('should be true, false or a number of seconds', async () => { for (const once of [true, false, 600, 86400]) { manifest.operations.add.once = once await assert.doesNotReject(validate(manifest), `once: ${once}`) } }) it('should stay true rather than be coerced into a number', async () => { manifest.operations.add.once = true await validate(manifest) assert.strictEqual(manifest.operations.add.once, true) }) it('should not be a window shorter than ten minutes', async () => { manifest.operations.add.once = 599 await assert.rejects(validate(manifest), (error) => /must be >= 600/.test(error.message) ) }) it('should not be a fraction of a second', async () => { manifest.operations.add.once = 600.5 await assert.rejects(validate(manifest)) }) it('should throw for an operation that takes none', async () => { manifest.operations.get.once = 600 await assert.rejects(validate(manifest)) }) }) describe('concurrency', () => { it('should be required for transitions', async () => { delete manifest.operations.add.concurrency await assert.rejects(validate(manifest), (error) => /required property/.test(error.message) ) }) it('should throw for observations, assignments', async () => { manifest.operations.get.concurrency = 'none' await assert.rejects(validate(manifest)) delete manifest.operations.get.concurrency manifest.operations.set.concurrency = 'none' await assert.rejects(validate(manifest)) }) }) describe('input, output', () => { it('should be schema', async () => { manifest.operations.get.input = { properties: { foo: 'bar' } } await assert.rejects(validate(manifest)) delete manifest.operations.get.input manifest.operations.get.output = { properties: { foo: 'bar' } } await assert.rejects(validate(manifest)) }) }) }) describe('receivers', () => { it('should throw if transition points to undefined operation', async () => { manifest.receivers['foo.bar.happened'].operation = 'notExists' await assert.rejects(validate(manifest), (error) => /refers to undefined operation/.test(error.message) ) }) it('should throw if transition points to observation', async () => { manifest.receivers['foo.bar.happened'].operation = 'get' await assert.rejects(validate(manifest), (error) => /of the allowed types/.test(error.message) ) }) it('should throw if source has a name `context`', async () => { manifest.receivers['foo.bar.happened'].source = 'context' await assert.rejects(validate(manifest), (error) => /must NOT be valid/.test(error.message) ) }) }) describe('stateful', () => { it('should be allowed on an operation', async () => { const [operation] = Object.values(manifest.operations) operation.stateful = true await assert.doesNotReject(validate(manifest)) }) it('should be a boolean', async () => { const [operation] = Object.values(manifest.operations) operation.stateful = 'yes' await assert.rejects(validate(manifest), (error) => /must be boolean/.test(error.message) ) }) }) describe('unchained', () => { it('should be allowed on an operation', async () => { const [operation] = Object.values(manifest.operations) operation.unchained = true await assert.doesNotReject(validate(manifest)) }) it('should be a boolean', async () => { const [operation] = Object.values(manifest.operations) operation.unchained = 'yes' await assert.rejects(validate(manifest), (error) => /must be boolean/.test(error.message) ) }) }) describe('stream', () => { beforeEach(() => { manifest.operations.read = { type: 'computation', scope: 'none', query: false, bridge: 'whatever', bindings: ['@toa.io/bindings.amqp'], stream: 'content' } }) it('should reject an operation whose bindings carry no stream', async () => { await assert.rejects(validate(manifest), (error) => /takes a stream, which none of its bindings carries/.test(error.message) ) }) it('should accept an operation served where it is composed', async () => { manifest.operations.read.bindings = [] await assert.doesNotReject(validate(manifest)) }) it('should accept an operation bound to a binding that carries a stream', async () => { manifest.operations.read.bindings = ['@toa.io/bindings.http'] await assert.doesNotReject(validate(manifest)) }) it('should reject a transition that takes a stream', async () => { manifest.operations.add.stream = 'content' await assert.rejects(validate(manifest), (error) => /must be equal to one of the allowed values/.test(error.message) ) }) })