141 lines
3.6 KiB
JavaScript
141 lines
3.6 KiB
JavaScript
const fs = require('node:fs/promises');
|
|
const os = require('node:os');
|
|
const path = require('node:path');
|
|
|
|
const {
|
|
createJsonContentStore,
|
|
} = require('../socket/contentStore.cjs');
|
|
|
|
const validCredits = {
|
|
contributors: [
|
|
{
|
|
credit: 'Implementation',
|
|
name: 'Test Contributor',
|
|
role: 'Developer',
|
|
url: null,
|
|
},
|
|
],
|
|
mainLogo: {
|
|
image: null,
|
|
subtitle: 'Test subtitle',
|
|
title: 'Test Credits',
|
|
},
|
|
specialThanks: ['Test thanks'],
|
|
technologies: [
|
|
{
|
|
name: 'Test Tech',
|
|
role: 'Testing',
|
|
url: null,
|
|
},
|
|
],
|
|
};
|
|
|
|
async function createTemporaryCreditsFile(content) {
|
|
const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'blb-credits-'));
|
|
const filePath = path.join(directory, 'credits.json');
|
|
|
|
await fs.writeFile(filePath, `${JSON.stringify(content, null, 2)}\n`, 'utf8');
|
|
|
|
return filePath;
|
|
}
|
|
|
|
async function createTemporaryJsonFile(content, prefix = 'blb-content-') {
|
|
const directory = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
|
const filePath = path.join(directory, 'content.json');
|
|
|
|
await fs.writeFile(filePath, `${JSON.stringify(content, null, 2)}\n`, 'utf8');
|
|
|
|
return filePath;
|
|
}
|
|
|
|
describe('socket content store', () => {
|
|
it('reads valid JSON content with resource and revision metadata', async () => {
|
|
const filePath = await createTemporaryCreditsFile(validCredits);
|
|
const store = createJsonContentStore({
|
|
credits: filePath,
|
|
});
|
|
|
|
await expect(store.readJsonResource('credits')).resolves.toMatchObject({
|
|
content: validCredits,
|
|
resource: 'credits',
|
|
revision: expect.any(String),
|
|
});
|
|
});
|
|
|
|
it('rejects invalid JSON content', async () => {
|
|
const filePath = await createTemporaryCreditsFile({
|
|
mainLogo: {
|
|
title: 'Invalid',
|
|
},
|
|
});
|
|
const store = createJsonContentStore({
|
|
credits: filePath,
|
|
});
|
|
|
|
await expect(store.readJsonResource('credits')).rejects.toThrow(
|
|
'Invalid toolkit content',
|
|
);
|
|
});
|
|
|
|
it('saves valid JSON content atomically and can read the changed payload', async () => {
|
|
const filePath = await createTemporaryCreditsFile(validCredits);
|
|
const store = createJsonContentStore({
|
|
credits: filePath,
|
|
});
|
|
const updatedCredits = {
|
|
...validCredits,
|
|
mainLogo: {
|
|
image: null,
|
|
subtitle: 'Changed subtitle',
|
|
title: 'Changed Credits',
|
|
},
|
|
};
|
|
|
|
await expect(
|
|
store.writeJsonResource('credits', updatedCredits),
|
|
).resolves.toMatchObject({
|
|
content: updatedCredits,
|
|
resource: 'credits',
|
|
revision: expect.any(String),
|
|
});
|
|
await expect(store.readJsonResource('credits')).resolves.toMatchObject({
|
|
content: updatedCredits,
|
|
resource: 'credits',
|
|
});
|
|
});
|
|
|
|
it('creates, updates, and deletes array-backed toolkit records', async () => {
|
|
const filePath = await createTemporaryJsonFile([]);
|
|
const store = createJsonContentStore({
|
|
petitioners: filePath,
|
|
});
|
|
const record = {
|
|
id: 'petitioner_test_001',
|
|
name: 'Test Petitioner',
|
|
};
|
|
const updatedRecord = {
|
|
...record,
|
|
name: 'Updated Petitioner',
|
|
};
|
|
|
|
await expect(
|
|
store.createJsonRecord('petitioners', record),
|
|
).resolves.toMatchObject({
|
|
content: [record],
|
|
resource: 'petitioners',
|
|
});
|
|
await expect(
|
|
store.updateJsonRecord('petitioners', updatedRecord),
|
|
).resolves.toMatchObject({
|
|
content: [updatedRecord],
|
|
resource: 'petitioners',
|
|
});
|
|
await expect(
|
|
store.deleteJsonRecord('petitioners', updatedRecord.id),
|
|
).resolves.toMatchObject({
|
|
content: [],
|
|
resource: 'petitioners',
|
|
});
|
|
});
|
|
});
|