Skip to content

Commit ced150d

Browse files
committed
Add unit test for ActionAffordance
1 parent 9ce4770 commit ced150d

2 files changed

Lines changed: 104 additions & 2 deletions

File tree

‎src/validation-error.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ class ValidationError extends Error {
3434
*
3535
* @param {any} error
3636
*/
37-
merge(error) {
37+
merge(error) {
3838
if (error instanceof ValidationError) {
3939
this.validationErrors.push(...error.validationErrors);
4040
} else {
4141
throw error;
4242
}
43-
}
43+
}
4444
}
4545

4646
export default ValidationError;

‎test/action-affordance.test.js‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert/strict';
3+
import ActionAffordance from '../src/action-affordance.js';
4+
import ValidationError from '../src/validation-error.js';
5+
6+
describe('ActionAffordance', () => {
7+
describe('constructor', () => {
8+
it('should parse valid action metadata', () => {
9+
const action = new ActionAffordance('toggle', {
10+
title: 'Toggle',
11+
description: 'Toggle the light',
12+
input: { type: 'boolean' },
13+
output: { type: 'boolean' },
14+
safe: true,
15+
idempotent: true,
16+
synchronous: false,
17+
});
18+
19+
assert.equal(action.name, 'toggle');
20+
assert.equal(action.title, 'Toggle');
21+
assert.deepEqual(action.input, { type: 'boolean' });
22+
assert.deepEqual(action.output, { type: 'boolean' });
23+
assert.equal(action.safe, true);
24+
assert.equal(action.idempotent, true);
25+
assert.equal(action.synchronous, false);
26+
});
27+
28+
it('should default safe and idempotent to false when omitted', () => {
29+
const action = new ActionAffordance('reboot', {});
30+
31+
assert.equal(action.safe, false);
32+
assert.equal(action.idempotent, false);
33+
assert.equal(action.synchronous, undefined);
34+
});
35+
36+
it('should reject invalid member types', () => {
37+
assert.throws(
38+
() => new ActionAffordance('badInput', { input: 'not-an-object' }),
39+
(error) => {
40+
assert.ok(error instanceof ValidationError);
41+
assert.equal(error.validationErrors[0].field, 'actions.badInput.input');
42+
assert.equal(error.validationErrors[0].description, 'input member is not valid');
43+
return true;
44+
},
45+
);
46+
47+
assert.throws(
48+
() => new ActionAffordance('badSafe', { safe: 'yes' }),
49+
(error) => {
50+
assert.ok(error instanceof ValidationError);
51+
assert.equal(error.validationErrors[0].field, 'actions.badSafe.safe');
52+
assert.equal(
53+
error.validationErrors[0].description,
54+
'safe member is not a boolean',
55+
);
56+
return true;
57+
},
58+
);
59+
});
60+
});
61+
62+
describe('getMetadata', () => {
63+
it('should return the action metadata with the generated invocation form', () => {
64+
const action = new ActionAffordance('blink', {
65+
title: 'Blink',
66+
output: { type: 'string' },
67+
safe: true,
68+
idempotent: true,
69+
synchronous: true,
70+
});
71+
72+
assert.deepEqual(action.getMetadata(), {
73+
title: 'Blink',
74+
output: { type: 'string' },
75+
safe: true,
76+
idempotent: true,
77+
synchronous: true,
78+
forms: [{ href: 'actions/blink', op: 'invokeaction' }],
79+
});
80+
});
81+
});
82+
83+
describe('invoke', () => {
84+
it('should call the registered invoke handler with the action input', async () => {
85+
const action = new ActionAffordance('setColor', {
86+
input: { type: 'string' },
87+
});
88+
action.setInvokeHandler(async (value) => ({ value }));
89+
90+
const result = await action.invoke('red');
91+
assert.deepEqual(result, { value: 'red' });
92+
});
93+
94+
it('should reject when no invoke handler has been registered', async () => {
95+
const action = new ActionAffordance('setColor', {
96+
input: { type: 'string' },
97+
});
98+
99+
await assert.rejects(() => action.invoke('red'), /InternalError/);
100+
});
101+
});
102+
});

0 commit comments

Comments
 (0)