const fs = require('fs'); const os = require('os'); const path = require('path'); const { buildSeed, loadSeeds, findSeed, normalizeCode, SeedError } = require('./index'); // The smallest seed there is, with whatever a test adds or changes. function seedWith(extra = {}) { return { code: 'TEST01', campaign: { name: 'Test World' }, ...extra }; } describe('the seeds in /backend/seeds', () => { const results = loadSeeds(); test('every file is a valid world', () => { expect(results.length).toBeGreaterThan(0); for (const { file, error } of results) expect({ file, error }).toEqual({ file, error: undefined }); }); test('each has its own code', () => { const codes = results.map((result) => result.code); expect(new Set(codes).size).toBe(codes.length); }); }); describe('buildSeed', () => { test('fills in what is left out', () => { const seed = buildSeed(seedWith()); expect(seed.weapons).toEqual([]); expect(seed.encounters).toEqual([]); expect(seed.sharedNotes).toEqual([]); }); test('copies library entries, with changes, and drops the library-only fields', () => { const seed = buildSeed(seedWith({ weapons: ['dagger', { library: 'longsword', isStartingOption: true, minLevel: 2 }], monsters: ['goblin'] })); expect(seed.weapons[0]).toMatchObject({ name: 'Dagger', damageDice: '1d4' }); expect(seed.weapons[1]).toMatchObject({ name: 'Longsword', isStartingOption: true, minLevel: 2 }); expect(seed.weapons[0]).not.toHaveProperty('key'); expect(seed.weapons[0]).not.toHaveProperty('category'); expect(seed.monsters[0].name).toBe('Goblin'); }); test('refuses a code that is not six capital letters or digits', () => { for (const code of ['abc123', 'ABC12', 'ABC1234', 'ABC-12', undefined]) { expect(() => buildSeed(seedWith({ code }))).toThrow(SeedError); } }); test('refuses a misspelt field rather than leaving it out', () => { expect(() => buildSeed(seedWith({ npcs: [{ name: 'Mira', desciption: 'A baker' }] }))).toThrow(/desciption/); expect(() => buildSeed(seedWith({ items: [{ name: 'Rope', starting: true }] }))).toThrow(/starting/); }); test('refuses an unknown library key', () => { expect(() => buildSeed(seedWith({ spells: ['fire-bolt', 'not-a-spell'] }))).toThrow(/no "not-a-spell" in the spells library/); }); test('refuses the same name twice in a pool', () => { expect(() => buildSeed(seedWith({ weapons: ['dagger', 'dagger'] }))).toThrow(/"Dagger" is there twice/); }); test('refuses an encounter that points at something the seed does not have', () => { const encounter = (extra) => seedWith({ encounters: [{ name: 'The Bridge', ...extra }] }); expect(() => buildSeed(encounter({ location: 'Nowhere' }))).toThrow(/no location called "Nowhere"/); expect(() => buildSeed(encounter({ monsters: [['Troll', 1]] }))).toThrow(/no monster called "Troll"/); expect(() => buildSeed(encounter({ npcs: ['Mira'] }))).toThrow(/no NPC called "Mira"/); expect(() => buildSeed(encounter({ rewards: [{ item: 'Rope' }] }))).toThrow(/no item called "Rope"/); expect(() => buildSeed(encounter({ wares: [{ weapon: 'Dagger', price: 2 }] }))).toThrow(/no weapon called "Dagger"/); }); test('a reward is exactly one thing', () => { const rewards = (list) => seedWith({ items: [{ name: 'Rope' }], encounters: [{ name: 'The Bridge', rewards: list }] }); expect(buildSeed(rewards([{ gold: 5 }, { cash: 20 }, { item: 'Rope', quantity: 2 }])).encounters[0].rewards).toHaveLength(3); expect(() => buildSeed(rewards([{ gold: 5, item: 'Rope' }]))).toThrow(SeedError); expect(() => buildSeed(rewards([{ note: 'nothing' }]))).toThrow(SeedError); }); test('gear cannot be sold or handed out below its starting level', () => { const world = (ware) => seedWith({ items: [{ name: 'Old Crown', minLevel: 4 }], npcs: [{ name: 'Mira', wares: [ware] }] }); expect(buildSeed(world({ item: 'Old Crown', price: 100, level: 5 })).npcs[0].wares).toHaveLength(1); expect(() => buildSeed(world({ item: 'Old Crown', price: 100, level: 2 }))).toThrow(/starts at level 4/); }); test('an exchange rate needs both halves', () => { expect(() => buildSeed(seedWith({ npcs: [{ name: 'Mira', exchangeGold: 1 }] }))).toThrow(/exchange rate/); expect(buildSeed(seedWith({ npcs: [{ name: 'Mira', exchangeGold: 1, exchangeCash: 20 }] })).npcs[0].exchangeCash).toBe(20); }); test('an effect has to be on a number the sheet has', () => { const item = (effects) => seedWith({ items: [{ name: 'Shield', effects }] }); expect(buildSeed(item([{ stat: 'AC', amount: 2 }])).items[0].effects).toEqual([{ stat: 'AC', amount: 2 }]); expect(() => buildSeed(item([{ stat: 'LUCK', amount: 2 }]))).toThrow(SeedError); }); }); describe('the seeds folder', () => { let dir; const drop = (file, seed) => fs.writeFileSync(path.join(dir, file), `module.exports = ${JSON.stringify(seed)};`); beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'seeds-')); }); afterEach(() => { fs.rmSync(dir, { recursive: true, force: true }); }); test('a code finds its world however it is typed', () => { drop('one.js', seedWith({ code: 'AB12CD' })); expect(normalizeCode(' ab1-2cd ')).toBe('AB12CD'); expect(findSeed(' ab1-2cd ', dir).campaign.name).toBe('Test World'); expect(findSeed('ZZZZZZ', dir)).toBeNull(); expect(findSeed('', dir)).toBeNull(); }); test('a file dropped in later is picked up', () => { expect(findSeed('LATER1', dir)).toBeNull(); drop('later.js', seedWith({ code: 'LATER1' })); expect(findSeed('LATER1', dir)).not.toBeNull(); }); test('a broken file is reported and never offered, without hiding the good ones', () => { drop('bad.js', seedWith({ code: 'BAD001', encounters: [{ name: 'X', monsters: [['Nobody', 1]] }] })); drop('good.js', seedWith({ code: 'GOOD01' })); fs.writeFileSync(path.join(dir, 'syntax.js'), 'module.exports = {'); const byFile = Object.fromEntries(loadSeeds(dir).map((result) => [result.file, result])); expect(byFile['bad.js'].error).toMatch(/no monster called "Nobody"/); expect(byFile['syntax.js'].error).toBeTruthy(); expect(byFile['good.js'].error).toBeUndefined(); expect(findSeed('BAD001', dir)).toBeNull(); expect(findSeed('GOOD01', dir)).not.toBeNull(); }); test('two files cannot share a code: the first keeps it', () => { drop('a.js', seedWith({ code: 'SAME01', campaign: { name: 'First' } })); drop('b.js', seedWith({ code: 'SAME01', campaign: { name: 'Second' } })); expect(loadSeeds(dir)[1].error).toMatch(/already used by a\.js/); expect(findSeed('SAME01', dir).campaign.name).toBe('First'); }); });