Files
blb-throne-of-the-bone-king/__tests__/socketContentStore.test.js
T

98 lines
2.4 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;
}
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',
});
});
});