#!/usr/bin/env vpython
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import mock
import os
import shutil
import tempfile
import unittest

import merge_js_lib as merger


class MergeJSLibTest(unittest.TestCase):
  def test_single_func_invocation_merge(self):
    input_segments = [{
      'startOffset': 0,
      'endOffset': 50,
      'count': 1
    }, {
      'startOffset': 15,
      'endOffset': 49,
      'count': 1
    }, {
      'startOffset': 41,
      'endOffset': 46,
      'count': 0
    }]

    expected_output_segments = [{
      'end': 41,
      'count': 1
    }, {
      'end': 46,
      'count': 0
    }, {
      'end': 50,
      'count': 1
    }]

    output_segments = merger._convert_to_disjoint_segments(
        input_segments)
    self.assertListEqual(output_segments, expected_output_segments)

  def test_multiple_subtractive_ranges(self):
    input_segments = [{
      'startOffset': 0,
      'endOffset': 151,
      'count': 1
    }, {
      'startOffset': 15,
      'endOffset': 151,
      'count': 2
    }, {
      'startOffset': 85,
      'endOffset': 114,
      'count': 0
    }, {
      'startOffset': 119,
      'endOffset': 145,
      'count': 0
    }]

    expected_output_segments = [
      {
        'count': 1,
        'end': 15
      },
      {
        'count': 2,
        'end': 85
      },
      {
        'count': 0,
        'end': 114
      },
      {
        'count': 2,
        'end': 119
      },
      {
        'count': 0,
        'end': 145
      },
      {
        'count': 2,
        'end': 151
      },
    ]

    output_segments = merger._convert_to_disjoint_segments(
        input_segments)
    self.assertListEqual(output_segments, expected_output_segments)

  def test_multiple_disjoint_segments_output_congiguous_ranges(self):
    input_segments = [{
      'startOffset': 0,
      'endOffset': 119,
      'count': 1
    }, {
      'startOffset': 15,
      'endOffset': 58,
      'count': 1
    }, {
      'startOffset': 50,
      'endOffset': 55,
      'count': 0
    }, {
      'startOffset': 76,
      'endOffset': 119,
      'count': 1
    }, {
      'startOffset': 111,
      'endOffset': 116,
      'count': 0
    }]

    expected_output_segments = [{
      'count': 1,
      'end': 50
    }, {
      'count': 0,
      'end': 55
    }, {
      'count': 1,
      'end': 111
    }, {
      'count': 0,
      'end': 116
    }, {
      'count': 1,
      'end': 119
    }]

    output_segments = merger._convert_to_disjoint_segments(
        input_segments)
    self.assertListEqual(output_segments, expected_output_segments)

  def test_overlap_segments_output_disjoint(self):
    input_segments = [{
      'count': 1,
      'startOffset': 0,
      'endOffset': 100
    }, {
      'count': 2,
      'startOffset': 50,
      'endOffset': 100
    }]

    expected_output_segments = [{
      'count': 1,
      'end': 50
    }, {
      'count': 2,
      'end': 100
    }]

    output_segments = merger._convert_to_disjoint_segments(
        input_segments)
    self.assertListEqual(output_segments, expected_output_segments)

  def test_disjoint_segments_stay_disjoint(self):
    input_segments = [{
      'count': 1,
      'startOffset': 0,
      'endOffset': 100
    }, {
      'count': 2,
      'startOffset': 100,
      'endOffset': 200
    }]

    expected_output_segments = [{
      'count': 1,
      'end': 100
    }, {
      'count': 2,
      'end': 200
    }]

    output_segments = merger._convert_to_disjoint_segments(
        input_segments)
    self.assertListEqual(output_segments, expected_output_segments)

  def test_first_element_is_ignored(self):
    input_segments = [
      {
        'count': 5,
        'startOffset': 0,
        'endOffset': 1000
      },
      {