-- SPDX-FileCopyrightText: © 2026 Vladimir Zorin -- SPDX-License-Identifier: LicenseRef-OWL-1.0-or-later -- Licensed under OWL v1.0+. See LICENSE. -- Exercises the DRM console backend's headless-testable parts: the pure -- integer-upscale layout math, drm_core's error paths on non-DRM inputs, -- and the gfx facade exports. The modeset/flip path needs a real -- /dev/dri/card* at a VT and is covered by manual testing; set -- LILUSH_TEST_DRM=1 to probe the real device (under a compositor this -- exercises the master-busy error path — the probe must never leave a -- modeset behind, so a successful open is closed immediately). local testimony = require("testimony") local drm_core = require("term.drm_core") local drm_display = require("term.gfx.drm_display") local gfx = require("term.gfx") local A = testimony local testify = testimony.new("== term.drm console backend ==") testify:that("layout: 1920x1080 fits 480x300 at 3x, centered", function() local k, dx, dy = drm_display.layout(1920, 1080, 480, 300) A.assert_equal(3, k) A.assert_equal(240, dx) A.assert_equal(90, dy) end) testify:that("layout: 1280x1024 fits 480x300 at 2x, centered", function() local k, dx, dy = drm_display.layout(1280, 1024, 480, 300) A.assert_equal(2, k) A.assert_equal(160, dx) A.assert_equal(212, dy) end) testify:that("layout: 640x480 fits 480x300 at 1x, centered", function() local k, dx, dy = drm_display.layout(640, 480, 480, 300) A.assert_equal(1, k) A.assert_equal(80, dx) A.assert_equal(90, dy) end) testify:that("layout: exact fit leaves no borders", function() local k, dx, dy = drm_display.layout(960, 600, 480, 300) A.assert_equal(2, k) A.assert_equal(0, dx) A.assert_equal(0, dy) end) testify:that("layout: the tighter axis picks the factor", function() -- width would allow 4x, height only 2x local k = drm_display.layout(1920, 700, 480, 300) A.assert_equal(2, k) end) testify:that("layout: mode smaller than the canvas errors", function() local k, err = drm_display.layout(320, 240, 480, 300) A.assert_nil(k) A.assert_match("smaller than", err) end) testify:that("drm_core.open: nonexistent path returns nil, err", function() local d, err = drm_core.open("/nonexistent/card0") A.assert_nil(d) A.assert_not_nil(err) end) testify:that("drm_core.open: a regular file is not a DRM device", function() -- ioctl(DRM_IOCTL_SET_MASTER) fails on regular files; must not crash local path = os.tmpname() local f = io.open(path, "w") f:write("not a DRM device") f:close() local d, err = drm_core.open(path) os.remove(path) A.assert_nil(d) A.assert_match("not a DRM device", err) end) testify:that("gfx facade exports drm_display and drm_layout, fbdev is gone", function() A.assert_equal("function", type(gfx.drm_display)) A.assert_equal("function", type(gfx.drm_layout)) -- the fbdev backend is removed, not aliased A.assert_nil(gfx.fb_display) A.assert_nil(gfx.fb_layout) -- the Kitty exports are untouched A.assert_equal("function", type(gfx.display)) A.assert_equal("function", type(gfx.loop)) A.assert_equal("function", type(gfx.canvas)) end) testify:that("drm display factory propagates device errors, console untouched", function() local c = gfx.canvas({ width = 8, height = 8 }) local d, err = gfx.drm_display(c, { path = "/nonexistent/card0" }) A.assert_nil(d) A.assert_not_nil(err) end) -- Opt-in probe against the real device: at a bare VT it takes master and -- modesets (then restores immediately); under a compositor it must fail -- with the clean master-busy error. Either verdict passes — what's tested -- is that open() never crashes and never returns a half-open handle. if os.getenv("LILUSH_TEST_DRM") then testify:that("drm_core.open: the real device opens cleanly or refuses cleanly", function() local d, err = drm_core.open() if d then local w, h = d:size() A.assert_true(w > 0 and h > 0) local info = d:info() A.assert_true(info.pitch >= w * 4) d:close() else A.assert_not_nil(err) end end) end testify:conclude()