|
1 | | -import fs from 'fs'; |
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { join } from 'node:path'; |
2 | 3 |
|
3 | | -import { NumberMatrix } from 'cheminfo-types'; |
| 4 | +import type { NumberMatrix } from 'cheminfo-types'; |
4 | 5 | import { convert } from 'jcampconverter'; |
5 | | -import { describe, it, expect } from 'vitest'; |
| 6 | +import { expect, test } from 'vitest'; |
6 | 7 |
|
7 | | -import { Conrec } from '..'; |
| 8 | +import { Conrec } from '../index.ts'; |
8 | 9 |
|
9 | | -const data = fs.readFileSync(`${__dirname}/data/zhmbc_0.jdx`, 'utf8'); |
| 10 | +const data = readFileSync( |
| 11 | + join(import.meta.dirname, 'data/zhmbc_0.jdx'), |
| 12 | + 'utf8', |
| 13 | +); |
10 | 14 | const parsed = convert(data, { noContour: true }).flatten[0]; |
11 | 15 | const matrix: NumberMatrix = parsed.minMax?.z || []; |
12 | | -describe('conrec basic test', () => { |
13 | | - it('no result because level too far', () => { |
14 | | - const conrec = new Conrec(matrix); |
15 | | - const { contours, timeout } = conrec.drawContour({ |
16 | | - contourDrawer: 'basic', |
17 | | - levels: [-1000000000, 1000000000], |
18 | | - timeout: 10000, |
19 | | - }); |
20 | | - expect(timeout).toBeFalsy(); |
21 | | - expect(contours).toStrictEqual([ |
22 | | - { lines: [], zValue: -1000000000 }, |
23 | | - { lines: [], zValue: 1000000000 }, |
24 | | - ]); |
| 16 | + |
| 17 | +test('no result because level too far', () => { |
| 18 | + const conrec = new Conrec(matrix); |
| 19 | + const { contours, timeout } = conrec.drawContour({ |
| 20 | + contourDrawer: 'basic', |
| 21 | + levels: [-1000000000, 1000000000], |
| 22 | + timeout: 10000, |
25 | 23 | }); |
26 | 24 |
|
27 | | - it('2 specified levels', () => { |
28 | | - const conrec = new Conrec(matrix); |
29 | | - const { contours, timeout } = conrec.drawContour({ |
30 | | - contourDrawer: 'basic', |
31 | | - levels: [-100000, 100000], |
32 | | - timeout: 10000, |
33 | | - }); |
34 | | - expect(timeout).toBeFalsy(); |
35 | | - expect(contours).toHaveLength(2); |
36 | | - expect(contours[0].lines).toHaveLength(36864); |
37 | | - expect(contours[1].lines).toHaveLength(119720); |
| 25 | + expect(timeout).toBe(false); |
| 26 | + expect(contours).toStrictEqual([ |
| 27 | + { lines: [], zValue: -1000000000 }, |
| 28 | + { lines: [], zValue: 1000000000 }, |
| 29 | + ]); |
| 30 | +}); |
| 31 | + |
| 32 | +test('2 specified levels', () => { |
| 33 | + const conrec = new Conrec(matrix); |
| 34 | + const { contours, timeout } = conrec.drawContour({ |
| 35 | + contourDrawer: 'basic', |
| 36 | + levels: [-100000, 100000], |
| 37 | + timeout: 10000, |
38 | 38 | }); |
39 | 39 |
|
40 | | - it('no levels', () => { |
41 | | - const conrec = new Conrec(matrix); |
42 | | - const start = Date.now(); |
43 | | - const { contours, timeout } = conrec.drawContour({ |
44 | | - contourDrawer: 'basic', |
45 | | - levels: [], |
46 | | - timeout: 10000, |
47 | | - }); |
48 | | - expect(Date.now() - start).toBeLessThan(25); |
49 | | - expect(timeout).toBeFalsy(); |
50 | | - expect(contours).toHaveLength(0); |
| 40 | + expect(timeout).toBe(false); |
| 41 | + expect(contours).toHaveLength(2); |
| 42 | + expect(contours[0].lines).toHaveLength(36864); |
| 43 | + expect(contours[1].lines).toHaveLength(119720); |
| 44 | +}); |
| 45 | + |
| 46 | +test('no levels', () => { |
| 47 | + const conrec = new Conrec(matrix); |
| 48 | + const start = Date.now(); |
| 49 | + const { contours, timeout } = conrec.drawContour({ |
| 50 | + contourDrawer: 'basic', |
| 51 | + levels: [], |
| 52 | + timeout: 10000, |
51 | 53 | }); |
52 | 54 |
|
53 | | - it('auto select levels', () => { |
54 | | - const conrec = new Conrec(matrix); |
55 | | - const { contours, timeout } = conrec.drawContour({ |
56 | | - contourDrawer: 'basic', |
57 | | - nbLevels: 10, |
58 | | - timeout: 10000, |
59 | | - }); |
| 55 | + expect(Date.now() - start).toBeLessThan(25); |
| 56 | + expect(timeout).toBe(false); |
| 57 | + expect(contours).toHaveLength(0); |
| 58 | +}); |
60 | 59 |
|
61 | | - expect(timeout).toBeFalsy(); |
62 | | - expect(contours).toHaveLength(10); |
63 | | - expect(contours[0].lines).toHaveLength(0); |
64 | | - expect(contours[1].lines).toHaveLength(4984); |
65 | | - expect(contours[8].lines).toHaveLength(32); |
66 | | - expect(contours[8].lines).toMatchSnapshot(); |
67 | | - expect(contours[9].lines).toHaveLength(0); |
| 60 | +test('auto select levels', () => { |
| 61 | + const conrec = new Conrec(matrix); |
| 62 | + const { contours, timeout } = conrec.drawContour({ |
| 63 | + contourDrawer: 'basic', |
| 64 | + nbLevels: 10, |
| 65 | + timeout: 10000, |
68 | 66 | }); |
69 | 67 |
|
70 | | - it('auto select levels with swapAxes', () => { |
71 | | - const conrec = new Conrec(matrix, { swapAxes: true }); |
72 | | - const { contours, timeout } = conrec.drawContour({ |
73 | | - contourDrawer: 'basic', |
74 | | - nbLevels: 10, |
75 | | - timeout: 10000, |
76 | | - }); |
| 68 | + expect(timeout).toBe(false); |
| 69 | + expect(contours).toHaveLength(10); |
| 70 | + expect(contours[0].lines).toHaveLength(0); |
| 71 | + expect(contours[1].lines).toHaveLength(4984); |
| 72 | + expect(contours[8].lines).toHaveLength(32); |
| 73 | + expect(contours[8].lines).toMatchSnapshot(); |
| 74 | + expect(contours[9].lines).toHaveLength(0); |
| 75 | +}); |
77 | 76 |
|
78 | | - expect(timeout).toBeFalsy(); |
79 | | - expect(contours).toHaveLength(10); |
80 | | - expect(contours[0].lines).toHaveLength(0); |
81 | | - expect(contours[1].lines).toHaveLength(4984); |
82 | | - expect(contours[8].lines).toHaveLength(32); |
83 | | - expect(contours[8].lines).toMatchSnapshot(); |
84 | | - expect(contours[9].lines).toHaveLength(0); |
| 77 | +test('auto select levels with swapAxes', () => { |
| 78 | + const conrec = new Conrec(matrix, { swapAxes: true }); |
| 79 | + const { contours, timeout } = conrec.drawContour({ |
| 80 | + contourDrawer: 'basic', |
| 81 | + nbLevels: 10, |
| 82 | + timeout: 10000, |
85 | 83 | }); |
86 | 84 |
|
87 | | - it('return available contours within 100ms', () => { |
88 | | - const conrec = new Conrec(matrix); |
89 | | - const { contours, timeout } = conrec.drawContour({ |
90 | | - contourDrawer: 'basic', |
91 | | - nbLevels: 10, |
92 | | - timeout: 10, |
93 | | - }); |
94 | | - expect(timeout).toBeTruthy(); |
95 | | - expect(contours).toHaveLength(10); |
96 | | - expect(contours[0].lines).toHaveLength(0); |
97 | | - expect(contours[1].lines.length).toBeLessThan(2000); |
98 | | - expect(contours[8].lines).toHaveLength(0); |
99 | | - expect(contours[9].lines).toHaveLength(0); |
| 85 | + expect(timeout).toBe(false); |
| 86 | + expect(contours).toHaveLength(10); |
| 87 | + expect(contours[0].lines).toHaveLength(0); |
| 88 | + expect(contours[1].lines).toHaveLength(4984); |
| 89 | + expect(contours[8].lines).toHaveLength(32); |
| 90 | + expect(contours[8].lines).toMatchSnapshot(); |
| 91 | + expect(contours[9].lines).toHaveLength(0); |
| 92 | +}); |
| 93 | + |
| 94 | +test('return available contours within 100ms', () => { |
| 95 | + const conrec = new Conrec(matrix); |
| 96 | + const { contours, timeout } = conrec.drawContour({ |
| 97 | + contourDrawer: 'basic', |
| 98 | + nbLevels: 10, |
| 99 | + timeout: 10, |
100 | 100 | }); |
| 101 | + |
| 102 | + expect(timeout).toBe(true); |
| 103 | + expect(contours).toHaveLength(10); |
| 104 | + expect(contours[0].lines).toHaveLength(0); |
| 105 | + expect(contours[1].lines.length).toBeLessThan(2000); |
| 106 | + expect(contours[8].lines).toHaveLength(0); |
| 107 | + expect(contours[9].lines).toHaveLength(0); |
101 | 108 | }); |
0 commit comments