const assert = require('assert'), fs = require('fs'), path = require('path'), vm = require('vm'); const api = vm.createContext({}); vm.runInContext(fs.readFileSync(path.join(__dirname, '../quickshell/services/WidgetLogic.js'), 'utf8'), api); const bounds = {w: 1000, h: 800}; // Item edits are immutable. const items = [{id: 'a', x: 0}, {id: 'b', x: 5}]; assert.deepEqual(api.updateItem(items, 'b', {x: 9}).map(i => i.x), [0, 9]); assert.equal(items[1].x, 5); assert.deepEqual(api.removeItem(items, 'a').map(i => i.id), ['b']); const id = api.newId(items); assert(/^w-[a-z0-9]+-[a-z0-9]+$/.test(id) && !items.some(i => i.id === id)); // Snaps to the screen edge and another widget's edge, with guides. let s = api.snapMove({x: 6, y: 300, w: 100, h: 100}, [], bounds, 0, 10); assert.equal(s.x, 0); assert.deepEqual(s.guides.x, [0]); s = api.snapMove({x: 207, y: 50, w: 100, h: 100}, [{x: 100, y: 400, w: 100, h: 100}], bounds, 0, 10); assert.equal(s.x, 200); assert.equal(s.y, 50); // Centres snap to the screen centre. s = api.snapMove({x: 446, y: 10, w: 100, h: 50}, [], bounds, 0, 10); assert.equal(s.x, 450); assert.deepEqual(s.guides.x, [500]); // Grid wins over guides; rects stay on screen. s = api.snapMove({x: 33, y: 49, w: 100, h: 100}, [], bounds, 20, 10); assert.deepEqual([s.x, s.y], [40, 40]); s = api.snapMove({x: 980, y: -30, w: 100, h: 100}, [], bounds, 0, 0); assert.deepEqual([s.x, s.y], [900, 0]); // Resizing honours minimums from either edge and the output bounds. const limits = {minW: 120, minH: 80, maxW: 600, maxH: 400}; let r = api.resizeRect({x: 100, y: 100, w: 200, h: 200}, -150, 0, {right: true}, limits, bounds, 0); assert.deepEqual([r.x, r.w], [100, 120]); r = api.resizeRect({x: 100, y: 100, w: 200, h: 200}, 150, 0, {left: true}, limits, bounds, 0); assert.deepEqual([r.x, r.w], [180, 120]); r = api.resizeRect({x: 100, y: 100, w: 200, h: 200}, 0, 500, {bottom: true}, limits, bounds, 0); assert.equal(r.h, 400); r = api.resizeRect({x: 900, y: 100, w: 90, h: 100}, 500, 0, {right: true}, {minW: 40, minH: 40, maxW: 4000, maxH: 4000}, bounds, 0); assert.equal(r.x + r.w, 1000); r = api.resizeRect({x: 100, y: 100, w: 200, h: 200}, 13, 27, {right: true, bottom: true}, limits, bounds, 20); assert.deepEqual([r.w, r.h], [220, 220]); console.log('Desktop widget snapping, guides, grid, resize limits and immutable edits passed.');