Tests: the native suite in seconds, every test on its own, all cores
CI / test (push) Canceled after 0s
Deploy to the handset / build-and-deploy (push) Successful in 3m11s

- scripts/test-native.py (just t WORD): builds once, runs each test in its own
  process in parallel, longest first, with a timeout; CI and just test use it.
- Tests no longer search this machine's apps and icon themes.
- Compiled wasm modules are cached between test processes (CHANDRA_WASM_CACHE).
- draw_circle fills its middle with spans (same pixels); encode_png writes into
  one buffer, with a CRC table.
- The ffprobe helper always waited out its 20 s: its done file was empty.
- The fake polkit helper no longer depends on which shell sh is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-29 23:45:29 -08:00
1 parent d1d1bb398c
commit 5ca7242615
12 files changed
+397 -81

No files matched your search

+1 -1
View File
@@ -170,7 +170,7 @@ fn need_ffmpeg() -> Unit raise {
fn run_capture(cmd : String, tag : String) -> String {
let out = "/tmp/chandra-rec-" + tag + "-" + @sys.getpid().to_string() + ".txt"
let done = out + ".done"
ignore(@sys.spawn("(" + cmd + ") > '" + out + "' 2>&1; touch '" + done + "'", ""))
ignore(@sys.spawn("(" + cmd + ") > '" + out + "' 2>&1; echo > '" + done + "'", ""))
let t0 = @sys.monotonic_ms()
while @sys.read_file_bytes(done).length() == 0 && @sys.monotonic_ms() - t0 < 20000UL {
@sys.sleep_ms(20)
+1 -1
View File
@@ -534,7 +534,7 @@ test "the setuid route: the helper runs on a socket pair and speaks the protocol
@sys.write_file_bytes(
script,
@utf8.encode(
"#!/bin/sh\nread cookie\n[ \"$1\" = nandi ] && [ \"$cookie\" = c-1 ] || { echo FAILURE; exit 1; }\necho 'PAM_TEXT_INFO hello\\\\tthere'\necho 'PAM_PROMPT_ECHO_OFF Password: '\nread pw\nif [ \"$pw\" = 'pässword' ]; then echo SUCCESS; else echo FAILURE; fi\n",
"#!/bin/sh\nread cookie\n[ \"$1\" = nandi ] && [ \"$cookie\" = c-1 ] || { echo FAILURE; exit 1; }\nprintf '%s\\n' 'PAM_TEXT_INFO hello\\tthere'\necho 'PAM_PROMPT_ECHO_OFF Password: '\nread pw\nif [ \"$pw\" = 'pässword' ]; then echo SUCCESS; else echo FAILURE; fi\n",
),
),
)
+38
View File
@@ -138,3 +138,41 @@ test "span-based strokes match the per-pixel reference" {
}
}
}
///|
/// What draw_circle must look like: the coverage of every pixel on its own, no spans.
fn plain_circle(dst : Pixmap, cx : Int, cy : Int, radius : Int, c : @scene.Rgba, filled : Bool, clip : @scene.Rect?) -> Unit {
for dy = -radius - 1; dy <= radius + 1; dy = dy + 1 {
for dx = -radius - 1; dx <= radius + 1; dx = dx + 1 {
let d = (dx * dx + dy * dy).to_double().sqrt()
let mut cov = (radius.to_double() + 0.5 - d).min(1.0)
if !filled {
cov = cov.min((d - (radius.to_double() - 1.5)).min(1.0))
}
if cov > 0.02 {
let a = (c.a.to_int().to_double() * cov).round().to_int()
blend_pixel(dst, cx + dx, cy + dy, c.r.to_int(), c.g.to_int(), c.b.to_int(), a, clip)
}
}
}
}
///|
test "draw_circle's spans give the same pixels as one at a time" {
for radius in [1, 2, 3, 7, 20, 33] {
for at in [(20, 15), (-3, 4), (38, 29)] {
for alpha in [b'\xff', b'\x50'] {
for filled in [true, false] {
for clip in [None, Some(@scene.Rect::new(5, 6, 20, 13))] {
let c : @scene.Rgba = { r: b'\xd0', g: b'\x40', b: b'\x11', a: alpha }
let one = Pixmap::new(40, 30)
let two = Pixmap::new(40, 30)
draw_circle(one, at.0, at.1, radius, c, filled, clip)
plain_circle(two, at.0, at.1, radius, c, filled, clip)
assert_true(one.data == two.data)
}
}
}
}
}
}
+136 -65
View File
@@ -346,10 +346,42 @@ fn draw_circle(
let cg = color.g.to_int()
let cb = color.b.to_int()
let ca = color.a.to_int()
// What can show: the pixmap, intersected with the clip, as offsets from the centre.
let mut x0 = 0
let mut y0 = 0
let mut x1 = dst.width
let mut y1 = dst.height
match clip {
Some(c) => {
x0 = x0.max(c.x)
y0 = y0.max(c.y)
x1 = x1.min(c.x + c.w)
y1 = y1.min(c.y + c.h)
}
None => ()
}
let dx0 = (x0 - cx).max(-radius - 1)
let dx1 = (x1 - 1 - cx).min(radius + 1)
let dy0 = (y0 - cy).max(-radius - 1)
let dy1 = (y1 - 1 - cy).min(radius + 1)
// Antialiased: per-pixel coverage from the distance to the edge (1px wide ring when unfilled).
let rad = radius.to_double()
for dy = -radius - 1; dy <= radius + 1; dy = dy + 1 {
for dx = -radius - 1; dx <= radius + 1; dx = dx + 1 {
let alpha = ca.to_double()
// Within this distance every pixel is alike: fully covered, or the empty middle of a ring.
let even = if filled { rad - 0.5 } else { rad - 1.5 }
for dy = dy0; dy <= dy1; dy = dy + 1 {
let rest = even * even - (dy * dy).to_double()
let half = if even > 0.0 && rest > 0.0 { rest.sqrt().to_int() - 1 } else { -1 }
let mut dx = dx0
while dx <= dx1 {
if half >= 0 && dx >= -half && dx <= half {
let last = half.min(dx1)
if filled {
blend_span(dst, cy + dy, cx + dx, cx + last + 1, cr, cg, cb, ca)
}
dx = last + 1
continue
}
let d = (dx * dx + dy * dy).to_double().sqrt()
let outer = rad + 0.5 - d
let mut cov = if outer > 1.0 { 1.0 } else { outer }
@@ -361,8 +393,9 @@ fn draw_circle(
}
}
if cov > 0.02 {
blend_pixel(dst, cx + dx, cy + dy, cr, cg, cb, (ca.to_double() * cov).round().to_int(), clip)
blend_pixel(dst, cx + dx, cy + dy, cr, cg, cb, (alpha * cov + 0.5).to_int(), clip)
}
dx = dx + 1
}
}
}
@@ -955,18 +988,20 @@ fn push_u32_be(out : Array[Byte], v : UInt) -> Unit {
out.push((v & 0xFFU).to_byte())
}
///|
let crc_table : FixedArray[UInt] = FixedArray::makei(256, n => {
let mut c = n.reinterpret_as_uint()
for _b = 0; _b < 8; _b = _b + 1 {
c = if (c & 1U) != 0U { (c >> 1) ^ 0xEDB8_8320U } else { c >> 1 }
}
c
})
///|
fn crc32(bytes : Array[Byte], start : Int, len : Int) -> UInt {
let mut c = 0xFFFF_FFFFU
for i = 0; i < len; i = i + 1 {
c = c ^ bytes[start + i].to_uint()
for _b = 0; _b < 8; _b = _b + 1 {
if (c & 1U) != 0U {
c = (c >> 1) ^ 0xEDB8_8320U
} else {
c = c >> 1
}
}
for i = start; i < start + len; i = i + 1 {
c = (c >> 8) ^ crc_table[((c ^ bytes[i].to_uint()) & 0xFFU).reinterpret_as_int()]
}
c ^ 0xFFFF_FFFFU
}
@@ -985,65 +1020,101 @@ fn push_png_chunk(out : Array[Byte], tag : String, payload : Array[Byte]) -> Uni
push_u32_be(out, crc)
}
///| Encode Pixmap as a valid uncompressed RGBA PNG in pure MoonBit.
///| Encode Pixmap as a valid uncompressed RGBA PNG in pure MoonBit. Written straight into a buffer
/// of the final size: the tests save many of these, unoptimised.
pub fn encode_png(p : Pixmap) -> Bytes {
let out : Array[Byte] = [
b'\x89', b'\x50', b'\x4e', b'\x47', b'\x0d', b'\x0a', b'\x1a', b'\x0a',
]
let row = 1 + p.width * 4 // the filter byte (0), then the pixels
let total = row * p.height
let blocks = (total + 65534) / 65535 // stored deflate blocks
let idat = 2 + total + 5 * blocks + 4
let out : FixedArray[Byte] = FixedArray::make(8 + 25 + 12 + idat + 12, b'\x00')
let mut at = 0
fn byte(v : Int) {
out[at] = v.to_byte()
at = at + 1
}
// IHDR
let ihdr : Array[Byte] = []
push_u32_be(ihdr, p.width.reinterpret_as_uint())
push_u32_be(ihdr, p.height.reinterpret_as_uint())
ihdr.push(b'\x08') // bit depth = 8
ihdr.push(b'\x06') // color type = RGBA
ihdr.push(b'\x00') // compression = 0
ihdr.push(b'\x00') // filter = 0
ihdr.push(b'\x00') // interlace = 0
push_png_chunk(out, "IHDR", ihdr)
fn u32(v : UInt) {
byte(((v >> 24) & 0xFFU).reinterpret_as_int())
byte(((v >> 16) & 0xFFU).reinterpret_as_int())
byte(((v >> 8) & 0xFFU).reinterpret_as_int())
byte((v & 0xFFU).reinterpret_as_int())
}
// Raw scanlines: 1 filter byte (0) + width * 4 bytes per row
let row_bytes = 1 + p.width * 4
let raw : Array[Byte] = Array(capacity=row_bytes * p.height)
fn tag(s : String) {
for ch in s {
byte(ch.to_int())
}
}
fn crc(start : Int) {
let mut c = 0xFFFF_FFFFU
let table = crc_table
for i = start; i < at; i = i + 1 {
c = (c >> 8) ^ table[((c ^ out[i].to_uint()) & 0xFFU).reinterpret_as_int()]
}
u32(c ^ 0xFFFF_FFFFU)
}
for b in [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a] {
byte(b)
}
u32(13U)
let start = at
tag("IHDR")
u32(p.width.reinterpret_as_uint())
u32(p.height.reinterpret_as_uint())
for b in [8, 6, 0, 0, 0] { // 8 bits, RGBA, deflate, filter method 0, not interlaced
byte(b)
}
crc(start)
u32(idat.reinterpret_as_uint())
let start = at
tag("IDAT")
byte(0x78)
byte(0x01)
let mut s1 = 1U
let mut s2 = 0U
for y = 0; y < p.height; y = y + 1 {
raw.push(b'\x00')
s1 = (s1 + 0U) % 65521U
s2 = (s2 + s1) % 65521U
let row_off = y * p.width * 4
for i = 0; i < p.width * 4; i = i + 1 {
let b = p.data[row_off + i]
raw.push(b)
s1 = (s1 + b.to_uint()) % 65521U
s2 = (s2 + s1) % 65521U
}
}
// Zlib stream with stored (uncompressed) deflate blocks (max 65535 bytes each)
let idat : Array[Byte] = [b'\x78', b'\x01']
let mut pos = 0
let total = raw.length()
let mut left = 0 // in this block
let mut pos = 0 // in the scanlines
let mut col = 0 // in the row
let mut src = 0
let mut due = 5552 // bytes until the Adler sums could overflow
let data = p.data
while pos < total {
let rem = total - pos
let blk = if rem > 65535 { 65535 } else { rem }
let is_last = pos + blk >= total
idat.push(if is_last { b'\x01' } else { b'\x00' })
idat.push((blk & 0xFF).to_byte())
idat.push(((blk >> 8) & 0xFF).to_byte())
let nlen = blk ^ 0xFFFF
idat.push((nlen & 0xFF).to_byte())
idat.push(((nlen >> 8) & 0xFF).to_byte())
for i = 0; i < blk; i = i + 1 {
idat.push(raw[pos + i])
if left == 0 {
left = (total - pos).min(65535)
byte(if pos + left == total { 1 } else { 0 })
byte(left & 0xFF)
byte(left >> 8)
byte((left ^ 0xFFFF) & 0xFF)
byte((left ^ 0xFFFF) >> 8)
}
pos = pos + blk
let b = if col == 0 {
b'\x00'
} else {
src = src + 1
data[src - 1]
}
out[at] = b
at = at + 1
s1 = s1 + b.to_uint()
s2 = s2 + s1
due = due - 1
if due == 0 {
s1 = s1 % 65521U
s2 = s2 % 65521U
due = 5552
}
col = if col + 1 == row { 0 } else { col + 1 }
pos = pos + 1
left = left - 1
}
let adler = (s2 << 16) | s1
push_u32_be(idat, adler)
push_png_chunk(out, "IDAT", idat)
// IEND
push_png_chunk(out, "IEND", [])
Bytes::from_array(out)
u32((s2 % 65521U << 16) | (s1 % 65521U))
crc(start)
u32(0U)
let start = at
tag("IEND")
crc(start)
Bytes::from_fixedarray(out)
}
+2 -1
View File
@@ -201,7 +201,8 @@ pub fn XdgDirs::from_env() -> XdgDirs {
}
// Flatpak's exports (flatpak usually puts them in XDG_DATA_DIRS).
let data_dirs = parts(or("XDG_DATA_DIRS", "/usr/local/share:/usr/share"))
for d in [home + "/.local/share/flatpak/exports/share", "/var/lib/flatpak/exports/share"] {
let flatpak = home + "/.local/share/flatpak/exports/share"
for d in if test_run() { [flatpak] } else { [flatpak, "/var/lib/flatpak/exports/share"] } {
if !data_dirs.contains(d) {
data_dirs.push(d)
}
+11 -2
View File
@@ -2,6 +2,12 @@
/// .desktop file (`resolve_app_id`), whose Icon= key is looked up in the freedesktop icon themes
/// (a simplified icon theme spec lookup), and the file is loaded for `Image` draw ops.
///| Under the tests: only what XDG_DATA_HOME / XDG_DATA_DIRS name, not this machine's own apps and
/// icons, which are slow to search and differ between machines.
fn test_run() -> Bool {
@sys.getenv("CHANDRA_TEST_CONFIG") == "1"
}
///| Base directories icon themes live in, highest priority first.
fn icon_base_dirs() -> Array[String] {
let home = @sys.getenv("HOME")
@@ -18,7 +24,9 @@ fn icon_base_dirs() -> Array[String] {
}
}
dirs.push(home + "/.local/share/flatpak/exports/share/icons")
dirs.push("/var/lib/flatpak/exports/share/icons")
if !test_run() {
dirs.push("/var/lib/flatpak/exports/share/icons")
}
dirs
}
@@ -398,7 +406,8 @@ fn IconSearch::from_env() -> IconSearch {
///| The icon file for Icon= value `name`, or None when no theme has it.
fn IconSearch::find(self : IconSearch, name : String, svg_ok : Bool) -> String? {
for path in icon_candidates(name, svg_ok, self.themes, self.bases, ["/usr/share/pixmaps"]) {
let pixmaps = if test_run() { [] } else { ["/usr/share/pixmaps"] }
for path in icon_candidates(name, svg_ok, self.themes, self.bases, pixmaps) {
if @sys.read_file_bytes(path).length() > 0 {
return Some(path)
}
+44 -4
View File
@@ -744,11 +744,51 @@ static void note_import(Inst *in, const wasm_name_t *m) {
in->imports[have + strlen(iface) + 1] = 0;
}
}
/* Compile a core module. With CHANDRA_WASM_CACHE=DIR (scripts/test-native.py, whose every test is
* a new process) the compiled code is kept there. Compiled code is loaded unchecked: the directory
* has to be ours alone and belong to one wasmer build, so a normal session doesn't use it. */
static wasm_module_t *compile_module(const uint8_t *bytes, size_t len) {
wasm_byte_vec_t v;
wasm_module_t *mod = NULL;
const char *dir = getenv("CHANDRA_WASM_CACHE");
char path[1024], tmp[1100];
int cache = 0;
if (dir && *dir) {
uint64_t h = 14695981039346656037ull;
for (size_t i = 0; i < len; i++) h = (h ^ bytes[i]) * 1099511628211ull;
int n = snprintf(path, sizeof path, "%s/%016llx-%zu", dir, (unsigned long long)h, len);
cache = n > 0 && (size_t)n < sizeof path;
}
FILE *f = cache ? fopen(path, "rb") : NULL;
if (f) {
fseek(f, 0, SEEK_END);
long n = ftell(f);
rewind(f);
if (n > 0) {
wasm_byte_vec_new_uninitialized(&v, (size_t)n);
if (fread(v.data, 1, (size_t)n, f) == (size_t)n) mod = wasm_module_deserialize(store, &v);
wasm_byte_vec_delete(&v);
}
fclose(f);
if (mod) return mod;
}
wasm_byte_vec_new(&v, len, (const wasm_byte_t *)bytes);
mod = wasm_module_new(store, &v);
wasm_byte_vec_delete(&v);
if (!mod || !cache) return mod;
wasm_module_serialize(mod, &v);
snprintf(tmp, sizeof tmp, "%s.%d", path, (int)getpid());
f = v.size ? fopen(tmp, "wb") : NULL;
if (f) {
int ok = fwrite(v.data, 1, v.size, f) == v.size;
if (fclose(f) != 0 || !ok || rename(tmp, path) != 0) unlink(tmp);
}
wasm_byte_vec_delete(&v);
return mod;
}
static int try_instantiate(Inst *in, const uint8_t *bytes, size_t len) {
wasm_byte_vec_t bin;
wasm_byte_vec_new(&bin, len, (const wasm_byte_t *)bytes);
wasm_module_t *mod = wasm_module_new(store, &bin);
wasm_byte_vec_delete(&bin);
wasm_module_t *mod = compile_module(bytes, len);
if (!mod) return 0;
wasm_exporttype_vec_t ets;