import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { runInNewContext } from "node:vm"; import { alignmentGuide, arrangeIconRow, builtInLayout, deviceMapper, itemLaunchMode, normalizeLayout, resizedIconBounds, validate } from "./src/layout.ts"; const fixtures = JSON.parse(readFileSync(new URL("../docs/layout-cases.json", import.meta.url), "utf8")); const offline = readFileSync(new URL("./canvas.html", import.meta.url), "utf8"); const offlineFunction = name => { const start = offline.indexOf("function " + name + "("); const end = offline.indexOf("\n}", start) + 2; assert.ok(start >= 0 && end > start, name); return offline.slice(start, end); }; for (const { name, accepted, layout } of fixtures) { let editorAccepted; try { editorAccepted = !validate(normalizeLayout(layout)).some(issue => issue.level === "error"); } catch { editorAccepted = false; } assert.equal(editorAccepted, accepted, "Vue editor: " + name); const offlineAccepted = runInNewContext(` let state, activePage, selectedId; function pushHistory() {} function syncControls() {} function fitCanvas() {} function render() {} ${offlineFunction("importLayout")} ${offlineFunction("validate")} try { importLayout(layout); validate().errors === 0; } catch { false; } `, { layout }); assert.equal(offlineAccepted, accepted, "Offline editor: " + name); } console.log(`${fixtures.length} shared cases passed in both editors`); const layout = builtInLayout(); const { canvas } = layout; const items = layout.pages[0].items; const tall = { w: 1080, h: 2160 }; const wide = { w: 1536, h: 2048 }; const centered = { mode: "contain", anchorY: 0.5 }; assert.deepEqual(deviceMapper(canvas, centered, items, tall).insets, { top: 120, bottom: 120, left: 0, right: 0 }); assert.deepEqual(deviceMapper(canvas, { ...centered, anchorY: 0 }, items, tall).insets, { top: 0, bottom: 240, left: 0, right: 0 }); assert.deepEqual(deviceMapper(canvas, centered, items, wide).insets, { top: 0, bottom: 0, left: 192, right: 192 }); const elastic = deviceMapper(canvas, { ...centered, mode: "elastic" }, items, tall); assert.equal(elastic.mode, "elastic"); assert.ok(elastic.gapScale > 1); assert.equal(elastic.insets, undefined); assert.equal(deviceMapper(canvas, { ...centered, mode: "elastic" }, items, wide).mode, "contain"); const stretch = deviceMapper(canvas, { ...centered, mode: "stretch" }, items, tall); assert.equal(stretch.insets, undefined); assert.ok(stretch.sy > stretch.sx); console.log("Tall and wide preview mappings passed"); assert.equal(alignmentGuide(82, 36, [100, 120], 4), 100); assert.equal(alignmentGuide(82, 36, [120, 118], 4), 118); assert.equal(alignmentGuide(82, 36, [122], 3), undefined); assert.equal(alignmentGuide(0, 36, [0], 0), 0); console.log("Drag alignment guides passed"); const icons = items.filter(item => item.type === "app_icon").slice(0, 4); const misplaced = icons.map((item, index) => ({ ...item, x: item.x + index * 7, y: item.y + index * 8 })); const arranged = arrangeIconRow(misplaced.reverse(), canvas); assert.deepEqual(arranged.map(({ item, x }) => x + item.w / 2), [216, 432, 648, 864]); assert.equal(new Set(arranged.map(({ item, y }) => y + item.h / 2)).size, 1); assert.deepEqual(arranged.map(({ item }) => item.id), icons.map(item => item.id)); assert.equal(arrangeIconRow(icons, { ...canvas, designWidth: 320 }), undefined); console.log("Four-column row arrangement passed"); const icon = icons[0]; assert.deepEqual(resizedIconBounds(icon, icon.w + 16, icon.h + 20, canvas), { x: icon.x - 8, y: icon.y - 10, w: icon.w + 16, h: icon.h + 20 }); assert.deepEqual(resizedIconBounds({ ...icon, x: 0, y: 0 }, 200, 220, canvas), { x: 0, y: 0, w: 200, h: 220 }); console.log("Icon resize centering and canvas bounds passed"); const settingsButton = normalizeLayout({ pages: [{ items: [{ id: "wifi", type: "image", x: 0, y: 200, w: 100, h: 100, payload: { imageId: "a".repeat(64), settingsAction: "ACTION_WIFI_SETTINGS", extras: [{ key: "example", type: "long", value: "9223372036854775807" }] }, }] }] }); assert.equal(validate(settingsButton).filter(issue => issue.level === "error").length, 0); settingsButton.pages[0].items[0].payload.extras[0].value = "9223372036854775808"; assert.ok(validate(settingsButton).some(issue => issue.level === "error" && issue.message.includes("Intent"))); console.log("Settings actions and typed extras passed"); const picture = settingsButton.pages[0].items[0]; picture.payload.extras = []; assert.equal(itemLaunchMode(picture), "settings"); picture.payload.launchMode = "component"; picture.payload.componentName = "com.example.app/.MainActivity"; assert.equal(itemLaunchMode(picture), "component"); assert.ok(!validate(settingsButton).some(issue => issue.level === "error")); picture.payload.componentName = "com.example.app"; assert.ok(validate(settingsButton).some(issue => issue.message.includes("ComponentName"))); console.log("Exclusive action mode and ComponentName validation passed");