@financial-times/n-concept-ids
Version:
A place to store concept ids as constants
75 lines (62 loc) • 2.27 kB
JavaScript
import ids from '../src/index.js';
import { readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, it, before, test } from 'node:test';
import assert from 'node:assert';
const __dirname = dirname(fileURLToPath(import.meta.url));
const { THINGS_API_KEY, FT_DEVELOPER_API_URL } = process.env;
Object.keys(ids).forEach(type => {
const members = Object.keys(ids[type]).map(name => [name, ids[type][name]]);
describe(`Type: ${type}`, () => {
if (type === 'collections') {
it('has expected collection values', () => {
assert.strictEqual(members[0][0], 'podcasts');
assert.ok(members[0][1].includes('e609bfe0-9deb-4deb-ac65-135da462ffd8'));
});
return; // avoid going into rest of loop
}
members.forEach(([name, id]) => {
describe(`ID: ${name}`, () => {
let response;
let json;
before(async () => {
if (!FT_DEVELOPER_API_URL || !THINGS_API_KEY) return;
response = await fetch(`${FT_DEVELOPER_API_URL}/things/${id}`, {
headers: {
'x-api-key': THINGS_API_KEY
}
});
if (response.ok) {
json = await response.json();
}
});
it('exists in Things API', () => {
if (!FT_DEVELOPER_API_URL || !THINGS_API_KEY) return;
assert.strictEqual(response.status, 200);
});
it('has not been given a new ID', () => {
if (!FT_DEVELOPER_API_URL || !THINGS_API_KEY) return;
assert.strictEqual(json.id.substr(json.id.lastIndexOf('/') + 1), id);
});
it(`has the direct type: ${type}`, () => {
if (!FT_DEVELOPER_API_URL || !THINGS_API_KEY) return;
assert.strictEqual(json.directType.substr(json.directType.lastIndexOf('/') + 1).toLowerCase(), type);
});
});
});
});
});
describe('Data consistency', () => {
// read the file to check for duplicate IDs with the same key name
const sourceCode = readFileSync(resolve(__dirname, '../src/index.js'), { encoding: 'utf8' });
const ids = sourceCode.match(/[a-z0-9-]{36}/g);
const checkedIds = [];
ids.forEach(id => {
if (checkedIds.includes(id)) return;
checkedIds.push(id);
test(`ID ${id} is not a duplicate`, () => {
assert.strictEqual(ids.filter(givenId => givenId === id).length, 1);
});
});
});