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/controlcenter/GridLogic.js'), 'utf8'), api); const ids = plan => plan.items.map(i => i.id + '@' + i.col + ',' + i.row + ':' + i.w + 'x' + i.h).join(' '); // First fit in list order; small tiles backfill gaps beside larger ones. let plan = api.pack([{id: 'wifi', w: 2, h: 1}, {id: 'media', w: 2, h: 2}, {id: 'bt', w: 1, h: 1}, {id: 'dnd', w: 1, h: 1}, {id: 'sound', w: 4, h: 1}]); assert.equal(ids(plan), 'wifi@0,0:2x1 media@2,0:2x2 bt@0,1:1x1 dnd@1,1:1x1 sound@0,2:4x1'); assert.equal(plan.rows, 3); plan = api.pack([{id: 'a', w: 3, h: 1}, {id: 'b', w: 2, h: 1}, {id: 'c', w: 1, h: 1}]); assert.equal(ids(plan), 'a@0,0:3x1 b@0,1:2x1 c@3,0:1x1'); // Sizes clamp to 1..4 cells. assert.equal(ids(api.pack([{id: 'x', w: 9, h: 0}, {id: 'y', w: 1.4, h: 4.6}])), 'x@0,0:4x1 y@0,1:1x4'); assert.equal(api.pack([]).rows, 0); // Hit testing covers spanned cells. plan = api.pack([{id: 'wifi', w: 2, h: 1}, {id: 'media', w: 2, h: 2}, {id: 'bt', w: 1, h: 1}]); assert.equal(api.at(plan, 3, 1).id, 'media'); assert.equal(api.at(plan, 3, 2), null); // Dragging onto a tile takes its place in either direction; onto itself is a no-op. const order = ['wifi', 'media', 'bt']; assert.equal(api.reorder(order, plan, 'bt', 0, 0).join(), 'bt,wifi,media'); assert.equal(api.reorder(order, plan, 'wifi', 0, 1).join(), 'media,bt,wifi'); assert.equal(api.reorder(order, plan, 'wifi', 1, 0).join(), 'wifi,media,bt'); // Empty space puts it after the last tile before that cell. assert.equal(api.reorder(order, plan, 'wifi', 1, 1).join(), 'media,bt,wifi'); assert.equal(api.reorder(order, plan, 'bt', 0, 5).join(), 'wifi,media,bt'); assert.equal(api.reorder(order, plan, 'nope', 0, 0).join(), order.join()); // Resizing rounds to whole cells within 1..4. assert.equal(JSON.stringify(api.resize({w: 1, h: 1}, 50, 0, 80, 68, 10)), '{"w":2,"h":1}'); assert.equal(JSON.stringify(api.resize({w: 2, h: 2}, -30, 400, 80, 68, 10)), '{"w":2,"h":4}'); assert.equal(JSON.stringify(api.resize({w: 1, h: 1}, -500, -500, 80, 68, 10)), '{"w":1,"h":1}'); // Geometry: an expanded tile pushes down everything from its bottom row. plan = api.pack([{id: 'out', w: 2, h: 1}, {id: 'media', w: 2, h: 2}, {id: 'a', w: 1, h: 1}, {id: 'b', w: 4, h: 1}]); let g = api.geometry(plan, 80, 68, 10); assert.equal(JSON.stringify(g.tiles.media), '{"x":180,"y":0,"w":170,"h":146}'); assert.equal(g.height, 3 * 78 - 10); g = api.geometry(plan, 80, 68, 10, {out: 100}); assert.equal(g.tiles.out.h, 68 + 110); assert.equal(g.tiles.media.y, 0); assert.equal(g.tiles.a.y, 78 + 110); assert.equal(g.tiles.b.y, 156 + 110); assert.equal(g.height, 3 * 78 - 10 + 110); console.log('Control Center grid packing, hit testing, reordering, resizing and expansion geometry passed.');