@veltra/sheet 2.1.1 → 2.3.0

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 (212) hide show
  1. package/dist/components/report/binding-badge.js +100 -0
  2. package/dist/components/report/designer/cell-coords.js +46 -0
  3. package/dist/components/report/designer/col-widths.js +29 -0
  4. package/dist/components/report/designer/conditional-rules/helpers.js +139 -0
  5. package/dist/components/report/designer/drop-highlight-overlay.js +76 -0
  6. package/dist/components/report/designer/float-panel.js +422 -0
  7. package/dist/components/report/designer/role.js +17 -0
  8. package/dist/components/report/designer/rule-preview.js +33 -0
  9. package/dist/components/report/designer/rule-row.js +253 -0
  10. package/dist/components/report/designer/rules-dialog.js +118 -0
  11. package/dist/components/report/designer/topology-overlay.js +122 -0
  12. package/dist/components/report/designer/topology.js +64 -0
  13. package/dist/components/report/designer/use-grid-overlay.js +75 -0
  14. package/dist/components/report/designer-hub.js +177 -0
  15. package/dist/components/report/field-panel-helpers.js +33 -0
  16. package/dist/components/report/field-panel.js +132 -0
  17. package/dist/components/report/filter-bar.js +74 -0
  18. package/dist/components/report/hub-connection-form.js +145 -0
  19. package/dist/components/report/hub-dataset-editor.js +394 -0
  20. package/dist/components/report/preview-grid-size.js +32 -0
  21. package/dist/components/report/report-designer.js +393 -0
  22. package/dist/components/report/report-viewer.js +124 -0
  23. package/dist/components/report/style.d.ts +17 -0
  24. package/dist/components/report/style.js +18 -0
  25. package/dist/components/report/style2.css +874 -0
  26. package/dist/components/report/use-report-designer.js +382 -0
  27. package/dist/components/report/use-report-viewer.js +55 -0
  28. package/dist/{vue → components/sheet}/formula-bar.js +1 -1
  29. package/dist/{vue → components/sheet}/formula-suggest-list.js +1 -1
  30. package/dist/{vue → components/sheet}/import-file.js +1 -1
  31. package/dist/{vue → components/sheet}/insert-count-menu-item.js +1 -1
  32. package/dist/{vue → components/sheet}/insert-image.js +1 -1
  33. package/dist/{vue → components/sheet}/popup-helpers.js +1 -1
  34. package/dist/{vue → components/sheet}/popups/border-popup.js +31 -3
  35. package/dist/{vue → components/sheet}/popups/border-preset-glyph.js +1 -1
  36. package/dist/{vue → components/sheet}/popups/export-popup.js +2 -2
  37. package/dist/{vue → components/sheet}/popups/fill-color-popup.js +3 -3
  38. package/dist/{vue → components/sheet}/popups/find-popup.js +1 -1
  39. package/dist/{vue → components/sheet}/popups/font-color-popup.js +3 -3
  40. package/dist/{vue → components/sheet}/popups/font-size-popup.js +3 -3
  41. package/dist/{vue → components/sheet}/popups/import.worker.d.ts +1 -1
  42. package/dist/{vue → components/sheet}/popups/import.worker.js +1 -1
  43. package/dist/{vue → components/sheet}/popups/insert-image-popup.js +1 -1
  44. package/dist/{vue → components/sheet}/sheet-context-menu.js +7 -4
  45. package/dist/{vue → components/sheet}/sheet-tabs.js +1 -1
  46. package/dist/{vue → components/sheet}/sheet-toolbar.js +1 -1
  47. package/dist/{vue → components/sheet}/sheet.js +16 -1
  48. package/dist/{vue → components/sheet}/use-find-replace.js +1 -1
  49. package/dist/{vue → components/sheet}/use-formula-ref-select.js +1 -1
  50. package/dist/{vue → components/sheet}/use-sheet-grid.js +22 -4
  51. package/dist/{vue → components/sheet}/use-sheet-state.js +6 -5
  52. package/dist/{vue → components/sheet}/use-sheet-tabs-bar.js +1 -1
  53. package/dist/{vue → components/sheet}/use-tool-groups.js +2 -2
  54. package/dist/{vue → components/sheet}/use-tool-popup.js +2 -2
  55. package/dist/{vue → components/sheet}/use-toolbar-scroll.js +1 -1
  56. package/dist/index.d.ts +17 -3
  57. package/dist/index.js +13 -3
  58. package/dist/report/binding.d.ts +83 -0
  59. package/dist/report/binding.js +332 -0
  60. package/dist/report/connector.d.ts +66 -0
  61. package/dist/report/connector.js +159 -0
  62. package/dist/report/export-xlsx.d.ts +17 -0
  63. package/dist/report/export-xlsx.js +92 -0
  64. package/dist/report/filter-bar.d.ts +10 -0
  65. package/dist/report/filter-bar.js +31 -0
  66. package/dist/report/index.d.ts +12 -0
  67. package/dist/report/params.d.ts +13 -0
  68. package/dist/report/params.js +61 -0
  69. package/dist/report/render/aggregate.js +169 -0
  70. package/dist/report/render/builder.d.ts +12 -0
  71. package/dist/report/render/builder.js +139 -0
  72. package/dist/report/render/coordinate.d.ts +25 -0
  73. package/dist/report/render/coordinate.js +733 -0
  74. package/dist/report/render/index.d.ts +14 -0
  75. package/dist/report/render/index.js +21 -0
  76. package/dist/report/render/style-resolver.js +50 -0
  77. package/dist/report/render/template-index.d.ts +24 -0
  78. package/dist/report/render/template-index.js +137 -0
  79. package/dist/report/rules/evaluate.d.ts +25 -0
  80. package/dist/report/rules/evaluate.js +82 -0
  81. package/dist/report/rules/index.d.ts +2 -0
  82. package/dist/report/template.d.ts +63 -0
  83. package/dist/report/template.js +83 -0
  84. package/dist/report/types.d.ts +85 -0
  85. package/dist/tools/apply-style.js +29 -0
  86. package/dist/tools/builtin.js +17 -17
  87. package/dist/tools/context.d.ts +39 -6
  88. package/dist/tools/context.js +38 -1
  89. package/dist/types/index.d.ts +3 -2
  90. package/dist/types/report-designer.d.ts +38 -0
  91. package/dist/types/report-viewer.d.ts +38 -0
  92. package/dist/types/sheet.d.ts +21 -1
  93. package/package.json +6 -8
  94. package/src/components/report/__test__/conditional-rules-helpers.test.ts +88 -0
  95. package/src/components/report/__test__/field-panel-helpers.test.ts +65 -0
  96. package/src/components/report/__test__/float-panel-position.test.ts +45 -0
  97. package/src/components/report/__test__/preview-grid-size.test.ts +63 -0
  98. package/src/components/report/__test__/report-designer.test.ts +464 -0
  99. package/src/components/report/__test__/report-viewer.test.ts +357 -0
  100. package/src/components/report/__test__/topology.test.ts +143 -0
  101. package/src/components/report/__test__/use-report-designer.test.ts +697 -0
  102. package/src/components/report/binding-badge.ts +92 -0
  103. package/src/components/report/designer/cell-coords.ts +94 -0
  104. package/src/components/report/designer/col-widths.ts +56 -0
  105. package/src/components/report/designer/conditional-rules/helpers.ts +189 -0
  106. package/src/components/report/designer/drop-highlight-overlay.vue +74 -0
  107. package/src/components/report/designer/float-panel.vue +431 -0
  108. package/src/components/report/designer/role.ts +32 -0
  109. package/src/components/report/designer/rule-preview.vue +40 -0
  110. package/src/components/report/designer/rule-row.vue +252 -0
  111. package/src/components/report/designer/rules-dialog.vue +110 -0
  112. package/src/components/report/designer/topology-overlay.vue +127 -0
  113. package/src/components/report/designer/topology.ts +124 -0
  114. package/src/components/report/designer/use-grid-overlay.ts +113 -0
  115. package/src/components/report/designer-hub.vue +198 -0
  116. package/src/components/report/field-panel-helpers.ts +44 -0
  117. package/src/components/report/field-panel.vue +136 -0
  118. package/src/components/report/filter-bar.vue +77 -0
  119. package/src/components/report/hub-connection-form.vue +124 -0
  120. package/src/components/report/hub-dataset-editor.vue +369 -0
  121. package/src/components/report/index.ts +2 -0
  122. package/src/components/report/preview-grid-size.ts +33 -0
  123. package/src/components/report/report-designer.vue +420 -0
  124. package/src/components/report/report-viewer.vue +136 -0
  125. package/src/components/report/style-designer.scss +587 -0
  126. package/src/components/report/style-hub.scss +292 -0
  127. package/src/components/report/style-viewer.scss +104 -0
  128. package/src/components/report/style.scss +3 -0
  129. package/src/components/report/style.ts +20 -0
  130. package/src/components/report/use-report-designer.ts +596 -0
  131. package/src/components/report/use-report-viewer.ts +88 -0
  132. package/src/{vue → components/sheet}/__test__/formula-bar.test.ts +37 -18
  133. package/src/{vue → components/sheet}/__test__/insert-image-popup.test.ts +1 -1
  134. package/src/{vue → components/sheet}/__test__/insert-image.test.ts +1 -1
  135. package/src/{vue → components/sheet}/__test__/sheet-component.test.ts +77 -3
  136. package/src/{vue → components/sheet}/__test__/sheet-context-menu.test.ts +4 -3
  137. package/src/{vue → components/sheet}/formula-bar.vue +1 -1
  138. package/src/{vue → components/sheet}/insert-image.ts +1 -1
  139. package/src/{vue → components/sheet}/popup-helpers.ts +1 -1
  140. package/src/{vue → components/sheet}/popups/border-popup.vue +32 -4
  141. package/src/{vue → components/sheet}/popups/export-popup.vue +2 -2
  142. package/src/{vue → components/sheet}/popups/fill-color-popup.vue +3 -3
  143. package/src/{vue → components/sheet}/popups/find-popup.vue +1 -1
  144. package/src/{vue → components/sheet}/popups/font-color-popup.vue +3 -3
  145. package/src/{vue → components/sheet}/popups/font-size-popup.vue +3 -3
  146. package/src/{vue → components/sheet}/popups/insert-image-popup.vue +1 -1
  147. package/src/{vue → components/sheet}/sheet-context-menu.ts +10 -4
  148. package/src/{vue → components/sheet}/sheet-toolbar.vue +2 -2
  149. package/src/{vue → components/sheet}/sheet.vue +6 -3
  150. package/src/{vue → components/sheet}/use-find-replace.ts +1 -1
  151. package/src/{vue → components/sheet}/use-sheet-grid.ts +23 -5
  152. package/src/{vue → components/sheet}/use-sheet-state.ts +11 -6
  153. package/src/{vue → components/sheet}/use-tool-groups.ts +2 -2
  154. package/src/{vue → components/sheet}/use-tool-popup.ts +2 -2
  155. package/src/index.ts +24 -188
  156. package/src/report/__test__/binding.test.ts +320 -0
  157. package/src/report/__test__/connector.test.ts +239 -0
  158. package/src/report/__test__/export-xlsx.test.ts +118 -0
  159. package/src/report/__test__/filter-bar.test.ts +58 -0
  160. package/src/report/__test__/params.test.ts +156 -0
  161. package/src/report/__test__/render.test.ts +830 -0
  162. package/src/report/__test__/rules.test.ts +131 -0
  163. package/src/report/__test__/template.test.ts +366 -0
  164. package/src/report/binding.ts +474 -0
  165. package/src/report/connector.ts +206 -0
  166. package/src/report/export-xlsx.ts +113 -0
  167. package/src/report/filter-bar.ts +36 -0
  168. package/src/report/index.ts +13 -0
  169. package/src/report/params.ts +96 -0
  170. package/src/report/render/__test__/aggregate.test.ts +16 -0
  171. package/src/report/render/__test__/coordinate.test.ts +405 -0
  172. package/src/report/render/aggregate.ts +261 -0
  173. package/src/report/render/builder.ts +211 -0
  174. package/src/report/render/coordinate.ts +1239 -0
  175. package/src/report/render/index.ts +30 -0
  176. package/src/report/render/style-resolver.ts +68 -0
  177. package/src/report/render/template-index.ts +200 -0
  178. package/src/report/rules/evaluate.ts +113 -0
  179. package/src/report/rules/index.ts +7 -0
  180. package/src/report/template.ts +145 -0
  181. package/src/report/types.ts +96 -0
  182. package/src/tools/__test__/apply-style.test.ts +129 -0
  183. package/src/tools/apply-style.ts +41 -0
  184. package/src/tools/builtin.ts +20 -20
  185. package/src/tools/context.ts +67 -4
  186. package/src/tools/index.ts +1 -0
  187. package/src/types/index.ts +2 -6
  188. package/src/types/report-designer.ts +38 -0
  189. package/src/types/report-viewer.ts +37 -0
  190. package/src/types/sheet.ts +26 -1
  191. /package/dist/{vue → components/sheet}/style.d.ts +0 -0
  192. /package/dist/{vue → components/sheet}/style.js +0 -0
  193. /package/dist/{vue → components/sheet}/style2.css +0 -0
  194. /package/dist/{vue → components/sheet}/use-formula-suggest.js +0 -0
  195. /package/src/{vue → components/sheet}/__test__/import-file.test.ts +0 -0
  196. /package/src/{vue → components/sheet}/__test__/insert-count-menu-item.test.ts +0 -0
  197. /package/src/{vue → components/sheet}/__test__/use-formula-ref-select.test.ts +0 -0
  198. /package/src/{vue → components/sheet}/__test__/use-formula-suggest.test.ts +0 -0
  199. /package/src/{vue → components/sheet}/formula-suggest-list.vue +0 -0
  200. /package/src/{vue → components/sheet}/import-file.ts +0 -0
  201. /package/src/{vue → components/sheet}/index.ts +0 -0
  202. /package/src/{vue → components/sheet}/insert-count-menu-item.vue +0 -0
  203. /package/src/{vue → components/sheet}/parsing.ts +0 -0
  204. /package/src/{vue → components/sheet}/popups/border-preset-glyph.vue +0 -0
  205. /package/src/{vue → components/sheet}/popups/import.worker.ts +0 -0
  206. /package/src/{vue → components/sheet}/sheet-tabs.vue +0 -0
  207. /package/src/{vue → components/sheet}/style.scss +0 -0
  208. /package/src/{vue → components/sheet}/style.ts +0 -0
  209. /package/src/{vue → components/sheet}/use-formula-ref-select.ts +0 -0
  210. /package/src/{vue → components/sheet}/use-formula-suggest.ts +0 -0
  211. /package/src/{vue → components/sheet}/use-sheet-tabs-bar.ts +0 -0
  212. /package/src/{vue → components/sheet}/use-toolbar-scroll.ts +0 -0
