verae-activate/test/add_numbers.test.js

111 lines
4 KiB
JavaScript

/**
* Activate-now tests: Add Numbers local + hosted paths.
*/
const { describe, it } = require('node:test');
const assert = require('node:assert/strict');
const App = require('../index');
const addNumbers = require('../creates/add_numbers');
const authentication = require('../authentication');
describe('verae-activate app', () => {
it('exports Add Numbers create and custom auth', () => {
assert.equal(authentication.type, 'custom');
assert.ok(App.creates.add_numbers);
assert.ok(App.creates.echo);
assert.ok(App.creates.sha256_hash);
assert.ok(App.creates.create_timestamp);
assert.equal(addNumbers.key, 'add_numbers');
assert.ok(addNumbers.operation.inputFields.some((f) => f.key === 'number1'));
assert.ok(addNumbers.operation.inputFields.some((f) => f.key === 'number2'));
});
it('adds two numbers locally with no API', async () => {
const z = { request: async () => { throw new Error('should not HTTP'); } };
const result = await addNumbers.operation.perform(z, {
inputData: { number1: 2, number2: 3 },
authData: {},
});
assert.deepEqual(result, { number1: 2, number2: 3, sum: 5, mode: 'local' });
});
it('coerces numeric strings', async () => {
const result = await addNumbers.operation.perform({}, {
inputData: { number1: '10.5', number2: '-0.5' },
authData: {},
});
assert.equal(result.sum, 10);
assert.equal(result.mode, 'local');
});
it('rejects non-finite number1', async () => {
await assert.rejects(
() => addNumbers.operation.perform({}, { inputData: { number1: 'x', number2: 1 }, authData: {} }),
/number1 must be a finite number/,
);
});
it('POSTs to hosted /v1/add when api_base and api_key are set', async () => {
const calls = [];
const z = {
request: async (opts) => {
calls.push(opts);
return { data: { number1: 1, number2: 4, sum: 5, quote: { totalCents: 0 } } };
},
};
const result = await addNumbers.operation.perform(z, {
inputData: { number1: 1, number2: 4 },
authData: { api_base: 'https://edge.example.com/', api_key: 'key-ada' },
});
assert.equal(calls[0].method, 'POST');
assert.equal(calls[0].url, 'https://edge.example.com/v1/add');
assert.equal(calls[0].headers['x-api-key'], 'key-ada');
assert.deepEqual(calls[0].body, { number1: 1, number2: 4 });
assert.equal(result.mode, 'hosted');
assert.equal(result.sum, 5);
});
it('create timestamp local mock without API', async () => {
const ts = require('../creates/timestamp');
const out = await ts.operation.perform(
{ request: async () => { throw new Error('no http'); } },
{ inputData: { data: 'x' }, authData: {} },
);
assert.equal(out.mode, 'local-mock');
assert.ok(out.jobId);
});
it('sha256 hashes hello', async () => {
const sha = require('../creates/sha256');
const out = await sha.operation.perform({}, { inputData: { text: 'hello' }, authData: {} });
assert.equal(out.sha256, '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824');
assert.equal(out.mode, 'local');
});
it('echo returns text length', async () => {
const echo = require('../creates/echo');
const out = await echo.operation.perform({}, { inputData: { text: 'ab' }, authData: {} });
assert.equal(out.length, 2);
assert.equal(out.mode, 'local');
});
it('auth test is local when fields empty', async () => {
const out = await authentication.test({ request: async () => ({ data: {} }) }, { authData: {} });
assert.equal(out.ok, true);
assert.equal(out.mode, 'local');
});
it('auth test hits /v1/status when hosted', async () => {
const z = {
request: async (opts) => {
assert.equal(opts.url, 'https://edge.example.com/v1/status');
return { data: { status: 'ok' } };
},
};
const out = await authentication.test(z, {
authData: { api_base: 'https://edge.example.com', api_key: 'k' },
});
assert.equal(out.mode, 'hosted');
});
});