import { gsd, optimizePeaks } from '..';

const { generateSpectrum } = require('spectrum-generator');

describe('Global spectra deconvolution with simulated spectra', () => {
  // Test case obtained from Pag 443, Chap 8.
  it('Should provide the right result ...', () => {
    const peaks = [
      { x: -0.1, y: 0.2, width: 0.3 },
      { x: 0.1, y: 0.2, width: 0.1 },
    ];

    const data = generateSpectrum(peaks, { from: -1, to: 1, nbPoints: 101 });

    let peakList = gsd(data, {
      minMaxRatio: 0,
      realTopDetection: false,
      smoothY: false,
      heightFactor: 1,
      shape: { kind: 'gaussian' },
    });

    expect(peakList[0].x).toBeCloseTo(-0.1, 2);
    expect(peakList[0].y).toBeCloseTo(0.2, 2);
    expect(peakList[0].width).toBeCloseTo(0.3, 2);
    expect(peakList[1].x).toBeCloseTo(0.1, 2);
    expect(peakList[1].y).toBeCloseTo(0.2, 2);
    expect(peakList[1].width).toBeCloseTo(0.1, 2);

    let optimizedPeaks = optimizePeaks(data, peakList);
    expect(optimizedPeaks[0].x).toBeCloseTo(-0.1, 2);
    expect(optimizedPeaks[0].y).toBeCloseTo(0.2, 2);
    expect(optimizedPeaks[0].width).toBeCloseTo(0.3, 2);
    expect(optimizedPeaks[0].group).toBe(0);
    expect(optimizedPeaks[1].x).toBeCloseTo(0.1, 2);
    expect(optimizedPeaks[1].y).toBeCloseTo(0.2, 2);
    expect(optimizedPeaks[1].width).toBeCloseTo(0.1, 2);
    expect(optimizedPeaks[1].group).toBe(1);
  });

  it('Overlaped peaks', () => {
    const peaks = [
      { x: 0.1, y: 0.4, width: 0.0 },
      { x: 0.101, y: 0.5, width: 0.01 },
      { x: 0.15, y: 0.4, width: 0.01 },
      { x: 0.151, y: 0.3, width: 0.03 },
    ];

    const data = generateSpectrum(peaks, { from: 0, to: 1, nbPoints: 101 });

    let optimizedPeaks = optimizePeaks(data, peaks, {
      factorWidth: 1,
      factorLimits: 2,
      optimization: { kind: 'lm', options: { maxIterations: 300 } },
    });

    expect(optimizedPeaks).toHaveLength(4);
    expect(optimizedPeaks[0].group).toBe(0);
    expect(optimizedPeaks[1].group).toBe(0);
    expect(optimizedPeaks[2].group).toBe(1);
    expect(optimizedPeaks[3].group).toBe(1);

    expect(optimizedPeaks[0].x).toBeCloseTo(0.15, 2);
    expect(optimizedPeaks[0].y).toBeCloseTo(0.4, 2);
    expect(optimizedPeaks[0].width).toBeCloseTo(0.01, 2);
  });
});

@@ -172,54 +172,8 @@
 %3E %7B%0A
-  // Test case obtained from Pag 443, Chap 8.%0A
   it
@@ -925,16 +925,17 @@
 kList);%0A
+%0A
     expe
@@ -1360,23 +1360,29 @@
 it('
-Overlaped
+Should provide 1
  peak
-s
 ', (
@@ -1403,31 +1403,24 @@
 st peaks = %5B
-%0A      
 %7B x: 0.1, y:
@@ -1418,25 +1418,25 @@
 : 0.1, y: 0.
-4
+2
 , width: 0.0
@@ -1438,139 +1438,12 @@
 : 0.
-0 %7D,%0A      %7B x: 0.101, y: 0.5, width: 0.01 %7D,%0A      %7B x: 0.15, y: 0.4, width: 0.01 %7D,%0A      %7B x: 0.151, y: 0.3, width: 0.03 %7D,%0A    
+12 %7D
 %5D;%0A%0A
@@ -1488,400 +1488,350 @@
 s, %7B
+%0A     
  from: 
-0, to: 1, nbPoints: 101 %7D);%0A%0A    let optimizedPeaks = optimizePeaks(data, peaks, %7B%0A      factorWidth: 1,%0A      factorLimits: 2,
+-0.5,%0A      to: 0.5,%0A      nbPoints: 10001,%0A      shape: %7B%0A        kind: 'gaussian',%0A        options: %7B
 %0A      
-optimization: %7B kind: 'lm', options: %7B maxIterations: 300 %7D %7D,%0A    %7D);%0A%0A    expect(optimizedPeaks).toHaveLength(4);%0A    expect(optimizedPeaks%5B0%5D.group).toBe(0);%0A    expect(optimizedPeaks%5B1%5D.group).toBe(0);%0A    expect(optimizedPeaks%5B2%5D.group).toBe(1
+    fwhm: 10000,%0A        %7D,%0A      %7D,%0A    %7D);%0A%0A    let peakList = gsd(data, %7B%0A      minMaxRatio: 0,%0A      realTopDetection: false,%0A      smoothY: false,%0A      heightFactor: 1,%0A      shape: %7B kind: 'gaussian' %7D,%0A    %7D
 );%0A
+%0A
     
@@ -1841,43 +1841,35 @@
 ect(
-optimizedPeaks%5B3%5D.group).toBe
+peakList).toHaveLength
 (1);%0A
-%0A
     
@@ -1871,38 +1871,32 @@
 %0A    expect(
-optimizedPeaks
+peakList
 %5B0%5D.x).toBeC
@@ -1906,12 +1906,12 @@
 eTo(
+-
 0.1
-5
 , 2)
@@ -1919,38 +1919,32 @@
 %0A    expect(
-optimizedPeaks
+peakList
 %5B0%5D.y).toBeC
@@ -1956,9 +1956,9 @@
 o(0.
-4
+2
 , 2)
@@ -1966,38 +1966,32 @@
 %0A    expect(
-optimizedPeaks
+peakList
 %5B0%5D.width).t
@@ -2003,18 +2003,17 @@
 oseTo(0.
-01
+3
 , 2);%0A  

add failling test case