/* jshint node: true, mocha: true */

/**
 *  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
 *
 *  https://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';

var files = require('../lib/files'),
   assert = require('assert'),
       fs = require('fs'),
     path = require('path');

describe('interop', function() {

  it('interop', function() {
    var codecs = files.streams.BlockDecoder.getDefaultCodecs();
    var dir = "../../build/interop/data";
    fs.readdirSync(dir).forEach(function (file) {
      var base = file.substr(0, file.lastIndexOf(".avro"));
      var pos = base.lastIndexOf("_");
      var codec = pos < 0 ? "null" : base.substr(pos + 1);

      // The JavaScript implementation is not fully interoperable for long types by default,
      // because JS represents all numbers as `double`s internally.
      // But the Java and Ruby bindings generate the interop test data randomly,
      // so it can contain values outside the range that double can represent.
      // As a workaround, we skip these languages to avoid random test failure.
      if (!base.startsWith("java") && !base.startsWith("ruby") && codec in codecs) {
        console.log("Reading " + file);
        var n = 0;
        files.createFileDecoder(path.join(dir, file))
          .on("data", function () {
            n++;
          })
          .on("end", function () {
            assert(n > 0);
          });
      }
      else {
        console.log("Skipped: " + file);
      }
    });
  });
});
