import QtQuick import QtTest import "../services/Policy.js" as Policy TestCase { name: "AdaptivePolicy" function verifyExceptionThrown(fn) { let threw = false; try { fn(); } catch (e) { threw = true; } verify(threw, "Invalid preferences must be rejected"); } function defaults() { return {version: 1, mode: "auto", theme: "dark", touch: false, reducedMotion: false, outputs: {}}; } function test_modes_data() { return [ {tag: "narrow desktop", preference: "auto", width: 400, touch: false, expected: "desktop"}, {tag: "phone", preference: "auto", width: 390, touch: true, expected: "phone"}, {tag: "tablet boundary", preference: "auto", width: 600, touch: true, expected: "tablet"}, {tag: "external monitor", preference: "auto", width: 1920, touch: true, expected: "desktop"}, {tag: "manual phone on PC", preference: "phone", width: 3440, touch: false, expected: "phone"} ]; } function test_modes(data) { compare(Policy.modeFor(data.preference, data.width, data.touch), data.expected); } function test_valid() { compare(Policy.validate(defaults()).version, 1); } function test_reject_schema() { const d = defaults(); d.version = 2; verifyExceptionThrown(() => Policy.validate(d)); } function test_reject_mode() { const d = defaults(); d.mode = "watch"; verifyExceptionThrown(() => Policy.validate(d)); } function test_reject_output() { const d = defaults(); d.outputs = {"DP-1": "invalid"}; verifyExceptionThrown(() => Policy.validate(d)); } function test_reject_flags() { const d = defaults(); d.touch = "yes"; verifyExceptionThrown(() => Policy.validate(d)); } function test_launcher_rank() { const apps = [{id: "a", name: "Alpha"}, {id: "b", name: "Beta"}, {id: "c", name: "Charlie"}]; compare(Policy.rankApps(apps, ["c"], ["b"]).map(a => a.id).join(","), "c,b,a"); compare(apps[0].id, "a"); // Ranking must not mutate the model. } function test_history_bounded() { const history = Array.from({length: 50}, (_, i) => String(i)); const next = Policy.recordLaunch(history, "new"); compare(next.length, 50); compare(next[0], "new"); compare(Policy.recordLaunch(next, "1").filter(id => id === "1").length, 1); } function test_favorite_toggle() { compare(Policy.toggleFavorite(["a"], "a").length, 0); compare(Policy.toggleFavorite([], "a")[0], "a"); } function test_invalid_application_lists() { for (const invalid of [["a", "a"], [""], [42], "a"]) { const data = defaults(); data.favorites = invalid; verifyExceptionThrown(() => Policy.validate(data)); } } }