summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 21:34:41 -0300
committerGabriel Schneider <[email protected]>2026-11-12 13:22:02 -0300
commit2348776791e9dd17afeb12a06ba42a4a71917bcf (patch)
tree780c4b12b76c8b97a28408dd40944d65f159fdc7 /test
parent590ba7b9b579b97e34b51ab7e3460ebc85713343 (diff)
downloadsketchbook-2348776791e9dd17afeb12a06ba42a4a71917bcf.tar.gz
sketchbook-2348776791e9dd17afeb12a06ba42a4a71917bcf.zip
sketchbook: a drawing layer on top, host fixes for replay and migration
Diffstat (limited to 'test')
-rw-r--r--test/fixtures/drat-meout-v1.state-fixturebin0 -> 229376 bytes
-rw-r--r--test/reload.script17
-rw-r--r--test/render.script43
-rw-r--r--test/render.zig101
-rw-r--r--test/replay.script73
-rw-r--r--test/replay.zig48
-rw-r--r--test/shapes.script30
-rw-r--r--test/shapes.zig63
8 files changed, 374 insertions, 1 deletions
diff --git a/test/fixtures/drat-meout-v1.state-fixture b/test/fixtures/drat-meout-v1.state-fixture
new file mode 100644
index 0000000..b736bb8
--- /dev/null
+++ b/test/fixtures/drat-meout-v1.state-fixture
Binary files differ
diff --git a/test/reload.script b/test/reload.script
index 863ecc6..d2b2b36 100644
--- a/test/reload.script
+++ b/test/reload.script
@@ -1,4 +1,16 @@
# The counter sketch reloads with a changed layout and keeps its values.
+# A recording made by this build plays without a warning.
+console snapshot 2
+console record 0
+click 10 10
+console stop
+console play 0
+frames 3
+expect clicks 1
+expect-no-log another build
+console stop
+console load 2
+expect clicks 0
click 10 10
click 10 10
expect clicks 2
@@ -8,7 +20,7 @@ console save
load {v2}
expect clicks 2
expect label warm
-expect streak 7
+expect streak 7 ~0.5
click 10 10
expect clicks 12
# The snapshot was migrated with the live state.
@@ -16,3 +28,6 @@ console load 3
expect clicks 2
console set streak 9
expect streak 9
+# The recording was made by the first build: playing it warns.
+console play 0
+expect-log another build
diff --git a/test/render.script b/test/render.script
new file mode 100644
index 0000000..286e1f0
--- /dev/null
+++ b/test/render.script
@@ -0,0 +1,43 @@
+# Phase 3's 2D features natively (test/render.zig); tools/render_web.mjs
+# compares the same frame in Chrome.
+frames 2
+screenshot .zig-cache/render/native.png
+frames 1
+# The target, drawn as is: its translucent circle over white, its rect.
+expect-pixel .zig-cache/render/native.png 60 40 255 127 127 255 2
+expect-pixel .zig-cache/render/native.png 30 80 0 0 255 255 0
+# Tinted half transparent (premultiplied natively: not darker at edges).
+expect-pixel .zig-cache/render/native.png 140 80 127 127 255 255 2
+expect-pixel .zig-cache/render/native.png 170 40 255 191 191 255 2
+# Turned 30 degrees about its centre at 300,60.
+expect-pixel .zig-cache/render/native.png 300 60 255 127 127 255 2
+# Added over 0x404040: red at alpha 128, green opaque, both, the rect.
+expect-pixel .zig-cache/render/native.png 40 175 192 64 64 255 2
+expect-pixel .zig-cache/render/native.png 80 175 192 255 64 255 2
+expect-pixel .zig-cache/render/native.png 160 150 89 89 255 255 2
+# One command, a colour per subpath: red, translucent green, the ring.
+expect-pixel .zig-cache/render/native.png 225 160 255 0 0 255 0
+expect-pixel .zig-cache/render/native.png 260 145 127 255 127 255 2
+expect-pixel .zig-cache/render/native.png 350 137 128 0 128 255 0
+expect-pixel .zig-cache/render/native.png 350 160 255 255 255 255 0
+# The painted target is kept: one repaint so far (the bar is 10 px).
+expect-pixel .zig-cache/render/native.png 15 117 0 0 0 255 0
+expect-pixel .zig-cache/render/native.png 25 117 255 255 255 255 0
+# `set` jumps: the target is painted again, from the new State.
+console set hue 1
+frames 5
+screenshot .zig-cache/render/set.png
+frames 1
+expect-pixel .zig-cache/render/set.png 30 80 0 160 0 255 0
+expect-pixel .zig-cache/render/set.png 25 117 0 0 0 255 0
+expect-pixel .zig-cache/render/set.png 35 117 255 255 255 255 0
+# A snapshot load jumps too; plain frames and `get` do not.
+console save 1
+console get hue
+frames 5
+console load 1
+frames 1
+screenshot .zig-cache/render/load.png
+frames 1
+expect-pixel .zig-cache/render/load.png 35 117 0 0 0 255 0
+expect-pixel .zig-cache/render/load.png 45 117 255 255 255 255 0
diff --git a/test/render.zig b/test/render.zig
new file mode 100644
index 0000000..597b252
--- /dev/null
+++ b/test/render.zig
@@ -0,0 +1,101 @@
+//! Phase 3's 2D features, for the native screenshot test
+//! (test/render.script) and the browser one (tools/render_web.mjs): an
+//! offscreen target with translucent content, drawn plain, with a
+//! translucent tint and turned; additive blending of shapes, a rect and
+//! an image over gray; shapes of several colours in one command; a soft
+//! image from `pixels`, tinted and turned; antialiased shape edges.
+//!
+//! The target is painted only when `in.jumped` (or when made): the bar
+//! under it grows by one step per repaint, and `hue` picks its colour, so
+//! a script can see that `set` repaints and plain frames do not.
+const std = @import("std");
+const sk = @import("sketch");
+const vec = sk.math;
+
+pub const name = "render";
+pub const width = 400;
+pub const height = 300;
+
+pub const State = struct {
+ hue: u8 = 0,
+};
+
+pub const Session = struct {
+ layer: sk.Image = .none,
+ soft: sk.Image = .none,
+ paints: u32 = 0,
+};
+
+const red: sk.Color = .{ .r = 255, .g = 0, .b = 0, .a = 128 };
+
+/// 32 x 32 white, alpha falling off from the middle, like NoC's texture.
+fn soft() [32 * 32]sk.Color {
+ @setEvalBranchQuota(100_000);
+ var px: [32 * 32]sk.Color = undefined;
+ for (0..32) |y| for (0..32) |x| {
+ const d = vec.distance(.{ @floatFromInt(x), @floatFromInt(y) }, .{ 15.5, 15.5 }) / 16;
+ px[y * 32 + x] = sk.Color.white.alphaFloat(1 - d);
+ };
+ return px;
+}
+const soft_pixels = soft();
+
+pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
+ f.clear(.white);
+ const fresh = ss.layer == .none;
+ if (fresh) {
+ ss.layer = f.host.newImage(100, 100);
+ ss.soft = f.host.newImage(32, 32);
+ f.pixels(ss.soft, 32, 32, std.mem.sliceAsBytes(&soft_pixels), .{});
+ }
+ if (fresh or f.in.jumped) {
+ ss.paints += 1;
+ f.target(ss.layer);
+ f.clear(sk.Color.black.alpha(0));
+ sk.draw.circle(f, .{ 50, 50 }, 40, .{ .fill = red });
+ f.fill(.{ .x = 10, .y = 60, .w = 50, .h = 30 }, if (s.hue == 0) sk.Color.hex(0x0000ff) else sk.Color.hex(0x00a000));
+ f.target(.none);
+ }
+ // The target three ways: as is, half transparent, turned.
+ f.image(ss.layer, .{ .x = 10, .y = 10, .w = 100, .h = 100 }, .{});
+ f.image(ss.layer, .{ .x = 120, .y = 10, .w = 100, .h = 100 }, .{ .tint = sk.Color.white.alpha(128) });
+ f.imageAt(ss.layer, .place(.{ 300, 60 }, std.math.pi / 6.0), .{});
+ f.fill(.{ .x = 10, .y = 115, .w = @as(f32, @floatFromInt(@min(ss.paints, 30))) * 10, .h = 4 }, .black);
+
+ // Additive blending over gray.
+ f.fill(.{ .x = 10, .y = 130, .w = 180, .h = 90 }, 0x404040);
+ f.blend(.add);
+ sk.draw.circle(f, .{ 60, 175 }, 35, .{ .fill = red });
+ sk.draw.circle(f, .{ 100, 175 }, 35, .{ .fill = sk.Color.hex(0x00c800) });
+ f.fill(.{ .x = 140, .y = 140, .w = 40, .h = 70 }, sk.Color.hex(0x2020ff).alpha(200));
+ f.image(ss.soft, .{ .x = 140, .y = 180, .w = 32, .h = 32 }, .{ .tint = sk.Color.hex(0xff64ff).alpha(200) });
+ f.blend(.alpha);
+
+ // Several colours in one shape: three circles and a ring (two subpaths).
+ var t: sk.draw.Tessellation = .empty;
+ var colors: std.ArrayList(sk.Color) = .empty;
+ const each = [_]struct { at: vec.Vec, color: sk.Color }{
+ .{ .at = .{ 230, 160 }, .color = sk.Color.hex(0xff0000) },
+ .{ .at = .{ 260, 160 }, .color = sk.Color.hex(0x00ff00).alpha(128) },
+ .{ .at = .{ 290, 160 }, .color = sk.Color.hex(0x0000ff).alpha(128) },
+ };
+ for (each) |e| {
+ t.fillCircle(f.scratch, e.at, 20, .identity) catch return;
+ while (colors.items.len < t.ends.items.len) colors.append(f.scratch, e.color) catch return;
+ }
+ t.strokeCircle(f.scratch, .{ 350, 160 }, 25, 8, .identity) catch return;
+ while (colors.items.len < t.ends.items.len) colors.append(f.scratch, sk.Color.hex(0x800080)) catch return;
+ f.shapeColors(&t, colors.items);
+
+ // The soft image: plain, tinted translucent, and turned 45 degrees.
+ f.fill(.{ .x = 210, .y = 200, .w = 180, .h = 90 }, 0x203040);
+ f.image(ss.soft, .{ .x = 220, .y = 220, .w = 32, .h = 32 }, .{});
+ f.image(ss.soft, .{ .x = 270, .y = 220, .w = 32, .h = 32 }, .{ .tint = sk.Color.hex(0xff64ff).alpha(128) });
+ f.imageAt(ss.soft, .place(.{ 346, 236 }, std.math.pi / 4.0), .{ .size = .{ 48, 48 } });
+
+ // Thin antialiased lines: a fan of 1 px strokes.
+ for (0..8) |i| {
+ const a = @as(f32, @floatFromInt(i)) * std.math.pi / 16.0;
+ sk.draw.line(f, .{ 30, 280 }, .{ 30 + 150 * @cos(a), 280 - 50 * @sin(a) }, .{ .stroke = .black });
+ }
+}
diff --git a/test/replay.script b/test/replay.script
new file mode 100644
index 0000000..1c19d3f
--- /dev/null
+++ b/test/replay.script
@@ -0,0 +1,73 @@
+# A recording replays to the state it stopped at: played, seeked, stepped,
+# resumed from the middle, and after the live pointer moved elsewhere.
+# The button is down before recording starts: the recording begins with it.
+move 100 80
+down 100 80
+console snapshot 1
+console record 0
+frames 5
+move 300 200
+frames 5
+up 300 200
+hold left_shift
+move 500 300
+frames 3
+click 400 100
+release left_shift
+click 120 140
+frames 20
+# Saving stops the recording first: slot 2 holds the state at the stop.
+console save 2
+# The button held from before the recording came up at 300,200.
+expect dots.len 3
+expect dots.buffer[0].pos[0] 300
+expect dots.buffer[1].kind big
+expect dots.buffer[1].pos[0] 400
+expect dots.buffer[2].pos[1] 140
+expect dots.buffer[2].kind small
+expect position[0] 120 ~60
+console set dots.buffer[3].pos[1] 7.5
+expect dots.buffer[3].pos[1] 7.5
+# Reading takes no frame: `print` and `console get` leave `updates` alone,
+# and vectors and structs print every value in them.
+expect updates 46
+print dots.buffer[1]
+console get dots.buffer[0].pos
+expect-log dots.buffer[0].pos = { 300, 200 }
+console get dots.buffer[1]
+expect-log dots.buffer[1] = { .pos[0] = 400, .pos[1] = 100, .kind = .big }
+expect updates 46
+
+# Played to the end, one recorded frame per frame.
+console play 0
+frames 60
+expect-snapshot 2
+# Paused at the end: the state does not move.
+frames 10
+expect-snapshot 2
+console seek end
+expect-snapshot 2
+console seek start
+console step 1000
+expect-snapshot 2
+console step -7
+console step 7
+expect-snapshot 2
+# From the middle, played on.
+console seek 200
+console pause
+frames 60
+expect-snapshot 2
+console diff 2
+expect-log same
+
+# A live move, then the same replay.
+console stop
+move 600 20
+down 600 20
+frames 3
+console play 0
+frames 60
+expect-snapshot 2
+expect-no-log another build
+console stop
diff --git a/test/replay.zig b/test/replay.zig
new file mode 100644
index 0000000..096acab
--- /dev/null
+++ b/test/replay.zig
@@ -0,0 +1,48 @@
+//! Replay fixture: state driven by the pointer, buttons, keys and time,
+//! frame by frame, so any difference between a live run and its replay
+//! (batched frames, input state, dt) shows in the bytes.
+const sk = @import("sketch");
+
+pub const name = "replay";
+pub const width = 640;
+pub const height = 360;
+
+const Dot = struct {
+ pos: @Vector(2, f32) = @splat(0),
+ kind: enum(u8) { small = 1, big = 2 } = .small,
+};
+
+pub const State = struct {
+ /// Chases the pointer a little every frame: where it ends depends on
+ /// where the pointer was at each frame, not only at the end.
+ position: @Vector(2, f32) = .{ 320, 180 },
+ velocity: @Vector(2, f32) = @splat(0),
+ /// Sum of `in.dt` over updates, and how many there were.
+ elapsed: u64 = 0,
+ updates: u32 = 0,
+ /// Frames with the left button, and with shift, held.
+ pressing: u32 = 0,
+ shifting: u32 = 0,
+ /// One per click, shaped like `sk.List`.
+ dots: struct { buffer: [8]Dot = @splat(.{}), len: u32 = 0 } = .{},
+};
+
+pub fn update(s: *State, in: *const sk.Input) void {
+ const mouse: @Vector(2, f32) = .{ in.mouse_x, in.mouse_y };
+ s.velocity = (s.velocity + (mouse - s.position) * @as(@Vector(2, f32), @splat(0.01))) * @as(@Vector(2, f32), @splat(0.9));
+ s.position += s.velocity;
+ s.elapsed += in.dt;
+ s.updates += 1;
+ if (in.buttons & 1 != 0) s.pressing += 1;
+ if (in.held(sk.Key.left_shift)) s.shifting += 1;
+ for (in.events) |e| if (e.kind == .mouse_up and s.dots.len < s.dots.buffer.len) {
+ s.dots.buffer[s.dots.len] = .{ .pos = .{ e.x, e.y }, .kind = if (in.held(sk.Key.left_shift)) .big else .small };
+ s.dots.len += 1;
+ };
+}
+
+pub fn draw(s: *const State, f: *sk.Frame) void {
+ f.clear(0x202020);
+ for (s.dots.buffer[0..s.dots.len]) |d| f.fill(.{ .x = d.pos[0] - 4, .y = d.pos[1] - 4, .w = 8, .h = 8 }, 0x80c0ff);
+ f.fill(.{ .x = s.position[0] - 10, .y = s.position[1] - 10, .w = 20, .h = 20 }, 0xff8040);
+}
diff --git a/test/shapes.script b/test/shapes.script
new file mode 100644
index 0000000..3111d5c
--- /dev/null
+++ b/test/shapes.script
@@ -0,0 +1,30 @@
+# Command.shape natively: each case at points well inside it (edges are
+# aliased here and antialiased on the web; tools/shapes_web.mjs compares
+# whole pictures). Translucent colours blend once per command.
+frames 2
+screenshot .zig-cache/shapes/native.png
+frames 1
+# Two translucent circles: once alone, twice where they overlap.
+expect-pixel .zig-cache/shapes/native.png 60 80 255 127 127 255 2
+expect-pixel .zig-cache/shapes/native.png 110 80 255 63 63 255 2
+# The concave polygon: one blend everywhere inside, nothing in its notch.
+expect-pixel .zig-cache/shapes/native.png 300 40 127 191 127 255 2
+expect-pixel .zig-cache/shapes/native.png 240 40 127 191 127 255 2
+expect-pixel .zig-cache/shapes/native.png 300 120 255 255 255 255 0
+# The acute zig-zag: one blend at its joins too (no overlap, no hole).
+expect-pixel .zig-cache/shapes/native.png 60 228 127 127 255 255 2
+expect-pixel .zig-cache/shapes/native.png 100 372 127 127 255 255 2
+expect-pixel .zig-cache/shapes/native.png 140 228 127 127 255 255 2
+expect-pixel .zig-cache/shapes/native.png 80 300 127 127 255 255 2
+# The ring and its hole.
+expect-pixel .zig-cache/shapes/native.png 300 212 175 95 175 255 2
+expect-pixel .zig-cache/shapes/native.png 300 240 255 255 255 255 0
+# A 1 px line on a pixel row: full intensity, one row.
+expect-pixel .zig-cache/shapes/native.png 300 300 0 0 0 255 0
+expect-pixel .zig-cache/shapes/native.png 300 299 255 255 255 255 0
+expect-pixel .zig-cache/shapes/native.png 300 301 255 255 255 255 0
+# Many circles, one command each and all in one.
+console circles 5000
+frames 2
+console batched 5000
+frames 2
diff --git a/test/shapes.zig b/test/shapes.zig
new file mode 100644
index 0000000..5145143
--- /dev/null
+++ b/test/shapes.zig
@@ -0,0 +1,63 @@
+//! The cases `Command.shape` must draw alike on both hosts, for the native
+//! screenshot test (test/shapes.script) and the browser one
+//! (tools/shapes_web.mjs): overlapping translucent circles, a concave
+//! polygon, an acute translucent zig-zag, a ring, a 1 px line. The console
+//! line `circles N` switches to N circles batched or not (`batched N`),
+//! for timing.
+const std = @import("std");
+const sk = @import("sketch");
+
+pub const name = "shapes";
+pub const width = 400;
+pub const height = 400;
+
+pub const State = struct {
+ circles: u32 = 0,
+ batched: bool = false,
+};
+
+pub const commands = .{ "circles", "batched" };
+
+pub fn command(s: *State, line: []const u8) !bool {
+ var words = std.mem.tokenizeScalar(u8, line, ' ');
+ const verb = words.next() orelse return false;
+ if (!std.mem.eql(u8, verb, "circles") and !std.mem.eql(u8, verb, "batched")) return false;
+ s.circles = try std.fmt.parseInt(u32, words.next() orelse "0", 10);
+ s.batched = std.mem.eql(u8, verb, "batched");
+ return true;
+}
+
+const red: sk.Color = .{ .r = 255, .g = 0, .b = 0, .a = 128 };
+const green: sk.Color = .{ .r = 0, .g = 128, .b = 0, .a = 128 };
+const blue: sk.Color = .{ .r = 0, .g = 0, .b = 255, .a = 128 };
+const purple: sk.Color = .{ .r = 128, .g = 0, .b = 128, .a = 160 };
+
+const arrow = [_]sk.math.Vec{ .{ 220, 20 }, .{ 380, 20 }, .{ 380, 180 }, .{ 300, 70 }, .{ 220, 180 } };
+const zigzag = [_]sk.math.Vec{ .{ 20, 380 }, .{ 60, 220 }, .{ 100, 380 }, .{ 140, 220 }, .{ 180, 380 } };
+
+pub fn draw(s: *const State, f: *sk.Frame) void {
+ f.clear(sk.Color.white);
+ if (s.circles > 0) return many(s, f);
+ // Two commands: the overlap blends twice, on both hosts.
+ sk.draw.circle(f, .{ 80, 80 }, 50, .{ .fill = red });
+ sk.draw.circle(f, .{ 140, 80 }, 50, .{ .fill = red });
+ sk.draw.polygon(f, &arrow, .{ .fill = green });
+ var t: sk.draw.Tessellation = .empty;
+ t.strokePolyline(f.scratch, &zigzag, 14, .identity) catch return;
+ f.shape(&t, blue);
+ sk.draw.circle(f, .{ 300, 240 }, 30, .{ .stroke = purple, .width = 8 });
+ sk.draw.line(f, .{ 220, 300.5 }, .{ 380, 300.5 }, .{ .stroke = .black });
+}
+
+/// Circles spread by a hash, in one shape each or all in one.
+fn many(s: *const State, f: *sk.Frame) void {
+ var t: sk.draw.Tessellation = .empty;
+ for (0..s.circles) |i| {
+ const h = std.hash.Wyhash.hash(0, std.mem.asBytes(&i));
+ const at: sk.math.Vec = .{ @floatFromInt(h % 400), @floatFromInt((h >> 16) % 400) };
+ if (s.batched) {
+ t.fillCircle(f.scratch, at, 4, .identity) catch return;
+ } else sk.draw.circle(f, at, 4, .{ .fill = .{ .r = @truncate(h >> 32), .g = 40, .b = 90, .a = 160 } });
+ }
+ if (s.batched) f.shape(&t, .{ .r = 200, .g = 40, .b = 90 });
+}