@oanda/labs-crowd-view-widget 1.0.45 → 1.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/CHANGELOG.md +380 -0
  2. package/dist/main/CrowdViewWidget/Main.js +9 -5
  3. package/dist/main/CrowdViewWidget/Main.js.map +1 -1
  4. package/dist/main/CrowdViewWidget/components/Chart/Chart.js +3 -2
  5. package/dist/main/CrowdViewWidget/components/Chart/Chart.js.map +1 -1
  6. package/dist/main/CrowdViewWidget/components/Chart/ChartWithData.js +5 -14
  7. package/dist/main/CrowdViewWidget/components/Chart/ChartWithData.js.map +1 -1
  8. package/dist/main/CrowdViewWidget/components/Chart/chartOptions.js +20 -8
  9. package/dist/main/CrowdViewWidget/components/Chart/chartOptions.js.map +1 -1
  10. package/dist/main/CrowdViewWidget/components/Chart/index.js +4 -4
  11. package/dist/main/CrowdViewWidget/components/Chart/index.js.map +1 -1
  12. package/dist/main/CrowdViewWidget/components/Chart/types.js.map +1 -1
  13. package/dist/main/CrowdViewWidget/components/Chart/useCrowdViewData.js +17 -101
  14. package/dist/main/CrowdViewWidget/components/Chart/useCrowdViewData.js.map +1 -1
  15. package/dist/main/CrowdViewWidget/components/Chart/utils/aggregateBuckets.js +37 -0
  16. package/dist/main/CrowdViewWidget/components/Chart/utils/aggregateBuckets.js.map +1 -0
  17. package/dist/main/CrowdViewWidget/components/Chart/utils/chartUtils.js +19 -4
  18. package/dist/main/CrowdViewWidget/components/Chart/utils/chartUtils.js.map +1 -1
  19. package/dist/main/CrowdViewWidget/components/Chart/utils/getTargetBucketWidth.js +14 -0
  20. package/dist/main/CrowdViewWidget/components/Chart/utils/getTargetBucketWidth.js.map +1 -0
  21. package/dist/main/CrowdViewWidget/components/Chart/utils/index.js +83 -0
  22. package/dist/main/CrowdViewWidget/components/Chart/utils/index.js.map +1 -0
  23. package/dist/main/CrowdViewWidget/components/Chart/utils/processBuckets.js +29 -0
  24. package/dist/main/CrowdViewWidget/components/Chart/utils/processBuckets.js.map +1 -0
  25. package/dist/main/CrowdViewWidget/components/Chart/utils/processOrderPositionBooks.js +23 -0
  26. package/dist/main/CrowdViewWidget/components/Chart/utils/processOrderPositionBooks.js.map +1 -0
  27. package/dist/main/CrowdViewWidget/components/Chart/utils/processPriceCandles.js +43 -0
  28. package/dist/main/CrowdViewWidget/components/Chart/utils/processPriceCandles.js.map +1 -0
  29. package/dist/main/CrowdViewWidget/components/Chart/utils/validateData.js +23 -0
  30. package/dist/main/CrowdViewWidget/components/Chart/utils/validateData.js.map +1 -0
  31. package/dist/main/CrowdViewWidget/components/Legend/Legend.js +6 -4
  32. package/dist/main/CrowdViewWidget/components/Legend/Legend.js.map +1 -1
  33. package/dist/main/CrowdViewWidget/components/Legend/LegendBar.js +5 -3
  34. package/dist/main/CrowdViewWidget/components/Legend/LegendBar.js.map +1 -1
  35. package/dist/main/CrowdViewWidget/constants.js +105 -5
  36. package/dist/main/CrowdViewWidget/constants.js.map +1 -1
  37. package/dist/main/CrowdViewWidget/selectConfig.js +18 -60
  38. package/dist/main/CrowdViewWidget/selectConfig.js.map +1 -1
  39. package/dist/main/CrowdViewWidget/types.js +20 -0
  40. package/dist/main/CrowdViewWidget/types.js.map +1 -1
  41. package/dist/main/translations/sources/en.json +21 -16
  42. package/dist/module/CrowdViewWidget/Main.js +9 -5
  43. package/dist/module/CrowdViewWidget/Main.js.map +1 -1
  44. package/dist/module/CrowdViewWidget/components/Chart/Chart.js +3 -2
  45. package/dist/module/CrowdViewWidget/components/Chart/Chart.js.map +1 -1
  46. package/dist/module/CrowdViewWidget/components/Chart/ChartWithData.js +5 -14
  47. package/dist/module/CrowdViewWidget/components/Chart/ChartWithData.js.map +1 -1
  48. package/dist/module/CrowdViewWidget/components/Chart/chartOptions.js +20 -8
  49. package/dist/module/CrowdViewWidget/components/Chart/chartOptions.js.map +1 -1
  50. package/dist/module/CrowdViewWidget/components/Chart/index.js +1 -1
  51. package/dist/module/CrowdViewWidget/components/Chart/index.js.map +1 -1
  52. package/dist/module/CrowdViewWidget/components/Chart/types.js.map +1 -1
  53. package/dist/module/CrowdViewWidget/components/Chart/useCrowdViewData.js +13 -97
  54. package/dist/module/CrowdViewWidget/components/Chart/useCrowdViewData.js.map +1 -1
  55. package/dist/module/CrowdViewWidget/components/Chart/utils/aggregateBuckets.js +29 -0
  56. package/dist/module/CrowdViewWidget/components/Chart/utils/aggregateBuckets.js.map +1 -0
  57. package/dist/module/CrowdViewWidget/components/Chart/utils/chartUtils.js +20 -5
  58. package/dist/module/CrowdViewWidget/components/Chart/utils/chartUtils.js.map +1 -1
  59. package/dist/module/CrowdViewWidget/components/Chart/utils/getTargetBucketWidth.js +7 -0
  60. package/dist/module/CrowdViewWidget/components/Chart/utils/getTargetBucketWidth.js.map +1 -0
  61. package/dist/module/CrowdViewWidget/components/Chart/utils/index.js +8 -0
  62. package/dist/module/CrowdViewWidget/components/Chart/utils/index.js.map +1 -0
  63. package/dist/module/CrowdViewWidget/components/Chart/utils/processBuckets.js +22 -0
  64. package/dist/module/CrowdViewWidget/components/Chart/utils/processBuckets.js.map +1 -0
  65. package/dist/module/CrowdViewWidget/components/Chart/utils/processOrderPositionBooks.js +16 -0
  66. package/dist/module/CrowdViewWidget/components/Chart/utils/processOrderPositionBooks.js.map +1 -0
  67. package/dist/module/CrowdViewWidget/components/Chart/utils/processPriceCandles.js +36 -0
  68. package/dist/module/CrowdViewWidget/components/Chart/utils/processPriceCandles.js.map +1 -0
  69. package/dist/module/CrowdViewWidget/components/Chart/utils/validateData.js +16 -0
  70. package/dist/module/CrowdViewWidget/components/Chart/utils/validateData.js.map +1 -0
  71. package/dist/module/CrowdViewWidget/components/Legend/Legend.js +6 -4
  72. package/dist/module/CrowdViewWidget/components/Legend/Legend.js.map +1 -1
  73. package/dist/module/CrowdViewWidget/components/Legend/LegendBar.js +5 -3
  74. package/dist/module/CrowdViewWidget/components/Legend/LegendBar.js.map +1 -1
  75. package/dist/module/CrowdViewWidget/constants.js +104 -4
  76. package/dist/module/CrowdViewWidget/constants.js.map +1 -1
  77. package/dist/module/CrowdViewWidget/selectConfig.js +3 -45
  78. package/dist/module/CrowdViewWidget/selectConfig.js.map +1 -1
  79. package/dist/module/CrowdViewWidget/types.js +19 -1
  80. package/dist/module/CrowdViewWidget/types.js.map +1 -1
  81. package/dist/module/translations/sources/en.json +21 -16
  82. package/dist/types/CrowdViewWidget/components/Chart/Chart.d.ts +1 -1
  83. package/dist/types/CrowdViewWidget/components/Chart/index.d.ts +1 -1
  84. package/dist/types/CrowdViewWidget/components/Chart/types.d.ts +13 -8
  85. package/dist/types/CrowdViewWidget/components/Chart/utils/aggregateBuckets.d.ts +2 -0
  86. package/dist/types/CrowdViewWidget/components/Chart/utils/chartUtils.d.ts +11 -6
  87. package/dist/types/CrowdViewWidget/components/Chart/utils/getTargetBucketWidth.d.ts +3 -0
  88. package/dist/types/CrowdViewWidget/components/Chart/utils/index.d.ts +7 -0
  89. package/dist/types/CrowdViewWidget/components/Chart/utils/processBuckets.d.ts +3 -0
  90. package/dist/types/CrowdViewWidget/components/Chart/utils/processOrderPositionBooks.d.ts +8 -0
  91. package/dist/types/CrowdViewWidget/components/Chart/utils/processPriceCandles.d.ts +27 -0
  92. package/dist/types/CrowdViewWidget/components/Chart/utils/validateData.d.ts +2 -0
  93. package/dist/types/CrowdViewWidget/components/Legend/Legend.d.ts +3 -1
  94. package/dist/types/CrowdViewWidget/constants.d.ts +12 -4
  95. package/dist/types/CrowdViewWidget/selectConfig.d.ts +2 -2
  96. package/dist/types/CrowdViewWidget/types.d.ts +18 -1
  97. package/dist/types/CrowdViewWidget/utils/instrumentUtils.d.ts +1 -4
  98. package/package.json +4 -3
  99. package/src/CrowdViewWidget/Main.tsx +46 -40
  100. package/src/CrowdViewWidget/components/Chart/Chart.tsx +2 -2
  101. package/src/CrowdViewWidget/components/Chart/ChartWithData.tsx +4 -26
  102. package/src/CrowdViewWidget/components/Chart/chartOptions.ts +41 -27
  103. package/src/CrowdViewWidget/components/Chart/index.ts +1 -1
  104. package/src/CrowdViewWidget/components/Chart/types.ts +14 -4
  105. package/src/CrowdViewWidget/components/Chart/useCrowdViewData.ts +30 -154
  106. package/src/CrowdViewWidget/components/Chart/utils/aggregateBuckets.ts +44 -0
  107. package/src/CrowdViewWidget/components/Chart/utils/chartUtils.ts +41 -12
  108. package/src/CrowdViewWidget/components/Chart/utils/getTargetBucketWidth.ts +13 -0
  109. package/src/CrowdViewWidget/components/Chart/utils/index.ts +7 -0
  110. package/src/CrowdViewWidget/components/Chart/utils/processBuckets.ts +43 -0
  111. package/src/CrowdViewWidget/components/Chart/utils/processOrderPositionBooks.ts +30 -0
  112. package/src/CrowdViewWidget/components/Chart/utils/processPriceCandles.ts +53 -0
  113. package/src/CrowdViewWidget/components/Chart/utils/validateData.ts +27 -0
  114. package/src/CrowdViewWidget/components/Legend/Legend.tsx +14 -3
  115. package/src/CrowdViewWidget/components/Legend/LegendBar.tsx +5 -2
  116. package/src/CrowdViewWidget/constants.ts +114 -4
  117. package/src/CrowdViewWidget/selectConfig.ts +5 -60
  118. package/src/CrowdViewWidget/types.ts +18 -1
  119. package/src/translations/sources/en.json +21 -16
  120. package/test/Main.test.tsx +1 -1
  121. package/test/components/Chart/utils/chartUtils.test.ts +13 -27
  122. package/test/components/Legend.test.tsx +8 -3
  123. package/test/components/LegendBar.test.tsx +3 -2
  124. package/test/utils/aggregateBuckets.test.ts +82 -0
  125. package/test/utils/getTargetBucketWidth.test.ts +37 -0
  126. package/test/utils/instrumentUtils.test.ts +13 -7
  127. package/test/utils/processBuckets.test.ts +153 -0
  128. package/test/utils/processOrderPositionBooks.test.ts +127 -0
  129. package/test/utils/processPriceCandles.test.ts +245 -0
  130. package/test/utils/validateData.test.ts +201 -0
  131. package/dist/main/CrowdViewWidget/types/index.js +0 -17
  132. package/dist/main/CrowdViewWidget/types/index.js.map +0 -1
  133. package/dist/main/CrowdViewWidget/types/instruments.js +0 -45
  134. package/dist/main/CrowdViewWidget/types/instruments.js.map +0 -1
  135. package/dist/module/CrowdViewWidget/types/index.js +0 -2
  136. package/dist/module/CrowdViewWidget/types/index.js.map +0 -1
  137. package/dist/module/CrowdViewWidget/types/instruments.js +0 -39
  138. package/dist/module/CrowdViewWidget/types/instruments.js.map +0 -1
  139. package/dist/types/CrowdViewWidget/types/index.d.ts +0 -1
  140. package/dist/types/CrowdViewWidget/types/instruments.d.ts +0 -36
  141. package/src/CrowdViewWidget/types/index.ts +0 -1
  142. package/src/CrowdViewWidget/types/instruments.ts +0 -37
