Something went wrong. Try again.
Zig bindings for all 506 operations of the Forgejo API, generated from its own description, and zfj, a command line built on them.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Tests over the shape of the generated bindings.//!//! Compiling `zfj` at all is the broadest check there is -- 506 operations and//! 246 models either agree with each other or the build fails -- so what is//! left for these is the part a successful compile does not prove: that the//! declarations a caller reaches for are present, named what they were named//! before, and carry the types the specification gives them. Each of these has//! failed silently at some point in getting the generator to this state.
const std = @import("std");const testing = std.testing;
const zfj = @import("zfj");
/// The payload of a `!Owned(T)`-returning operation, i.e. the `T` it parses/// the response body into.fn ResponseModel(comptime operation: anytype) type { const return_type = @typeInfo(@TypeOf(operation)).@"fn".return_type.?; return @typeInfo(return_type).error_union.payload;}
test "the client is constructed from an allocator, an Io and a token" { const init = @typeInfo(@TypeOf(zfj.Client.init)).@"fn"; try testing.expectEqual(@as(usize, 3), init.params.len); try testing.expectEqual(std.mem.Allocator, init.params[0].type.?); try testing.expectEqual(std.Io, init.params[1].type.?); try testing.expectEqual([]const u8, init.params[2].type.?); try testing.expectEqual(zfj.Client, init.return_type.?);}
test "operations return the model their response names" { // Forgejo writes almost every successful response as a $ref into the // document's global response table. Left unresolved they all generated as // `!void` and handed the caller nothing. try testing.expectEqual(zfj.Owned(zfj.ServerVersion), ResponseModel(zfj.getVersion)); try testing.expectEqual(zfj.Owned(zfj.Issue), ResponseModel(zfj.issueGetIssue)); try testing.expectEqual(zfj.Owned(zfj.Repository), ResponseModel(zfj.repoGet));}
test "operations returning a list are typed by their element" { // The element type lives in the response schema's `items`; dropping it // leaves `[]const std.json.Value` and the caller parsing the body twice. try testing.expectEqual(zfj.Owned([]const zfj.Issue), ResponseModel(zfj.issueListIssues)); try testing.expectEqual(zfj.Owned([]const zfj.Label), ResponseModel(zfj.issueGetLabels)); try testing.expectEqual(zfj.Owned([]const zfj.Branch), ResponseModel(zfj.repoListBranches));}
test "an operation with an empty response returns nothing" { // `204` against `#/responses/empty` is the one case where `!void` is right. const return_type = @typeInfo(@TypeOf(zfj.issueDelete)).@"fn".return_type.?; try testing.expectEqual(void, @typeInfo(return_type).error_union.payload);}
test "path and query parameters arrive in one options struct" { const options = @typeInfo(zfj.issueGetIssueOptions).@"struct"; try testing.expectEqual(@as(usize, 3), options.fields.len); try testing.expectEqualStrings("owner", options.fields[0].name); try testing.expectEqual([]const u8, options.fields[0].type); try testing.expectEqualStrings("repo", options.fields[1].name); try testing.expectEqual([]const u8, options.fields[1].type); // `index` is an int64 in the specification, not a string. try testing.expectEqualStrings("index", options.fields[2].name); try testing.expectEqual(i64, options.fields[2].type);}
test "optional query parameters default to null" { // Only the two path parameters are required, so a listing that wants no // filtering says so by leaving the rest out. const options: zfj.issueListIssuesOptions = .{ .owner = "forgejo", .repo = "forgejo" }; try testing.expectEqual(?[]const u8, @FieldType(zfj.issueListIssuesOptions, "state")); try testing.expect(options.state == null); try testing.expect(options.labels == null); // Paging parameters keep the integer type the specification gives them. try testing.expectEqual(?i64, @FieldType(zfj.issueListIssuesOptions, "page")); try testing.expect(options.page == null);}
test "every tag in the specification has a client" { // The ten Swagger tags Forgejo groups its operations under. A tag client // disappearing means its operations moved namespace under the caller. inline for (.{ zfj.ActivitypubClient, zfj.AdminClient, zfj.IssueClient, zfj.MiscellaneousClient, zfj.NotificationClient, zfj.OrganizationClient, zfj.PackageClient, zfj.RepositoryClient, zfj.SettingsClient, zfj.UserClient, }) |Tag| { const init = @typeInfo(@TypeOf(Tag.init)).@"fn"; try testing.expectEqual(*zfj.Client, init.params[0].type.?); try testing.expectEqual(Tag, init.return_type.?); }}
test "a tag client method returns what its flat operation returns" { try testing.expectEqual( ResponseModel(zfj.issueGetIssue), ResponseModel(zfj.IssueClient.issueGetIssue), );}
test "an operation named after a model does not displace it" { // Forgejo declares both an `ActionRun` schema and an `ActionRun` operation. // The model keeps the name and the operation takes the suffix, so a caller // asking for `zfj.ActionRun` gets the type. try testing.expect(@typeInfo(zfj.ActionRun) == .@"struct"); try testing.expectEqual(zfj.Owned(zfj.ActionRun), ResponseModel(zfj.ActionRun_));}
test "a model parses a response body from the API" { const body = \\{ \\ "id": 42, \\ "number": 7, \\ "title": "Something is broken", \\ "state": "open", \\ "user": { "login": "someone", "id": 1 }, \\ "labels": [ { "id": 3, "name": "bug", "color": "ff0000" } ], \\ "created_at": "2026-09-06T00:00:00Z", \\ "an_unknown_field": true \\} ;
const parsed = try std.json.parseFromSlice( zfj.Issue, testing.allocator, body, .{ .ignore_unknown_fields = true }, ); defer parsed.deinit();
const issue = parsed.value; try testing.expectEqual(@as(i64, 42), issue.id.?); try testing.expectEqual(@as(i64, 7), issue.number.?); try testing.expectEqualStrings("Something is broken", issue.title.?); try testing.expectEqualStrings("someone", issue.user.?.login.?); try testing.expectEqual(@as(usize, 1), issue.labels.?.len); try testing.expectEqualStrings("bug", issue.labels.?[0].name.?); // Fields the response leaves out stay null rather than failing the parse. try testing.expect(issue.body == null); try testing.expect(issue.milestone == null);}