// Tests for types.zig
const std = @import("std");
const types = @import("types.zig");
const TypeId = types.TypeId;
const TypeTable = types.TypeTable;
const TypeInfo = types.TypeInfo;

test "builtin types pre-populated" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    // Verify builtin slots
    try std.testing.expectEqual(TypeInfo.void, table.get(.void));
    try std.testing.expectEqual(TypeInfo.bool, table.get(.bool));
    try std.testing.expectEqual(TypeInfo{ .signed = 32 }, table.get(.s32));
    try std.testing.expectEqual(TypeInfo{ .unsigned = 8 }, table.get(.u8));
    try std.testing.expectEqual(TypeInfo.f64, table.get(.f64));
    try std.testing.expectEqual(TypeInfo.string, table.get(.string));
    try std.testing.expectEqual(TypeInfo.any, table.get(.any));
}

test "intern deduplicates structural types" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const ptr1 = table.ptrTo(.s32);
    const ptr2 = table.ptrTo(.s32);
    try std.testing.expectEqual(ptr1, ptr2);

    const ptr3 = table.ptrTo(.f64);
    try std.testing.expect(ptr1 != ptr3);
}

test "slice and array interning" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const slice1 = table.sliceOf(.s32);
    const slice2 = table.sliceOf(.s32);
    try std.testing.expectEqual(slice1, slice2);

    const arr1 = table.arrayOf(.u8, 10);
    const arr2 = table.arrayOf(.u8, 10);
    const arr3 = table.arrayOf(.u8, 20);
    try std.testing.expectEqual(arr1, arr2);
    try std.testing.expect(arr1 != arr3);
}

test "optional interning" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const opt1 = table.optionalOf(.s32);
    const opt2 = table.optionalOf(.s32);
    try std.testing.expectEqual(opt1, opt2);

    const opt3 = table.optionalOf(.f64);
    try std.testing.expect(opt1 != opt3);
}

test "function type interning" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const params = &[_]TypeId{ .s32, .s32 };
    const fn1 = table.functionType(params, .s64);
    const fn2 = table.functionType(params, .s64);
    try std.testing.expectEqual(fn1, fn2);

    const fn3 = table.functionType(params, .f64);
    try std.testing.expect(fn1 != fn3);
}

test "string pool interning" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const id1 = table.internString("Point");
    const id2 = table.internString("Point");
    const id3 = table.internString("Rect");

    try std.testing.expectEqual(id1, id2);
    try std.testing.expect(id1 != id3);
    try std.testing.expectEqualStrings("Point", table.getString(id1));
    try std.testing.expectEqualStrings("Rect", table.getString(id3));
}

test "sizeOf builtins" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    try std.testing.expectEqual(@as(u32, 0), table.sizeOf(.void));
    try std.testing.expectEqual(@as(u32, 1), table.sizeOf(.bool));
    try std.testing.expectEqual(@as(u32, 4), table.sizeOf(.s32));
    try std.testing.expectEqual(@as(u32, 8), table.sizeOf(.s64));
    try std.testing.expectEqual(@as(u32, 1), table.sizeOf(.u8));
    try std.testing.expectEqual(@as(u32, 4), table.sizeOf(.f32));
    try std.testing.expectEqual(@as(u32, 8), table.sizeOf(.f64));
    try std.testing.expectEqual(@as(u32, 16), table.sizeOf(.string));
    try std.testing.expectEqual(@as(u32, 8), table.sizeOf(table.ptrTo(.s32)));
    try std.testing.expectEqual(@as(u32, 16), table.sizeOf(table.sliceOf(.s32)));
}

test "typeName for builtins" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    try std.testing.expectEqualStrings("s32", table.typeName(.s32));
    try std.testing.expectEqualStrings("bool", table.typeName(.bool));
    try std.testing.expectEqualStrings("string", table.typeName(.string));
    try std.testing.expectEqualStrings("void", table.typeName(.void));
    try std.testing.expectEqualStrings("Any", table.typeName(.any));
}

// ── Pack type (Feature 1, Step 2.1) ──────────────────────────────────

test "pack type: construct, element access, intern dedup (N=3)" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const elems = &[_]TypeId{ .bool, .s32, .string };
    const p1 = table.packType(elems);
    const p2 = table.packType(elems);
    try std.testing.expectEqual(p1, p2); // structural dedup

    const info = table.get(p1);
    try std.testing.expect(info == .pack);
    try std.testing.expectEqual(@as(usize, 3), info.pack.elements.len);
    try std.testing.expectEqual(TypeId.bool, info.pack.elements[0]);
    try std.testing.expectEqual(TypeId.s32, info.pack.elements[1]);
    try std.testing.expectEqual(TypeId.string, info.pack.elements[2]);
}