@@ -10,7 +10,11 @@ import {
10
10
  BOOKS_THRESHOLDS,
11
11
  COLOR_MAP,
12
12
  } from '../../../../src/CrowdViewWidget/constants';
13
- import { Granularity, TimeSpan } from '../../../../src/gql/types/graphql';
13
+ import {
14
+ BookType,
15
+ Granularity,
16
+ TimeSpan,
17
+ } from '../../../../src/gql/types/graphql';
14
18
 
15
19
  describe('chartUtils', () => {
16
20
  describe('getTimeSpanForGranularity', () => {
@@ -131,13 +135,15 @@ describe('chartUtils', () => {
131
135
  ],
132
136
  ];
133
137
 
134
- const html = getTooltipFormatter(
138
+ const html = getTooltipFormatter({
135
139
  params,
136
140
  buckets,
137
- 0.0005,
138
- 1.3306,
139
- labelCallback
140
- );
141
+ bucketWidth: 0.0005,
142
+ selectedPrice: 1.3306,
143
+ precision: 5,
144
+ bookType: BookType.Order,
145
+ labelCallback,
146
+ });
141
147
  expect(html).toContain('candle');
142
148
  expect(html).toContain('open_price');
143
149
  expect(html).toContain('close_price');
@@ -146,27 +152,7 @@ describe('chartUtils', () => {
146
152
  expect(html).toContain('orders');
147
153
  expect(html).toContain('price_range');
148
154
  // Selected price 1.3306 falls into second bucket 1.3305 - 1.3310 which has negative sentiment
149
- expect(html).toContain('sell_advantage');
150
- });
151
-
152
- it('omits sections when data is missing', () => {
153
- const params = [
154
- {
155
- axisValue: '2025-03-15T10:30:00Z',
156
- value: [0, 0, 0, 0, 0], // no candle values
157
- },
158
- { value: ['2025-03-15T10:30:00Z', 0, 0] },
159
- ];
160
- const buckets: Array<Array<{ price: number; sentiment: number }>> = [[]];
161
- const html = getTooltipFormatter(
162
- params,
163
- buckets,
164
- 0.0005,
165
- 0,
166
- labelCallback
167
- );
168
- expect(html).not.toContain('open_price');
169
- expect(html).not.toContain('orders');
155
+ expect(html).toContain('sell_overbalance');
170
156
  });
171
157
  });
172
158
  });
