lib/choir/src/composition/fixture/record.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2 const product = @import("../../product/root.zig");
3 const revision = product.revision;
4
5 /// Test metadata is structurally complete and deliberately has no sealed receipt.
6 pub const Records = struct {
7 allocator: std.mem.Allocator,
8 owner: *revision.Store,
9
10 pub fn init(allocator: std.mem.Allocator) !Records {
11 return .{ .allocator = allocator, .owner = try revision.Store.create(allocator, .{
12 .revisions = 64,
13 .kinds = 1,
14 .builders = 1,
15 .compiler_manifests = 2,
16 .record_bytes = 64 * 1024,
17 .gate_scratch_bytes = 0,
18 .candidate_count = 1,
19 .screening_bytes = 0,
20 }) };
21 }
22
23 pub fn deinit(self: Records) void {
24 self.owner.release();
25 }
26
27 pub fn key(
28 self: Records,
29 stage: []const u8,
30 source: []const u8,
31 image: []const u8,
32 ) !product.ProductKey {
33 return self.importUnder(stage, source, image, "composition-fixture-v1");
34 }
35
36 /// Builds the same record under a named compiler, so a caller can place two
37 /// products at one address and differ only in what produced them.
38 pub fn importUnder(
39 self: Records,
40 stage: []const u8,
41 source: []const u8,
42 image: []const u8,
43 manifest: []const u8,
44 ) !product.ProductKey {
45 const address = try revision.record.encodeAddress(
46 self.allocator,
47 product.productRef("composition-fixture", source, stage, "default"),
48 );
49 defer self.allocator.free(address);
50 const inputs = try revision.record.encodeInputs(self.allocator, .{
51 .compiler_manifest = manifest,
52 .versions = &.{},
53 .pipeline = &.{},
54 .options = "",
55 .policy = "",
56 }, &.{}, "fixture-metadata");
57 defer self.allocator.free(inputs);
58 const bytes = try revision.record.encodeExact(self.allocator, .{
59 .address = address,
60 .inputs = inputs,
61 .image = image,
62 });
63 defer self.allocator.free(bytes);
64 const imported = try self.owner.importRecord(bytes, manifest, .{
65 .bytes = 64 * 1024,
66 .records = 1,
67 .depth = 1,
68 });
69 imported.release();
70 return product.productKey(imported);
71 }
72 };