rob-revisited/ide/ui/js/openapi-site.test.mjs

28 lines
1.8 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { isStaleLocal, pickSource, sourceOptions, siteOf, exportTarget } from './openapi-site.js';
const site = { url: 'http://localhost:8301', urls: ['http://localhost:8301/api/docs.jsonopenapi', 'http://localhost:8301/api/docs.json'] };
test('a remembered placeholder address is stale once the site answers elsewhere', () => {
assert.equal(isStaleLocal('http://localhost/openapi.json', site), true);
assert.equal(isStaleLocal('http://localhost:8000/api/docs.json', site), true);
assert.equal(isStaleLocal('http://localhost:8301/api/docs.json', site), false);
assert.equal(isStaleLocal('https://api.example.com/openapi.json', site), false);
assert.equal(isStaleLocal('docs/openapi.json', site), false);
});
test('the first source: the remembered one, else a file, else the site\'s address', () => {
assert.equal(pickSource({ files: [], urls: [] }, site, 'http://localhost/openapi.json'), 'http://localhost:8301/api/docs.jsonopenapi');
assert.equal(pickSource({ files: ['docs/openapi.json'] }, site, ''), 'docs/openapi.json');
assert.equal(pickSource({ files: ['a.json'] }, site, 'https://api.example.com/x.json'), 'https://api.example.com/x.json');
assert.equal(pickSource({ files: [], urls: ['http://localhost:8000/openapi.json'] }, null, ''), 'http://localhost:8000/openapi.json');
});
test('the options and the helpers', () => {
assert.deepEqual(sourceOptions({ files: ['var/openapi.json'] }, site), ['var/openapi.json', ...site.urls]);
assert.equal(siteOf('http://localhost:8301/api/docs.json'), 'http://localhost:8301');
assert.equal(siteOf('var/openapi.json'), '');
assert.equal(exportTarget(''), 'var/openapi.json');
assert.equal(exportTarget(' build/api.json '), 'build/api.json');
});