<commit_before>import * as assert from "assert";
import * as mocha from "mocha";
import { ContractServer, IEndpointContract, EndpointContract, ContractServerResponse, ContractServerRequest } from "../lib";

export class ContractServerTest extends ContractServer {

    public listen(port: number): Promise<void> {
        throw new Error("Method not implemented.");
    }
    
    constructor(contracts: IEndpointContract[]) {
        super(contracts);
    }

    public static generateDummyContracts() {
        return [
            new EndpointContract("getdummy1", "read", async function (kvs) { return "success"; }),
            new EndpointContract("getdummy2", "read", async function (kvs, id) { if (id === "abc") { return "success"; } else { return "error"; } }),
            new EndpointContract("getdummy3", "read", async function (kvs, id, name) { if (id === "abc" && name === "bcd") { return "success"; } else { return "error" } })
        ];
    }
}

describe("Unit Testing ContractServer", () => {
    
    let test: ContractServerTest;

    before(async () => {
        test = new ContractServerTest(ContractServerTest.generateDummyContracts());
    });

    it("should resolve with Format Error when argument list is empty", async () => {
        let response = await test.invokeMatchingContractToRequest(new ContractServerRequest("read", []));
        assert.deepEqual(response, ContractServerResponse.FormatError());
    });

    it("should resolve with Format Error when argument rpc is missing", async () => {
        let response = await test.invokeMatchingContractToRequest(new ContractServerRequest("read", [{ key: "id", value: "abc" }]));
        assert.deepEqual(response, ContractServerResponse.FormatError());
    });

    it("should resolve with Not Found Error when contract cannot be found - cause 'name'", async () => {
        let response = await test.invokeMatchingContractToRequest(new ContractServerRequest("read", [{ key: "rpc", value: "getdummy0" }]));
        assert.deepEqual(response, ContractServerResponse.NotFoundError());
    });

    it("should resolve with Not Found Error when contract cannot be found - cause 'role'", async () => {
        let response = await test.invokeMatchingContractToRequest(new ContractServerRequest("create", [{ key: "rpc", value: "getdummy1" }]));
        assert.deepEqual(response, ContractServerResponse.NotFoundError());
    });

    it("should resolve with Not Found Error when contract cannot be found - combined cause", async () => {
        let response = await test.invokeMatchingContractToRequest(new ContractServerRequest("read", [{ key: "rpc", value: "getdummy0" }]));
        assert.deepEqual(response, ContractServerResponse.NotFoundError());
    });

    it("should resolve with Format Error when arguments are missing", async () => {
        let response = await test.invokeMatchingContractToRequest(new ContractServerRequest("read", [{ key: "rpc", value: "getdummy2" }]));
        assert.deepEqual(response, ContractServerResponse.FormatError());
    });

    it("should resolve with success when invoking with correct arguments", async () => {
        let response = await test.invokeMatchingContractToRequest(new ContractServerRequest("read", [{ key: "rpc", value: "getdummy2" }, { key: "id", value: "abc" }]));
        assert.deepEqual(response, ContractServerResponse.Success("read", "success"));
    });

    it("should resolve with success when invoking with correct arguments + optional arguments", async () => {
        let response = await test.invokeMatchingContractToRequest(new ContractServerRequest("read", [{ key: "rpc", value: "getdummy2" }, { key: "id", value: "abc" }, { key: "name", value: "bcd" }]));
        assert.deepEqual(response, ContractServerResponse.Success("read", "success"));
    });

    it("should resolve with success when invoking with correct but duplicate arguments", async () => {
        let response = await test.invokeMatchingContractToRequest(new ContractServerRequest("read", [{ key: "rpc", value: "getdummy2" }, { key: "id", value: "abc" }, { key: "id", value: "bcd" }]));
        assert.deepEqual(response, ContractServerResponse.Success("read", "success"));
    });

    it("should resolve with success when invoking with correct muliple arguments", async () => {
        let response = await test.invokeMatchingContractToRequest(new ContractServerRequest("read", [{ key: "rpc", value: "getdummy3" }, { key: "id", value: "abc" }, { key: "name", value: "bcd" }]));
        assert.deepEqual(response, ContractServerResponse.Success("read", "success"));
    });

});<commit_msg>adjusted tests to static method structure<commit_after>import * as assert from "assert";
import * as mocha from "mocha";
import { ContractServer, IEndpointContract, EndpointContract, ContractServerResponse, ContractServerRequest } from "../lib";

