@363045841yyt/klinechart-core 0.7.3 → 0.7.5-alpha.2
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.
- package/README.md +201 -201
- package/README.zh-CN.md +201 -201
- package/dist/engine/renderers/webgl/candleSurface.js +47 -47
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -2
- package/dist/version.js.map +1 -1
- package/package.json +129 -122
- package/src/__tests__/signal.test.ts +124 -124
- package/src/config/chartSettings.ts +66 -66
- package/src/controllers/__tests__/drawing.test.ts +214 -214
- package/src/controllers/__tests__/indicatorSelector.test.ts +481 -481
- package/src/controllers/__tests__/toolbar.test.ts +225 -225
- package/src/controllers/createChartController.ts +665 -665
- package/src/controllers/createDrawingController.ts +96 -96
- package/src/controllers/createIndicatorSelectorController.ts +307 -307
- package/src/controllers/createToolbarController.ts +146 -146
- package/src/controllers/index.ts +19 -19
- package/src/controllers/types.ts +284 -284
- package/src/engine/__tests__/chart.dpr.test.ts +401 -401
- package/src/engine/__tests__/paneRenderer.resize.test.ts +92 -92
- package/src/engine/chart-store.ts +121 -121
- package/src/engine/chart.d.ts +617 -617
- package/src/engine/chart.ts +2815 -2815
- package/src/engine/controller/__tests__/interaction.dpr.test.ts +259 -259
- package/src/engine/controller/interaction.ts +722 -722
- package/src/engine/controller/markerInteraction.ts +130 -130
- package/src/engine/controller/pinchTracker.ts +82 -82
- package/src/engine/controller/tooltipPosition.ts +48 -48
- package/src/engine/draw/__tests__/pixelAlign.spec.ts +176 -176
- package/src/engine/draw/pixelAlign.ts +259 -259
- package/src/engine/drawing/index.ts +655 -655
- package/src/engine/drawing/interaction.ts +842 -842
- package/src/engine/drawing/plugin.ts +343 -343
- package/src/engine/indicators/__tests__/__fixtures__/golden/atr.json +38 -38
- package/src/engine/indicators/__tests__/__fixtures__/golden/dema.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/hma.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/index.ts +55 -55
- package/src/engine/indicators/__tests__/__fixtures__/golden/kama.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/tema.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/wma.json +40 -40
- package/src/engine/indicators/__tests__/__fixtures__/synthetic.ts +65 -65
- package/src/engine/indicators/__tests__/_propertyAssertions.ts +76 -76
- package/src/engine/indicators/__tests__/atr.test.ts +153 -153
- package/src/engine/indicators/__tests__/calculators.test.ts +614 -614
- package/src/engine/indicators/__tests__/cmf-mfi.test.ts +100 -100
- package/src/engine/indicators/__tests__/dema.test.ts +73 -73
- package/src/engine/indicators/__tests__/donchian.test.ts +70 -70
- package/src/engine/indicators/__tests__/hma.test.ts +73 -73
- package/src/engine/indicators/__tests__/ichimoku.test.ts +105 -105
- package/src/engine/indicators/__tests__/kama.test.ts +80 -80
- package/src/engine/indicators/__tests__/keltner.test.ts +65 -65
- package/src/engine/indicators/__tests__/pivot-fib.test.ts +110 -110
- package/src/engine/indicators/__tests__/roc.test.ts +68 -68
- package/src/engine/indicators/__tests__/sar.test.ts +86 -86
- package/src/engine/indicators/__tests__/scheduler.test.ts +831 -831
- package/src/engine/indicators/__tests__/soa.test.ts +533 -533
- package/src/engine/indicators/__tests__/structure.test.ts +110 -110
- package/src/engine/indicators/__tests__/supertrend.test.ts +65 -65
- package/src/engine/indicators/__tests__/tema.test.ts +68 -68
- package/src/engine/indicators/__tests__/trix.test.ts +70 -70
- package/src/engine/indicators/__tests__/volatility.test.ts +117 -117
- package/src/engine/indicators/__tests__/volume.test.ts +115 -115
- package/src/engine/indicators/__tests__/volumeProfile.test.ts +74 -74
- package/src/engine/indicators/__tests__/vwap.test.ts +69 -69
- package/src/engine/indicators/__tests__/wma.test.ts +112 -112
- package/src/engine/indicators/__tests__/zones.test.ts +95 -95
- package/src/engine/indicators/atrState.ts +27 -27
- package/src/engine/indicators/bollState.ts +51 -51
- package/src/engine/indicators/calculators.ts +2593 -2593
- package/src/engine/indicators/cciState.ts +25 -25
- package/src/engine/indicators/chaikinVolState.ts +32 -32
- package/src/engine/indicators/cmfState.ts +27 -27
- package/src/engine/indicators/demaState.ts +27 -27
- package/src/engine/indicators/donchianState.ts +43 -43
- package/src/engine/indicators/eneState.ts +43 -43
- package/src/engine/indicators/expmaState.ts +43 -43
- package/src/engine/indicators/fastkState.ts +25 -25
- package/src/engine/indicators/fibState.ts +41 -41
- package/src/engine/indicators/hmaState.ts +27 -27
- package/src/engine/indicators/hvState.ts +28 -28
- package/src/engine/indicators/ichimokuState.ts +70 -70
- package/src/engine/indicators/indicator.worker.ts +169 -169
- package/src/engine/indicators/indicatorDefinitionRegistry.ts +62 -62
- package/src/engine/indicators/indicatorMetadata.ts +110 -110
- package/src/engine/indicators/indicatorRegistry.ts +106 -106
- package/src/engine/indicators/indicatorRuntime.ts +1548 -1548
- package/src/engine/indicators/kamaState.ts +34 -34
- package/src/engine/indicators/keltnerState.ts +49 -49
- package/src/engine/indicators/kstState.ts +42 -42
- package/src/engine/indicators/maState.ts +36 -36
- package/src/engine/indicators/macdState.ts +76 -76
- package/src/engine/indicators/mfiState.ts +27 -27
- package/src/engine/indicators/momState.ts +25 -25
- package/src/engine/indicators/obvState.ts +25 -25
- package/src/engine/indicators/parkinsonState.ts +28 -28
- package/src/engine/indicators/pivotState.ts +51 -51
- package/src/engine/indicators/pvtState.ts +25 -25
- package/src/engine/indicators/rocState.ts +27 -27
- package/src/engine/indicators/rsiState.ts +65 -65
- package/src/engine/indicators/sarState.ts +41 -41
- package/src/engine/indicators/scheduler.ts +1205 -1205
- package/src/engine/indicators/soa.ts +352 -352
- package/src/engine/indicators/stateComposer.ts +1262 -1262
- package/src/engine/indicators/stochState.ts +26 -26
- package/src/engine/indicators/structureState.ts +69 -69
- package/src/engine/indicators/supertrendState.ts +37 -37
- package/src/engine/indicators/temaState.ts +27 -27
- package/src/engine/indicators/trixState.ts +35 -35
- package/src/engine/indicators/vmaState.ts +27 -27
- package/src/engine/indicators/volumeProfileState.ts +63 -63
- package/src/engine/indicators/vwapState.ts +29 -29
- package/src/engine/indicators/wmaState.ts +27 -27
- package/src/engine/indicators/wmsrState.ts +25 -25
- package/src/engine/indicators/workerProtocol.ts +613 -613
- package/src/engine/indicators/zonesState.ts +47 -47
- package/src/engine/layout/pane.ts +161 -161
- package/src/engine/marker/registry.ts +265 -265
- package/src/engine/paneRenderer.ts +169 -169
- package/src/engine/renderers/Indicator/atr.ts +237 -237
- package/src/engine/renderers/Indicator/boll.ts +317 -317
- package/src/engine/renderers/Indicator/cci.ts +275 -275
- package/src/engine/renderers/Indicator/chaikinVol.ts +138 -138
- package/src/engine/renderers/Indicator/cmf.ts +137 -137
- package/src/engine/renderers/Indicator/dema.ts +136 -136
- package/src/engine/renderers/Indicator/donchian.ts +137 -137
- package/src/engine/renderers/Indicator/ene.ts +271 -271
- package/src/engine/renderers/Indicator/expma.ts +197 -197
- package/src/engine/renderers/Indicator/fastk.ts +316 -316
- package/src/engine/renderers/Indicator/fib.ts +141 -141
- package/src/engine/renderers/Indicator/hma.ts +136 -136
- package/src/engine/renderers/Indicator/hv.ts +124 -124
- package/src/engine/renderers/Indicator/ichimoku.ts +181 -181
- package/src/engine/renderers/Indicator/index.ts +241 -241
- package/src/engine/renderers/Indicator/indicatorData.ts +650 -650
- package/src/engine/renderers/Indicator/kama.ts +136 -136
- package/src/engine/renderers/Indicator/keltner.ts +137 -137
- package/src/engine/renderers/Indicator/kst.ts +302 -302
- package/src/engine/renderers/Indicator/ma.ts +200 -200
- package/src/engine/renderers/Indicator/macd.ts +477 -477
- package/src/engine/renderers/Indicator/macdLegend.ts +141 -141
- package/src/engine/renderers/Indicator/mainIndicatorLegend.ts +272 -272
- package/src/engine/renderers/Indicator/mfi.ts +142 -142
- package/src/engine/renderers/Indicator/mom.ts +311 -311
- package/src/engine/renderers/Indicator/obv.ts +123 -123
- package/src/engine/renderers/Indicator/parkinson.ts +124 -124
- package/src/engine/renderers/Indicator/pivot.ts +131 -131
- package/src/engine/renderers/Indicator/pvt.ts +123 -123
- package/src/engine/renderers/Indicator/roc.ts +143 -143
- package/src/engine/renderers/Indicator/rsi.ts +390 -390
- package/src/engine/renderers/Indicator/sar.ts +113 -113
- package/src/engine/renderers/Indicator/scale/atr_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/cci_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/fastk_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/indicator_scale.ts +204 -204
- package/src/engine/renderers/Indicator/scale/kst_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/macd_scale.ts +22 -22
- package/src/engine/renderers/Indicator/scale/mom_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/rsi_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/stoch_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/volume_scale.ts +26 -26
- package/src/engine/renderers/Indicator/scale/wmsr_scale.ts +19 -19
- package/src/engine/renderers/Indicator/stoch.ts +359 -359
- package/src/engine/renderers/Indicator/structure.ts +126 -126
- package/src/engine/renderers/Indicator/subPaneConfig.ts +265 -265
- package/src/engine/renderers/Indicator/supertrend.ts +115 -115
- package/src/engine/renderers/Indicator/tema.ts +136 -136
- package/src/engine/renderers/Indicator/trix.ts +158 -158
- package/src/engine/renderers/Indicator/vma.ts +124 -124
- package/src/engine/renderers/Indicator/volumeProfile.ts +125 -125
- package/src/engine/renderers/Indicator/vwap.ts +123 -123
- package/src/engine/renderers/Indicator/wma.ts +136 -136
- package/src/engine/renderers/Indicator/wmsr.ts +328 -328
- package/src/engine/renderers/Indicator/zones.ts +104 -104
- package/src/engine/renderers/__tests__/boll.renderer.test.ts +314 -314
- package/src/engine/renderers/__tests__/ene.renderer.test.ts +305 -305
- package/src/engine/renderers/__tests__/expma.renderer.test.ts +279 -279
- package/src/engine/renderers/__tests__/ma.renderer.test.ts +426 -426
- package/src/engine/renderers/__tests__/mainIndicatorLegend.renderer.test.ts +502 -502
- package/src/engine/renderers/__tests__/yAxis.renderer.test.ts +173 -173
- package/src/engine/renderers/candle.ts +459 -459
- package/src/engine/renderers/crosshair.ts +69 -69
- package/src/engine/renderers/customMarkers.ts +162 -162
- package/src/engine/renderers/extremaMarkers.ts +246 -246
- package/src/engine/renderers/gridLines.ts +90 -90
- package/src/engine/renderers/lastPrice.ts +97 -97
- package/src/engine/renderers/paneTitle.ts +136 -136
- package/src/engine/renderers/subVolume.ts +236 -236
- package/src/engine/renderers/timeAxis.ts +121 -121
- package/src/engine/renderers/webgl/candleSurface.ts +955 -955
- package/src/engine/renderers/webgl/sharedWebGLSurface.ts +146 -146
- package/src/engine/renderers/yAxis.ts +105 -105
- package/src/engine/scale/__tests__/logFormula.spec.ts +148 -148
- package/src/engine/scale/logFormula.ts +130 -130
- package/src/engine/scale/price.ts +39 -39
- package/src/engine/scale/priceScale.ts +264 -264
- package/src/engine/subPaneManager.ts +427 -427
- package/src/engine/theme/colors.ts +642 -642
- package/src/engine/theme/fonts.ts +20 -20
- package/src/engine/utils/klineConfig.ts +49 -49
- package/src/engine/utils/tickCount.ts +11 -11
- package/src/engine/utils/tickPosition.ts +214 -214
- package/src/engine/utils/zoom.ts +83 -83
- package/src/engine/viewport/viewport.ts +67 -67
- package/src/index.ts +3 -3
- package/src/plugin/ConfigManager.ts +93 -93
- package/src/plugin/EventBus.ts +77 -77
- package/src/plugin/HookSystem.ts +106 -106
- package/src/plugin/PluginHost.ts +243 -243
- package/src/plugin/PluginRegistry.ts +92 -92
- package/src/plugin/StateStore.ts +73 -73
- package/src/plugin/index.ts +19 -19
- package/src/plugin/rendererPluginManager.ts +368 -368
- package/src/plugin/stateKeys.ts +8 -8
- package/src/plugin/types.ts +526 -526
- package/src/reactivity/index.ts +2 -2
- package/src/reactivity/signal.ts +119 -119
- package/src/semantic/controller.ts +251 -251
- package/src/semantic/drawShape.ts +260 -260
- package/src/semantic/index.ts +28 -28
- package/src/semantic/schema.json +256 -256
- package/src/semantic/types.ts +251 -251
- package/src/semantic/validator.ts +349 -349
- package/src/types/kLine.ts +13 -13
- package/src/types/price.ts +56 -56
- package/src/types/volumePrice.ts +33 -33
- package/src/utils/dateFormat.ts +208 -208
- package/src/utils/kLineDraw/axis.ts +562 -562
- package/src/utils/priceToY.ts +34 -34
- package/src/utils/volumePrice.ts +202 -202
- package/src/version.ts +1 -1
|
@@ -1,502 +1,502 @@
|
|
|
1
|
-
// @ts-nocheck - Test file with intentional type relaxations for mocking
|
|
2
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
3
|
-
import { createMainIndicatorLegendRendererPlugin } from '../Indicator/mainIndicatorLegend'
|
|
4
|
-
import { MA_STATE_KEY, type MARenderState } from '@/core/indicators/maState'
|
|
5
|
-
import { BOLL_STATE_KEY } from '@/core/indicators/bollState'
|
|
6
|
-
import { EXPMA_STATE_KEY } from '@/core/indicators/expmaState'
|
|
7
|
-
import { ENE_STATE_KEY } from '@/core/indicators/eneState'
|
|
8
|
-
import { MA_COLORS } from '@/core/theme/colors'
|
|
9
|
-
import type { PluginHost, RenderContext, RendererPluginWithHost } from '@/plugin'
|
|
10
|
-
import type { KLineData } from '@/types/price'
|
|
11
|
-
import type { Pane } from '@/core/layout/pane'
|
|
12
|
-
|
|
13
|
-
// Type helper for tests - we know these methods exist on the implementation
|
|
14
|
-
interface TestableLegendRenderer extends RendererPluginWithHost {
|
|
15
|
-
draw: (context: RenderContext) => void
|
|
16
|
-
getConfig: () => { yPaddingPx: number; indicators: Record<string, { enabled: boolean; params: Record<string, unknown> }> }
|
|
17
|
-
setConfig: (config: Record<string, unknown>) => void
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 创建 mock canvas context
|
|
22
|
-
*/
|
|
23
|
-
function createMockCanvasContext(): CanvasRenderingContext2D {
|
|
24
|
-
const measureTextMock = vi.fn().mockReturnValue({ width: 50 })
|
|
25
|
-
|
|
26
|
-
return {
|
|
27
|
-
save: vi.fn(),
|
|
28
|
-
restore: vi.fn(),
|
|
29
|
-
fillText: vi.fn(),
|
|
30
|
-
measureText: measureTextMock,
|
|
31
|
-
font: '',
|
|
32
|
-
fillStyle: '',
|
|
33
|
-
textAlign: '',
|
|
34
|
-
} as unknown as CanvasRenderingContext2D
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 创建 mock PluginHost
|
|
39
|
-
*/
|
|
40
|
-
function createMockPluginHost(state?: MARenderState): PluginHost {
|
|
41
|
-
return {
|
|
42
|
-
setSharedState: vi.fn(),
|
|
43
|
-
getSharedState: vi.fn(<T>(key: string): T | undefined => {
|
|
44
|
-
if (key === MA_STATE_KEY) {
|
|
45
|
-
return state as T
|
|
46
|
-
}
|
|
47
|
-
return undefined
|
|
48
|
-
}),
|
|
49
|
-
clearByOwner: vi.fn(),
|
|
50
|
-
getCanvas: vi.fn(),
|
|
51
|
-
getMainPane: vi.fn(),
|
|
52
|
-
getSubPane: vi.fn(),
|
|
53
|
-
getAllSubPanes: vi.fn(),
|
|
54
|
-
getTheme: vi.fn(),
|
|
55
|
-
getStyles: vi.fn(),
|
|
56
|
-
getBarStyles: vi.fn(),
|
|
57
|
-
getConfig: vi.fn(),
|
|
58
|
-
setConfig: vi.fn(),
|
|
59
|
-
on: vi.fn(),
|
|
60
|
-
off: vi.fn(),
|
|
61
|
-
once: vi.fn(),
|
|
62
|
-
emit: vi.fn(),
|
|
63
|
-
} as unknown as PluginHost
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* 创建 mock RenderContext
|
|
68
|
-
*/
|
|
69
|
-
function createMockRenderContext(
|
|
70
|
-
ctx: CanvasRenderingContext2D,
|
|
71
|
-
overrides: Partial<RenderContext> = {}
|
|
72
|
-
): RenderContext {
|
|
73
|
-
const mockPane = {
|
|
74
|
-
yAxis: {
|
|
75
|
-
priceToY: (price: number) => price * 10,
|
|
76
|
-
},
|
|
77
|
-
} as unknown as Pane
|
|
78
|
-
|
|
79
|
-
const defaultKLineData: KLineData[] = Array.from({ length: 100 }, (_, i) => ({
|
|
80
|
-
timestamp: 1000000000000 + i * 60000,
|
|
81
|
-
open: 100 + i,
|
|
82
|
-
high: 101 + i,
|
|
83
|
-
low: 99 + i,
|
|
84
|
-
close: 100 + i,
|
|
85
|
-
volume: 1000 + i * 100,
|
|
86
|
-
}))
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
ctx,
|
|
90
|
-
// f5f5706 moved legend rendering to the overlay layer; renderer now reads
|
|
91
|
-
// `overlayCtx`. Mock both to the same spy canvas so the existing
|
|
92
|
-
// `vi.mocked(ctx.fillText).mock.calls` assertions continue to capture writes.
|
|
93
|
-
overlayCtx: ctx,
|
|
94
|
-
data: defaultKLineData,
|
|
95
|
-
range: { start: 0, end: 100 },
|
|
96
|
-
visibleRange: { start: 0, end: 100 },
|
|
97
|
-
crosshair: null,
|
|
98
|
-
crosshairIndex: null,
|
|
99
|
-
dpr: 1,
|
|
100
|
-
scrollLeft: 0,
|
|
101
|
-
pane: mockPane,
|
|
102
|
-
kLineCenters: Array.from({ length: 100 }, (_, i) => i * 10 + 5),
|
|
103
|
-
...overrides,
|
|
104
|
-
} as RenderContext
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 创建测试用的 MARenderState
|
|
109
|
-
*/
|
|
110
|
-
function createTestMARenderState(
|
|
111
|
-
overrides: Partial<MARenderState> = {}
|
|
112
|
-
): MARenderState {
|
|
113
|
-
const series: Record<number, (number | undefined)[]> = {
|
|
114
|
-
5: Array.from({ length: 100 }, () => 105),
|
|
115
|
-
10: Array.from({ length: 100 }, () => 110),
|
|
116
|
-
20: Array.from({ length: 100 }, () => 120),
|
|
117
|
-
30: Array.from({ length: 100 }, () => 130),
|
|
118
|
-
60: Array.from({ length: 100 }, () => 160),
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Add some undefined values at the beginning
|
|
122
|
-
for (let i = 0; i < 4; i++) series[5][i] = undefined
|
|
123
|
-
for (let i = 0; i < 9; i++) series[10][i] = undefined
|
|
124
|
-
for (let i = 0; i < 19; i++) series[20][i] = undefined
|
|
125
|
-
|
|
126
|
-
return {
|
|
127
|
-
timestamp: Date.now(),
|
|
128
|
-
series,
|
|
129
|
-
enabledPeriods: [5, 10, 20, 30, 60],
|
|
130
|
-
visibleMin: 105,
|
|
131
|
-
visibleMax: 160,
|
|
132
|
-
...overrides,
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
describe('createMainIndicatorLegendRendererPlugin', () => {
|
|
137
|
-
it('should create a renderer plugin with correct metadata', () => {
|
|
138
|
-
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
139
|
-
|
|
140
|
-
expect(plugin.name).toBe('mainIndicatorLegend')
|
|
141
|
-
expect(plugin.version).toBe('2.1.0')
|
|
142
|
-
expect(plugin.description).toBe('主图指标图例渲染器(MA 数据来自 StateStore)')
|
|
143
|
-
expect(plugin.debugName).toBe('主图指标图例')
|
|
144
|
-
expect(plugin.paneId).toBe('main')
|
|
145
|
-
expect(plugin.enabled).toBe(true)
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
it('should have onInstall method', () => {
|
|
149
|
-
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
150
|
-
expect(typeof plugin.onInstall).toBe('function')
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
it('should declare all indicator namespace keys', () => {
|
|
154
|
-
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
155
|
-
expect(plugin.getDeclaredNamespaces()).toEqual([
|
|
156
|
-
MA_STATE_KEY,
|
|
157
|
-
BOLL_STATE_KEY,
|
|
158
|
-
EXPMA_STATE_KEY,
|
|
159
|
-
ENE_STATE_KEY,
|
|
160
|
-
])
|
|
161
|
-
})
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
describe('MainIndicatorLegend draw', () => {
|
|
165
|
-
let ctx: CanvasRenderingContext2D
|
|
166
|
-
let plugin: TestableLegendRenderer
|
|
167
|
-
|
|
168
|
-
beforeEach(() => {
|
|
169
|
-
ctx = createMockCanvasContext()
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
it('should not draw MA when MA is disabled', () => {
|
|
173
|
-
const state = createTestMARenderState()
|
|
174
|
-
const mockHost = createMockPluginHost(state)
|
|
175
|
-
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
176
|
-
plugin.onInstall(mockHost)
|
|
177
|
-
|
|
178
|
-
// Disable MA
|
|
179
|
-
plugin.setConfig({
|
|
180
|
-
indicators: { MA: { enabled: false, params: {} } },
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
const context = createMockRenderContext(ctx)
|
|
184
|
-
plugin.draw(context)
|
|
185
|
-
|
|
186
|
-
// Should not draw any MA legend text
|
|
187
|
-
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
188
|
-
const maLabelCalls = fillTextCalls.filter(call => call[0] === 'MA')
|
|
189
|
-
expect(maLabelCalls).toHaveLength(0)
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
it('should draw MA values from StateStore', () => {
|
|
193
|
-
const state = createTestMARenderState()
|
|
194
|
-
const mockHost = createMockPluginHost(state)
|
|
195
|
-
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
196
|
-
plugin.onInstall(mockHost)
|
|
197
|
-
|
|
198
|
-
const context = createMockRenderContext(ctx)
|
|
199
|
-
plugin.draw(context)
|
|
200
|
-
|
|
201
|
-
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
202
|
-
|
|
203
|
-
// Should have drawn 'MA' label
|
|
204
|
-
const maLabelCalls = fillTextCalls.filter(call => call[0] === 'MA')
|
|
205
|
-
expect(maLabelCalls).toHaveLength(1)
|
|
206
|
-
|
|
207
|
-
// Should have drawn MA5, MA10, etc. labels
|
|
208
|
-
const ma5Calls = fillTextCalls.filter(call => String(call[0]).includes('MA5'))
|
|
209
|
-
expect(ma5Calls.length).toBeGreaterThan(0)
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
it('should use crosshairIndex when available', () => {
|
|
213
|
-
const state = createTestMARenderState({
|
|
214
|
-
series: {
|
|
215
|
-
5: Array.from({ length: 100 }, (_, i) => 100 + i),
|
|
216
|
-
},
|
|
217
|
-
enabledPeriods: [5],
|
|
218
|
-
})
|
|
219
|
-
const mockHost = createMockPluginHost(state)
|
|
220
|
-
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
221
|
-
plugin.onInstall(mockHost)
|
|
222
|
-
|
|
223
|
-
// Use crosshair at index 50
|
|
224
|
-
const context = createMockRenderContext(ctx, { crosshairIndex: 50 })
|
|
225
|
-
plugin.draw(context)
|
|
226
|
-
|
|
227
|
-
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
228
|
-
|
|
229
|
-
// Should show value 150 at index 50 (100 + 50)
|
|
230
|
-
const maValueCalls = fillTextCalls.filter(call =>
|
|
231
|
-
String(call[0]).includes('150.00')
|
|
232
|
-
)
|
|
233
|
-
expect(maValueCalls.length).toBeGreaterThan(0)
|
|
234
|
-
})
|
|
235
|
-
|
|
236
|
-
it('should use last index when crosshairIndex is null', () => {
|
|
237
|
-
const state = createTestMARenderState({
|
|
238
|
-
series: {
|
|
239
|
-
5: Array.from({ length: 10 }, (_, i) => 100 + i),
|
|
240
|
-
},
|
|
241
|
-
enabledPeriods: [5],
|
|
242
|
-
})
|
|
243
|
-
const mockHost = createMockPluginHost(state)
|
|
244
|
-
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
245
|
-
plugin.onInstall(mockHost)
|
|
246
|
-
|
|
247
|
-
const context = createMockRenderContext(ctx, {
|
|
248
|
-
crosshairIndex: null,
|
|
249
|
-
range: { start: 0, end: 10 },
|
|
250
|
-
data: Array.from({ length: 10 }, (_, i) => ({
|
|
251
|
-
timestamp: 1000000000000 + i * 60000,
|
|
252
|
-
open: 100 + i,
|
|
253
|
-
high: 101 + i,
|
|
254
|
-
low: 99 + i,
|
|
255
|
-
close: 100 + i,
|
|
256
|
-
volume: 1000,
|
|
257
|
-
})),
|
|
258
|
-
})
|
|
259
|
-
plugin.draw(context)
|
|
260
|
-
|
|
261
|
-
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
262
|
-
|
|
263
|
-
// Should show last value (109) at index 9
|
|
264
|
-
const maValueCalls = fillTextCalls.filter(call =>
|
|
265
|
-
String(call[0]).includes('109.00')
|
|
266
|
-
)
|
|
267
|
-
expect(maValueCalls.length).toBeGreaterThan(0)
|
|
268
|
-
})
|
|
269
|
-
|
|
270
|
-
it('should not crash when StateStore is empty', () => {
|
|
271
|
-
const mockHost = createMockPluginHost(undefined)
|
|
272
|
-
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
273
|
-
plugin.onInstall(mockHost)
|
|
274
|
-
|
|
275
|
-
const context = createMockRenderContext(ctx)
|
|
276
|
-
|
|
277
|
-
// Should not throw
|
|
278
|
-
expect(() => plugin.draw(context)).not.toThrow()
|
|
279
|
-
})
|
|
280
|
-
|
|
281
|
-
it('should not draw MA when state has no valid data', () => {
|
|
282
|
-
const state = createTestMARenderState({
|
|
283
|
-
visibleMin: Infinity,
|
|
284
|
-
visibleMax: -Infinity,
|
|
285
|
-
enabledPeriods: [],
|
|
286
|
-
})
|
|
287
|
-
const mockHost = createMockPluginHost(state)
|
|
288
|
-
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
289
|
-
plugin.onInstall(mockHost)
|
|
290
|
-
|
|
291
|
-
const context = createMockRenderContext(ctx)
|
|
292
|
-
plugin.draw(context)
|
|
293
|
-
|
|
294
|
-
// Should not draw any MA values
|
|
295
|
-
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
296
|
-
const ma5Calls = fillTextCalls.filter(call => String(call[0]).includes('MA5'))
|
|
297
|
-
expect(ma5Calls).toHaveLength(0)
|
|
298
|
-
})
|
|
299
|
-
|
|
300
|
-
it('should display values with 2 decimal places', () => {
|
|
301
|
-
const state = createTestMARenderState({
|
|
302
|
-
series: {
|
|
303
|
-
5: Array.from({ length: 100 }, () => 123.4567),
|
|
304
|
-
},
|
|
305
|
-
enabledPeriods: [5],
|
|
306
|
-
})
|
|
307
|
-
const mockHost = createMockPluginHost(state)
|
|
308
|
-
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
309
|
-
plugin.onInstall(mockHost)
|
|
310
|
-
|
|
311
|
-
const context = createMockRenderContext(ctx)
|
|
312
|
-
plugin.draw(context)
|
|
313
|
-
|
|
314
|
-
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
315
|
-
|
|
316
|
-
// Should show formatted value
|
|
317
|
-
const formattedValueCalls = fillTextCalls.filter(call =>
|
|
318
|
-
String(call[0]).includes('123.46')
|
|
319
|
-
)
|
|
320
|
-
expect(formattedValueCalls.length).toBeGreaterThan(0)
|
|
321
|
-
})
|
|
322
|
-
|
|
323
|
-
it('should use correct colors for each MA period', () => {
|
|
324
|
-
const state = createTestMARenderState()
|
|
325
|
-
const mockHost = createMockPluginHost(state)
|
|
326
|
-
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
327
|
-
plugin.onInstall(mockHost)
|
|
328
|
-
|
|
329
|
-
const context = createMockRenderContext(ctx)
|
|
330
|
-
plugin.draw(context)
|
|
331
|
-
|
|
332
|
-
const fillStyleSetter = vi.mocked(ctx).fillStyle as unknown as ReturnType<typeof vi.fn>
|
|
333
|
-
|
|
334
|
-
// Should have set fillStyle for each period's color
|
|
335
|
-
expect(fillStyleSetter).not.toBeUndefined()
|
|
336
|
-
})
|
|
337
|
-
|
|
338
|
-
it('should save and restore context', () => {
|
|
339
|
-
const state = createTestMARenderState()
|
|
340
|
-
const mockHost = createMockPluginHost(state)
|
|
341
|
-
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
342
|
-
plugin.onInstall(mockHost)
|
|
343
|
-
|
|
344
|
-
const context = createMockRenderContext(ctx)
|
|
345
|
-
plugin.draw(context)
|
|
346
|
-
|
|
347
|
-
expect(ctx.save).toHaveBeenCalledTimes(1)
|
|
348
|
-
expect(ctx.restore).toHaveBeenCalledTimes(1)
|
|
349
|
-
})
|
|
350
|
-
})
|
|
351
|
-
|
|
352
|
-
describe('MainIndicatorLegend MA data source', () => {
|
|
353
|
-
it('should read from StateStore instead of calculating', () => {
|
|
354
|
-
const state = createTestMARenderState({
|
|
355
|
-
series: {
|
|
356
|
-
5: [undefined, undefined, undefined, undefined, 999.99],
|
|
357
|
-
},
|
|
358
|
-
enabledPeriods: [5],
|
|
359
|
-
})
|
|
360
|
-
const mockGetSharedState = vi.fn().mockReturnValue(state)
|
|
361
|
-
const mockHost = {
|
|
362
|
-
setSharedState: vi.fn(),
|
|
363
|
-
getSharedState: mockGetSharedState,
|
|
364
|
-
clearByOwner: vi.fn(),
|
|
365
|
-
} as unknown as PluginHost
|
|
366
|
-
|
|
367
|
-
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
368
|
-
plugin.onInstall(mockHost)
|
|
369
|
-
|
|
370
|
-
const ctx = createMockCanvasContext()
|
|
371
|
-
const context = createMockRenderContext(ctx, {
|
|
372
|
-
crosshairIndex: 4,
|
|
373
|
-
range: { start: 0, end: 5 },
|
|
374
|
-
})
|
|
375
|
-
plugin.draw(context)
|
|
376
|
-
|
|
377
|
-
// Verify it read from StateStore
|
|
378
|
-
expect(mockGetSharedState).toHaveBeenCalledWith(MA_STATE_KEY)
|
|
379
|
-
|
|
380
|
-
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
381
|
-
|
|
382
|
-
// Should show the value from StateStore (999.99), not a calculated value
|
|
383
|
-
const valueCalls = fillTextCalls.filter(call =>
|
|
384
|
-
String(call[0]).includes('999.99')
|
|
385
|
-
)
|
|
386
|
-
expect(valueCalls.length).toBeGreaterThan(0)
|
|
387
|
-
})
|
|
388
|
-
})
|
|
389
|
-
|
|
390
|
-
describe('MainIndicatorLegend config management', () => {
|
|
391
|
-
it('getConfig should return current config', () => {
|
|
392
|
-
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
393
|
-
|
|
394
|
-
const config = plugin.getConfig()
|
|
395
|
-
|
|
396
|
-
expect(config.yPaddingPx).toBe(20)
|
|
397
|
-
expect(config.indicators.MA.enabled).toBe(true)
|
|
398
|
-
expect(config.indicators.BOLL.enabled).toBe(false)
|
|
399
|
-
})
|
|
400
|
-
|
|
401
|
-
it('setConfig should update yPaddingPx', () => {
|
|
402
|
-
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
403
|
-
|
|
404
|
-
plugin.setConfig({ yPaddingPx: 30 })
|
|
405
|
-
|
|
406
|
-
const config = plugin.getConfig()
|
|
407
|
-
expect(config.yPaddingPx).toBe(30)
|
|
408
|
-
})
|
|
409
|
-
|
|
410
|
-
it('setConfig should merge indicator config', () => {
|
|
411
|
-
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
412
|
-
|
|
413
|
-
plugin.setConfig({
|
|
414
|
-
indicators: {
|
|
415
|
-
MA: { enabled: false, params: {} },
|
|
416
|
-
BOLL: { enabled: true, params: { period: 26 } },
|
|
417
|
-
},
|
|
418
|
-
})
|
|
419
|
-
|
|
420
|
-
const config = plugin.getConfig()
|
|
421
|
-
expect(config.indicators.MA.enabled).toBe(false)
|
|
422
|
-
expect(config.indicators.BOLL.enabled).toBe(true)
|
|
423
|
-
expect(config.indicators.BOLL.params.period).toBe(26)
|
|
424
|
-
})
|
|
425
|
-
})
|
|
426
|
-
|
|
427
|
-
describe('MainIndicatorLegend with other indicators', () => {
|
|
428
|
-
it('should draw BOLL when enabled', () => {
|
|
429
|
-
const mockHost = createMockPluginHost(undefined)
|
|
430
|
-
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
431
|
-
plugin.onInstall(mockHost)
|
|
432
|
-
|
|
433
|
-
plugin.setConfig({
|
|
434
|
-
indicators: {
|
|
435
|
-
MA: { enabled: false, params: {} },
|
|
436
|
-
BOLL: { enabled: true, params: { period: 20, multiplier: 2 } },
|
|
437
|
-
},
|
|
438
|
-
})
|
|
439
|
-
|
|
440
|
-
const ctx = createMockCanvasContext()
|
|
441
|
-
const context = createMockRenderContext(ctx)
|
|
442
|
-
plugin.draw(context)
|
|
443
|
-
|
|
444
|
-
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
445
|
-
|
|
446
|
-
// Should have drawn BOLL label
|
|
447
|
-
const bollLabelCalls = fillTextCalls.filter(call =>
|
|
448
|
-
String(call[0]).includes('BOLL')
|
|
449
|
-
)
|
|
450
|
-
expect(bollLabelCalls.length).toBeGreaterThan(0)
|
|
451
|
-
})
|
|
452
|
-
|
|
453
|
-
it('should draw EXPMA when enabled', () => {
|
|
454
|
-
const mockHost = createMockPluginHost(undefined)
|
|
455
|
-
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
456
|
-
plugin.onInstall(mockHost)
|
|
457
|
-
|
|
458
|
-
plugin.setConfig({
|
|
459
|
-
indicators: {
|
|
460
|
-
MA: { enabled: false, params: {} },
|
|
461
|
-
EXPMA: { enabled: true, params: { fastPeriod: 12, slowPeriod: 50 } },
|
|
462
|
-
},
|
|
463
|
-
})
|
|
464
|
-
|
|
465
|
-
const ctx = createMockCanvasContext()
|
|
466
|
-
const context = createMockRenderContext(ctx)
|
|
467
|
-
plugin.draw(context)
|
|
468
|
-
|
|
469
|
-
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
470
|
-
|
|
471
|
-
// Should have drawn EXPMA label
|
|
472
|
-
const expmaLabelCalls = fillTextCalls.filter(call =>
|
|
473
|
-
String(call[0]).includes('EXPMA')
|
|
474
|
-
)
|
|
475
|
-
expect(expmaLabelCalls.length).toBeGreaterThan(0)
|
|
476
|
-
})
|
|
477
|
-
|
|
478
|
-
it('should draw ENE when enabled', () => {
|
|
479
|
-
const mockHost = createMockPluginHost(undefined)
|
|
480
|
-
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
481
|
-
plugin.onInstall(mockHost)
|
|
482
|
-
|
|
483
|
-
plugin.setConfig({
|
|
484
|
-
indicators: {
|
|
485
|
-
MA: { enabled: false, params: {} },
|
|
486
|
-
ENE: { enabled: true, params: { period: 10, deviation: 11 } },
|
|
487
|
-
},
|
|
488
|
-
})
|
|
489
|
-
|
|
490
|
-
const ctx = createMockCanvasContext()
|
|
491
|
-
const context = createMockRenderContext(ctx)
|
|
492
|
-
plugin.draw(context)
|
|
493
|
-
|
|
494
|
-
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
495
|
-
|
|
496
|
-
// Should have drawn ENE label
|
|
497
|
-
const eneLabelCalls = fillTextCalls.filter(call =>
|
|
498
|
-
String(call[0]).includes('ENE')
|
|
499
|
-
)
|
|
500
|
-
expect(eneLabelCalls.length).toBeGreaterThan(0)
|
|
501
|
-
})
|
|
502
|
-
})
|
|
1
|
+
// @ts-nocheck - Test file with intentional type relaxations for mocking
|
|
2
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
3
|
+
import { createMainIndicatorLegendRendererPlugin } from '../Indicator/mainIndicatorLegend'
|
|
4
|
+
import { MA_STATE_KEY, type MARenderState } from '@/core/indicators/maState'
|
|
5
|
+
import { BOLL_STATE_KEY } from '@/core/indicators/bollState'
|
|
6
|
+
import { EXPMA_STATE_KEY } from '@/core/indicators/expmaState'
|
|
7
|
+
import { ENE_STATE_KEY } from '@/core/indicators/eneState'
|
|
8
|
+
import { MA_COLORS } from '@/core/theme/colors'
|
|
9
|
+
import type { PluginHost, RenderContext, RendererPluginWithHost } from '@/plugin'
|
|
10
|
+
import type { KLineData } from '@/types/price'
|
|
11
|
+
import type { Pane } from '@/core/layout/pane'
|
|
12
|
+
|
|
13
|
+
// Type helper for tests - we know these methods exist on the implementation
|
|
14
|
+
interface TestableLegendRenderer extends RendererPluginWithHost {
|
|
15
|
+
draw: (context: RenderContext) => void
|
|
16
|
+
getConfig: () => { yPaddingPx: number; indicators: Record<string, { enabled: boolean; params: Record<string, unknown> }> }
|
|
17
|
+
setConfig: (config: Record<string, unknown>) => void
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 创建 mock canvas context
|
|
22
|
+
*/
|
|
23
|
+
function createMockCanvasContext(): CanvasRenderingContext2D {
|
|
24
|
+
const measureTextMock = vi.fn().mockReturnValue({ width: 50 })
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
save: vi.fn(),
|
|
28
|
+
restore: vi.fn(),
|
|
29
|
+
fillText: vi.fn(),
|
|
30
|
+
measureText: measureTextMock,
|
|
31
|
+
font: '',
|
|
32
|
+
fillStyle: '',
|
|
33
|
+
textAlign: '',
|
|
34
|
+
} as unknown as CanvasRenderingContext2D
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 创建 mock PluginHost
|
|
39
|
+
*/
|
|
40
|
+
function createMockPluginHost(state?: MARenderState): PluginHost {
|
|
41
|
+
return {
|
|
42
|
+
setSharedState: vi.fn(),
|
|
43
|
+
getSharedState: vi.fn(<T>(key: string): T | undefined => {
|
|
44
|
+
if (key === MA_STATE_KEY) {
|
|
45
|
+
return state as T
|
|
46
|
+
}
|
|
47
|
+
return undefined
|
|
48
|
+
}),
|
|
49
|
+
clearByOwner: vi.fn(),
|
|
50
|
+
getCanvas: vi.fn(),
|
|
51
|
+
getMainPane: vi.fn(),
|
|
52
|
+
getSubPane: vi.fn(),
|
|
53
|
+
getAllSubPanes: vi.fn(),
|
|
54
|
+
getTheme: vi.fn(),
|
|
55
|
+
getStyles: vi.fn(),
|
|
56
|
+
getBarStyles: vi.fn(),
|
|
57
|
+
getConfig: vi.fn(),
|
|
58
|
+
setConfig: vi.fn(),
|
|
59
|
+
on: vi.fn(),
|
|
60
|
+
off: vi.fn(),
|
|
61
|
+
once: vi.fn(),
|
|
62
|
+
emit: vi.fn(),
|
|
63
|
+
} as unknown as PluginHost
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 创建 mock RenderContext
|
|
68
|
+
*/
|
|
69
|
+
function createMockRenderContext(
|
|
70
|
+
ctx: CanvasRenderingContext2D,
|
|
71
|
+
overrides: Partial<RenderContext> = {}
|
|
72
|
+
): RenderContext {
|
|
73
|
+
const mockPane = {
|
|
74
|
+
yAxis: {
|
|
75
|
+
priceToY: (price: number) => price * 10,
|
|
76
|
+
},
|
|
77
|
+
} as unknown as Pane
|
|
78
|
+
|
|
79
|
+
const defaultKLineData: KLineData[] = Array.from({ length: 100 }, (_, i) => ({
|
|
80
|
+
timestamp: 1000000000000 + i * 60000,
|
|
81
|
+
open: 100 + i,
|
|
82
|
+
high: 101 + i,
|
|
83
|
+
low: 99 + i,
|
|
84
|
+
close: 100 + i,
|
|
85
|
+
volume: 1000 + i * 100,
|
|
86
|
+
}))
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
ctx,
|
|
90
|
+
// f5f5706 moved legend rendering to the overlay layer; renderer now reads
|
|
91
|
+
// `overlayCtx`. Mock both to the same spy canvas so the existing
|
|
92
|
+
// `vi.mocked(ctx.fillText).mock.calls` assertions continue to capture writes.
|
|
93
|
+
overlayCtx: ctx,
|
|
94
|
+
data: defaultKLineData,
|
|
95
|
+
range: { start: 0, end: 100 },
|
|
96
|
+
visibleRange: { start: 0, end: 100 },
|
|
97
|
+
crosshair: null,
|
|
98
|
+
crosshairIndex: null,
|
|
99
|
+
dpr: 1,
|
|
100
|
+
scrollLeft: 0,
|
|
101
|
+
pane: mockPane,
|
|
102
|
+
kLineCenters: Array.from({ length: 100 }, (_, i) => i * 10 + 5),
|
|
103
|
+
...overrides,
|
|
104
|
+
} as RenderContext
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 创建测试用的 MARenderState
|
|
109
|
+
*/
|
|
110
|
+
function createTestMARenderState(
|
|
111
|
+
overrides: Partial<MARenderState> = {}
|
|
112
|
+
): MARenderState {
|
|
113
|
+
const series: Record<number, (number | undefined)[]> = {
|
|
114
|
+
5: Array.from({ length: 100 }, () => 105),
|
|
115
|
+
10: Array.from({ length: 100 }, () => 110),
|
|
116
|
+
20: Array.from({ length: 100 }, () => 120),
|
|
117
|
+
30: Array.from({ length: 100 }, () => 130),
|
|
118
|
+
60: Array.from({ length: 100 }, () => 160),
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Add some undefined values at the beginning
|
|
122
|
+
for (let i = 0; i < 4; i++) series[5][i] = undefined
|
|
123
|
+
for (let i = 0; i < 9; i++) series[10][i] = undefined
|
|
124
|
+
for (let i = 0; i < 19; i++) series[20][i] = undefined
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
timestamp: Date.now(),
|
|
128
|
+
series,
|
|
129
|
+
enabledPeriods: [5, 10, 20, 30, 60],
|
|
130
|
+
visibleMin: 105,
|
|
131
|
+
visibleMax: 160,
|
|
132
|
+
...overrides,
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
describe('createMainIndicatorLegendRendererPlugin', () => {
|
|
137
|
+
it('should create a renderer plugin with correct metadata', () => {
|
|
138
|
+
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
139
|
+
|
|
140
|
+
expect(plugin.name).toBe('mainIndicatorLegend')
|
|
141
|
+
expect(plugin.version).toBe('2.1.0')
|
|
142
|
+
expect(plugin.description).toBe('主图指标图例渲染器(MA 数据来自 StateStore)')
|
|
143
|
+
expect(plugin.debugName).toBe('主图指标图例')
|
|
144
|
+
expect(plugin.paneId).toBe('main')
|
|
145
|
+
expect(plugin.enabled).toBe(true)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('should have onInstall method', () => {
|
|
149
|
+
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
150
|
+
expect(typeof plugin.onInstall).toBe('function')
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('should declare all indicator namespace keys', () => {
|
|
154
|
+
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
155
|
+
expect(plugin.getDeclaredNamespaces()).toEqual([
|
|
156
|
+
MA_STATE_KEY,
|
|
157
|
+
BOLL_STATE_KEY,
|
|
158
|
+
EXPMA_STATE_KEY,
|
|
159
|
+
ENE_STATE_KEY,
|
|
160
|
+
])
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
describe('MainIndicatorLegend draw', () => {
|
|
165
|
+
let ctx: CanvasRenderingContext2D
|
|
166
|
+
let plugin: TestableLegendRenderer
|
|
167
|
+
|
|
168
|
+
beforeEach(() => {
|
|
169
|
+
ctx = createMockCanvasContext()
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('should not draw MA when MA is disabled', () => {
|
|
173
|
+
const state = createTestMARenderState()
|
|
174
|
+
const mockHost = createMockPluginHost(state)
|
|
175
|
+
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
176
|
+
plugin.onInstall(mockHost)
|
|
177
|
+
|
|
178
|
+
// Disable MA
|
|
179
|
+
plugin.setConfig({
|
|
180
|
+
indicators: { MA: { enabled: false, params: {} } },
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
const context = createMockRenderContext(ctx)
|
|
184
|
+
plugin.draw(context)
|
|
185
|
+
|
|
186
|
+
// Should not draw any MA legend text
|
|
187
|
+
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
188
|
+
const maLabelCalls = fillTextCalls.filter(call => call[0] === 'MA')
|
|
189
|
+
expect(maLabelCalls).toHaveLength(0)
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
it('should draw MA values from StateStore', () => {
|
|
193
|
+
const state = createTestMARenderState()
|
|
194
|
+
const mockHost = createMockPluginHost(state)
|
|
195
|
+
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
196
|
+
plugin.onInstall(mockHost)
|
|
197
|
+
|
|
198
|
+
const context = createMockRenderContext(ctx)
|
|
199
|
+
plugin.draw(context)
|
|
200
|
+
|
|
201
|
+
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
202
|
+
|
|
203
|
+
// Should have drawn 'MA' label
|
|
204
|
+
const maLabelCalls = fillTextCalls.filter(call => call[0] === 'MA')
|
|
205
|
+
expect(maLabelCalls).toHaveLength(1)
|
|
206
|
+
|
|
207
|
+
// Should have drawn MA5, MA10, etc. labels
|
|
208
|
+
const ma5Calls = fillTextCalls.filter(call => String(call[0]).includes('MA5'))
|
|
209
|
+
expect(ma5Calls.length).toBeGreaterThan(0)
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
it('should use crosshairIndex when available', () => {
|
|
213
|
+
const state = createTestMARenderState({
|
|
214
|
+
series: {
|
|
215
|
+
5: Array.from({ length: 100 }, (_, i) => 100 + i),
|
|
216
|
+
},
|
|
217
|
+
enabledPeriods: [5],
|
|
218
|
+
})
|
|
219
|
+
const mockHost = createMockPluginHost(state)
|
|
220
|
+
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
221
|
+
plugin.onInstall(mockHost)
|
|
222
|
+
|
|
223
|
+
// Use crosshair at index 50
|
|
224
|
+
const context = createMockRenderContext(ctx, { crosshairIndex: 50 })
|
|
225
|
+
plugin.draw(context)
|
|
226
|
+
|
|
227
|
+
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
228
|
+
|
|
229
|
+
// Should show value 150 at index 50 (100 + 50)
|
|
230
|
+
const maValueCalls = fillTextCalls.filter(call =>
|
|
231
|
+
String(call[0]).includes('150.00')
|
|
232
|
+
)
|
|
233
|
+
expect(maValueCalls.length).toBeGreaterThan(0)
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
it('should use last index when crosshairIndex is null', () => {
|
|
237
|
+
const state = createTestMARenderState({
|
|
238
|
+
series: {
|
|
239
|
+
5: Array.from({ length: 10 }, (_, i) => 100 + i),
|
|
240
|
+
},
|
|
241
|
+
enabledPeriods: [5],
|
|
242
|
+
})
|
|
243
|
+
const mockHost = createMockPluginHost(state)
|
|
244
|
+
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
245
|
+
plugin.onInstall(mockHost)
|
|
246
|
+
|
|
247
|
+
const context = createMockRenderContext(ctx, {
|
|
248
|
+
crosshairIndex: null,
|
|
249
|
+
range: { start: 0, end: 10 },
|
|
250
|
+
data: Array.from({ length: 10 }, (_, i) => ({
|
|
251
|
+
timestamp: 1000000000000 + i * 60000,
|
|
252
|
+
open: 100 + i,
|
|
253
|
+
high: 101 + i,
|
|
254
|
+
low: 99 + i,
|
|
255
|
+
close: 100 + i,
|
|
256
|
+
volume: 1000,
|
|
257
|
+
})),
|
|
258
|
+
})
|
|
259
|
+
plugin.draw(context)
|
|
260
|
+
|
|
261
|
+
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
262
|
+
|
|
263
|
+
// Should show last value (109) at index 9
|
|
264
|
+
const maValueCalls = fillTextCalls.filter(call =>
|
|
265
|
+
String(call[0]).includes('109.00')
|
|
266
|
+
)
|
|
267
|
+
expect(maValueCalls.length).toBeGreaterThan(0)
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
it('should not crash when StateStore is empty', () => {
|
|
271
|
+
const mockHost = createMockPluginHost(undefined)
|
|
272
|
+
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
273
|
+
plugin.onInstall(mockHost)
|
|
274
|
+
|
|
275
|
+
const context = createMockRenderContext(ctx)
|
|
276
|
+
|
|
277
|
+
// Should not throw
|
|
278
|
+
expect(() => plugin.draw(context)).not.toThrow()
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
it('should not draw MA when state has no valid data', () => {
|
|
282
|
+
const state = createTestMARenderState({
|
|
283
|
+
visibleMin: Infinity,
|
|
284
|
+
visibleMax: -Infinity,
|
|
285
|
+
enabledPeriods: [],
|
|
286
|
+
})
|
|
287
|
+
const mockHost = createMockPluginHost(state)
|
|
288
|
+
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
289
|
+
plugin.onInstall(mockHost)
|
|
290
|
+
|
|
291
|
+
const context = createMockRenderContext(ctx)
|
|
292
|
+
plugin.draw(context)
|
|
293
|
+
|
|
294
|
+
// Should not draw any MA values
|
|
295
|
+
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
296
|
+
const ma5Calls = fillTextCalls.filter(call => String(call[0]).includes('MA5'))
|
|
297
|
+
expect(ma5Calls).toHaveLength(0)
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
it('should display values with 2 decimal places', () => {
|
|
301
|
+
const state = createTestMARenderState({
|
|
302
|
+
series: {
|
|
303
|
+
5: Array.from({ length: 100 }, () => 123.4567),
|
|
304
|
+
},
|
|
305
|
+
enabledPeriods: [5],
|
|
306
|
+
})
|
|
307
|
+
const mockHost = createMockPluginHost(state)
|
|
308
|
+
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
309
|
+
plugin.onInstall(mockHost)
|
|
310
|
+
|
|
311
|
+
const context = createMockRenderContext(ctx)
|
|
312
|
+
plugin.draw(context)
|
|
313
|
+
|
|
314
|
+
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
315
|
+
|
|
316
|
+
// Should show formatted value
|
|
317
|
+
const formattedValueCalls = fillTextCalls.filter(call =>
|
|
318
|
+
String(call[0]).includes('123.46')
|
|
319
|
+
)
|
|
320
|
+
expect(formattedValueCalls.length).toBeGreaterThan(0)
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
it('should use correct colors for each MA period', () => {
|
|
324
|
+
const state = createTestMARenderState()
|
|
325
|
+
const mockHost = createMockPluginHost(state)
|
|
326
|
+
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
327
|
+
plugin.onInstall(mockHost)
|
|
328
|
+
|
|
329
|
+
const context = createMockRenderContext(ctx)
|
|
330
|
+
plugin.draw(context)
|
|
331
|
+
|
|
332
|
+
const fillStyleSetter = vi.mocked(ctx).fillStyle as unknown as ReturnType<typeof vi.fn>
|
|
333
|
+
|
|
334
|
+
// Should have set fillStyle for each period's color
|
|
335
|
+
expect(fillStyleSetter).not.toBeUndefined()
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
it('should save and restore context', () => {
|
|
339
|
+
const state = createTestMARenderState()
|
|
340
|
+
const mockHost = createMockPluginHost(state)
|
|
341
|
+
plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 }) as TestableLegendRenderer
|
|
342
|
+
plugin.onInstall(mockHost)
|
|
343
|
+
|
|
344
|
+
const context = createMockRenderContext(ctx)
|
|
345
|
+
plugin.draw(context)
|
|
346
|
+
|
|
347
|
+
expect(ctx.save).toHaveBeenCalledTimes(1)
|
|
348
|
+
expect(ctx.restore).toHaveBeenCalledTimes(1)
|
|
349
|
+
})
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
describe('MainIndicatorLegend MA data source', () => {
|
|
353
|
+
it('should read from StateStore instead of calculating', () => {
|
|
354
|
+
const state = createTestMARenderState({
|
|
355
|
+
series: {
|
|
356
|
+
5: [undefined, undefined, undefined, undefined, 999.99],
|
|
357
|
+
},
|
|
358
|
+
enabledPeriods: [5],
|
|
359
|
+
})
|
|
360
|
+
const mockGetSharedState = vi.fn().mockReturnValue(state)
|
|
361
|
+
const mockHost = {
|
|
362
|
+
setSharedState: vi.fn(),
|
|
363
|
+
getSharedState: mockGetSharedState,
|
|
364
|
+
clearByOwner: vi.fn(),
|
|
365
|
+
} as unknown as PluginHost
|
|
366
|
+
|
|
367
|
+
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
368
|
+
plugin.onInstall(mockHost)
|
|
369
|
+
|
|
370
|
+
const ctx = createMockCanvasContext()
|
|
371
|
+
const context = createMockRenderContext(ctx, {
|
|
372
|
+
crosshairIndex: 4,
|
|
373
|
+
range: { start: 0, end: 5 },
|
|
374
|
+
})
|
|
375
|
+
plugin.draw(context)
|
|
376
|
+
|
|
377
|
+
// Verify it read from StateStore
|
|
378
|
+
expect(mockGetSharedState).toHaveBeenCalledWith(MA_STATE_KEY)
|
|
379
|
+
|
|
380
|
+
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
381
|
+
|
|
382
|
+
// Should show the value from StateStore (999.99), not a calculated value
|
|
383
|
+
const valueCalls = fillTextCalls.filter(call =>
|
|
384
|
+
String(call[0]).includes('999.99')
|
|
385
|
+
)
|
|
386
|
+
expect(valueCalls.length).toBeGreaterThan(0)
|
|
387
|
+
})
|
|
388
|
+
})
|
|
389
|
+
|
|
390
|
+
describe('MainIndicatorLegend config management', () => {
|
|
391
|
+
it('getConfig should return current config', () => {
|
|
392
|
+
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
393
|
+
|
|
394
|
+
const config = plugin.getConfig()
|
|
395
|
+
|
|
396
|
+
expect(config.yPaddingPx).toBe(20)
|
|
397
|
+
expect(config.indicators.MA.enabled).toBe(true)
|
|
398
|
+
expect(config.indicators.BOLL.enabled).toBe(false)
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
it('setConfig should update yPaddingPx', () => {
|
|
402
|
+
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
403
|
+
|
|
404
|
+
plugin.setConfig({ yPaddingPx: 30 })
|
|
405
|
+
|
|
406
|
+
const config = plugin.getConfig()
|
|
407
|
+
expect(config.yPaddingPx).toBe(30)
|
|
408
|
+
})
|
|
409
|
+
|
|
410
|
+
it('setConfig should merge indicator config', () => {
|
|
411
|
+
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
412
|
+
|
|
413
|
+
plugin.setConfig({
|
|
414
|
+
indicators: {
|
|
415
|
+
MA: { enabled: false, params: {} },
|
|
416
|
+
BOLL: { enabled: true, params: { period: 26 } },
|
|
417
|
+
},
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
const config = plugin.getConfig()
|
|
421
|
+
expect(config.indicators.MA.enabled).toBe(false)
|
|
422
|
+
expect(config.indicators.BOLL.enabled).toBe(true)
|
|
423
|
+
expect(config.indicators.BOLL.params.period).toBe(26)
|
|
424
|
+
})
|
|
425
|
+
})
|
|
426
|
+
|
|
427
|
+
describe('MainIndicatorLegend with other indicators', () => {
|
|
428
|
+
it('should draw BOLL when enabled', () => {
|
|
429
|
+
const mockHost = createMockPluginHost(undefined)
|
|
430
|
+
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
431
|
+
plugin.onInstall(mockHost)
|
|
432
|
+
|
|
433
|
+
plugin.setConfig({
|
|
434
|
+
indicators: {
|
|
435
|
+
MA: { enabled: false, params: {} },
|
|
436
|
+
BOLL: { enabled: true, params: { period: 20, multiplier: 2 } },
|
|
437
|
+
},
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
const ctx = createMockCanvasContext()
|
|
441
|
+
const context = createMockRenderContext(ctx)
|
|
442
|
+
plugin.draw(context)
|
|
443
|
+
|
|
444
|
+
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
445
|
+
|
|
446
|
+
// Should have drawn BOLL label
|
|
447
|
+
const bollLabelCalls = fillTextCalls.filter(call =>
|
|
448
|
+
String(call[0]).includes('BOLL')
|
|
449
|
+
)
|
|
450
|
+
expect(bollLabelCalls.length).toBeGreaterThan(0)
|
|
451
|
+
})
|
|
452
|
+
|
|
453
|
+
it('should draw EXPMA when enabled', () => {
|
|
454
|
+
const mockHost = createMockPluginHost(undefined)
|
|
455
|
+
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
456
|
+
plugin.onInstall(mockHost)
|
|
457
|
+
|
|
458
|
+
plugin.setConfig({
|
|
459
|
+
indicators: {
|
|
460
|
+
MA: { enabled: false, params: {} },
|
|
461
|
+
EXPMA: { enabled: true, params: { fastPeriod: 12, slowPeriod: 50 } },
|
|
462
|
+
},
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
const ctx = createMockCanvasContext()
|
|
466
|
+
const context = createMockRenderContext(ctx)
|
|
467
|
+
plugin.draw(context)
|
|
468
|
+
|
|
469
|
+
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
470
|
+
|
|
471
|
+
// Should have drawn EXPMA label
|
|
472
|
+
const expmaLabelCalls = fillTextCalls.filter(call =>
|
|
473
|
+
String(call[0]).includes('EXPMA')
|
|
474
|
+
)
|
|
475
|
+
expect(expmaLabelCalls.length).toBeGreaterThan(0)
|
|
476
|
+
})
|
|
477
|
+
|
|
478
|
+
it('should draw ENE when enabled', () => {
|
|
479
|
+
const mockHost = createMockPluginHost(undefined)
|
|
480
|
+
const plugin = createMainIndicatorLegendRendererPlugin({ yPaddingPx: 20 })
|
|
481
|
+
plugin.onInstall(mockHost)
|
|
482
|
+
|
|
483
|
+
plugin.setConfig({
|
|
484
|
+
indicators: {
|
|
485
|
+
MA: { enabled: false, params: {} },
|
|
486
|
+
ENE: { enabled: true, params: { period: 10, deviation: 11 } },
|
|
487
|
+
},
|
|
488
|
+
})
|
|
489
|
+
|
|
490
|
+
const ctx = createMockCanvasContext()
|
|
491
|
+
const context = createMockRenderContext(ctx)
|
|
492
|
+
plugin.draw(context)
|
|
493
|
+
|
|
494
|
+
const fillTextCalls = vi.mocked(ctx.fillText).mock.calls
|
|
495
|
+
|
|
496
|
+
// Should have drawn ENE label
|
|
497
|
+
const eneLabelCalls = fillTextCalls.filter(call =>
|
|
498
|
+
String(call[0]).includes('ENE')
|
|
499
|
+
)
|
|
500
|
+
expect(eneLabelCalls.length).toBeGreaterThan(0)
|
|
501
|
+
})
|
|
502
|
+
})
|