"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Name_1 = require("@wireio/sdk-core/chain/Name");
const Integer_1 = require("@wireio/sdk-core/chain/Integer");
const Transaction_1 = require("@wireio/sdk-core/chain/Transaction");
describe("Name", () => {
    test("Name.from creates a valid name", () => {
        const name = Name_1.Name.from("sysio");
        expect(name).toBeInstanceOf(Name_1.Name);
    });
    test("toString returns the name string", () => {
        expect(Name_1.Name.from("sysio").toString()).toBe("sysio");
    });
    test("equals returns true for matching string", () => {
        expect(Name_1.Name.from("sysio").equals("sysio")).toBe(true);
    });
    test("equals returns true for matching Name instance", () => {
        expect(Name_1.Name.from("sysio").equals(Name_1.Name.from("sysio"))).toBe(true);
    });
    test("equals returns false for different name", () => {
        expect(Name_1.Name.from("sysio").equals("other")).toBe(false);
    });
    test("empty name has empty toString", () => {
        const name = Name_1.Name.from("");
        expect(name.toString()).toBe("");
    });
    test("valid name patterns", () => {
        expect(Name_1.Name.from("a").toString()).toBe("a");
        expect(Name_1.Name.from("abcde12345").toString()).toBe("abcde12345");
        expect(Name_1.Name.from("a.b.c").toString()).toBe("a.b.c");
        expect(Name_1.Name.from("abcdefghijkl1").toString()).toBe("abcdefghijkl1");
        expect(Name_1.Name.from("............1").toString()).toBe("............1");
    });
    test("Name.from with UInt64 numeric value", () => {
        const original = Name_1.Name.from("sysio");
        const fromUint = Name_1.Name.from(original.value);
        expect(fromUint.toString()).toBe("sysio");
    });
    test("Name.from with UInt64.from(0) creates empty name", () => {
        const name = Name_1.Name.from(Integer_1.UInt64.from(0));
        expect(name.toString()).toBe("");
    });
    test("isValid rejects names that would be rewritten by packing", () => {
        expect(Name_1.Name.isValid("sysio.token")).toBe(true);
        expect(Name_1.Name.isValid("abc!")).toBe(false);
        expect(Name_1.Name.isValid("wire.tokenX")).toBe(false);
        expect(Name_1.Name.isValid("abcdefghijklmn")).toBe(false);
        expect(Name_1.Name.isValid("..............")).toBe(false);
        expect(Name_1.Name.isValid("abc.")).toBe(false);
        expect(Name_1.Name.isValid("abcdefghijklm")).toBe(false);
    });
    test("Name.from rejects non-canonical strings before packing", () => {
        for (const value of [
            "abc!",
            "wire.tokenX",
            "abcdefghijklmn",
            "..............",
            "abc.",
            "abcdefghijklm"
        ]) {
            expect(() => Name_1.Name.from(value)).toThrow("Invalid name");
        }
    });
    test("Transaction.from rejects invalid action and authorization names", () => {
        const action = {
            account: "sysio.token",
            name: "transfer",
            authorization: [{ actor: "alice", permission: "active" }],
            data: ""
        };
        const transaction = {
            expiration: "1970-01-01T00:00:00.000",
            ref_block_num: 0,
            ref_block_prefix: 0,
            actions: [action]
        };
        for (const invalidAction of [
            { ...action, account: "abc!" },
            { ...action, name: "transfer." },
            {
                ...action,
                authorization: [{ actor: "alice.", permission: "active" }]
            },
            {
                ...action,
                authorization: [{ actor: "alice", permission: "active." }]
            }
        ]) {
            expect(() => Transaction_1.Transaction.from({ ...transaction, actions: [invalidAction] })).toThrow("Invalid name");
        }
    });
});
//# sourceMappingURL=Name.test.js.map