Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub fn build(b: *std.Build) void {
@import("zig/build/glp_serve.zig").tests(b, test_step);
@import("zig/build/steer.zig").tests(b, test_step);
@import("zig/build/dsv41_metal.zig").hostSteps(b);
@import("zig/build/step.zig").steps(b, test_step);
if (b.graph.host.result.os.tag == .macos) {
const step5_mods = metalEngineModules(b, b.graph.host, .Debug, draft_ids);
const empty = b.addWriteFiles().add("step5-compile-only.metallib", "");
Expand Down
26 changes: 26 additions & 0 deletions zig/build/step.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! The leader step header (zig/src/fabric/step): a std-only module, its host tests, and compile checks for aarch64.
const std = @import("std");

pub fn module(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module {
return b.createModule(.{ .root_source_file = b.path("zig/src/fabric/step/step.zig"), .target = target, .optimize = optimize });
}

/// The tests' root: the same files, with libc for the golden fixtures' rewrite only (the module itself has none).
fn tests(b: *std.Build, target: std.Build.ResolvedTarget) *std.Build.Module {
return b.createModule(.{ .root_source_file = b.path("zig/src/fabric/step/step.zig"), .target = target, .optimize = .Debug, .link_libc = true });
}

pub fn steps(b: *std.Build, all: *std.Build.Step) void {
const host = b.graph.host;
const run = b.addRunArtifact(b.addTest(.{ .root_module = tests(b, host) }));
run.has_side_effects = true; // TF_STEP_FIXTURES_OUT rewrites files, so a cached result must not skip it
const step = b.step("test-fabric-step", "Step header: encode, decode, named refusals, golden fixtures");
step.dependOn(&run.step);
all.dependOn(step);
const compile = b.step("test-fabric-step-compile", "Compile the step header's tests for Linux and macOS on aarch64");
for ([_]std.Target.Os.Tag{ .linux, .macos }) |os| {
const target = b.resolveTargetQuery(.{ .cpu_arch = .aarch64, .os_tag = os });
compile.dependOn(&b.addTest(.{ .root_module = tests(b, target) }).step);
}
all.dependOn(compile);
}
189 changes: 189 additions & 0 deletions zig/src/fabric/step/fixtures.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
//! Golden bytes of step messages, good and bad, for every user of the header to test against (fixtures/).
const std = @import("std");
const header = @import("header.zig");
const section = @import("section.zig");
const message = @import("message.zig");

/// What every fixture is judged against: a receiver's limits and the round and group it expects.
pub const limits: message.Limits = .{ .bytes = 512, .streams = 4, .rows = 8, .context = 64 };
pub const context: header.Context = .{ .round = 3, .group = 0x0A0B_0C0D };

fn head(kind: header.Kind) header.Header {
return .{ .kind = kind, .round = context.round, .group = context.group };
}

/// The ring fixtures' round (fabric/stage/fixtures.zig): one stream of each op but cancel, 7 rows, a cut at row 4.
pub const streams = [_]section.Stream{
.{ .id = 3, .op = .open, .first = 0, .rows = 4 },
.{ .id = 7, .op = .@"continue", .first = 10, .rows = 1, .accepted = 2, .generation = 1 },
.{ .id = 9, .op = .@"resume", .first = 32, .rows = 2, .resume_at = 32, .generation = 2 },
.{ .id = 12, .op = .close, .first = 20, .rows = 0, .generation = 1 },
};
pub const cuts = [_]u32{4};
const tokens = [_]u32{ 10, 11, 12, 13, 14, 15, 16, 90, 91 }; // the round's 7 ids, then a 2-id tail

const round: message.Message = .{ .header = head(.round), .streams = &streams, .cuts = &cuts };
const round_tokens: message.Message = .{ .header = head(.round), .streams = &streams, .tokens = &tokens, .cuts = &cuts };
const keepalive: message.Message = .{ .header = head(.round) };
const begin_pages = [_]u32{ 4, 5, 6 };
const begin_sampling = [_]section.Sampling{.{ .stream = 5, .top_k = 40, .seed = 42, .temperature = 0.7, .top_p = 0.9, .min_p = 0.05, .has = .{ .seed = true } }};
const begin: message.Message = .{
.header = withFamily(head(.state), 1),
.streams = &.{.{ .id = 5, .op = .open, .first = 0, .rows = 0 }},
.sampling = &begin_sampling,
.pages = &.{.{ .stream = 5, .first = 0, .kept = 1, .ids = &begin_pages }},
};
const drops: message.Message = .{ .header = head(.state), .snapshots = &.{
.{ .op = .drop, .id = 11 },
.{ .op = .drop, .id = 12 },
.{ .op = .drop, .id = 0x1_0000_0003 },
} };
const restore_chunk: message.Message = .{
.header = withMode(head(.round), .prompt),
.streams = &.{.{ .id = 6, .op = .@"resume", .first = 16, .rows = 4, .resume_at = 16, .generation = 1 }},
.tokens = &.{ 20, 21, 22, 23, 24 },
.snapshots = &.{.{ .op = .restore, .stream = 6, .id = 21, .at = 16 }},
};
const compact: message.Message = .{ .header = head(.round), .rows = &.{ .{ .id = 1, .rows = 1 }, .{ .id = 2, .rows = 1 }, .{ .id = 4, .rows = 1 } }, .tokens = &.{ 7, 8, 9 } };
const copy_page = [_]u32{9};
const copies: message.Message = .{ .header = head(.state), .copies = &.{.{ .from = 7, .to = 9 }}, .pages = &.{.{ .stream = 2, .first = 3, .ids = &copy_page }} };
const family: message.Message = .{ .header = withFamily(head(.family), 7), .family = "fivex" };
const stop: message.Message = .{ .header = head(.stop) };
const sample: message.Message = .{
.header = withFamily(head(.family), 20),
.sampling = &.{
// DeepSeek's 7-word form
.{ .stream = 0, .top_k = 50, .temperature = 0.8, .top_p = 0.95, .row0 = 3, .rows = 2 },
// 9 words
.{ .stream = 0, .seed = 99, .temperature = 1.0, .rows = 1, .pos0 = 12, .has = .{ .seed = true, .pos0 = true } },
},
};

fn withFamily(h: header.Header, f: u32) header.Header {
var x = h;
x.family = f;
return x;
}

fn withMode(h: header.Header, m: header.Mode) header.Header {
var x = h;
x.mode = m;
return x;
}

const Build = *const fn (out: []u8) anyerror![]u8;

pub const Fixture = struct { name: []const u8, build: Build, want: ?anyerror };

fn fx(name: []const u8, build: Build, want: ?anyerror) Fixture {
return .{ .name = name, .build = build, .want = want };
}

fn good(comptime m: message.Message) Build {
return struct {
fn b(out: []u8) anyerror![]u8 {
return message.write(m, out);
}
}.b;
}

/// `m` with one edit to the message before it is written.
fn with(comptime m: message.Message, comptime edit: fn (*message.Message) void) Build {
return struct {
fn b(out: []u8) anyerror![]u8 {
var x = m;
edit(&x);
return message.write(x, out);
}
}.b;
}

/// `m`'s bytes with byte `at` set to `v`.
fn poke(comptime m: message.Message, comptime at: usize, comptime v: u8) Build {
return struct {
fn b(out: []u8) anyerror![]u8 {
const p = try message.write(m, out);
p[at] = v;
return p;
}
}.b;
}

/// `m` with stream `i` replaced (the ring's round only).
fn stream(comptime i: usize, comptime s: section.Stream) fn (*message.Message) void {
return struct {
const list = blk: {
var l = streams;
l[i] = s;
break :blk l;
};
fn f(x: *message.Message) void {
x.streams = &list;
}
}.f;
}

const big: [520]u8 = @splat(0x5A);
const six_tokens = [_]u32{ 1, 2, 3, 4, 5, 6 };
const late_cut = [_]u32{7};

pub const all = [_]Fixture{
fx("round.bin", good(round), null),
fx("round_tokens.bin", good(round_tokens), null),
fx("keepalive.bin", good(keepalive), null),
fx("begin.bin", good(begin), null),
fx("drops.bin", good(drops), null),
fx("restore_chunk.bin", good(restore_chunk), null),
fx("compact.bin", good(compact), null),
fx("copies.bin", good(copies), null),
fx("family.bin", good(family), null),
fx("stop.bin", good(stop), null),
fx("sample.bin", good(sample), null),
fx("bad_magic.bin", poke(round, 0, 0), error.NotStepHeader),
fx("bad_version.bin", poke(round, 4, 2), error.BadVersion),
fx("bad_kind.bin", poke(round, 6, 9), error.BadKind),
fx("bad_mode.bin", poke(begin, 7, 1), error.BadMode),
fx("bad_keepalive_mode.bin", poke(keepalive, 7, 1), error.BadMode),
fx("bad_reserved_header.bin", poke(round, 50, 1), error.BadReserved),
fx("too_large.bin", good(.{ .header = head(.family), .family = &big }), error.TooLarge),
fx("bad_length_bytes.bin", poke(round, 32, 136 + 8), error.BadLength),
fx("bad_section_version.bin", poke(round, 65, 2), error.BadSectionVersion),
fx("bad_reserved_prefix.bin", poke(round, 66, 1), error.BadReserved),
fx("unknown_section.bin", poke(round, 64, 9), error.UnknownSection),
fx("unordered_sections.bin", poke(round_tokens, 232, 2), error.UnorderedSections),
fx("bad_padding.bin", poke(round_tokens, 228, 1), error.BadReserved),
fx("bad_sections.bin", poke(round, 20, 0b1110), error.BadSections),
fx("both_row_maps.bin", good(.{ .header = head(.round), .streams = &streams, .rows = &.{.{ .id = 1, .rows = 1 }} }), error.BothRowMaps),
fx("bad_streams.bin", poke(round, 24, 3), error.BadStreams),
fx("bad_op.bin", poke(round, 64 + 8 + 4, 9), error.BadOp),
fx("bad_open.bin", with(round, stream(0, .{ .id = 3, .op = .open, .first = 1, .rows = 4 })), error.BadOpen),
fx("bad_resume.bin", with(round, stream(2, .{ .id = 9, .op = .@"resume", .first = 32, .rows = 2, .resume_at = 33, .generation = 2 })), error.BadResume),
fx("rows_on_close.bin", with(round, stream(3, .{ .id = 12, .op = .close, .first = 20, .rows = 1, .generation = 1 })), error.RowsOnClose),
fx("past_context.bin", with(round, stream(2, .{ .id = 9, .op = .@"resume", .first = 63, .rows = 2, .resume_at = 32, .generation = 2 })), error.PastContext),
fx("too_many_rows.bin", with(round, stream(0, .{ .id = 3, .op = .open, .first = 0, .rows = 6 })), error.TooManyRows),
fx("bad_total.bin", poke(round, 28, 6), error.BadTotal),
fx("rows_off_round.bin", good(.{ .header = head(.state), .streams = &.{.{ .id = 5, .op = .open, .first = 0, .rows = 2 }} }), error.RowsOffRound),
fx("sections_off_round.bin", good(.{ .header = head(.stop), .family = "x" }), error.SectionsOffRound),
fx("bad_tokens.bin", good(.{ .header = head(.round), .streams = &streams, .tokens = &six_tokens, .cuts = &cuts }), error.BadTokens),
fx("bad_cut.bin", good(.{ .header = head(.round), .streams = &streams, .cuts = &late_cut }), error.BadCut),
fx("bad_sampling.bin", with(begin, struct {
fn f(x: *message.Message) void {
x.sampling = &.{.{ .stream = 5, .top_k = 40, .seed = 42, .temperature = 0.7 }}; // a seed with its bit clear
}
}.f), error.BadSampling),
fx("bad_pages.bin", with(begin, struct {
fn f(x: *message.Message) void {
x.pages = &.{.{ .stream = 5, .first = 0, .kept = 4, .ids = &begin_pages }};
}
}.f), error.BadPages),
fx("bad_snapshot_op.bin", poke(drops, 72, 9), error.BadSnapshotOp),
fx("bad_snapshot.bin", good(.{ .header = head(.state), .snapshots = &.{.{ .op = .drop, .stream = 5, .id = 11 }} }), error.BadSnapshot),
fx("other_round.bin", good(.{ .header = .{ .kind = .round, .round = 4, .group = context.group } }), error.OtherRound),
fx("other_group.bin", good(.{ .header = .{ .kind = .round, .round = context.round, .group = 1 } }), error.OtherGroup),
};

/// What a receiver makes of a fixture's bytes: null when it takes them, else the error it refuses them with.
pub fn judge(bytes: []const u8) anyerror!void {
const s = try message.decode(bytes, limits);
try header.expect(s.header, context);
}
99 changes: 99 additions & 0 deletions zig/src/fabric/step/fixtures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Step header fixtures

Golden bytes of leader step messages (T2 of #2560), good and bad. Every user of the header (the ring, a tensor
group's leader and followers, on any backend) decodes each file and must take the good ones and refuse each bad one
with the error named below. `fixtures_test.zig` re-encodes every file and compares bytes, and runs `fixtures.judge`
on each. To rewrite them after a deliberate format change:
`TF_STEP_FIXTURES_OUT=$PWD/zig/src/fabric/step/fixtures zig build test-fabric-step`.

Everything is little-endian. A message is the 64-byte header, then its sections in ascending id, each padded to a
multiple of 8 bytes with zeros.

## The receiver

`judge` decodes with limits of 512 bytes after the header, 4 streams, 8 rows and a 64-token context, then expects
round 3 and group `0x0A0B0C0D`. Every good fixture carries that round and group.

## Layouts

Header, 64 bytes:

| bytes | field | bytes | field |
|---|---|---|---|
| 0..4 | magic `0x48534654` ("TFSH") | 24..28 | streams: the row map's entries |
| 4..6 | version 1 | 28..32 | rows: the row map's sum |
| 6 | kind (1 round, 2 state, 3 idle, 4 stop, 5 family) | 32..40 | bytes after the header |
| 7 | mode (0 window, 1 prompt; 0 unless kind is round) | 40..44 | family step kind |
| 8..16 | round | 44..48 | family flags |
| 16..20 | group | 48..64 | reserved 0 |
| 20..24 | sections: bit i set when section i follows | | |

Section prefix, 8 bytes: id u8, version u8 (1), reserved u16 0, count u32. Entries:

| id | section | entry |
|---|---|---|
| 1 | streams | 28 bytes: id u32, op u8 (1 open, 2 continue, 3 close, 4 cancel, 5 resume), 3 reserved, first, rows, accepted, resume point, generation (u32 each) |
| 2 | tokens | u32: the round's rows in entry order, then a tail that is the family's |
| 3 | cuts | u32 row offsets, strictly ascending, each in 1..rows-1 |
| 4 | sampling | 64 bytes: stream u32, top_k u32, seed u64, temperature f64, top_p f64, min_p f64, row0 u32, rows u32, pos0 u64, has u32 (bit 0 seed, bit 1 pos0), reserved u32; an absent field is 0 |
| 5 | pages | stream, first, n, kept (u32 each), then n u32 page ids; count is the number of patches |
| 6 | copies | 8 bytes: from u32, to u32 |
| 7 | snapshots | 24 bytes: op u8 (1 drop, 2 restore, 3 save), 3 reserved, stream u32 (0 for drop), id u64, at u32, reserved u32 |
| 8 | rows | 8 bytes: id u32, rows u32 (a compact row map; never with section 1) |
| 15 | family | raw bytes; count is their number |

Rules the shared decoder does not make, because they are each user's: ascending stream ids (the ring's), a `resume`
paired with a `restore`, a sampler's rows inside the header's rows, and anything about page or snapshot ids.

## Files

The round in `round.bin` is the ring fixtures' round: stream 3 open at 0 with 4 rows; stream 7 continue at 10 with
1 row, 2 accepted, generation 1; stream 9 resume from 32 at 32 with 2 rows, generation 2; stream 12 close at 20,
generation 1; one cut at row 4; 7 rows.

| file | bytes | what | result |
|---|---|---|---|
| `round.bin` | 200 | the ring's round: sections 1 and 3 | taken |
| `round_tokens.bin` | 248 | `round.bin` with 7 token ids and a 2-id tail | taken |
| `keepalive.bin` | 64 | a `round`, the header alone | taken |
| `begin.bin` | 216 | `state`, family 1: one open, a sampler with a seed, a patch of 3 pages with 1 kept | taken |
| `drops.bin` | 144 | `state`: three snapshot drops, one id past u32 | taken |
| `restore_chunk.bin` | 168 | `round`, mode prompt: a resume at 16 with 4 rows, 5 tokens, a restore | taken |
| `compact.bin` | 120 | `round`: section 8 with three streams of 1 row, 3 tokens | taken |
| `copies.bin` | 112 | `state`: a copy, then a one-page patch mid-table | taken |
| `family.bin` | 80 | `family` kind 7 with 5 bytes of section 15 | taken |
| `stop.bin` | 64 | `stop`, the header alone | taken |
| `sample.bin` | 200 | `family` kind 20, no row map: two samplers, one with rows 3..5 and no seed, one with a seed and a draw position | taken |
| `bad_magic.bin` | 200 | `round.bin`, byte 0 = 0 | `NotStepHeader` |
| `bad_version.bin` | 200 | `round.bin`, byte 4 = 2 | `BadVersion` |
| `bad_kind.bin` | 200 | `round.bin`, byte 6 = 9 | `BadKind` |
| `bad_mode.bin` | 216 | `begin.bin`, mode 1 on a `state` | `BadMode` |
| `bad_keepalive_mode.bin` | 64 | `keepalive.bin`, mode 1 | `BadMode` |
| `bad_reserved_header.bin` | 200 | `round.bin`, byte 50 = 1 | `BadReserved` |
| `too_large.bin` | 592 | a `family` with 520 bytes of section 15 | `TooLarge` |
| `bad_length_bytes.bin` | 200 | `round.bin`, header bytes 144 | `BadLength` |
| `bad_section_version.bin` | 200 | `round.bin`, section 1 version 2 | `BadSectionVersion` |
| `bad_reserved_prefix.bin` | 200 | `round.bin`, byte 66 = 1 | `BadReserved` |
| `unknown_section.bin` | 200 | `round.bin`, section id 9 | `UnknownSection` |
| `unordered_sections.bin` | 248 | `round_tokens.bin`, the cuts' id 2 | `UnorderedSections` |
| `bad_padding.bin` | 248 | `round_tokens.bin`, a padding byte 1 | `BadReserved` |
| `bad_sections.bin` | 200 | `round.bin`, mask bit 2 set with no section 2 | `BadSections` |
| `both_row_maps.bin` | 200 | sections 1 and 8 | `BothRowMaps` |
| `bad_streams.bin` | 200 | `round.bin`, header streams 3 | `BadStreams` |
| `bad_op.bin` | 200 | `round.bin`, stream 3's op 9 | `BadOp` |
| `bad_open.bin` | 200 | stream 3 opens at 1 | `BadOpen` |
| `bad_resume.bin` | 200 | stream 9 resumes from 33, past its first row 32 | `BadResume` |
| `rows_on_close.bin` | 200 | stream 12 closes with a row | `RowsOnClose` |
| `past_context.bin` | 200 | stream 9 at 63 with 2 rows | `PastContext` |
| `too_many_rows.bin` | 200 | stream 3 with 6 rows: 9 in all | `TooManyRows` |
| `bad_total.bin` | 200 | `round.bin`, header rows 6 | `BadTotal` |
| `rows_off_round.bin` | 104 | a `state` that opens a stream with 2 rows | `RowsOffRound` |
| `sections_off_round.bin` | 80 | a `stop` with section 15 | `SectionsOffRound` |
| `bad_tokens.bin` | 232 | `round.bin` with 6 tokens for 7 rows | `BadTokens` |
| `bad_cut.bin` | 200 | a cut at row 7 of 7 | `BadCut` |
| `bad_sampling.bin` | 216 | `begin.bin`, a seed with its `has` bit clear | `BadSampling` |
| `bad_pages.bin` | 216 | `begin.bin`, 4 kept of 3 pages | `BadPages` |
| `bad_snapshot_op.bin` | 144 | `drops.bin`, the first op 9 | `BadSnapshotOp` |
| `bad_snapshot.bin` | 96 | a drop that names stream 5 | `BadSnapshot` |
| `other_round.bin` | 64 | a keep-alive of round 4 | `OtherRound` |
| `other_group.bin` | 64 | a keep-alive of group 1 | `OtherGroup` |
Binary file added zig/src/fabric/step/fixtures/bad_cut.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_keepalive_mode.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_kind.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_length_bytes.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_magic.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_mode.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_op.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_open.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_padding.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_pages.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_reserved_header.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_reserved_prefix.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_resume.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_sampling.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_section_version.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_sections.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_snapshot.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_snapshot_op.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_streams.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_tokens.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_total.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/bad_version.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/begin.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/both_row_maps.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/compact.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/copies.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/drops.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/family.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/keepalive.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/other_group.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/other_round.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/past_context.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/restore_chunk.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/round.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/round_tokens.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/rows_off_round.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/rows_on_close.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/sample.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/sections_off_round.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/stop.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/too_large.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/too_many_rows.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/unknown_section.bin
Binary file not shown.
Binary file added zig/src/fabric/step/fixtures/unordered_sections.bin
Binary file not shown.
54 changes: 54 additions & 0 deletions zig/src/fabric/step/fixtures_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! The step fixtures are byte-stable and each is taken or refused as listed; TF_STEP_FIXTURES_OUT rewrites them.
const std = @import("std");
const fixtures = @import("fixtures.zig");
const t = std.testing;
const out_env = "TF_STEP_FIXTURES_OUT";

/// The directory named by the fixture-output variable, when it is set.
fn outDir() ?[]const u8 {
return if (std.c.getenv(out_env)) |dir| std.mem.span(dir) else null;
}

/// The fixtures written into that directory, when it is set; true if they were.
fn rewrite() !bool {
const dir = outDir() orelse return false;
inline for (fixtures.all) |f| {
var buf: [1024]u8 = undefined;
const bytes = try f.build(&buf);
var name: [4096]u8 = undefined;
const path = try std.fmt.bufPrint(&name, "{s}{c}{s}", .{ dir, std.fs.path.sep, f.name });
try std.Io.Dir.cwd().writeFile(t.io, .{ .sub_path = path, .data = bytes });
}
return true;
}

test "step fixtures: every message's golden bytes are what the writer writes today" {
if (try rewrite()) return;
inline for (fixtures.all) |f| {
var buf: [1024]u8 = undefined;
const bytes = try f.build(&buf);
t.expectEqualSlices(u8, @embedFile("fixtures/" ++ f.name), bytes) catch |e| {
std.debug.print("fixture {s} changed\n", .{f.name});
return e;
};
}
}

test "step fixtures: each good fixture is taken and each bad one refused by its name" {
inline for (fixtures.all) |f| {
const got = fixtures.judge(@embedFile("fixtures/" ++ f.name));
if (f.want) |want| {
t.expectError(want, got) catch |e| {
std.debug.print("fixture {s}\n", .{f.name});
return e;
};
} else got catch |e| {
std.debug.print("fixture {s}\n", .{f.name});
return e;
};
}
}

test "step fixtures: every name is unique" {
for (fixtures.all, 0..) |a, i| for (fixtures.all[i + 1 ..]) |b| try t.expect(!std.mem.eql(u8, a.name, b.name));
}
Loading
Loading