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 show = plan => plan.items.map(i => i.id + '@' + i.col + ',' + i.row + ':' + i.w + 'x' + i.h).join(' '); const at = (plan, col, row) => (plan.items.find(i => col >= i.col && col < i.col + i.w && row >= i.row && row < i.row + i.h) || {}).id; // Without positions: first free spot in order, so small tiles backfill gaps. let plan = api.layout([{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(show(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); assert.equal(show(api.layout([{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.layout([]).rows, 0); // Positions are kept, gaps included; unpositioned tiles fill free spots. plan = api.layout([{id: 'a', w: 1, h: 1, x: 3, y: 2}, {id: 'b', w: 2, h: 1, x: 0, y: 0}, {id: 'c', w: 1, h: 1}]); assert.equal(show(plan), 'a@3,2:1x1 b@0,0:2x1 c@2,0:1x1'); assert.equal(plan.rows, 3); assert.equal(at(plan, 0, 1), undefined); // Overlapping saved positions: the earlier entry wins, the later one moves down. assert.equal(show(api.layout([{id: 'a', w: 2, h: 2, x: 0, y: 0}, {id: 'b', w: 1, h: 1, x: 1, y: 1}])), 'a@0,0:2x2 b@1,2:1x1'); // Positions clamp inside the grid. assert.equal(show(api.layout([{id: 'a', w: 3, h: 1, x: 3, y: -2}])), 'a@1,0:3x1'); // A free spot for a new tile. plan = api.layout([{id: 'a', w: 4, h: 1, x: 0, y: 0}, {id: 'b', w: 1, h: 1, x: 0, y: 1}]); assert.equal(JSON.stringify(api.freeSpot(plan, 2, 1)), '{"id":"\\u0000new","col":1,"row":1,"w":2,"h":1}'); // Moving into empty space leaves the old spot empty. plan = api.layout([{id: 'wifi', w: 2, h: 1}, {id: 'bt', w: 1, h: 1}, {id: 'sound', w: 4, h: 1}]); let moved = api.moveTo(plan, 'bt', {col: 3, row: 3}); assert.equal(show(moved), 'wifi@0,0:2x1 bt@3,3:1x1 sound@0,1:4x1'); assert.equal(at(moved, 2, 0), undefined); assert.equal(moved.rows, 4); assert.equal(show(plan), 'wifi@0,0:2x1 bt@2,0:1x1 sound@0,1:4x1', 'moveTo leaves the input plan alone'); // Landing on tiles pushes them straight down, cascading; nothing moves up. moved = api.moveTo(plan, 'bt', {col: 0, row: 0}); assert.equal(show(moved), 'wifi@0,1:2x1 bt@0,0:1x1 sound@0,2:4x1'); // Resizing pushes the same way. moved = api.moveTo(plan, 'wifi', {w: 3, h: 2}); assert.equal(show(moved), 'wifi@0,0:3x2 bt@2,2:1x1 sound@0,3:4x1'); // A tile pushed onto the moved one goes below it instead. plan = api.layout([{id: 'tall', w: 1, h: 3, x: 0, y: 0}, {id: 'top', w: 2, h: 1, x: 1, y: 0}]); moved = api.moveTo(plan, 'top', {col: 0, row: 1}); assert.equal(show(moved), 'tall@0,2:1x3 top@0,1:2x1'); // Moves clamp to the grid. assert.equal(show(api.moveTo(plan, 'top', {col: 9, row: -4})), 'tall@0,0:1x3 top@2,0:2x1'); // Pixel corner to the nearest cell; resizing rounds to whole cells within 1..4. assert.equal(JSON.stringify(api.cellAt(130, 40, 80, 68, 10)), '{"col":1,"row":1}'); 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.layout([{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 free placement, push-down moves and resizes, free spots and expansion geometry passed.');