import { ApolloServer } from "apollo-server-lambda";
import { expect } from "chai";
import { GraphQLResponse } from "graphql-extensions";
import { createMockServer } from "../../helpers";

describe("getPackage", () => {
  let server: ApolloServer;

  beforeEach(() => {
    server = createMockServer({
      Query: {
        package: () => ({
          name: "foo",
          description: "foo description",
        }),
      },
      Package: {
        scriptMetadata: () => [
          {
            version: "0.0.1",
            platform: [
              {
                os: "linux",
                dist: "Ubuntu Linux",
                codename: "none",
              },
            ],
          },
        ],
      },
    });
  });

  describe("query package", () => {
    let response: GraphQLResponse;

    beforeEach(async () => {
      response = await server.executeOperation({
        query: `
                    query GetPackage($input: PackageQueryInput!) {
                        package(input: $input) {
                            name,
                            description,
                            scriptMetadata {
                                version,
                                platform {os, dist, codename}
                            }
                        }
                    }
                `,
        variables: {
          input: {
            name: "test",
          },
        },
      });
    });

    it("should resolve without errors", () => {
      const { errors } = response;

      expect(errors, JSON.stringify(errors)).to.be.undefined;
    });

    it("should respond with correct name and description", () => {
      const { data } = response;

      expect(data).to.deep.equal({
        package: {
          name: "foo",
          description: "foo description",
          scriptMetadata: [
            {
              version: "0.0.1",
              platform: [
                {
                  os: "linux",
                  dist: "Ubuntu Linux",
                  codename: "none",
                },
              ],
            },
          ],
        },
      });
    });
  });

  describe("query package without invalid input", () => {
    let response: GraphQLResponse;

    beforeEach(async () => {
      response = await server.executeOperation({
        query: `
                    query GetPackage($input: PackageQueryInput!) {
                        package(input: $input) {
                            name,
                            description
                        }
                    }
                `,
        variables: {
          input: {},
        },
      });
    });

    it("should resolve with error='got invalid value'", async () => {
      const { errors } = response;
      expect(errors).to.lengthOf(1);

      const [error] = errors;
      const { message } = error;

      expect(message).to.contain("got invalid value");
      expect(message).to.contain("value.name of required type String");
    });
  });
});
