import { Lacuna } from '../'; import { EmpireBlock } from '../types/status'; import { getLacuna } from '../__utils__/get-lacuna'; import { expectMatchesApiSchema } from '../__utils__/validate-schema'; let lacuna: Lacuna; let empire: EmpireBlock; beforeAll(async () => { lacuna = await getLacuna(); const { result } = await lacuna.empire.getStatus(); empire = result!.empire; }); test('getBuildings', async () => { const { result } = await lacuna.body.getBuildings({ body_id: empire.home_planet_id }); expect(result?.buildings).toBeDefined(); // The client splices the id-keyed map key onto each row as `id`. for (const [id, building] of Object.entries(result!.buildings)) { expect(building.id).toBe(Number(id)); } // Spec bug: the `buildings_list` component wraps the id-keyed map in a // second `buildings` level, so the spec asks for `result.buildings.buildings` // where the server sends `result.buildings`. See README "Spec Discrepancies". expectMatchesApiSchema('body', 'get_buildings', result, [ { keyword: 'required', instancePath: '/buildings' }, ]); }); test('getBuildable', async () => { // Find an empty plot - get_buildable errors on an occupied one. const { result: buildings } = await lacuna.body.getBuildings({ body_id: empire.home_planet_id, }); const occupied = new Set(Object.values(buildings!.buildings).map((b) => `${b.x},${b.y}`)); let plot = { x: 0, y: 0 }; outer: for (let x = -5; x <= 5; x++) { for (let y = -5; y <= 5; y++) { if (!occupied.has(`${x},${y}`)) { plot = { x, y }; break outer; } } } const { result } = await lacuna.body.getBuildable({ body_id: empire.home_planet_id, ...plot, tag: '', }); expect(result?.buildable).toBeDefined(); expect(result?.build_queue).toBeDefined(); const entry = Object.values(result!.buildable)[0]; expect(entry.build).toBeDefined(); expect(entry.production).toBeDefined(); // Spec bug: the spec's response requires a top-level // `max_items_in_build_queue`, but the server sends `build_queue: {max, // current}` instead. See README #21. expectMatchesApiSchema('body', 'get_buildable', result, [ { keyword: 'required', instancePath: '' }, ]); }); test('abandon / rename / setColonyNotes reject a bogus body id', async () => { for (const call of [ lacuna.body.abandon({ body_id: 123456789 }), lacuna.body.rename({ body_id: 123456789, name: 'nope' }), lacuna.body.setColonyNotes({ body_id: 123456789, options: { notes: 'nope' } }), ]) { const { error } = await call; expect(error).toBeDefined(); expect(error?.code).toEqual(expect.any(Number)); } }); test('getBuildingsResources', async () => { const { result } = await lacuna.body.getBuildingsResources({ body_id: empire.home_planet_id, }); expect(result?.buildings).toBeDefined(); const building = Object.values(result!.buildings)[0]; expect(building.resources).toBeDefined(); expect(typeof building.resources.food_hour).toBe('number'); expect(typeof building.resources.happiness_hour).toBe('number'); expectMatchesApiSchema('body', 'get_buildings_resources', result); }); test('rearrangeBuildings', async () => { console.warn = jest.fn(); const { result } = await lacuna.body.rearrangeBuildings({ body_id: empire.home_planet_id, arrangement: [], }); expect(result?.body).toBeDefined(); expect(result?.moved).toStrictEqual([]); }); test('getStatus', async () => { const { result } = await lacuna.body.getStatus({ body_id: empire.home_planet_id }); expect(result?.body).toBeDefined(); expect(result?.empire).toBeDefined(); expect(result?.server).toBeDefined(); expectMatchesApiSchema('body', 'get_status', result); }); test('getBodyStatus', async () => { // Using a known good body id from the homelab server to test this const { result, error } = await lacuna.body.getBodyStatus({ body_id: 467646 }); expect(result?.body).toBeDefined(); expectMatchesApiSchema('body', 'get_body_status', result); });