Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python
5.6 KB
Raw Blame
//! Catalog parsing and precedence fixtures.

const std = @import("std");
const Value = std.json.Value;
const pricing = @import("pricing.zig");

pub fn numberFields(num_field: anytype, u64_field: anytype) !void {
    var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena.deinit();
    const value = try std.json.parseFromSliceLeaky(Value, arena.allocator(),
        \\{"i": 42, "f": 2.5, "neg": -3, "s": "x"}
    , .{ .allocate = .alloc_always });
    try std.testing.expectEqual(@as(f64, 42), num_field(value.object, "i").?);
    try std.testing.expectEqual(@as(f64, 2.5), num_field(value.object, "f").?);
    try std.testing.expect(num_field(value.object, "s") == null);
    try std.testing.expect(num_field(value.object, "missing") == null);
    try std.testing.expectEqual(@as(u64, 42), u64_field(value.object, "i"));
    try std.testing.expectEqual(@as(u64, 0), u64_field(value.object, "neg"));
    try std.testing.expectEqual(@as(u64, 0), u64_field(value.object, "missing"));
}

pub fn lazyCatalog(comptime Catalog: type, source: *[]const u8) !void {
    const previous_source = source.*;
    defer source.* = previous_source;
    // The loader installs whatever catalog it discovers into the process-wide
    // table, allocated from the arena it is handed — here the testing
    // allocator, whose memory dies with this test. On a machine with a real
    // Codex cache that leaves `pricing.models()` walking freed memory for
    // every later test, so put the baked table back.
    const previous_table = pricing.active_model_table;
    defer pricing.active_model_table = previous_table;
    var catalog: Catalog = .{ .codex_home = "" };
    catalog.ensureCached(std.testing.io, std.testing.allocator, std.testing.allocator, "", "", "");
    try std.testing.expect(catalog.loaded);
    try std.testing.expect(!catalog.online_loaded);
    catalog.ensure(std.testing.io, std.testing.allocator, std.testing.allocator, "", "", "");
    try std.testing.expect(catalog.online_loaded);
    catalog.invalidate();
    try std.testing.expect(!catalog.loaded);
    try std.testing.expect(!catalog.online_loaded);
}

pub fn canonicalVendor(find_model: anytype) !void {
    var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena.deinit();
    const value = try std.json.parseFromSliceLeaky(Value, arena.allocator(),
        \\{"reseller":{"models":{"gpt-5.6":{"limit":{"context":100},"cost":{"input":99,"output":99,"cache_read":9}}}},
        \\ "openai":{"models":{"gpt-5.6":{"limit":{"context":1050000},"cost":{"input":5,"output":30,"cache_read":0.5}}}}}
    , .{ .allocate = .alloc_always });
    const model = find_model(value.object, "gpt-5.6").?;
    try std.testing.expectEqual(@as(f64, 5), model.in);
    try std.testing.expectEqual(@as(u64, 1_050_000), model.context);
    try std.testing.expect(find_model(value.object, "gpt5.6") != null);
    try std.testing.expect(find_model(value.object, "no-such-model") == null);
}

pub fn visibleSnapshots(parse_snapshot: anytype) !void {
    var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena.deinit();
    const snapshot = parse_snapshot(arena.allocator(),
        \\{"client_version":"9.9.9","models":[
        \\ {"slug":"future-sol","visibility":"list","context_window":372000},
        \\ {"slug":"future-hidden","visibility":"hide","context_window":999000},
        \\ {"slug":"future-luna","visibility":"list","context_window":128000},
        \\ {"slug":"future-luna","visibility":"list","context_window":1}]}
    ).?;
    try std.testing.expectEqualStrings("9.9.9", snapshot.client_version);
    try std.testing.expectEqual(@as(usize, 2), snapshot.models.len);
    try std.testing.expectEqualStrings("future-sol", snapshot.models[0].name);
    try std.testing.expectEqual(@as(u64, 372_000), snapshot.models[0].context);
    try std.testing.expectEqualStrings("future-luna", snapshot.models[1].name);
    try std.testing.expectEqual(@as(u64, 128_000), snapshot.models[1].context);

    // ADR 0226: Codex's own defaults ride along (native keys and graff's
    // compact cache keys); a level the wire does not accept is dropped.
    const defaults = parse_snapshot(arena.allocator(),
        \\{"models":[
        \\ {"slug":"native","visibility":"list","default_reasoning_level":"low","default_verbosity":"low"},
        \\ {"name":"compact","context":272000,"default_effort":"high","default_verbosity":"medium"},
        \\ {"slug":"odd","visibility":"list","default_reasoning_level":"extreme","default_verbosity":"loud"},
        \\ {"slug":"plain","visibility":"list"}]}
    ).?;
    try std.testing.expectEqualStrings("low", defaults.models[0].default_effort.?);
    try std.testing.expectEqualStrings("low", defaults.models[0].default_verbosity.?);
    try std.testing.expectEqualStrings("high", defaults.models[1].default_effort.?);
    try std.testing.expectEqualStrings("medium", defaults.models[1].default_verbosity.?);
    try std.testing.expect(defaults.models[2].default_effort == null and defaults.models[2].default_verbosity == null);
    try std.testing.expect(defaults.models[3].default_effort == null and defaults.models[3].default_verbosity == null);
}

pub fn versionFloor(floor: []const u8, effective_version: anytype, snapshot_matches: anytype) !void {
    try std.testing.expectEqualStrings(floor, effective_version("0.130.0", null));
    try std.testing.expectEqualStrings("0.200.0", effective_version("0.200.0", null));
    try std.testing.expectEqualStrings(floor, effective_version("invalid", null));
    try std.testing.expect(!snapshot_matches(.{
        .models = &.{},
        .client_version = "0.130.0",
    }, floor));
}