test "pack type: empty pack (N=0)" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const empty1 = table.packType(&.{});
    const empty2 = table.packType(&.{});
    try std.testing.expectEqual(empty1, empty2);
    const info = table.get(empty1);
    try std.testing.expect(info == .pack);
    try std.testing.expectEqual(@as(usize, 0), info.pack.elements.len);
}

test "pack type: single element (N=1)" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const p = table.packType(&[_]TypeId{.f64});
    const info = table.get(p);
    try std.testing.expectEqual(@as(usize, 1), info.pack.elements.len);
    try std.testing.expectEqual(TypeId.f64, info.pack.elements[0]);
}

test "pack type: distinct element lists are distinct types" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const a = table.packType(&[_]TypeId{ .bool, .s32 });
    const b = table.packType(&[_]TypeId{ .s32, .bool }); // order matters
    const c = table.packType(&[_]TypeId{.bool}); // arity matters
    try std.testing.expect(a != b);
    try std.testing.expect(a != c);
    try std.testing.expect(b != c);
    // A pack is distinct from the tuple of the same elements.
    const tup = table.intern(.{ .tuple = .{ .fields = &[_]TypeId{ .bool, .s32 }, .names = null } });
    try std.testing.expect(a != tup);
}

test "pack type: formatTypeName" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    var arena = std.heap.ArenaAllocator.init(alloc);
    defer arena.deinit();

    const p = table.packType(&[_]TypeId{ .bool, .s32, .string });
    try std.testing.expectEqualStrings("pack(bool, s32, string)", table.formatTypeName(arena.allocator(), p));

    const empty = table.packType(&.{});
    try std.testing.expectEqualStrings("pack()", table.formatTypeName(arena.allocator(), empty));
}

// ── ERR E1.1 (Slice 1) — error sets + tag registry ──

test "TagRegistry interns tags, id 0 reserved, global identity" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const bad = table.internTag("BadDigit");
    const over = table.internTag("Overflow");
    const bad_again = table.internTag("BadDigit");

    try std.testing.expect(bad >= 1); // id 0 reserved for "no error"
    try std.testing.expect(bad != over); // distinct names → distinct ids
    try std.testing.expectEqual(bad, bad_again); // same name → same id (global-flat)
    try std.testing.expectEqualStrings("BadDigit", table.getTagName(bad));
    try std.testing.expectEqualStrings("Overflow", table.getTagName(over));
    try std.testing.expectEqualStrings("", table.getTagName(0)); // reserved slot
}

test "errorSetType: u32 layout, named display, dedup by name" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const name = table.internString("ParseErr");
    const tags = [_]u32{ table.internTag("BadDigit"), table.internTag("Overflow"), table.internTag("Empty") };
    const set = table.errorSetType(name, &tags);

    // u32 runtime layout (the error channel's tag value).
    try std.testing.expectEqual(@as(u32, 4), table.sizeOf(set));
    try std.testing.expectEqual(@as(usize, 4), table.typeSizeBytes(set));
    try std.testing.expectEqual(@as(usize, 4), table.typeAlignBytes(set));
    // Displays by name; resolvable by name.
    try std.testing.expectEqualStrings("ParseErr", table.typeName(set));
    try std.testing.expectEqual(set, table.findByName(name).?);
    // Info shape.
    const info = table.get(set);
    try std.testing.expect(info == .error_set);
    try std.testing.expectEqual(@as(usize, 3), info.error_set.tags.len);
    // Identity is the name → re-constructing the same set dedups.
    try std.testing.expectEqual(set, table.errorSetType(name, &tags));
}

test "errorSetType: tags stored sorted by global id" {
    const alloc = std.testing.allocator;
    var table = TypeTable.init(alloc);
    defer table.deinit();

    const name = table.internString("E");
    const c = table.internTag("C");
    const a = table.internTag("A");
    const b = table.internTag("B");
    // Pass out of order; errorSetType sorts for canonical storage.
    const set = table.errorSetType(name, &[_]u32{ c, a, b });
    const stored = table.get(set).error_set.tags;
    try std.testing.expectEqual(@as(usize, 3), stored.len);
    try std.testing.expect(stored[0] <= stored[1] and stored[1] <= stored[2]);
}
