/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

'use strict';

const assert = require('assert');
const Bytecode = require('../../lib/process/bytecode');
const graphModule = require('../../lib/structure/graph');
const helper = require('../helper');
const t = require('../../lib/process/traversal');

let client;

describe('Client', function () {
  before(function () {
    client = helper.getClient('gmodern');
    return client.open();
  });
  after(function () {
    return client.close();
  });
  describe('#submit()', function () {
    it('should send bytecode', function () {
      return client.submit(new Bytecode().addStep('V', []).addStep('tail', []))
        .then(function (result) {
          assert.ok(result);
          assert.strictEqual(result.length, 1);
          assert.ok(result.first().object instanceof graphModule.Vertex);
        });
    });
    it('should send and parse a script', function () {
      return client.submit('g.V().tail()')
        .then(function (result) {
          assert.ok(result);
          assert.strictEqual(result.length, 1);
          assert.ok(result.first() instanceof graphModule.Vertex);
        });
    });
    it('should send and parse a script with bindings', function () {
      return client.submit('x + x', { x: 3 })
        .then(function (result) {
          assert.ok(result);
          assert.strictEqual(result.first(), 6);
        });
    });
    it('should send and parse a script with non-native javascript bindings', function () {
      return client.submit('card.class.simpleName + ":" + card', { card: t.cardinality.set } )
        .then(function (result) {
          assert.ok(result);
          assert.strictEqual(result.first(), 'Cardinality:set');
        });
    });

    it('should retrieve the attributes', () => {
      return client.submit(new Bytecode().addStep('V', []).addStep('tail', []))
        .then(rs => {
          assert.ok(rs.attributes instanceof Map);
          assert.ok(rs.attributes.get('host'));
        });
    });

    it('should be able to stream results from the gremlin server', (done) => {
      const output = [];
      let calls = 0;
      const readable = client.stream('g.V().limit(3)', {}, { batchSize: 2 });

      readable.on('data', (data) => {
        calls += 1;
        data.toArray().forEach(v => output.push(v))
      })

      readable.on('end', () => {
        assert.strictEqual(calls, 2); // limit of 3 with batchSize of 2 should be two function calls
        assert.strictEqual(output.length, 3);
        assert.ok(output[0] instanceof graphModule.Vertex);
        done();
      })
    });

    it("should be able to iterate stream results async", async () => {
      const output = [];
      let calls = 0;
      const readable = client.stream("g.V().limit(3)", {}, { batchSize: 2 });

      for await (const result of readable) {
        calls += 1;
        result.toArray().forEach((v) => output.push(v));
      }

      assert.strictEqual(calls, 2); // limit of 3 with batchSize of 2 should be two function calls
      assert.strictEqual(output.length, 3);
      assert.ok(output[0] instanceof graphModule.Vertex);
    });

    it("should get error for malformed requestId for script stream", async () => {
      try {
        const readable = client.stream('g.V()', {}, {requestId: 'malformed'});
        for await (const result of readable) {
          assert.fail("malformed requestId should throw");
        }
      } catch (e) {
        assert.ok(e);
        assert.ok(e.message);
        assert.ok(e.message.includes("is not a valid UUID."));
      }
    });

    it("should get error for malformed requestId for script submit", async () => {
      try {
        await client.submit('g.V()', {}, {requestId: 'malformed'});
        assert.fail("malformed requestId should throw");
      } catch (e) {
        assert.ok(e);
        assert.ok(e.message);
        assert.ok(e.message.includes("is not a valid UUID."));
      }
    });

    it("should get error for malformed requestId for bytecode stream", async () => {
      try {
        const readable = client.stream(new Bytecode().addStep('V', []), {}, {requestId: 'malformed'});
        for await (const result of readable) {
          assert.fail("malformed requestId should throw");
        }
      } catch (e) {
        assert.ok(e);
        assert.ok(e.message);
        assert.ok(e.message.includes("is not a valid UUID."));
      }
    });

    it("should get error for malformed requestId for bytecode submit", async () => {
      try {
        await client.submit(new Bytecode().addStep('V', []), {}, {requestId: 'malformed'});
        assert.fail("malformed requestId should throw");
      } catch (e) {
        assert.ok(e);
        assert.ok(e.message);
        assert.ok(e.message.includes("is not a valid UUID."));
      }
    });

    it("should reject pending traversal promises if connection closes", async () => {
      const closingClient = helper.getClient('gmodern');
      await closingClient.open();
      const timeout = 10000;
      const startTime = Date.now();
      let isRejected = false;
      
      const pending = async function submitTraversals() {
        while (Date.now() < startTime + timeout) {
          try {
            await closingClient.submit(new Bytecode().addStep('V', []).addStep('tail', []));
          } catch (e) {
            isRejected = true;
            return;
          }
        }
      };
      const pendingPromise = pending();

      await closingClient.close();
      await pendingPromise;
      assert.strictEqual(isRejected, true);
    });

    it("should end streams on traversals if connection closes", async () => {
      const closingClient = helper.getClient('gmodern');
      await closingClient.open();
      let isRejected = false;

      const readable = client.stream('g.V().limit(3)', {}, { batchSize: 2 });

      readable.on('end', () => {
        isRejected = true;
      });

      await closingClient.close();
      for await (const result of readable) {
        // Consume the stream
      }

      assert.strictEqual(isRejected, true);
    });
  });
});