export class ContractServerTest extends ContractServer {

    public listen(port: number): Promise<void> {
        throw new Error("Method not implemented.");
    }
    
    constructor(contracts: IEndpointContract[]) {
        super(contracts);
    }

    public static generateDummyContracts() {
        return [
            new EndpointContract("getdummy1", "read", async function (kvs) { return "success"; }),
            new EndpointContract("getdummy2", "read", async function (kvs, id) { if (id === "abc") { return "success"; } else { return "error"; } }),
            new EndpointContract("getdummy3", "read", async function (kvs, id, name) { if (id === "abc" && name === "bcd") { return "success"; } else { return "error" } })
        ];
    }
}

describe("Unit Testing ContractServer", () => {
    
    let test: ContractServerTest;

    before(async () => {
        test = new ContractServerTest(ContractServerTest.generateDummyContracts());
    });

    it("should resolve with Format Error when argument list is empty", async () => {
        let response = await ContractServer.invokeMatchingContractToRequest(test.contracts, new ContractServerRequest("read", []));
        assert.deepEqual(response, ContractServerResponse.FormatError());
    });

    it("should resolve with Format Error when argument rpc is missing", async () => {
        let response = await ContractServer.invokeMatchingContractToRequest(test.contracts, new ContractServerRequest("read", [{ key: "id", value: "abc" }]));
        assert.deepEqual(response, ContractServerResponse.FormatError());
    });

    it("should resolve with Not Found Error when contract cannot be found - cause 'name'", async () => {
        let response = await ContractServer.invokeMatchingContractToRequest(test.contracts, new ContractServerRequest("read", [{ key: "rpc", value: "getdummy0" }]));
        assert.deepEqual(response, ContractServerResponse.NotFoundError());
    });

    it("should resolve with Not Found Error when contract cannot be found - cause 'role'", async () => {
        let response = await ContractServer.invokeMatchingContractToRequest(test.contracts, new ContractServerRequest("create", [{ key: "rpc", value: "getdummy1" }]));
        assert.deepEqual(response, ContractServerResponse.NotFoundError());
    });

    it("should resolve with Not Found Error when contract cannot be found - combined cause", async () => {
        let response = await ContractServer.invokeMatchingContractToRequest(test.contracts, new ContractServerRequest("read", [{ key: "rpc", value: "getdummy0" }]));
        assert.deepEqual(response, ContractServerResponse.NotFoundError());
    });

    it("should resolve with Format Error when arguments are missing", async () => {
        let response = await ContractServer.invokeMatchingContractToRequest(test.contracts, new ContractServerRequest("read", [{ key: "rpc", value: "getdummy2" }]));
        assert.deepEqual(response, ContractServerResponse.FormatError());
    });

    it("should resolve with success when invoking with correct arguments", async () => {
        let response = await ContractServer.invokeMatchingContractToRequest(test.contracts, new ContractServerRequest("read", [{ key: "rpc", value: "getdummy2" }, { key: "id", value: "abc" }]));
        assert.deepEqual(response, ContractServerResponse.Success("read", "success"));
    });

    it("should resolve with success when invoking with correct arguments + optional arguments", async () => {
        let response = await ContractServer.invokeMatchingContractToRequest(test.contracts, new ContractServerRequest("read", [{ key: "rpc", value: "getdummy2" }, { key: "id", value: "abc" }, { key: "name", value: "bcd" }]));
        assert.deepEqual(response, ContractServerResponse.Success("read", "success"));
    });

    it("should resolve with success when invoking with correct but duplicate arguments", async () => {
        let response = await ContractServer.invokeMatchingContractToRequest(test.contracts, new ContractServerRequest("read", [{ key: "rpc", value: "getdummy2" }, { key: "id", value: "abc" }, { key: "id", value: "bcd" }]));
        assert.deepEqual(response, ContractServerResponse.Success("read", "success"));
    });

    it("should resolve with success when invoking with correct muliple arguments", async () => {
        let response = await ContractServer.invokeMatchingContractToRequest(test.contracts, new ContractServerRequest("read", [{ key: "rpc", value: "getdummy3" }, { key: "id", value: "abc" }, { key: "name", value: "bcd" }]));
        assert.deepEqual(response, ContractServerResponse.Success("read", "success"));
    });

});
{'fraction_non_alphanumeric': 0.14279705573080967, 'fraction_numerical': 0.00231335436382755, 'mean_word_length': 4.266334440753045, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4010927', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2417, 'n_words': 715}