@@ -0,0 +1,131 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import {
4
+ evaluateCondition,
5
+ evaluateConditionalStyle,
6
+ resolveRuleCompareValue,
7
+ type ConditionalEvalContext
8
+ } from '../rules'
9
+ import type { ConditionalRule } from '../types'
10
+
11
+ function rule(
12
+ operator: ConditionalRule['operator'],
13
+ value: unknown,
14
+ style: ConditionalRule['style'],
15
+ extra?: Partial<ConditionalRule>
16
+ ): ConditionalRule {
17
+ return { operator, value, style, ...extra }
18
+ }
19
+
20
+ function ctx(
21
+ cellValue: unknown,
22
+ record?: Record<string, unknown>,
23
+ bindingField = 'amount'
24
+ ): ConditionalEvalContext {
25
+ return { cellValue, bindingField, record }
26
+ }
27
+
28
+ describe('report rules', () => {
29
+ describe('evaluateCondition', () => {
30
+ it('数值比较运算符', () => {
31
+ expect(evaluateCondition(150, rule('gt', 100, {}))).toBe(true)
32
+ expect(evaluateCondition(100, rule('gt', 100, {}))).toBe(false)
33
+ expect(evaluateCondition(100, rule('gte', 100, {}))).toBe(true)
34
+ expect(evaluateCondition(99, rule('lt', 100, {}))).toBe(true)
35
+ expect(evaluateCondition(100, rule('lte', 100, {}))).toBe(true)
36
+ expect(evaluateCondition(42, rule('eq', 42, {}))).toBe(true)
37
+ expect(evaluateCondition('42', rule('eq', 42, {}))).toBe(true)
38
+ })
39
+
40
+ it('between 含端点范围比较', () => {
41
+ const between = rule('between', [100, 200], {})
42
+ expect(evaluateCondition(100, between)).toBe(true)
43
+ expect(evaluateCondition(200, between)).toBe(true)
44
+ expect(evaluateCondition(150, between)).toBe(true)
45
+ expect(evaluateCondition(99, between)).toBe(false)
46
+ expect(evaluateCondition(201, between)).toBe(false)
47
+ })
48
+
49
+ it('contains 字符串包含', () => {
50
+ expect(evaluateCondition('华东-上海', rule('contains', '上海', {}))).toBe(true)
51
+ expect(evaluateCondition('华北', rule('contains', '上海', {}))).toBe(false)
52
+ expect(evaluateCondition(null, rule('contains', 'x', {}))).toBe(false)
53
+ })
54
+
55
+ it('非数值比较在 gt 等运算符下返回 false', () => {
56
+ expect(evaluateCondition('abc', rule('gt', 1, {}))).toBe(false)
57
+ })
58
+ })
59
+
60
+ describe('resolveRuleCompareValue', () => {
61
+ it('缺省 field 时取 cellValue', () => {
62
+ const context = ctx(150, { amount: 150, region: '华东' })
63
+ expect(resolveRuleCompareValue(context, rule('gt', 100, {}))).toBe(150)
64
+ })
65
+
66
+ it('指定 field 时取同记录另一字段', () => {
67
+ const context = ctx(150, { amount: 150, region: '华东' })
68
+ expect(resolveRuleCompareValue(context, rule('eq', '华东', {}, { field: 'region' }))).toBe(
69
+ '华东'
70
+ )
71
+ })
72
+ })
73
+
74
+ describe('evaluateConditionalStyle', () => {
75
+ const baseStyle = { fill: { color: '#FFFFFF' }, font: { color: '#000000' } }
76
+
77
+ it('未命中规则时原样返回 baseStyle', () => {
78
+ const rules = [rule('gt', 1000, { fill: { color: '#FF0000' } })]
79
+ expect(evaluateConditionalStyle(ctx(50), baseStyle, rules)).toBe(baseStyle)
80
+ })
81
+
82
+ it('命中单条规则时叠加样式增量', () => {
83
+ const rules = [rule('gt', 100, { fill: { color: '#FFCCCC' }, font: { bold: true } })]
84
+ expect(evaluateConditionalStyle(ctx(150), baseStyle, rules)).toEqual({
85
+ fill: { color: '#FFCCCC' },
86
+ font: { color: '#000000', bold: true }
87
+ })
88
+ })
89
+
90
+ it('跨字段求值命中时叠加样式', () => {
91
+ const rules = [rule('eq', '华东', { fill: { color: '#FFCCCC' } }, { field: 'region' })]
92
+ const context = ctx(100, { amount: 100, region: '华东' })
93
+ expect(evaluateConditionalStyle(context, baseStyle, rules)?.fill?.color).toBe('#FFCCCC')
94
+ })
95
+
96
+ it('scope: row 仅评估行级规则', () => {
97
+ const rules = [
98
+ rule('gt', 100, { fill: { color: '#FFCCCC' } }, { scope: 'row' }),
99
+ rule('gt', 100, { font: { bold: true } })
100
+ ]
101
+ expect(evaluateConditionalStyle(ctx(150), baseStyle, rules, 'row')?.fill?.color).toBe(
102
+ '#FFCCCC'
103
+ )
104
+ expect(evaluateConditionalStyle(ctx(150), baseStyle, rules, 'cell')?.font?.bold).toBe(true)
105
+ expect(evaluateConditionalStyle(ctx(150), baseStyle, rules, 'cell')?.fill?.color).toBe(
106
+ '#FFFFFF'
107
+ )
108
+ })
109
+
110
+ it('多规则按优先级合并,后命中规则覆盖同名字段', () => {
111
+ const rules = [
112
+ rule('gt', 0, { fill: { color: '#FFFFCC' } }),
113
+ rule('gt', 100, { fill: { color: '#FFCCCC' }, font: { bold: true } }),
114
+ rule('gt', 200, { font: { color: '#FF0000' } })
115
+ ]
116
+
117
+ const style150 = evaluateConditionalStyle(ctx(150), baseStyle, rules)
118
+ expect(style150?.fill?.color).toBe('#FFCCCC')
119
+ expect(style150?.font).toEqual({ color: '#000000', bold: true })
120
+
121
+ const style250 = evaluateConditionalStyle(ctx(250), baseStyle, rules)
122
+ expect(style250?.fill?.color).toBe('#FFCCCC')
123
+ expect(style250?.font).toEqual({ color: '#FF0000', bold: true })
124
+ })
125
+
126
+ it('无规则或未传规则时返回 baseStyle', () => {
127
+ expect(evaluateConditionalStyle(ctx(100), baseStyle, undefined)).toBe(baseStyle)
128
+ expect(evaluateConditionalStyle(ctx(100), baseStyle, [])).toBe(baseStyle)
129
+ })
130
+ })
131
+ })
@@ -0,0 +1,366 @@
1
+ import { Sheet } from '@veltra/sheet-core'
2
+ import { describe, expect, it } from 'vitest'
3
+
4
+ import { REPORT_META_NAMESPACE } from '../binding'
5
+ import type { DataConnection, DataConnector } from '../connector'
6
+ import {
7
+ assertCompatibleTemplateVersion,
8
+ createReportTemplate,
9
+ fetchTemplateRecords,
10
+ getBoundDatasetIds,
11
+ getTemplateDatasets,
12
+ IncompatibleTemplateVersionError,
13
+ REPORT_TEMPLATE_VERSION,
14
+ resolveParamDefaults,
15
+ resolveTemplateParams,
16
+ type ReportDatasetDef,
17
+ type ReportTemplate
18
+ } from '../template'
19
+ import type { ParamValues, ReportBinding } from '../types'
20
+
21
+ // ---- 内联 fixtures ----
22
+
23
+ const MYSQL: DataConnection = {
24
+ id: 'c-mysql',
25
+ label: '本地 MySQL',
26
+ type: 'mysql',
27
+ host: '127.0.0.1',
28
+ port: 3306,
29
+ database: 'demo',
30
+ username: 'root',
31
+ password: 'secret'
32
+ }
33
+
34
+ const PG: DataConnection = {
35
+ ...MYSQL,
36
+ id: 'c-pg',
37
+ label: '本地 PG',
38
+ type: 'postgresql',
39
+ port: 5432
40
+ }
41
+
42
+ // BETWEEN ${dateRange} 置于 SQL 末尾:后随 AND 时不会推断为 date-range(params.ts 既有推断规则)
43
+ const ORDERS_SQL =
44
+ 'SELECT customer, region, amount, orderDate FROM orders ' +
45
+ 'WHERE region = ${region} AND amount >= ${minAmount} ' +
46
+ 'AND customer LIKE ${keyword} AND orderDate BETWEEN ${dateRange}'
47
+
48
+ const CUSTOMERS_SQL = 'SELECT id, region FROM customers WHERE region = ${region}'
49
+
50
+ const ORDERS_DATASET: ReportDatasetDef = {
51
+ id: 'orders',
52
+ label: '销售明细',
53
+ connection: MYSQL,
54
+ sql: ORDERS_SQL
55
+ }
56
+
57
+ const CUSTOMERS_DATASET: ReportDatasetDef = {
58
+ id: 'customers',
59
+ label: '客户',
60
+ connection: PG,
61
+ sql: CUSTOMERS_SQL,
62
+ paramOverrides: {
63
+ region: {
64
+ label: '地区',
65
+ type: 'select',
66
+ defaultValue: '华东',
67
+ options: [
68
+ { label: '华东', value: '华东' },
69
+ { label: '华南', value: '华南' }
70
+ ]
71
+ }
72
+ }
73
+ }
74
+
75
+ const UNUSED_DATASET: ReportDatasetDef = {
76
+ id: 'unused',
77
+ label: '未绑定',
78
+ connection: MYSQL,
79
+ sql: 'SELECT id FROM t WHERE x = ${unusedParam}'
80
+ }
81
+
82
+ function binding(dataset: string, field: string): ReportBinding {
83
+ return { dataset, field, aggregate: 'list', expand: 'down', preset: 'detail' }
84
+ }
85
+
86
+ /** 构造自包含 Report Template:SheetSnapshot + 内嵌数据集定义 */
87
+ function createTemplate(
88
+ datasets: ReportDatasetDef[],
89
+ bindings: Array<{ row: number; col: number; dataset: string; field: string }>
90
+ ): ReportTemplate {
91
+ const sheet = new Sheet()
92
+ for (const item of bindings) {
93
+ sheet.setCellMeta(
94
+ { row: item.row, col: item.col },
95
+ REPORT_META_NAMESPACE,
96
+ binding(item.dataset, item.field)
97
+ )
98
+ }
99
+ return createReportTemplate(sheet.snapshot(), datasets)
100
+ }
101
+
102
+ describe('ReportTemplate version', () => {
103
+ it('createReportTemplate 写入当前版本', () => {
104
+ const template = createTemplate([ORDERS_DATASET], [])
105
+ expect(template.version).toBe(REPORT_TEMPLATE_VERSION)
106
+ })
107
+
108
+ it('createReportTemplate 继承 snapshot.colWidths', () => {
109
+ const sheet = new Sheet()
110
+ sheet.setColWidth(0, 120)
111
+ sheet.setColWidth(2, 90)
112
+ const template = createReportTemplate(sheet.snapshot(), [ORDERS_DATASET])
113
+ expect(template.colWidths).toEqual([
114
+ [0, 120],
115
+ [2, 90]
116
+ ])
117
+ })
118
+
119
+ it('version 缺失时 assertCompatibleTemplateVersion 抛可读错误', () => {
120
+ const sheet = new Sheet()
121
+ const template = sheet.snapshot() as ReportTemplate
122
+ expect(() => assertCompatibleTemplateVersion(template)).toThrow(
123
+ IncompatibleTemplateVersionError
124
+ )
125
+ expect(() => assertCompatibleTemplateVersion(template)).toThrow(/缺少 version/)
126
+ })
127
+
128
+ it('version 高于当前支持时 assertCompatibleTemplateVersion 抛可读错误', () => {
129
+ const template = createTemplate([ORDERS_DATASET], [])
130
+ template.version = REPORT_TEMPLATE_VERSION + 1
131
+ expect(() => assertCompatibleTemplateVersion(template)).toThrow(
132
+ IncompatibleTemplateVersionError
133
+ )
134
+ expect(() => assertCompatibleTemplateVersion(template)).toThrow(/高于当前支持/)
135
+ })
136
+
137
+ it('version 为当前值时通过校验', () => {
138
+ const template = createTemplate([ORDERS_DATASET], [])
139
+ expect(() => assertCompatibleTemplateVersion(template)).not.toThrow()
140
+ })
141
+ })
142
+
143
+ describe('getTemplateDatasets', () => {
144
+ it('返回模板内嵌的数据集定义', () => {
145
+ const template = createTemplate([ORDERS_DATASET], [])
146
+ expect(getTemplateDatasets(template)).toEqual([ORDERS_DATASET])
147
+ })
148
+
149
+ it('未内嵌数据集(旧模板)回退为空数组', () => {
150
+ const sheet = new Sheet()
151
+ const template = sheet.snapshot() as ReportTemplate
152
+ expect(getTemplateDatasets(template)).toEqual([])
153
+ })
154
+ })
155
+
156
+ describe('getBoundDatasetIds', () => {
157
+ it('按绑定出现顺序返回去重的数据集 id(绑定即真相)', () => {
158
+ const template = createTemplate(
159
+ [],
160
+ [
161
+ { row: 1, col: 0, dataset: 'orders', field: 'customer' },
162
+ { row: 1, col: 1, dataset: 'orders', field: 'amount' },
163
+ { row: 2, col: 0, dataset: 'customers', field: 'region' }
164
+ ]
165
+ )
166
+ expect(getBoundDatasetIds(template)).toEqual(['orders', 'customers'])
167
+ })
168
+
169
+ it('无绑定时返回空数组', () => {
170
+ expect(getBoundDatasetIds(createTemplate([], []))).toEqual([])
171
+ })
172
+ })
173
+
174
+ describe('resolveTemplateParams', () => {
175
+ it('从实际绑定的数据集提取参数并集(未绑定数据集的参数不出现)', () => {
176
+ const template = createTemplate(
177
+ [ORDERS_DATASET, CUSTOMERS_DATASET, UNUSED_DATASET],
178
+ [
179
+ { row: 1, col: 0, dataset: 'orders', field: 'customer' },
180
+ { row: 2, col: 0, dataset: 'customers', field: 'region' }
181
+ ]
182
+ )
183
+ const params = resolveTemplateParams(template)
184
+ expect(params.map((p) => p.id)).toEqual(['region', 'minAmount', 'keyword', 'dateRange'])
185
+ })
186
+
187
+ it('按 SQL 上下文推断控件类型(= 数值 / LIKE 文本 / BETWEEN 单参 date-range)', () => {
188
+ const template = createTemplate(
189
+ [ORDERS_DATASET],
190
+ [{ row: 1, col: 0, dataset: 'orders', field: 'customer' }]
191
+ )
192
+ const params = resolveTemplateParams(template)
193
+ const byId = new Map(params.map((p) => [p.id, p]))
194
+ expect(byId.get('region')?.type).toBe('number')
195
+ expect(byId.get('minAmount')?.type).toBe('number')
196
+ expect(byId.get('dateRange')?.type).toBe('date-range')
197
+ expect(byId.get('keyword')?.type).toBe('text')
198
+ })
199
+
200
+ it('同名参数合并,先见数据集为准', () => {
201
+ const ordersFirst = createTemplate(
202
+ [ORDERS_DATASET, CUSTOMERS_DATASET],
203
+ [
204
+ { row: 1, col: 0, dataset: 'orders', field: 'customer' },
205
+ { row: 2, col: 0, dataset: 'customers', field: 'region' }
206
+ ]
207
+ )
208
+ const region = resolveTemplateParams(ordersFirst).find((p) => p.id === 'region')
209
+ // orders 先见:无覆盖元数据,label 回退 id、类型按 orders SQL 推断
210
+ expect(region?.label).toBe('region')
211
+ expect(region?.type).toBe('number')
212
+ })
213
+
214
+ it('应用数据集参数元数据覆盖(label / 类型 / 默认值 / 选项)', () => {
215
+ const template = createTemplate(
216
+ [CUSTOMERS_DATASET],
217
+ [{ row: 1, col: 0, dataset: 'customers', field: 'region' }]
218
+ )
219
+ const [region] = resolveTemplateParams(template)
220
+ expect(region).toEqual({
221
+ id: 'region',
222
+ label: '地区',
223
+ type: 'select',
224
+ defaultValue: '华东',
225
+ options: [
226
+ { label: '华东', value: '华东' },
227
+ { label: '华南', value: '华南' }
228
+ ]
229
+ })
230
+ })
231
+
232
+ it('绑定引用了模板未定义的数据集时跳过该数据集', () => {
233
+ const template = createTemplate(
234
+ [],
235
+ [
236
+ { row: 1, col: 0, dataset: 'ghost', field: 'x' },
237
+ { row: 1, col: 1, dataset: 'orders', field: 'amount' }
238
+ ]
239
+ )
240
+ // orders 未内嵌 → 也无参数;整体为空
241
+ expect(resolveTemplateParams(template)).toEqual([])
242
+
243
+ const withOrders = createTemplate(
244
+ [ORDERS_DATASET],
245
+ [
246
+ { row: 1, col: 0, dataset: 'ghost', field: 'x' },
247
+ { row: 1, col: 1, dataset: 'orders', field: 'amount' }
248
+ ]
249
+ )
250
+ expect(resolveTemplateParams(withOrders).map((p) => p.id)).toEqual([
251
+ 'region',
252
+ 'minAmount',
253
+ 'keyword',
254
+ 'dateRange'
255
+ ])
256
+ })
257
+ })
258
+
259
+ describe('resolveParamDefaults', () => {
260
+ it('以参数默认值生成初始运行时值', () => {
261
+ const template = createTemplate(
262
+ [ORDERS_DATASET, CUSTOMERS_DATASET],
263
+ [
264
+ { row: 1, col: 0, dataset: 'customers', field: 'region' },
265
+ { row: 2, col: 0, dataset: 'orders', field: 'amount' }
266
+ ]
267
+ )
268
+ const values = resolveParamDefaults(resolveTemplateParams(template))
269
+ expect(values.region).toBe('华东')
270
+ expect(values.minAmount).toBe(0)
271
+ expect(values.dateRange).toEqual(['', ''])
272
+ expect(values.keyword).toBe('')
273
+ })
274
+ })
275
+
276
+ // ---- fetchTemplateRecords(stub connector:实现 DataConnector 接口的内存测试夹具)----
277
+
278
+ function createStubConnector(handlers: {
279
+ rowsBySql?: Record<string, Record<string, unknown>[]>
280
+ errorBySql?: Record<string, { code: string; message: string }>
281
+ }) {
282
+ const calls: Array<{ connection: DataConnection; sql: string; values: ParamValues }> = []
283
+ const connector: DataConnector = {
284
+ test: () => Promise.resolve({ ok: true, data: undefined }),
285
+ describe: () => Promise.resolve({ ok: true, data: [] }),
286
+ query: (connection, sql, values) => {
287
+ calls.push({ connection, sql, values })
288
+ const error = handlers.errorBySql?.[sql]
289
+ if (error) return Promise.resolve({ ok: false, error })
290
+ return Promise.resolve({
291
+ ok: true,
292
+ data: { fields: [], rows: handlers.rowsBySql?.[sql] ?? [] }
293
+ })
294
+ }
295
+ }
296
+ return { connector, calls }
297
+ }
298
+
299
+ describe('fetchTemplateRecords', () => {
300
+ it('按绑定的数据集逐一经连接器取数并按数据集 id 折叠行记录', async () => {
301
+ const template = createTemplate(
302
+ [ORDERS_DATASET, CUSTOMERS_DATASET, UNUSED_DATASET],
303
+ [
304
+ { row: 1, col: 0, dataset: 'orders', field: 'customer' },
305
+ { row: 2, col: 0, dataset: 'customers', field: 'region' }
306
+ ]
307
+ )
308
+ const { connector, calls } = createStubConnector({
309
+ rowsBySql: {
310
+ [ORDERS_SQL]: [{ customer: '甲公司', amount: 100 }],
311
+ [CUSTOMERS_SQL]: [{ id: 'C-01', region: '华东' }]
312
+ }
313
+ })
314
+ const values: ParamValues = { region: '华东' }
315
+ const result = await fetchTemplateRecords(connector, template, values)
316
+
317
+ expect(result).toEqual({
318
+ ok: true,
319
+ data: {
320
+ orders: [{ customer: '甲公司', amount: 100 }],
321
+ customers: [{ id: 'C-01', region: '华东' }]
322
+ }
323
+ })
324
+ // 未绑定的数据集不取数;连接对象与参数原样透传
325
+ expect(calls).toHaveLength(2)
326
+ expect(calls[0]).toEqual({ connection: MYSQL, sql: ORDERS_SQL, values })
327
+ expect(calls[1]).toEqual({ connection: PG, sql: CUSTOMERS_SQL, values })
328
+ })
329
+
330
+ it('业务错误(ok:false)原样透传', async () => {
331
+ const template = createTemplate(
332
+ [ORDERS_DATASET],
333
+ [{ row: 1, col: 0, dataset: 'orders', field: 'customer' }]
334
+ )
335
+ const { connector } = createStubConnector({
336
+ errorBySql: { [ORDERS_SQL]: { code: 'SQL_ERROR', message: '语法错误 near WHERE' } }
337
+ })
338
+ const result = await fetchTemplateRecords(connector, template, {})
339
+ expect(result).toEqual({
340
+ ok: false,
341
+ error: { code: 'SQL_ERROR', message: '语法错误 near WHERE' }
342
+ })
343
+ })
344
+
345
+ it('跳过模板未定义的绑定数据集', async () => {
346
+ const template = createTemplate(
347
+ [ORDERS_DATASET],
348
+ [
349
+ { row: 1, col: 0, dataset: 'ghost', field: 'x' },
350
+ { row: 1, col: 1, dataset: 'orders', field: 'amount' }
351
+ ]
352
+ )
353
+ const { connector, calls } = createStubConnector({ rowsBySql: { [ORDERS_SQL]: [] } })
354
+ const result = await fetchTemplateRecords(connector, template, {})
355
+ expect(result).toEqual({ ok: true, data: { orders: [] } })
356
+ expect(calls).toHaveLength(1)
357
+ })
358
+
359
+ it('无绑定数据集时不取数,返回空记录集', async () => {
360
+ const template = createTemplate([ORDERS_DATASET], [])
361
+ const { connector, calls } = createStubConnector({})
362
+ const result = await fetchTemplateRecords(connector, template, {})
363
+ expect(result).toEqual({ ok: true, data: {} })
364
+ expect(calls).toHaveLength(0)
365
+ })
366
+ })