@@ -6,17 +6,22 @@ import { render } from '@testing-library/react';
6
6
  import React from 'react';
7
7
 
8
8
  import { Legend } from '../../src/CrowdViewWidget/components';
9
+ import { BookType } from '../../src/gql/types/graphql';
9
10
 
10
11
  describe('Crowd View Widget', () => {
11
12
  describe('components', () => {
12
13
  describe('<Legend />', () => {
13
14
  it('renders two LegendBar components', () => {
14
15
  const { getAllByText } = render(
15
- <Legend longValues={[0.15, 0.55]} shortValues={[0.15, 0.55]} />
16
+ <Legend
17
+ bookType={BookType.Position}
18
+ longValues={[0.15, 0.55]}
19
+ shortValues={[0.15, 0.55]}
20
+ />
16
21
  );
17
22
 
18
- expect(getAllByText(/long/)).toHaveLength(2);
19
- expect(getAllByText(/short/)).toHaveLength(2);
23
+ expect(getAllByText(/long/)).toHaveLength(1);
24
+ expect(getAllByText(/short/)).toHaveLength(1);
20
25
  });
21
26
  });
22
27
  });
@@ -17,8 +17,9 @@ describe('Crowd View Widget', () => {
17
17
  <LegendBar label="long" type="long" values={mockValues} />
18
18
  );
19
19
 
20
- expect(getByText('0.15% long')).toBeInTheDocument();
21
- expect(getByText('0.55% long')).toBeInTheDocument();
20
+ expect(getByText('long')).toBeInTheDocument();
21
+ expect(getByText('0.15%')).toBeInTheDocument();
22
+ expect(getByText('≤ 0.55%')).toBeInTheDocument();
22
23
  });
23
24
 
24
25
  it('renders exactly 1 segment', () => {
@@ -0,0 +1,82 @@
1
+ import { aggregateBuckets } from '../../src/CrowdViewWidget/components';
2
+
3
+ describe('aggregateBuckets', () => {
4
+ it('should correctly aggregate a standard set of buckets', () => {
5
+ const buckets = [
6
+ { price: 1, sentiment: 1 },
7
+ { price: 1.05, sentiment: 2 },
8
+ { price: 1.1, sentiment: 4 },
9
+ { price: 1.15, sentiment: 3 },
10
+ { price: 1.2, sentiment: 7 },
11
+ { price: 1.35, sentiment: 8 },
12
+ { price: 1.6, sentiment: 10 },
13
+ { price: 1.65, sentiment: 1 },
14
+ { price: 1.8, sentiment: 18 },
15
+ ];
16
+ const newBucketWidth = 0.1;
17
+ const expected = [
18
+ { price: 1.0, sentiment: 3 },
19
+ { price: 1.1, sentiment: 7 },
20
+ { price: 1.2, sentiment: 7 },
21
+ { price: 1.3, sentiment: 8 },
22
+ { price: 1.6, sentiment: 11 },
23
+ { price: 1.8, sentiment: 18 },
24
+ ];
25
+
26
+ expect(aggregateBuckets(buckets, newBucketWidth)).toEqual(expected);
27
+ });
28
+
29
+ it('should return an empty array if the input array is empty', () => {
30
+ expect(aggregateBuckets([], 0.1)).toEqual([]);
31
+ });
32
+
33
+ it('should correctly process an array with a single bucket', () => {
34
+ const buckets = [{ price: 2.58, sentiment: 15 }];
35
+ const newBucketWidth = 0.2;
36
+ const expected = [{ price: 2.4, sentiment: 15 }];
37
+
38
+ expect(aggregateBuckets(buckets, newBucketWidth)).toEqual(expected);
39
+ });
40
+
41
+ it('should handle gaps in data without creating empty buckets', () => {
42
+ const buckets = [
43
+ { price: 10.1, sentiment: 5 }, // Belongs to bucket 10.0
44
+ { price: 10.9, sentiment: 8 }, // Belongs to bucket 10.8
45
+ ];
46
+ const newBucketWidth = 0.2;
47
+ const expected = [
48
+ { price: 10.0, sentiment: 5 },
49
+ { price: 10.8, sentiment: 8 },
50
+ ];
51
+
52
+ expect(aggregateBuckets(buckets, newBucketWidth)).toEqual(expected);
53
+ });
54
+
55
+ it('should aggregate all items into a single bucket if they fall within the new width', () => {
56
+ const buckets = [
57
+ { price: 1.1, sentiment: 10 },
58
+ { price: 1.5, sentiment: 20 },
59
+ { price: 1.9, sentiment: 30 },
60
+ ];
61
+ const newBucketWidth = 2.0;
62
+ const expected = [{ price: 0.0, sentiment: 60 }];
63
+
64
+ expect(aggregateBuckets(buckets, newBucketWidth)).toEqual(expected);
65
+ });
66
+
67
+ it('should correctly sum sentiments including zero and negative values', () => {
68
+ const buckets = [
69
+ { price: 5.0, sentiment: 100 },
70
+ { price: 5.05, sentiment: -20 },
71
+ { price: 5.11, sentiment: 0 },
72
+ { price: 5.18, sentiment: 5 },
73
+ ];
74
+ const newBucketWidth = 0.1;
75
+ const expected = [
76
+ { price: 5.0, sentiment: 80 },
77
+ { price: 5.1, sentiment: 5 },
78
+ ];
79
+
80
+ expect(aggregateBuckets(buckets, newBucketWidth)).toEqual(expected);
81
+ });
82
+ });
@@ -0,0 +1,37 @@
1
+ import { getTargetBucketWidth } from '../../src/CrowdViewWidget/components';
2
+ import {
3
+ BUCKET_CONFIG,
4
+ INSTRUMENTS_CONFIG,
5
+ } from '../../src/CrowdViewWidget/constants';
6
+ import { InstrumentId } from '../../src/CrowdViewWidget/types';
7
+ import { Granularity } from '../../src/gql/types/graphql';
8
+
9
+ describe('getTargetBucketWidth', () => {
10
+ const instrument = InstrumentId.EUR_AUD;
11
+ const { defaultBucketWidth } = INSTRUMENTS_CONFIG[instrument];
12
+ const optimizedBucketWidth = defaultBucketWidth * BUCKET_CONFIG.MULTIPLIER;
13
+
14
+ it('should return optimized width for H1 granularity', () => {
15
+ expect(getTargetBucketWidth(Granularity.H1, instrument)).toBe(
16
+ optimizedBucketWidth
17
+ );
18
+ });
19
+
20
+ it('should return optimized width for H4 granularity', () => {
21
+ expect(getTargetBucketWidth(Granularity.H4, instrument)).toBe(
22
+ optimizedBucketWidth
23
+ );
24
+ });
25
+
26
+ it('should return default width for M5 granularity', () => {
27
+ expect(getTargetBucketWidth(Granularity.M5, instrument)).toBe(
28
+ defaultBucketWidth
29
+ );
30
+ });
31
+
32
+ it('should return default width for M15 granularity', () => {
33
+ expect(getTargetBucketWidth(Granularity.M15, instrument)).toBe(
34
+ defaultBucketWidth
35
+ );
36
+ });
37
+ });
@@ -14,11 +14,13 @@ describe('instrumentUtils', () => {
14
14
  expect(Array.isArray(result)).toBe(true);
15
15
  expect(result.length).toBeGreaterThan(0);
16
16
 
17
- // Check that the first instrument has the expected OC format
17
+ // Check that the first instrument has the expected structure
18
18
  const firstInstrument = result[0];
19
19
  expect(firstInstrument).toHaveProperty('id');
20
20
  expect(firstInstrument).toHaveProperty('label');
21
- expect(firstInstrument.id).toMatch(/^[A-Z]{3}_[A-Z]{3}$/); // OC format: EUR_USD
21
+ // Instrument IDs are now enum values like 'EURAUD', not 'EUR_AUD'
22
+ expect(typeof firstInstrument.id).toBe('string');
23
+ expect(firstInstrument.id.length).toBeGreaterThan(0);
22
24
  });
23
25
 
24
26
  it('should return OAP instrument config for OAP division', () => {
@@ -28,11 +30,13 @@ describe('instrumentUtils', () => {
28
30
  expect(Array.isArray(result)).toBe(true);
29
31
  expect(result.length).toBeGreaterThan(0);
30
32
 
31
- // Check that the first instrument has the expected OAP format
33
+ // Check that the first instrument has the expected structure
32
34
  const firstInstrument = result[0];
33
35
  expect(firstInstrument).toHaveProperty('id');
34
36
  expect(firstInstrument).toHaveProperty('label');
35
- expect(firstInstrument.id).toMatch(/^[A-Z]{6}$/); // OAP format: EURUSD
37
+ // Instrument IDs are now enum values like 'EURAUD', not 'EURUSD'
38
+ expect(typeof firstInstrument.id).toBe('string');
39
+ expect(firstInstrument.id.length).toBeGreaterThan(0);
36
40
  });
37
41
 
38
42
  it('should return different configs for different divisions', () => {
@@ -41,12 +45,14 @@ describe('instrumentUtils', () => {
41
45
 
42
46
  expect(ocConfig).not.toEqual(oapConfig);
43
47
 
44
- // Check that the instrument IDs have different formats
48
+ // Both configs should have valid instrument IDs
45
49
  const ocFirstId = ocConfig[0].id;
46
50
  const oapFirstId = oapConfig[0].id;
47
51
 
48
- expect(ocFirstId).toMatch(/^[A-Z]{3}_[A-Z]{3}$/);
49
- expect(oapFirstId).toMatch(/^[A-Z]{6}$/);
52
+ expect(typeof ocFirstId).toBe('string');
53
+ expect(typeof oapFirstId).toBe('string');
54
+ // OAP config includes XAU_USD and XAG_USD which OC doesn't have
55
+ expect(oapConfig.length).toBeGreaterThan(ocConfig.length);
50
56
  });
51
57
  });
52
58
  });
@@ -0,0 +1,153 @@
1
+ import { processBuckets } from '../../src/CrowdViewWidget/components';
2
+ import type { GetOrderPositionBooksQuery } from '../../src/gql/types/graphql';
3
+
4
+ describe('processBuckets', () => {
5
+ const DEFAULT_BUCKET_WIDTH = 0.0005;
6
+ const OPTIMIZED_BUCKET_WIDTH = 0.002; // DEFAULT_BUCKET_WIDTH * 4
7
+
8
+ const createMockOrderPositionBook = (
9
+ time: string,
10
+ buckets: Array<{ price: number; sentiment: number | null } | null>,
11
+ bucketWidth: number = DEFAULT_BUCKET_WIDTH
12
+ ) => ({
13
+ bucketWidth,
14
+ price: 1.0,
15
+ time,
16
+ buckets,
17
+ });
18
+
19
+ it('should return empty array when orderPositionData is undefined', () => {
20
+ expect(processBuckets(undefined, DEFAULT_BUCKET_WIDTH)).toEqual([]);
21
+ });
22
+
23
+ it('should return empty array when orderPositionBooks is empty', () => {
24
+ const mockData: GetOrderPositionBooksQuery = {
25
+ orderPositionBooks: [],
26
+ };
27
+ expect(processBuckets(mockData, DEFAULT_BUCKET_WIDTH)).toEqual([]);
28
+ });
29
+
30
+ it('should filter out null books', () => {
31
+ const mockData: GetOrderPositionBooksQuery = {
32
+ orderPositionBooks: [null, createMockOrderPositionBook('2025-01-01', [])],
33
+ };
34
+ expect(processBuckets(mockData, DEFAULT_BUCKET_WIDTH)).toEqual([]);
35
+ });
36
+
37
+ it('should filter out books with no buckets', () => {
38
+ const mockData: GetOrderPositionBooksQuery = {
39
+ orderPositionBooks: [
40
+ createMockOrderPositionBook('2025-01-01', []),
41
+ createMockOrderPositionBook('2025-01-02', [
42
+ { price: 1.0, sentiment: 0.2 },
43
+ ]),
44
+ ],
45
+ };
46
+ const result = processBuckets(mockData, DEFAULT_BUCKET_WIDTH);
47
+ expect(result).toHaveLength(1);
48
+ expect(result[0]).toEqual([{ price: 1.0, sentiment: 0.2 }]);
49
+ });
50
+
51
+ it('should filter out null buckets and buckets with missing price or sentiment', () => {
52
+ const mockData: GetOrderPositionBooksQuery = {
53
+ orderPositionBooks: [
54
+ createMockOrderPositionBook('2025-01-01', [
55
+ null,
56
+ { price: 1.0, sentiment: null },
57
+ { price: undefined as any, sentiment: 0.2 },
58
+ { price: 1.1, sentiment: 0.2 },
59
+ ]),
60
+ ],
61
+ };
62
+ const result = processBuckets(mockData, DEFAULT_BUCKET_WIDTH);
63
+ expect(result).toHaveLength(1);
64
+ expect(result[0]).toEqual([{ price: 1.1, sentiment: 0.2 }]);
65
+ });
66
+
67
+ it('should filter out buckets below sentiment threshold', () => {
68
+ const mockData: GetOrderPositionBooksQuery = {
69
+ orderPositionBooks: [
70
+ createMockOrderPositionBook('2025-01-01', [
71
+ { price: 1.0, sentiment: 0.1 }, // below threshold
72
+ { price: 1.1, sentiment: 0.15 }, // at threshold
73
+ { price: 1.2, sentiment: 0.2 }, // above threshold
74
+ { price: 1.3, sentiment: -0.1 }, // below threshold (absolute)
75
+ { price: 1.4, sentiment: -0.15 }, // at threshold (absolute)
76
+ { price: 1.5, sentiment: -0.2 }, // above threshold (absolute)
77
+ ]),
78
+ ],
79
+ };
80
+ const result = processBuckets(mockData, DEFAULT_BUCKET_WIDTH);
81
+ expect(result).toHaveLength(1);
82
+ expect(result[0]).toEqual([
83
+ { price: 1.1, sentiment: 0.15 },
84
+ { price: 1.2, sentiment: 0.2 },
85
+ { price: 1.4, sentiment: -0.15 },
86
+ { price: 1.5, sentiment: -0.2 },
87
+ ]);
88
+ });
89
+
90
+ it('should aggregate buckets when using optimized width', () => {
91
+ const mockData: GetOrderPositionBooksQuery = {
92
+ orderPositionBooks: [
93
+ createMockOrderPositionBook(
94
+ '2025-01-01',
95
+ [
96
+ { price: 1.0, sentiment: 0.2 },
97
+ { price: 1.0003, sentiment: 0.3 },
98
+ { price: 1.0007, sentiment: 0.1 },
99
+ { price: 1.0012, sentiment: 0.25 },
100
+ ],
101
+ DEFAULT_BUCKET_WIDTH
102
+ ),
103
+ ],
104
+ };
105
+ const result = processBuckets(mockData, OPTIMIZED_BUCKET_WIDTH);
106
+ expect(result).toHaveLength(1);
107
+ // Buckets should be aggregated: 1.0 + 1.0003 + 1.0007 = 1.0 bucket (0.6 sentiment)
108
+ // 1.0012 = 1.001 bucket (0.25 sentiment)
109
+ expect(result[0].length).toBeGreaterThan(0);
110
+ const aggregatedSentiments = result[0].reduce(
111
+ (acc, bucket) => acc + bucket.sentiment,
112
+ 0
113
+ );
114
+ expect(aggregatedSentiments).toBeCloseTo(0.85, 2);
115
+ });
116
+
117
+ it('should not aggregate buckets when using default width', () => {
118
+ const mockData: GetOrderPositionBooksQuery = {
119
+ orderPositionBooks: [
120
+ createMockOrderPositionBook('2025-01-01', [
121
+ { price: 1.0, sentiment: 0.2 },
122
+ { price: 1.0003, sentiment: 0.3 },
123
+ { price: 1.0012, sentiment: 0.25 },
124
+ ]),
125
+ ],
126
+ };
127
+ const result = processBuckets(mockData, DEFAULT_BUCKET_WIDTH);
128
+ expect(result).toHaveLength(1);
129
+ // Should have same number of buckets as input (assuming all pass threshold)
130
+ expect(result[0].length).toBeGreaterThanOrEqual(3);
131
+ });
132
+
133
+ it('should process multiple books correctly', () => {
134
+ const mockData: GetOrderPositionBooksQuery = {
135
+ orderPositionBooks: [
136
+ createMockOrderPositionBook('2025-01-01', [
137
+ { price: 1.0, sentiment: 0.2 },
138
+ ]),
139
+ createMockOrderPositionBook('2025-01-02', [
140
+ { price: 1.1, sentiment: 0.3 },
141
+ { price: 1.2, sentiment: 0.4 },
142
+ ]),
143
+ ],
144
+ };
145
+ const result = processBuckets(mockData, DEFAULT_BUCKET_WIDTH);
146
+ expect(result).toHaveLength(2);
147
+ expect(result[0]).toEqual([{ price: 1.0, sentiment: 0.2 }]);
148
+ expect(result[1]).toEqual([
149
+ { price: 1.1, sentiment: 0.3 },
150
+ { price: 1.2, sentiment: 0.4 },
151
+ ]);
152
+ });
153
+ });
@@ -0,0 +1,127 @@
1
+ import { processOrderPositionBooks } from '../../src/CrowdViewWidget/components';
2
+ import type { GetOrderPositionBooksQuery } from '../../src/gql/types/graphql';
3
+
4
+ describe('processOrderPositionBooks', () => {
5
+ const createMockOrderPositionBook = (time: string) => ({
6
+ bucketWidth: 0.0005,
7
+ price: 1.0,
8
+ time,
9
+ buckets: [{ price: 1.0, sentiment: 0.2 }],
10
+ });
11
+
12
+ it('should return empty array when orderPositionData is undefined', () => {
13
+ const candleMap = new Map();
14
+ expect(processOrderPositionBooks(undefined, candleMap)).toEqual([]);
15
+ });
16
+
17
+ it('should return empty array when orderPositionBooks is empty', () => {
18
+ const mockData: GetOrderPositionBooksQuery = {
19
+ orderPositionBooks: [],
20
+ };
21
+ const candleMap = new Map();
22
+ expect(processOrderPositionBooks(mockData, candleMap)).toEqual([]);
23
+ });
24
+
25
+ it('should filter out null books', () => {
26
+ const mockData: GetOrderPositionBooksQuery = {
27
+ orderPositionBooks: [null, createMockOrderPositionBook('2025-01-01')],
28
+ };
29
+ const candleMap = new Map();
30
+ const result = processOrderPositionBooks(mockData, candleMap);
31
+ expect(result).toHaveLength(1);
32
+ expect(result[0]).toEqual(['2025-01-01', null, 0]);
33
+ });
34
+
35
+ it('should filter out books with no buckets', () => {
36
+ const mockData: GetOrderPositionBooksQuery = {
37
+ orderPositionBooks: [
38
+ {
39
+ bucketWidth: 0.0005,
40
+ price: 1.0,
41
+ time: '2025-01-01',
42
+ buckets: [],
43
+ },
44
+ createMockOrderPositionBook('2025-01-02'),
45
+ ],
46
+ };
47
+ const candleMap = new Map();
48
+ const result = processOrderPositionBooks(mockData, candleMap);
49
+ expect(result).toHaveLength(1);
50
+ expect(result[0]).toEqual(['2025-01-02', null, 0]);
51
+ });
52
+
53
+ it('should use candle high price when available in map', () => {
54
+ const mockData: GetOrderPositionBooksQuery = {
55
+ orderPositionBooks: [
56
+ createMockOrderPositionBook('2025-01-01'),
57
+ createMockOrderPositionBook('2025-01-02'),
58
+ ],
59
+ };
60
+ const candleMap = new Map([
61
+ ['2025-01-01', { high: 1.1234 }],
62
+ ['2025-01-02', { high: 1.5678 }],
63
+ ]);
64
+ const result = processOrderPositionBooks(mockData, candleMap);
65
+ expect(result).toHaveLength(2);
66
+ expect(result[0]).toEqual(['2025-01-01', 1.1234, 0]);
67
+ expect(result[1]).toEqual(['2025-01-02', 1.5678, 1]);
68
+ });
69
+
70
+ it('should use null when candle is not in map', () => {
71
+ const mockData: GetOrderPositionBooksQuery = {
72
+ orderPositionBooks: [createMockOrderPositionBook('2025-01-01')],
73
+ };
74
+ const candleMap = new Map();
75
+ const result = processOrderPositionBooks(mockData, candleMap);
76
+ expect(result).toHaveLength(1);
77
+ expect(result[0]).toEqual(['2025-01-01', null, 0]);
78
+ });
79
+
80
+ it('should use null when candle high is undefined', () => {
81
+ const mockData: GetOrderPositionBooksQuery = {
82
+ orderPositionBooks: [createMockOrderPositionBook('2025-01-01')],
83
+ };
84
+ const candleMap = new Map([
85
+ ['2025-01-01', { low: 1.0 }], // no high property
86
+ ]);
87
+ const result = processOrderPositionBooks(mockData, candleMap);
88
+ expect(result).toHaveLength(1);
89
+ expect(result[0]).toEqual(['2025-01-01', null, 0]);
90
+ });
91
+
92
+ it('should correctly assign index to each book', () => {
93
+ const mockData: GetOrderPositionBooksQuery = {
94
+ orderPositionBooks: [
95
+ createMockOrderPositionBook('2025-01-01'),
96
+ createMockOrderPositionBook('2025-01-02'),
97
+ createMockOrderPositionBook('2025-01-03'),
98
+ ],
99
+ };
100
+ const candleMap = new Map();
101
+ const result = processOrderPositionBooks(mockData, candleMap);
102
+ expect(result).toHaveLength(3);
103
+ expect(result[0][2]).toBe(0);
104
+ expect(result[1][2]).toBe(1);
105
+ expect(result[2][2]).toBe(2);
106
+ });
107
+
108
+ it('should handle mixed cases with and without candles', () => {
109
+ const mockData: GetOrderPositionBooksQuery = {
110
+ orderPositionBooks: [
111
+ createMockOrderPositionBook('2025-01-01'),
112
+ createMockOrderPositionBook('2025-01-02'),
113
+ createMockOrderPositionBook('2025-01-03'),
114
+ ],
115
+ };
116
+ const candleMap = new Map([
117
+ ['2025-01-01', { high: 1.1111 }],
118
+ // 2025-01-02 missing from map
119
+ ['2025-01-03', { high: 1.3333 }],
120
+ ]);
121
+ const result = processOrderPositionBooks(mockData, candleMap);
122
+ expect(result).toHaveLength(3);
123
+ expect(result[0]).toEqual(['2025-01-01', 1.1111, 0]);
124
+ expect(result[1]).toEqual(['2025-01-02', null, 1]);
125
+ expect(result[2]).toEqual(['2025-01-03', 1.3333, 2]);
126
+ });
127
+ });