import { ApolloServer } from "apollo-server-lambda";
import { readFileSync } from "fs";
import * as nock from "nock";
import { join, resolve } from "path";
import privateResolvers from "../resolvers/private";
import publicResolvers from "../resolvers/public";

export function createMockServer(
  mockResolvers,
  env: string = "public",
): ApolloServer {
  const typeDefs = readFileSync(
    resolve(join(__dirname, "../", `schema/${env}.gql`)),
  ).toString();

  const server = new ApolloServer({
    typeDefs,
    resolvers: {
      ...(env === "public" ? publicResolvers : privateResolvers),
      ...mockResolvers,
    },
  });

  return server;
}

export function mockLambdaInvocation(
  serviceName: string,
  funcName: string,
  mockedResult?: object,
  expectedResponse?: object,
  statusCode: number = 200,
): nock.Scope {
  return nock("https://lambda.eu-central-1.amazonaws.com")
    .post(
      `/2015-03-31/functions/openscript-${serviceName}-test-${funcName}/invocations`,
      expectedResponse,
    )
    .reply(statusCode, mockedResult);
}
