@arcticdata/dsh-datarc-assistant 0.0.0-stage → 26.39.1
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 +46 -2
- package/api-operations.js +404 -0
- package/client.js +475 -0
- package/cordis.patch.yml +4 -0
- package/entry/SKILL.md +34 -0
- package/icon.png +0 -0
- package/index.js +69 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/native-api.mjs +326 -0
- package/native-contracts.js +195 -0
- package/native-execution.js +184 -0
- package/native-runner.mjs +107 -0
- package/native-state.js +67 -0
- package/native-tools.js +241 -0
- package/operations.js +1260 -0
- package/package.json +65 -6
- package/runtime/datarc-access-permissions/SKILL.md +30 -0
- package/runtime/datarc-access-permissions/references/api.md +31 -0
- package/runtime/datarc-access-permissions/references/write-order.md +16 -0
- package/runtime/datarc-analysis-report/SKILL.md +125 -0
- package/runtime/datarc-analysis-report/agents/openai.yaml +6 -0
- package/runtime/datarc-analysis-report/references/analyst-frameworks.md +215 -0
- package/runtime/datarc-analysis-report/references/api-contracts.md +478 -0
- package/runtime/datarc-analysis-report/references/direct-card-fallback-design.md +66 -0
- package/runtime/datarc-analysis-report/references/human-in-the-loop.md +142 -0
- package/runtime/datarc-analysis-report/references/loop-personalization-policy.md +138 -0
- package/runtime/datarc-analysis-report/references/manifest-example.json +779 -0
- package/runtime/datarc-analysis-report/references/narrative-schema.md +160 -0
- package/runtime/datarc-analysis-report/references/question-syntax.md +151 -0
- package/runtime/datarc-analysis-report/scripts/run_analysis_report.py +2225 -0
- package/runtime/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -0
- package/runtime/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -0
- package/runtime/datarc-data-search/SKILL.md +243 -0
- package/runtime/datarc-data-search/scripts/calculate.py +116 -0
- package/runtime/datarc-data-search/scripts/data-search.mjs +628 -0
- package/runtime/datarc-data-search/scripts/datarc-client.mjs +464 -0
- package/runtime/datarc-data-search/scripts/search-result.mjs +211 -0
- package/runtime/datarc-dataseek-batch-evaluation/SKILL.md +59 -0
- package/runtime/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -0
- package/runtime/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -0
- package/runtime/datarc-dataseek-batch-evaluation/scripts/datarc-client.mjs +464 -0
- package/runtime/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -0
- package/runtime/datarc-dataseek-search/SKILL.md +96 -0
- package/runtime/datarc-dataseek-search/scripts/datarc-client.mjs +464 -0
- package/runtime/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -0
- package/runtime/datarc-dataseek-semantic-debugging/SKILL.md +44 -0
- package/runtime/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -0
- package/runtime/datarc-dataseek-semantic-debugging/evals/evals.json +165 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/dataset.md +77 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/interventions.md +64 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/rollback.md +11 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -0
- package/runtime/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -0
- package/runtime/datarc-dataseek-testset-generation/SKILL.md +47 -0
- package/runtime/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -0
- package/runtime/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -0
- package/runtime/datarc-datasource/SKILL.md +97 -0
- package/runtime/datarc-oauth/SKILL.md +50 -0
- package/runtime/datarc-oauth/scripts/auth.mjs +192 -0
- package/runtime/datarc-oauth/scripts/credentials.mjs +237 -0
- package/runtime/datarc-report-add-card/SKILL.md +90 -0
- package/runtime/datarc-report-add-card/agents/openai.yaml +4 -0
- package/runtime/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
- package/runtime/datarc-report-add-card/scripts/report-card.mjs +439 -0
- package/runtime/datarc-semantic-alias/SKILL.md +20 -0
- package/runtime/datarc-semantic-alias/scripts/alias-plan.mjs +422 -0
- package/runtime/datarc-semantic-alias/scripts/alias.mjs +11 -0
- package/runtime/datarc-semantic-calibration/SKILL.md +20 -0
- package/runtime/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -0
- package/runtime/datarc-semantic-calibration/scripts/calibration.mjs +11 -0
- package/runtime/datarc-semantic-core/SKILL.md +12 -0
- package/runtime/datarc-semantic-core/scripts/artifact-journal.mjs +853 -0
- package/runtime/datarc-semantic-core/scripts/canonical.mjs +48 -0
- package/runtime/datarc-semantic-core/scripts/case-model.mjs +608 -0
- package/runtime/datarc-semantic-core/scripts/cli.mjs +180 -0
- package/runtime/datarc-semantic-core/scripts/datarc-client.mjs +180 -0
- package/runtime/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -0
- package/runtime/datarc-semantic-core/scripts/interventions.mjs +693 -0
- package/runtime/datarc-semantic-core/scripts/live-resolution.mjs +305 -0
- package/runtime/datarc-semantic-core/scripts/model-scope.mjs +49 -0
- package/runtime/datarc-semantic-core/scripts/reproduction.mjs +330 -0
- package/runtime/datarc-semantic-core/scripts/semantic-cli.mjs +799 -0
- package/runtime/datarc-semantic-core/scripts/shared.mjs +312 -0
- package/runtime/datarc-semantic-core/scripts/verification.mjs +1111 -0
- package/runtime/datarc-semantic-dataset/SKILL.md +20 -0
- package/runtime/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -0
- package/runtime/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -0
- package/runtime/datarc-semantic-dataset/scripts/dataset.mjs +11 -0
- package/runtime/datarc-semantic-knowledge/SKILL.md +38 -0
- package/runtime/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -0
- package/runtime/datarc-semantic-knowledge/references/knowledge-generation.json +16 -0
- package/runtime/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -0
- package/runtime/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -0
- package/runtime/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -0
- package/runtime/datarc-semantic-prompt/SKILL.md +20 -0
- package/runtime/datarc-semantic-prompt/scripts/prompt-plan.mjs +598 -0
- package/runtime/datarc-semantic-prompt/scripts/prompt.mjs +15 -0
- package/runtime/datarc-semantic-synonym/SKILL.md +20 -0
- package/runtime/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -0
- package/runtime/datarc-semantic-synonym/scripts/synonym.mjs +11 -0
- package/runtime/datarc-viz-analysis-report/SKILL.md +108 -0
- package/runtime/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -0
- package/runtime/datarc-viz-analysis-report/assets/echarts.min.js +45 -0
- package/runtime/datarc-viz-analysis-report/references/api-contracts.md +163 -0
- package/runtime/datarc-viz-analysis-report/references/derivation-log.md +157 -0
- package/runtime/datarc-viz-analysis-report/references/evidence-framework.md +76 -0
- package/runtime/datarc-viz-analysis-report/references/html-template.md +92 -0
- package/runtime/datarc-viz-analysis-report/references/provenance-format.md +56 -0
- package/runtime/datarc-viz-analysis-report/references/question-syntax.md +151 -0
- package/runtime/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -0
- package/runtime/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -0
- package/runtime/datarc-workspace-configuration/SKILL.md +59 -0
- package/runtime/datarc-workspace-configuration/references/artifacts.md +75 -0
- package/runtime/datarc-workspace-configuration/references/verification.md +20 -0
- package/runtime/datarc-workspace-configuration/scripts/configuration.mjs +722 -0
- package/runtime/datarc-workspace-context/SKILL.md +43 -0
- package/runtime/datarc-workspace-context/agents/openai.yaml +4 -0
- package/runtime/datarc-workspace-context/scripts/datarc-client.mjs +31 -0
- package/runtime/datarc-workspace-context/scripts/query-display.mjs +114 -0
- package/runtime/datarc-workspace-context/scripts/workspace-context.mjs +58 -0
- package/runtime/datarc-workspace-custom-tables/SKILL.md +89 -0
- package/runtime/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -0
- package/runtime/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -0
- package/runtime/datarc-workspace-keywords/SKILL.md +197 -0
- package/runtime/datarc-workspace-keywords/agents/openai.yaml +4 -0
- package/runtime/datarc-workspace-keywords/references/api-fields.md +79 -0
- package/runtime/datarc-workspace-keywords/references/derived-measures.md +177 -0
- package/runtime/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -0
- package/runtime/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -0
- package/runtime/datarc-workspace-membership/SKILL.md +29 -0
- package/runtime/datarc-workspace-membership/references/api.md +19 -0
- package/runtime/datarc-workspace-modeling/SKILL.md +209 -0
- package/runtime/datarc-workspace-modeling/agents/openai.yaml +4 -0
- package/runtime/datarc-workspace-modeling/references/join-preview.md +38 -0
- package/runtime/datarc-workspace-modeling/references/model-plan.md +300 -0
- package/runtime/datarc-workspace-modeling/references/model-tags.md +95 -0
- package/runtime/datarc-workspace-modeling/references/modeling-contract.json +29 -0
- package/runtime/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -0
- package/runtime/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -0
- package/runtime/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -0
- package/runtime/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -0
- package/skills-provider.js +51 -0
- package/workflow-index.json +434 -0
|
@@ -0,0 +1,964 @@
|
|
|
1
|
+
"""Direct-run unit tests for render_viz_report.py.
|
|
2
|
+
|
|
3
|
+
Run from this scripts directory:
|
|
4
|
+
|
|
5
|
+
python3 -m unittest test_render_viz_report -v
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
import re
|
|
10
|
+
import tempfile
|
|
11
|
+
import unittest
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
import render_viz_report
|
|
15
|
+
from render_viz_report import ReportContractError
|
|
16
|
+
|
|
17
|
+
STUB_ECHARTS = "/* stub echarts build for unit tests */"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _sample_input() -> dict:
|
|
21
|
+
"""Small but complete ledger covering line/pie/table and one derivation."""
|
|
22
|
+
return {
|
|
23
|
+
"plan_meta": {
|
|
24
|
+
"title": "华东区销售月度回顾",
|
|
25
|
+
"workspace_name": "华东经营分析",
|
|
26
|
+
"output_language": "zh-CN",
|
|
27
|
+
"generated_at": "2025-07-08T10:00:00Z",
|
|
28
|
+
},
|
|
29
|
+
"cards": [
|
|
30
|
+
{
|
|
31
|
+
"card_id": "r1-q1",
|
|
32
|
+
"question": "近半年各月销售额",
|
|
33
|
+
"fetched_at": "2025-07-08T09:30:00Z",
|
|
34
|
+
"payload_rows": [
|
|
35
|
+
["月份", "销售额"],
|
|
36
|
+
["2月", 1200000],
|
|
37
|
+
["3月", 1344000],
|
|
38
|
+
["4月", None],
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"card_id": "r2-q2",
|
|
43
|
+
"question": "各渠道销售额构成",
|
|
44
|
+
"fetched_at": "2025-07-08T09:40:00Z",
|
|
45
|
+
"payload_rows": [["渠道", "销售额"], ["线上", 600000], ["门店", 400000]],
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
"derivations": [
|
|
49
|
+
{
|
|
50
|
+
"derivation_id": "d-mom",
|
|
51
|
+
"source_card_ids": ["r1-q1"],
|
|
52
|
+
"input_fields": ["销售额"],
|
|
53
|
+
"output_column": "环比增幅",
|
|
54
|
+
"method": {"type": "growth_rate"},
|
|
55
|
+
"output_values": [None, 0.12],
|
|
56
|
+
"key_field": "月份",
|
|
57
|
+
"keys": ["2月", "3月"],
|
|
58
|
+
"key_card_id": "r1-q1",
|
|
59
|
+
}
|
|
60
|
+
],
|
|
61
|
+
"charts": [
|
|
62
|
+
{
|
|
63
|
+
"chart_id": "chart-trend",
|
|
64
|
+
"title": "月度销售额趋势",
|
|
65
|
+
"chart_type": "line",
|
|
66
|
+
"source_card_id": "r1-q1",
|
|
67
|
+
"evidence_role": "primary",
|
|
68
|
+
"categories": {"values": ["2月", "3月"], "field": "月份"},
|
|
69
|
+
"series": [
|
|
70
|
+
{"name": "销售额", "data": [1200000.0, 1344000], "field": "销售额"},
|
|
71
|
+
{"name": "环比增幅", "data": [None, 0.12], "derivation_id": "d-mom", "value_format": "percent"},
|
|
72
|
+
],
|
|
73
|
+
"interpretation": "如图所示,3月销售额环比增长12%;4月行级数据缺失。",
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"chart_id": "chart-mix",
|
|
77
|
+
"title": "渠道构成",
|
|
78
|
+
"chart_type": "pie",
|
|
79
|
+
"source_card_id": "r2-q2",
|
|
80
|
+
"evidence_role": "diagnostic",
|
|
81
|
+
"categories": {"values": ["线上", "门店"], "field": "渠道"},
|
|
82
|
+
"series": [{"name": "销售额", "data": [600000, 400000], "field": "销售额"}],
|
|
83
|
+
"interpretation": "综合图 chart-trend 与本图:线上渠道贡献六成。",
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"chart_id": "chart-detail",
|
|
87
|
+
"title": "明细表",
|
|
88
|
+
"chart_type": "table",
|
|
89
|
+
"source_card_id": "r2-q2",
|
|
90
|
+
"evidence_role": "validation",
|
|
91
|
+
"columns": [{"header": "渠道", "field": "渠道"}, {"header": "销售额", "field": "销售额"}],
|
|
92
|
+
"rows": [["门店", 400000]],
|
|
93
|
+
"interpretation": "门店单列明细备查。",
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
"claims": [
|
|
97
|
+
{"claim_id": "f-mom", "kind": "fact", "text": "3月销售额环比增长12%", "evidence_card_ids": ["r1-q1"], "confidence": "high"},
|
|
98
|
+
{"claim_id": "f-online", "kind": "fact", "text": "线上渠道贡献六成", "evidence_card_ids": ["r2-q2"], "confidence": "high"},
|
|
99
|
+
{
|
|
100
|
+
"claim_id": "i-stable",
|
|
101
|
+
"kind": "inference",
|
|
102
|
+
"text": "近两个月销售趋势平稳,线上渠道贡献约六成",
|
|
103
|
+
"evidence_card_ids": ["r1-q1", "r2-q2"],
|
|
104
|
+
"confidence": "medium",
|
|
105
|
+
},
|
|
106
|
+
{"claim_id": "l-april", "kind": "limitation", "text": "4月行级数据缺失", "evidence_card_ids": [], "confidence": "high"},
|
|
107
|
+
],
|
|
108
|
+
"excluded_paths": [{"question": "华北与华南对比探索", "reason": "中间结果波动无稳定模式,未进入主线结论"}],
|
|
109
|
+
"executive_summary": {
|
|
110
|
+
"summary": "全局结论:近两个月销售趋势平稳,线上渠道贡献约六成;4月行级数据缺失待补。",
|
|
111
|
+
"recommendations": [
|
|
112
|
+
"保持线上渠道的投放与补货节奏,并按月跟踪构成变化。",
|
|
113
|
+
"补齐 4 月行级数据后复跑本报告,验证趋势结论是否稳定。",
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
"chapters": [
|
|
117
|
+
{
|
|
118
|
+
"chapter_id": "ch-overview",
|
|
119
|
+
"title": "一、总体概览",
|
|
120
|
+
"purpose": "近半年销售趋势是否平稳、渠道结构是否集中?",
|
|
121
|
+
"digest": "本章小结:近两个月销售趋势平稳,线上渠道贡献约六成;下面依次给出趋势线、构成占比与门店明细佐证。",
|
|
122
|
+
"chart_ids": ["chart-trend", "chart-mix", "chart-detail"],
|
|
123
|
+
"claim_refs": ["f-mom", "f-online", "i-stable", "l-april"],
|
|
124
|
+
}
|
|
125
|
+
],
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class RenderVizReportTest(unittest.TestCase):
|
|
130
|
+
def test(self):
|
|
131
|
+
self._test_valid_end_to_end_sample()
|
|
132
|
+
self._test_rejects_off_ledger_values()
|
|
133
|
+
self._test_builtin_derivations()
|
|
134
|
+
self._test_growth_rate_follows_key_order_not_source_row_order()
|
|
135
|
+
self._test_requires_complete_derivation_log()
|
|
136
|
+
self._test_generates_footnotes_and_appendix()
|
|
137
|
+
self._test_escapes_line_separators_in_chart_payload()
|
|
138
|
+
self._test_structural_validation()
|
|
139
|
+
self._test_csv_download_assets()
|
|
140
|
+
self._test_cli_roundtrip()
|
|
141
|
+
self._test_rejects_row_swapped_values()
|
|
142
|
+
self._test_rejects_missing_field_binding()
|
|
143
|
+
self._test_rejects_derivation_key_mismatch()
|
|
144
|
+
self._test_rejects_duplicate_headers()
|
|
145
|
+
self._test_scatter_row_pair_tracing()
|
|
146
|
+
self._test_en_us_output_labels()
|
|
147
|
+
self._test_rejects_cross_card_derived_series()
|
|
148
|
+
self._test_derived_table_column_without_field_renders()
|
|
149
|
+
self._test_rejects_cross_card_derived_table_column()
|
|
150
|
+
self._test_rejects_multi_series_category_splice()
|
|
151
|
+
self._test_rejects_ragged_payload_rows()
|
|
152
|
+
self._test_rejects_multi_derivation_missing_key_column()
|
|
153
|
+
self._test_doc_examples_validate()
|
|
154
|
+
self._test_chapter_depth_gates()
|
|
155
|
+
self._test_claims_contract()
|
|
156
|
+
self._test_auto_dual_axis_and_value_format()
|
|
157
|
+
self._test_rejects_pie_over_six_categories()
|
|
158
|
+
self._test_same_value_float_tolerance()
|
|
159
|
+
self._test_execution_meta_appendix()
|
|
160
|
+
self._test_toc_anchors()
|
|
161
|
+
self._test_datazoom()
|
|
162
|
+
self._test_table_number_formatting()
|
|
163
|
+
self._test_rejects_invalid_series_extras()
|
|
164
|
+
|
|
165
|
+
def _test_valid_end_to_end_sample(self):
|
|
166
|
+
document = render_viz_report.render_report(_sample_input(), echarts_js=STUB_ECHARTS)
|
|
167
|
+
self.assertIn('<html lang="zh-CN">', document)
|
|
168
|
+
self.assertIn("window.VIZ_REPORT_DATA=", document)
|
|
169
|
+
self.assertIn('id="chart-chart-trend" class="chart-canvas"', document)
|
|
170
|
+
# 表格图直接内嵌为静态表格;交互图通过 JSON 注入 echarts option。
|
|
171
|
+
self.assertIn("<td>门店</td>", document)
|
|
172
|
+
option_payload = document.split("window.VIZ_REPORT_DATA=", 1)[1]
|
|
173
|
+
for fragment in ("月度销售额趋势", "环比增幅", '"type": "line"', '"type": "pie"'):
|
|
174
|
+
with self.subTest(fragment=fragment):
|
|
175
|
+
self.assertIn(fragment, option_payload if '"type"' in fragment else document)
|
|
176
|
+
# 离线零外链:不得出现任何外部资源引用。
|
|
177
|
+
for forbidden in ('src="http', 'href="http', "<script src="):
|
|
178
|
+
with self.subTest(forbidden=forbidden):
|
|
179
|
+
self.assertNotIn(forbidden, document)
|
|
180
|
+
|
|
181
|
+
def _test_rejects_off_ledger_values(self):
|
|
182
|
+
mutations = {
|
|
183
|
+
"series_value": (lambda data: data["charts"][0]["series"][0].__setitem__("data", [1200000, 999999])),
|
|
184
|
+
"category_label": (lambda data: data["charts"][1]["categories"].__setitem__("values", ["线上", "线上门店"])),
|
|
185
|
+
"table_cell": (lambda data: data["charts"][2]["rows"].__setitem__(0, ["门店", 123456])),
|
|
186
|
+
"undeclared_derived_value": (lambda data: data["charts"][0]["series"][1].__setitem__("data", [None, 0.15])),
|
|
187
|
+
}
|
|
188
|
+
# 每种篡改都应被溯源校验拦截,且错误分类精确可断言(不降低断言强度)。
|
|
189
|
+
expected = {
|
|
190
|
+
"series_value": "越源数值",
|
|
191
|
+
"category_label": "越源数值",
|
|
192
|
+
"table_cell": "越源行",
|
|
193
|
+
"undeclared_derived_value": "越源数值",
|
|
194
|
+
}
|
|
195
|
+
for name, mutate in mutations.items():
|
|
196
|
+
with self.subTest(case=name):
|
|
197
|
+
data = _sample_input()
|
|
198
|
+
mutate(data)
|
|
199
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
200
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
201
|
+
message = str(ctx.exception)
|
|
202
|
+
self.assertIn(expected[name], message)
|
|
203
|
+
# 错误提示必须包含可执行的下一步建议(仓库硬规)。
|
|
204
|
+
self.assertIn("重跑", message)
|
|
205
|
+
|
|
206
|
+
def _test_builtin_derivations(self):
|
|
207
|
+
data = _sample_input()
|
|
208
|
+
entry = data["derivations"][0]
|
|
209
|
+
entry["output_values"][1] = data["charts"][0]["series"][1]["data"][1] = 0.80
|
|
210
|
+
with self.assertRaisesRegex(ReportContractError, "复算"):
|
|
211
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
212
|
+
del entry["output_values"]
|
|
213
|
+
del data["charts"][0]["series"][1]["data"]
|
|
214
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
215
|
+
self.assertIn("已按来源行复算", document)
|
|
216
|
+
self.assertIn("产出值:缺失、0.12", document)
|
|
217
|
+
entry.update(method={"type": "formula", "expression": "custom"}, output_values=[None, 0.12])
|
|
218
|
+
self.assertIn("自定义计算,未自动复算", render_viz_report.render_report(data, echarts_js=STUB_ECHARTS))
|
|
219
|
+
|
|
220
|
+
cards = [
|
|
221
|
+
{"card_id": "a", "payload_rows": [["k", "v"], ["a", 0], ["b", 6], ["c", 2]]},
|
|
222
|
+
{"card_id": "b", "payload_rows": [["k", "w"], ["c", None], ["b", 3], ["a", 0]]},
|
|
223
|
+
]
|
|
224
|
+
entry.update(key_card_id="a", key_field="k", keys=["a", "b", "c"])
|
|
225
|
+
del entry["output_values"]
|
|
226
|
+
for operation, fields, sources, expected in (
|
|
227
|
+
("growth_rate", ["v"], ["a"], [None, None, -2 / 3]),
|
|
228
|
+
("share", ["v"], ["a"], [0, 0.75, 0.25]),
|
|
229
|
+
("ratio", ["v", "w"], ["a", "b"], [None, 2, None]),
|
|
230
|
+
("difference", ["v", "w"], ["a", "b"], [0, 3, None]),
|
|
231
|
+
):
|
|
232
|
+
with self.subTest(operation=operation):
|
|
233
|
+
entry.update(method={"type": operation}, source_card_ids=sources, input_fields=fields)
|
|
234
|
+
result = render_viz_report.validate_derivations([entry], cards)[entry["derivation_id"]]
|
|
235
|
+
self.assertEqual(result["output_values"], expected)
|
|
236
|
+
entry.update(method={"type": "share"}, source_card_ids=["b"], key_card_id="b", input_fields=["w"])
|
|
237
|
+
self.assertEqual(render_viz_report.validate_derivations([entry], cards)["d-mom"]["output_values"], [None] * 3)
|
|
238
|
+
cards[1]["payload_rows"].append(["b", 5])
|
|
239
|
+
with self.assertRaisesRegex(ReportContractError, "重复"):
|
|
240
|
+
render_viz_report.validate_derivations([entry], cards)
|
|
241
|
+
|
|
242
|
+
def _test_growth_rate_follows_key_order_not_source_row_order(self):
|
|
243
|
+
"""接口按倒序返回来源行时,环比仍按 keys 升序取上期,不静默反号。"""
|
|
244
|
+
entry = {
|
|
245
|
+
"derivation_id": "d-mom",
|
|
246
|
+
"source_card_ids": ["a"],
|
|
247
|
+
"input_fields": ["销售额"],
|
|
248
|
+
"output_column": "环比增幅",
|
|
249
|
+
"method": {"type": "growth_rate"},
|
|
250
|
+
"key_field": "月份",
|
|
251
|
+
"keys": ["2月", "3月"],
|
|
252
|
+
"key_card_id": "a",
|
|
253
|
+
}
|
|
254
|
+
ascending = [{"card_id": "a", "payload_rows": [["月份", "销售额"], ["2月", 120], ["3月", 134]]}]
|
|
255
|
+
descending = [{"card_id": "a", "payload_rows": [["月份", "销售额"], ["3月", 134], ["2月", 120]]}]
|
|
256
|
+
for label, cards in (("升序", ascending), ("倒序", descending)):
|
|
257
|
+
with self.subTest(source_order=label):
|
|
258
|
+
outputs = render_viz_report.validate_derivations([dict(entry)], cards)["d-mom"]["output_values"]
|
|
259
|
+
self.assertEqual(outputs, [None, 7 / 60])
|
|
260
|
+
|
|
261
|
+
def _test_requires_complete_derivation_log(self):
|
|
262
|
+
cases = {
|
|
263
|
+
"missing_expression": lambda entry: entry.update(method={"type": "python"}),
|
|
264
|
+
"bad_method_type": lambda entry: entry["method"].__setitem__("type", "估算"),
|
|
265
|
+
"unknown_source_card": lambda entry: entry.__setitem__("source_card_ids", ["ghost-card"]),
|
|
266
|
+
"field_not_in_header": lambda entry: entry.__setitem__("input_fields", ["退货额"]),
|
|
267
|
+
}
|
|
268
|
+
for name, break_entry in cases.items():
|
|
269
|
+
with self.subTest(case=name):
|
|
270
|
+
data = _sample_input()
|
|
271
|
+
break_entry(data["derivations"][0])
|
|
272
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
273
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
274
|
+
message = str(ctx.exception)
|
|
275
|
+
self.assertIn("d-mom", message)
|
|
276
|
+
self.assertTrue(
|
|
277
|
+
any(hint in message for hint in ("重跑", "补齐", "补写")),
|
|
278
|
+
f"错误信息缺少下一步建议:{message}",
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
def _test_generates_footnotes_and_appendix(self):
|
|
282
|
+
document = render_viz_report.render_report(_sample_input(), echarts_js=STUB_ECHARTS)
|
|
283
|
+
expected_footnote = (
|
|
284
|
+
"数据来源:华东经营分析 · 查询:“近半年各月销售额” · 检索时间:2025-07-08T09:30:00Z · 数据 3 行;含派生数据(环比增幅),计算方式见附录B"
|
|
285
|
+
)
|
|
286
|
+
self.assertIn(expected_footnote, document)
|
|
287
|
+
# 脚注行数不含表头:r2-q2 共两行数据。
|
|
288
|
+
self.assertIn("查询:“各渠道销售额构成” · 检索时间:2025-07-08T09:40:00Z · 数据 2 行", document)
|
|
289
|
+
self.assertIn("A.1 卡片 r1-q1", document)
|
|
290
|
+
self.assertIn("B.1 【环比增幅】基于卡 r1-q1 的「销售额」:", document)
|
|
291
|
+
self.assertIn("产出值:缺失、0.12(id=d-mom)", document)
|
|
292
|
+
self.assertIn("问题“华北与华南对比探索”——中间结果波动无稳定模式,未进入主线结论", document)
|
|
293
|
+
# 缺失值保持显式标注,不被估算填充。
|
|
294
|
+
self.assertIn(">缺失<", document)
|
|
295
|
+
|
|
296
|
+
def _test_escapes_line_separators_in_chart_payload(self):
|
|
297
|
+
separator_label = "渠道A\u2028B"
|
|
298
|
+
data = _sample_input()
|
|
299
|
+
data["cards"][1]["payload_rows"][1][0] = separator_label
|
|
300
|
+
data["charts"][1]["categories"]["values"] = [separator_label, "门店"]
|
|
301
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
302
|
+
payload_region = document.split("window.VIZ_REPORT_DATA=", 1)[1].split(";", 1)[0]
|
|
303
|
+
# 行分隔符字符在脚本注入区必须以字面转义序列出现,避免 JS 解析中断。
|
|
304
|
+
self.assertIn("\\u2028", payload_region)
|
|
305
|
+
self.assertNotIn("\u2028", payload_region)
|
|
306
|
+
|
|
307
|
+
def _test_structural_validation(self):
|
|
308
|
+
cases = {
|
|
309
|
+
"missing_executive_summary": (lambda d: d.pop("executive_summary"), "executive_summary"),
|
|
310
|
+
"empty_summary": (lambda d: d["executive_summary"].__setitem__("summary", " "), "executive_summary.summary"),
|
|
311
|
+
"empty_recommendations": (lambda d: d["executive_summary"].__setitem__("recommendations", []), "recommendations"),
|
|
312
|
+
"missing_chapters": (lambda d: d.pop("chapters"), "chapters"),
|
|
313
|
+
"orphan_chart": (lambda d: d["chapters"][0].__setitem__("chart_ids", ["chart-trend", "chart-mix"]), "孤儿图"),
|
|
314
|
+
"duplicate_chart_reference": (
|
|
315
|
+
lambda d: d["chapters"].append(
|
|
316
|
+
{
|
|
317
|
+
"chapter_id": "ch-extra",
|
|
318
|
+
"title": "二、重复章",
|
|
319
|
+
"purpose": "x",
|
|
320
|
+
"digest": "x",
|
|
321
|
+
"chart_ids": ["chart-detail", "chart-trend"],
|
|
322
|
+
}
|
|
323
|
+
),
|
|
324
|
+
"多个章节",
|
|
325
|
+
),
|
|
326
|
+
"unknown_chart_reference": (
|
|
327
|
+
lambda d: d["chapters"][0].__setitem__("chart_ids", ["chart-trend", "ghost-chart"]),
|
|
328
|
+
"不存在的图表",
|
|
329
|
+
),
|
|
330
|
+
}
|
|
331
|
+
for name, (mutate, fragment) in cases.items():
|
|
332
|
+
with self.subTest(case=name):
|
|
333
|
+
data = _sample_input()
|
|
334
|
+
mutate(data)
|
|
335
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
336
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
337
|
+
message = str(ctx.exception)
|
|
338
|
+
self.assertIn(fragment, message)
|
|
339
|
+
# 结构性错误同样必须给出下一步建议(仓库硬规)。
|
|
340
|
+
self.assertIn("重跑", message)
|
|
341
|
+
|
|
342
|
+
def _test_csv_download_assets(self):
|
|
343
|
+
document = render_viz_report.render_report(_sample_input(), echarts_js=STUB_ECHARTS)
|
|
344
|
+
# 附录卡片与下载按钮:每张卡有编号按钮,脚本数据区嵌入了全部 payload。
|
|
345
|
+
expected_button = '<button class="download-btn" type="button" onclick="event.preventDefault();VIZ_DOWNLOAD_CSV(0)">下载 CSV</button>'
|
|
346
|
+
self.assertIn(expected_button, document)
|
|
347
|
+
self.assertIn("VIZ_DOWNLOAD_CSV(1)", document)
|
|
348
|
+
# 默认收起:每张来源卡一个 <details> 折叠块,summary 行存在,下载按钮数==卡数。
|
|
349
|
+
self.assertEqual(document.count("<details"), 2)
|
|
350
|
+
self.assertIn("<summary>", document)
|
|
351
|
+
self.assertEqual(document.count("VIZ_DOWNLOAD_CSV("), 2)
|
|
352
|
+
self.assertIn("window.VIZ_APPENDIX_CARDS=", document)
|
|
353
|
+
self.assertIn('"cardId": "r1-q1"', document)
|
|
354
|
+
# CSV 脚本片段:RFC4180 转义函数 + BOM 常量真实存在于输出文档。
|
|
355
|
+
self.assertIn("window.__vizCsvCell = function", document)
|
|
356
|
+
self.assertIn("\ufeff", document)
|
|
357
|
+
self.assertIn("<h2>总结与建议</h2>", document)
|
|
358
|
+
# 总分顺序:全局总结 → 章节小结 → 章内首图 → 附录。
|
|
359
|
+
positions = {
|
|
360
|
+
"summary": document.find('class="executive-summary"'),
|
|
361
|
+
"digest": document.find('class="digest"'),
|
|
362
|
+
"first_chart": document.find('id="chart-chart-trend"'),
|
|
363
|
+
"appendix": document.find('class="appendix"'),
|
|
364
|
+
}
|
|
365
|
+
ordering = list(positions.values())
|
|
366
|
+
self.assertTrue(all(p >= 0 for p in ordering))
|
|
367
|
+
self.assertEqual(sorted(ordering), ordering)
|
|
368
|
+
|
|
369
|
+
def _test_cli_roundtrip(self):
|
|
370
|
+
with tempfile.TemporaryDirectory() as workdir:
|
|
371
|
+
input_path = Path(workdir) / "input.json"
|
|
372
|
+
output_path = Path(workdir) / "report.html"
|
|
373
|
+
input_path.write_text(json.dumps(_sample_input(), ensure_ascii=False), encoding="utf-8")
|
|
374
|
+
exit_code = render_viz_report.main([str(input_path), "-o", str(output_path)])
|
|
375
|
+
self.assertEqual(0, exit_code)
|
|
376
|
+
document = output_path.read_text(encoding="utf-8")
|
|
377
|
+
# CLI 直跑走真实 assets/echarts.min.js 内联路径。
|
|
378
|
+
self.assertIn("Licensed to the Apache Software Foundation", document)
|
|
379
|
+
bad_input = Path(workdir) / "broken.json"
|
|
380
|
+
bad_input.write_text(json.dumps({"plan_meta": {}, "cards": [], "charts": []}), encoding="utf-8")
|
|
381
|
+
exit_code = render_viz_report.main([str(bad_input), "-o", str(output_path)])
|
|
382
|
+
self.assertEqual(1, exit_code)
|
|
383
|
+
|
|
384
|
+
def _test_rejects_row_swapped_values(self):
|
|
385
|
+
# 评审复现场景:类目不变、两行数值对调——集合成员检查放行、行元组校验必须拒绝。
|
|
386
|
+
data = _sample_input()
|
|
387
|
+
data["charts"][0]["series"][0]["data"] = [1344000, 1200000]
|
|
388
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
389
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
390
|
+
message = str(ctx.exception)
|
|
391
|
+
self.assertIn("越源数值", message)
|
|
392
|
+
self.assertIn("同一行", message)
|
|
393
|
+
self.assertIn("重跑", message)
|
|
394
|
+
|
|
395
|
+
def _test_rejects_missing_field_binding(self):
|
|
396
|
+
data = _sample_input()
|
|
397
|
+
data["charts"][0]["categories"].pop("field")
|
|
398
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
399
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
400
|
+
self.assertIn("field 列绑定", str(ctx.exception))
|
|
401
|
+
|
|
402
|
+
def _test_rejects_derivation_key_mismatch(self):
|
|
403
|
+
data = _sample_input()
|
|
404
|
+
data["derivations"][0]["keys"] = ["3月", "2月"]
|
|
405
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
406
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
407
|
+
message = str(ctx.exception)
|
|
408
|
+
self.assertIn("d-mom", message)
|
|
409
|
+
self.assertIn("顺序敏感", message)
|
|
410
|
+
|
|
411
|
+
def _test_rejects_duplicate_headers(self):
|
|
412
|
+
data = _sample_input()
|
|
413
|
+
data["cards"][0]["payload_rows"][0] = ["月份", "月份"]
|
|
414
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
415
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
416
|
+
self.assertIn("重名", str(ctx.exception))
|
|
417
|
+
|
|
418
|
+
def _test_scatter_row_pair_tracing(self):
|
|
419
|
+
data = _sample_input()
|
|
420
|
+
data["charts"].append(
|
|
421
|
+
{
|
|
422
|
+
"chart_id": "chart-scatter",
|
|
423
|
+
"title": "月份-销售额散点",
|
|
424
|
+
"chart_type": "scatter",
|
|
425
|
+
"source_card_id": "r1-q1",
|
|
426
|
+
"evidence_role": "diagnostic",
|
|
427
|
+
"series": [
|
|
428
|
+
{"name": "月份", "data": ["2月", "3月"], "field": "月份"},
|
|
429
|
+
{"name": "销售额", "data": [1200000, 1344000], "field": "销售额"},
|
|
430
|
+
],
|
|
431
|
+
"interpretation": "散点按行取数验证。",
|
|
432
|
+
}
|
|
433
|
+
)
|
|
434
|
+
data["chapters"][0]["chart_ids"].append("chart-scatter")
|
|
435
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
436
|
+
self.assertIn("chart-scatter", document)
|
|
437
|
+
data["charts"][3]["series"][1]["data"] = [999999, 1344000]
|
|
438
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
439
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
440
|
+
self.assertIn("越源数值", str(ctx.exception))
|
|
441
|
+
|
|
442
|
+
def _test_en_us_output_labels(self):
|
|
443
|
+
data = _sample_input()
|
|
444
|
+
data["plan_meta"]["output_language"] = "en-US"
|
|
445
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
446
|
+
self.assertIn('<html lang="en-US">', document)
|
|
447
|
+
self.assertIn("Summary & Recommendations", document)
|
|
448
|
+
self.assertIn("Download CSV", document)
|
|
449
|
+
self.assertIn("Appendix A Source Data", document)
|
|
450
|
+
self.assertIn("Data source: ", document)
|
|
451
|
+
for forbidden in ("总结与建议", "下载 CSV", "附录A", "图表库未能加载"):
|
|
452
|
+
with self.subTest(forbidden=forbidden):
|
|
453
|
+
self.assertNotIn(forbidden, document)
|
|
454
|
+
|
|
455
|
+
def _test_rejects_cross_card_derived_series(self):
|
|
456
|
+
# 评审场景:派生系列来自卡 r1-q1(key_card),却挂在来源卡 r2-q2 的图上,
|
|
457
|
+
# 且类目键名与 r1-q1 的 keys 相同——旧实现会因同名键放行、脚注只标 r2-q2,造成来源误标。
|
|
458
|
+
data = _sample_input()
|
|
459
|
+
data["derivations"].append(
|
|
460
|
+
{
|
|
461
|
+
"derivation_id": "d-cross",
|
|
462
|
+
"source_card_ids": ["r1-q1"],
|
|
463
|
+
"input_fields": ["销售额"],
|
|
464
|
+
"output_column": "跨卡占比",
|
|
465
|
+
"method": {"type": "formula", "expression": "1200000/1344000 ≈ 0.5"},
|
|
466
|
+
"output_values": [0.5, 0.5],
|
|
467
|
+
"key_field": "月份",
|
|
468
|
+
"keys": ["2月", "3月"],
|
|
469
|
+
"key_card_id": "r1-q1",
|
|
470
|
+
}
|
|
471
|
+
)
|
|
472
|
+
data["charts"].append(
|
|
473
|
+
{
|
|
474
|
+
"chart_id": "chart-cross",
|
|
475
|
+
"title": "跨卡派生系列",
|
|
476
|
+
"chart_type": "bar",
|
|
477
|
+
"source_card_id": "r2-q2",
|
|
478
|
+
"evidence_role": "diagnostic",
|
|
479
|
+
"categories": {"values": ["2月", "3月"], "field": "渠道"},
|
|
480
|
+
"series": [{"name": "跨卡占比", "data": [0.5, 0.5], "derivation_id": "d-cross"}],
|
|
481
|
+
"interpretation": "跨卡引用应被拒绝。",
|
|
482
|
+
}
|
|
483
|
+
)
|
|
484
|
+
data["chapters"][0]["chart_ids"].append("chart-cross")
|
|
485
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
486
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
487
|
+
message = str(ctx.exception)
|
|
488
|
+
self.assertIn("d-cross", message)
|
|
489
|
+
self.assertIn("同源", message)
|
|
490
|
+
self.assertIn("重跑", message)
|
|
491
|
+
|
|
492
|
+
def _test_derived_table_column_without_field_renders(self):
|
|
493
|
+
# 评审场景:按文档构造的无 field 派生表格列(仅 header+derivation_id)必须能渲染,
|
|
494
|
+
# 取值与行对齐由键列与 output_values 校验兜底。
|
|
495
|
+
data = _sample_input()
|
|
496
|
+
data["derivations"].append(
|
|
497
|
+
{
|
|
498
|
+
"derivation_id": "d-share",
|
|
499
|
+
"source_card_ids": ["r2-q2"],
|
|
500
|
+
"input_fields": ["销售额"],
|
|
501
|
+
"output_column": "渠道占比",
|
|
502
|
+
"method": {"type": "formula", "expression": "600000/1000000=0.6;400000/1000000=0.4"},
|
|
503
|
+
"output_values": [0.6, 0.4],
|
|
504
|
+
"key_field": "渠道",
|
|
505
|
+
"keys": ["线上", "门店"],
|
|
506
|
+
"key_card_id": "r2-q2",
|
|
507
|
+
}
|
|
508
|
+
)
|
|
509
|
+
data["charts"].append(
|
|
510
|
+
{
|
|
511
|
+
"chart_id": "chart-share",
|
|
512
|
+
"title": "渠道占比明细",
|
|
513
|
+
"chart_type": "table",
|
|
514
|
+
"source_card_id": "r2-q2",
|
|
515
|
+
"evidence_role": "diagnostic",
|
|
516
|
+
"columns": [
|
|
517
|
+
{"header": "渠道", "field": "渠道"},
|
|
518
|
+
{"header": "销售额", "field": "销售额"},
|
|
519
|
+
{"header": "占比", "derivation_id": "d-share"},
|
|
520
|
+
],
|
|
521
|
+
"rows": [["线上", 600000, 0.6], ["门店", 400000, 0.4]],
|
|
522
|
+
"interpretation": "无 field 的派生列按 keys 顺序逐行对齐。",
|
|
523
|
+
}
|
|
524
|
+
)
|
|
525
|
+
data["chapters"][0]["chart_ids"].append("chart-share")
|
|
526
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
527
|
+
self.assertIn("<th>占比</th>", document)
|
|
528
|
+
self.assertIn("<td>0.6</td>", document)
|
|
529
|
+
self.assertIn("含派生数据(渠道占比)", document)
|
|
530
|
+
|
|
531
|
+
def _test_rejects_cross_card_derived_table_column(self):
|
|
532
|
+
# 与派生系列同一条契约:表格列引用的派生 key_card 必须与本图来源卡同源。
|
|
533
|
+
data = _sample_input()
|
|
534
|
+
data["derivations"].append(
|
|
535
|
+
{
|
|
536
|
+
"derivation_id": "d-cross-table",
|
|
537
|
+
"source_card_ids": ["r1-q1"],
|
|
538
|
+
"input_fields": ["销售额"],
|
|
539
|
+
"output_column": "跨卡占比",
|
|
540
|
+
"method": {"type": "formula", "expression": "1200000/1344000 ≈ 0.9"},
|
|
541
|
+
"output_values": [0.9],
|
|
542
|
+
"key_field": "月份",
|
|
543
|
+
"keys": ["2月"],
|
|
544
|
+
"key_card_id": "r1-q1",
|
|
545
|
+
}
|
|
546
|
+
)
|
|
547
|
+
data["charts"].append(
|
|
548
|
+
{
|
|
549
|
+
"chart_id": "chart-cross-table",
|
|
550
|
+
"title": "跨卡派生列",
|
|
551
|
+
"chart_type": "table",
|
|
552
|
+
"source_card_id": "r2-q2",
|
|
553
|
+
"evidence_role": "diagnostic",
|
|
554
|
+
"columns": [{"header": "渠道", "field": "渠道"}, {"header": "占比", "derivation_id": "d-cross-table"}],
|
|
555
|
+
"rows": [["线上", 0.9]],
|
|
556
|
+
"interpretation": "跨卡派生列应被拒绝。",
|
|
557
|
+
}
|
|
558
|
+
)
|
|
559
|
+
data["chapters"][0]["chart_ids"].append("chart-cross-table")
|
|
560
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
561
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
562
|
+
message = str(ctx.exception)
|
|
563
|
+
self.assertIn("d-cross-table", message)
|
|
564
|
+
self.assertIn("同源", message)
|
|
565
|
+
|
|
566
|
+
def _test_rejects_multi_series_category_splice(self):
|
|
567
|
+
# 评审场景:来源卡存在重复类目(线上出现两行),多系列逐点单独校验会放行
|
|
568
|
+
# 从不同行拼出的 (线上, 100, 20) 组合;整行元组校验必须拒绝。
|
|
569
|
+
data = _sample_input()
|
|
570
|
+
data["cards"].append(
|
|
571
|
+
{
|
|
572
|
+
"card_id": "r4-q4",
|
|
573
|
+
"question": "各渠道销售额与利润",
|
|
574
|
+
"fetched_at": "2025-07-08T09:50:00Z",
|
|
575
|
+
"payload_rows": [
|
|
576
|
+
["渠道", "销售额", "利润"],
|
|
577
|
+
["线上", 100, 10],
|
|
578
|
+
["线上", 200, 20],
|
|
579
|
+
],
|
|
580
|
+
}
|
|
581
|
+
)
|
|
582
|
+
chart = {
|
|
583
|
+
"chart_id": "chart-splice",
|
|
584
|
+
"title": "渠道销售额与利润",
|
|
585
|
+
"chart_type": "bar",
|
|
586
|
+
"source_card_id": "r4-q4",
|
|
587
|
+
"evidence_role": "diagnostic",
|
|
588
|
+
"categories": {"values": ["线上", "线上"], "field": "渠道"},
|
|
589
|
+
"series": [
|
|
590
|
+
{"name": "销售额", "data": [100, 200], "field": "销售额"},
|
|
591
|
+
{"name": "利润", "data": [10, 20], "field": "利润"},
|
|
592
|
+
],
|
|
593
|
+
"interpretation": "重复类目下逐行取数。",
|
|
594
|
+
}
|
|
595
|
+
data["charts"].append(chart)
|
|
596
|
+
data["chapters"][0]["chart_ids"].append("chart-splice")
|
|
597
|
+
# 每个数据点都与来源卡某一行完整对应:合法,正常渲染。
|
|
598
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
599
|
+
self.assertIn("chart-splice", document)
|
|
600
|
+
# 利润两行对调:拼出的组合从未在同一行出现,必须拒绝。
|
|
601
|
+
chart["series"][1]["data"] = [20, 10]
|
|
602
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
603
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
604
|
+
message = str(ctx.exception)
|
|
605
|
+
self.assertIn("越源数值", message)
|
|
606
|
+
self.assertIn("同一行", message)
|
|
607
|
+
self.assertIn("重跑", message)
|
|
608
|
+
|
|
609
|
+
def _test_rejects_ragged_payload_rows(self):
|
|
610
|
+
# 评审场景:短行/空表头必须在账本加载期给出契约错误,而不是让列下标访问抛 IndexError。
|
|
611
|
+
cases = {
|
|
612
|
+
"short_row": (lambda rows: rows.__setitem__(3, [None]), "不等长"),
|
|
613
|
+
"empty_header": (lambda rows: rows.__setitem__(0, []), "表头行为空"),
|
|
614
|
+
}
|
|
615
|
+
for name, (mutate, fragment) in cases.items():
|
|
616
|
+
with self.subTest(case=name):
|
|
617
|
+
data = _sample_input()
|
|
618
|
+
mutate(data["cards"][0]["payload_rows"])
|
|
619
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
620
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
621
|
+
message = str(ctx.exception)
|
|
622
|
+
self.assertIn("r1-q1", message)
|
|
623
|
+
self.assertIn(fragment, message)
|
|
624
|
+
self.assertIn("重跑", message)
|
|
625
|
+
|
|
626
|
+
def _multi_derivation_fixture(self):
|
|
627
|
+
"""渠道+月份双派生 fixture:两行分别对齐两个 key_field 的 keys 顺序。"""
|
|
628
|
+
data = _sample_input()
|
|
629
|
+
data["cards"].append(
|
|
630
|
+
{
|
|
631
|
+
"card_id": "r3-q3",
|
|
632
|
+
"question": "各渠道各月销售额",
|
|
633
|
+
"fetched_at": "2025-07-08T09:45:00Z",
|
|
634
|
+
"payload_rows": [
|
|
635
|
+
["渠道", "月份", "销售额"],
|
|
636
|
+
["线上", "1月", 500],
|
|
637
|
+
["门店", "2月", 700],
|
|
638
|
+
],
|
|
639
|
+
}
|
|
640
|
+
)
|
|
641
|
+
data["derivations"].extend(
|
|
642
|
+
[
|
|
643
|
+
{
|
|
644
|
+
"derivation_id": "d-channel",
|
|
645
|
+
"source_card_ids": ["r3-q3"],
|
|
646
|
+
"input_fields": ["销售额"],
|
|
647
|
+
"output_column": "渠道占比",
|
|
648
|
+
"method": {"type": "formula", "expression": "渠道占比_i = 销售额_i / 1200"},
|
|
649
|
+
"output_values": [500 / 1200, 700 / 1200],
|
|
650
|
+
"key_field": "渠道",
|
|
651
|
+
"keys": ["线上", "门店"],
|
|
652
|
+
"key_card_id": "r3-q3",
|
|
653
|
+
},
|
|
654
|
+
{
|
|
655
|
+
"derivation_id": "d-month",
|
|
656
|
+
"source_card_ids": ["r3-q3"],
|
|
657
|
+
"input_fields": ["销售额"],
|
|
658
|
+
"output_column": "月度环比",
|
|
659
|
+
"method": {"type": "python", "expression": "vals = [500, 700]\nmom = [None, (700 - 500) / 500]"},
|
|
660
|
+
"output_values": [None, 0.4],
|
|
661
|
+
"key_field": "月份",
|
|
662
|
+
"keys": ["1月", "2月"],
|
|
663
|
+
"key_card_id": "r3-q3",
|
|
664
|
+
},
|
|
665
|
+
]
|
|
666
|
+
)
|
|
667
|
+
return data
|
|
668
|
+
|
|
669
|
+
def _test_rejects_multi_derivation_missing_key_column(self):
|
|
670
|
+
# 评审复现:渠道派生 + 月份派生,表格只提供渠道键列。旧实现共用首个键列位置,
|
|
671
|
+
# 月份派生完全不与月份键对齐即放行;新实现必须逐派生列校验并拒绝。
|
|
672
|
+
data = self._multi_derivation_fixture()
|
|
673
|
+
chart = {
|
|
674
|
+
"chart_id": "chart-multi-deriv",
|
|
675
|
+
"title": "双派生明细",
|
|
676
|
+
"chart_type": "table",
|
|
677
|
+
"source_card_id": "r3-q3",
|
|
678
|
+
"evidence_role": "diagnostic",
|
|
679
|
+
"columns": [
|
|
680
|
+
{"header": "渠道", "field": "渠道"},
|
|
681
|
+
{"header": "销售额", "field": "销售额"},
|
|
682
|
+
{"header": "渠道占比", "derivation_id": "d-channel"},
|
|
683
|
+
{"header": "月度环比", "derivation_id": "d-month"},
|
|
684
|
+
],
|
|
685
|
+
"rows": [["线上", 500, 500 / 1200, None], ["门店", 700, 700 / 1200, 0.4]],
|
|
686
|
+
"interpretation": "双派生键列校验。",
|
|
687
|
+
}
|
|
688
|
+
data["charts"].append(chart)
|
|
689
|
+
data["chapters"][0]["chart_ids"].append("chart-multi-deriv")
|
|
690
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
691
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
692
|
+
message = str(ctx.exception)
|
|
693
|
+
self.assertIn("d-month", message)
|
|
694
|
+
self.assertIn("键列", message)
|
|
695
|
+
self.assertIn("重跑", message)
|
|
696
|
+
# 补齐月份键列且行序同时满足两个 keys 顺序时放行。
|
|
697
|
+
chart["columns"].insert(1, {"header": "月份", "field": "月份"})
|
|
698
|
+
chart["rows"] = [["线上", "1月", 500, 500 / 1200, None], ["门店", "2月", 700, 700 / 1200, 0.4]]
|
|
699
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
700
|
+
self.assertIn("双派生明细", document)
|
|
701
|
+
self.assertIn("<th>月度环比</th>", document)
|
|
702
|
+
# 键列行序对调后两个 keys 无法同时满足,必须拒绝。
|
|
703
|
+
chart["rows"] = [["门店", "2月", 700, 700 / 1200, 0.4], ["线上", "1月", 500, 500 / 1200, None]]
|
|
704
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
705
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
706
|
+
self.assertIn("顺序敏感", str(ctx.exception))
|
|
707
|
+
|
|
708
|
+
def _test_doc_examples_validate(self):
|
|
709
|
+
# 用来源行复算文档中的两个示例,保持参考资料与脚本一致。
|
|
710
|
+
doc_path = Path(__file__).resolve().parent.parent / "references" / "derivation-log.md"
|
|
711
|
+
doc_text = doc_path.read_text(encoding="utf-8")
|
|
712
|
+
examples = re.findall(r"### 示例[一二][^`]*```json\n(.*?)```", doc_text, re.S)
|
|
713
|
+
self.assertEqual(2, len(examples), "derivation-log.md 应包含示例一与示例二两个 JSON 示例")
|
|
714
|
+
cards = [
|
|
715
|
+
{
|
|
716
|
+
"card_id": "r1-q1",
|
|
717
|
+
"question": "月度销售额",
|
|
718
|
+
"fetched_at": "2025-07-08T09:00:00Z",
|
|
719
|
+
"payload_rows": [["月份", "月度销售额"], ["1月", 57320.5], ["2月", 1200000]],
|
|
720
|
+
},
|
|
721
|
+
{
|
|
722
|
+
"card_id": "r1-q2",
|
|
723
|
+
"question": "各月销售额与退货金额",
|
|
724
|
+
"fetched_at": "2025-07-08T09:01:00Z",
|
|
725
|
+
"payload_rows": [["月份", "销售额", "退货金额"], ["1月", 100, 3.1], ["2月", 100, 2.8], ["3月", 100, 4.4]],
|
|
726
|
+
},
|
|
727
|
+
{
|
|
728
|
+
"card_id": "r2-q4",
|
|
729
|
+
"question": "月度目标值",
|
|
730
|
+
"fetched_at": "2025-07-08T09:02:00Z",
|
|
731
|
+
"payload_rows": [["月份", "目标值"], ["1月", 42000.0], ["2月", 1202100]],
|
|
732
|
+
},
|
|
733
|
+
]
|
|
734
|
+
entries = [json.loads(example) for example in examples]
|
|
735
|
+
indexed = render_viz_report.validate_derivations(entries, cards)
|
|
736
|
+
self.assertEqual({"d-refund-rate", "d-diff-vs-target"}, set(indexed))
|
|
737
|
+
for actual, expected in zip(indexed["d-refund-rate"]["output_values"], [0.031, 0.028, 0.044], strict=True):
|
|
738
|
+
with self.subTest(rate=expected):
|
|
739
|
+
self.assertAlmostEqual(actual, expected)
|
|
740
|
+
self.assertEqual(indexed["d-diff-vs-target"]["output_values"], [15320.5, -2100.0])
|
|
741
|
+
|
|
742
|
+
def _test_chapter_depth_gates(self):
|
|
743
|
+
# 证据充分性下限:每章 minCards=2、至少一张 primary、至少两种角色、purpose 必填、depends_on 只能向前。
|
|
744
|
+
cases = {
|
|
745
|
+
"too_few_charts": (
|
|
746
|
+
lambda d: d["chapters"][0].__setitem__("chart_ids", ["chart-trend"]),
|
|
747
|
+
"证据下限",
|
|
748
|
+
),
|
|
749
|
+
"missing_purpose": (lambda d: d["chapters"][0].pop("purpose"), "purpose"),
|
|
750
|
+
"forward_depends_on": (
|
|
751
|
+
lambda d: d["chapters"][0].__setitem__("depends_on", ["ch-later"]),
|
|
752
|
+
"排在它之前",
|
|
753
|
+
),
|
|
754
|
+
}
|
|
755
|
+
for name, (mutate, fragment) in cases.items():
|
|
756
|
+
with self.subTest(case=name):
|
|
757
|
+
data = _sample_input()
|
|
758
|
+
mutate(data)
|
|
759
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
760
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
761
|
+
self.assertIn(fragment, str(ctx.exception))
|
|
762
|
+
# 全章同一角色 / 无 primary:两个独立场景分别断言。
|
|
763
|
+
data = _sample_input()
|
|
764
|
+
for chart in data["charts"]:
|
|
765
|
+
chart["evidence_role"] = "diagnostic"
|
|
766
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
767
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
768
|
+
self.assertIn("primary", str(ctx.exception))
|
|
769
|
+
data = _sample_input()
|
|
770
|
+
for chart in data["charts"]:
|
|
771
|
+
chart["evidence_role"] = "primary"
|
|
772
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
773
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
774
|
+
self.assertIn("同一证据角色", str(ctx.exception))
|
|
775
|
+
# 合法的递进链:后章 depends_on 引用前章时放行。
|
|
776
|
+
document = render_viz_report.render_report(self._two_chapter_input(), echarts_js=STUB_ECHARTS)
|
|
777
|
+
self.assertIn('id="chapter-ch-branch"', document)
|
|
778
|
+
|
|
779
|
+
def _test_claims_contract(self):
|
|
780
|
+
cases = {
|
|
781
|
+
"missing_claims": (lambda d: d.pop("claims"), "claims"),
|
|
782
|
+
"fact_without_evidence": (lambda d: d["claims"][0].__setitem__("evidence_card_ids", []), "fact"),
|
|
783
|
+
"inference_single_source": (lambda d: d["claims"][2].__setitem__("evidence_card_ids", ["r1-q1"]), "两个不同问题"),
|
|
784
|
+
"bad_kind": (lambda d: d["claims"][0].__setitem__("kind", "guess"), "kind"),
|
|
785
|
+
"bad_confidence": (lambda d: d["claims"][0].__setitem__("confidence", "sure"), "confidence"),
|
|
786
|
+
"unknown_card": (lambda d: d["claims"][0].__setitem__("evidence_card_ids", ["ghost-card"]), "不存在"),
|
|
787
|
+
"orphan_claim": (
|
|
788
|
+
lambda d: d["claims"].append(
|
|
789
|
+
{"claim_id": "f-orphan", "kind": "fact", "text": "无人引用的事实", "evidence_card_ids": ["r1-q1"], "confidence": "low"}
|
|
790
|
+
),
|
|
791
|
+
"孤儿 claim",
|
|
792
|
+
),
|
|
793
|
+
"text_not_in_chapter": (lambda d: d["claims"][0].__setitem__("text", "正文里找不到这句话"), "未出现在章节"),
|
|
794
|
+
"too_few_facts": (lambda d: d["chapters"][0].__setitem__("claim_refs", ["f-mom", "i-stable"]), "fact"),
|
|
795
|
+
}
|
|
796
|
+
for name, (mutate, fragment) in cases.items():
|
|
797
|
+
with self.subTest(case=name):
|
|
798
|
+
data = _sample_input()
|
|
799
|
+
mutate(data)
|
|
800
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
801
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
802
|
+
self.assertIn(fragment, str(ctx.exception))
|
|
803
|
+
|
|
804
|
+
def _test_auto_dual_axis_and_value_format(self):
|
|
805
|
+
# 样例中销售额(~1.3e6)与环比(0.12)量纲差 >10³:自动双轴,右轴为百分比轴。
|
|
806
|
+
document = render_viz_report.render_report(_sample_input(), echarts_js=STUB_ECHARTS)
|
|
807
|
+
payload = document.split("window.VIZ_REPORT_DATA=", 1)[1].split(";window.VIZ_APPENDIX_CARDS=", 1)[0]
|
|
808
|
+
specs = json.loads(payload)
|
|
809
|
+
trend = next(spec for spec in specs if spec["domId"] == "chart-trend")
|
|
810
|
+
option = trend["option"]
|
|
811
|
+
self.assertIsInstance(option["yAxis"], list)
|
|
812
|
+
self.assertEqual(2, len(option["yAxis"]))
|
|
813
|
+
mom = next(series for series in option["series"] if series["name"] == "环比增幅")
|
|
814
|
+
self.assertEqual(1, mom["yAxisIndex"])
|
|
815
|
+
self.assertEqual([1], option["__vizPercentAxes"])
|
|
816
|
+
self.assertEqual("percent", option["__vizSeriesFmt"]["环比增幅"])
|
|
817
|
+
# 显式声明系列级 type 的混合图照常渲染。
|
|
818
|
+
data = _sample_input()
|
|
819
|
+
data["charts"][0]["series"][0]["type"] = "bar"
|
|
820
|
+
data["charts"][0]["series"][1]["type"] = "line"
|
|
821
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
822
|
+
payload = document.split("window.VIZ_REPORT_DATA=", 1)[1].split(";window.VIZ_APPENDIX_CARDS=", 1)[0]
|
|
823
|
+
trend = next(spec for spec in json.loads(payload) if spec["domId"] == "chart-trend")
|
|
824
|
+
self.assertEqual("bar", trend["option"]["series"][0]["type"])
|
|
825
|
+
self.assertEqual("line", trend["option"]["series"][1]["type"])
|
|
826
|
+
|
|
827
|
+
def _test_rejects_pie_over_six_categories(self):
|
|
828
|
+
data = _sample_input()
|
|
829
|
+
labels = [f"类别{i}" for i in range(1, 8)]
|
|
830
|
+
data["cards"][1]["payload_rows"] = [["渠道", "销售额"]] + [[label, 100] for label in labels]
|
|
831
|
+
data["charts"][1]["categories"]["values"] = labels
|
|
832
|
+
data["charts"][1]["series"][0]["data"] = [100] * 7
|
|
833
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
834
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
835
|
+
message = str(ctx.exception)
|
|
836
|
+
self.assertIn("类别", message)
|
|
837
|
+
self.assertIn("重跑", message)
|
|
838
|
+
|
|
839
|
+
def _test_same_value_float_tolerance(self):
|
|
840
|
+
# 浮点噪声容忍:payload 200.30000000000001 与图上 200.3 视为同值(不放宽到可见差异)。
|
|
841
|
+
data = _sample_input()
|
|
842
|
+
data["cards"][1]["payload_rows"][1][1] = 100.1 + 100.2
|
|
843
|
+
data["charts"][1]["series"][0]["data"] = [200.3, 400000]
|
|
844
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
845
|
+
self.assertIn("chart-mix", document)
|
|
846
|
+
# 大整数精确比较:>2**53 的整数不被 float 精度模糊放行。
|
|
847
|
+
big = 2**53 + 1
|
|
848
|
+
data = _sample_input()
|
|
849
|
+
data["cards"][1]["payload_rows"][1][1] = big
|
|
850
|
+
data["charts"][1]["series"][0]["data"] = [big, 400000]
|
|
851
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
852
|
+
data["charts"][1]["series"][0]["data"] = [big - 1, 400000]
|
|
853
|
+
with self.assertRaises(ReportContractError):
|
|
854
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
855
|
+
|
|
856
|
+
def _test_execution_meta_appendix(self):
|
|
857
|
+
data = _sample_input()
|
|
858
|
+
data["plan_meta"]["execution"] = {"实际轮数": "2", "提问数": "6", "应用SOP": "无"}
|
|
859
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
860
|
+
self.assertIn("附录D 执行元数据", document)
|
|
861
|
+
self.assertIn("<th>实际轮数</th>", document)
|
|
862
|
+
self.assertIn("<td>6</td>", document)
|
|
863
|
+
for name, bad in {"empty": {}, "blank_value": {"轮数": " "}}.items():
|
|
864
|
+
with self.subTest(case=name):
|
|
865
|
+
data = _sample_input()
|
|
866
|
+
data["plan_meta"]["execution"] = bad
|
|
867
|
+
with self.assertRaises(ReportContractError):
|
|
868
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
869
|
+
|
|
870
|
+
def _test_toc_anchors(self):
|
|
871
|
+
data = self._two_chapter_input()
|
|
872
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
873
|
+
self.assertIn('<nav class="toc">', document)
|
|
874
|
+
self.assertIn('href="#chapter-ch-overview"', document)
|
|
875
|
+
self.assertIn('href="#chapter-ch-branch"', document)
|
|
876
|
+
self.assertIn('id="chapter-ch-branch"', document)
|
|
877
|
+
# 单章报告不出目录。
|
|
878
|
+
document = render_viz_report.render_report(_sample_input(), echarts_js=STUB_ECHARTS)
|
|
879
|
+
self.assertNotIn('<nav class="toc">', document)
|
|
880
|
+
|
|
881
|
+
def _two_chapter_input(self):
|
|
882
|
+
"""合法的双章输入:第二章依赖第一章,各章满足证据与 claim 下限。"""
|
|
883
|
+
data = _sample_input()
|
|
884
|
+
data["charts"].append(
|
|
885
|
+
{
|
|
886
|
+
"chart_id": "chart-branch",
|
|
887
|
+
"title": "分行复核",
|
|
888
|
+
"chart_type": "table",
|
|
889
|
+
"source_card_id": "r2-q2",
|
|
890
|
+
"evidence_role": "primary",
|
|
891
|
+
"columns": [{"header": "渠道", "field": "渠道"}, {"header": "销售额", "field": "销售额"}],
|
|
892
|
+
"rows": [["线上", 600000]],
|
|
893
|
+
"interpretation": "线上渠道贡献六成,复核一致。",
|
|
894
|
+
}
|
|
895
|
+
)
|
|
896
|
+
data["claims"].extend(
|
|
897
|
+
[
|
|
898
|
+
{"claim_id": "f-recheck", "kind": "fact", "text": "复核一致", "evidence_card_ids": ["r2-q2"], "confidence": "high"},
|
|
899
|
+
{
|
|
900
|
+
"claim_id": "l-branch",
|
|
901
|
+
"kind": "limitation",
|
|
902
|
+
"text": "复核仅覆盖线上渠道",
|
|
903
|
+
"evidence_card_ids": [],
|
|
904
|
+
"confidence": "high",
|
|
905
|
+
},
|
|
906
|
+
]
|
|
907
|
+
)
|
|
908
|
+
data["chapters"][0]["chart_ids"] = ["chart-trend", "chart-mix"]
|
|
909
|
+
data["chapters"].append(
|
|
910
|
+
{
|
|
911
|
+
"chapter_id": "ch-branch",
|
|
912
|
+
"title": "二、渠道复核",
|
|
913
|
+
"purpose": "构成结论是否经得起复核?",
|
|
914
|
+
"digest": "本章小结:线上渠道贡献六成,复核一致;复核仅覆盖线上渠道。",
|
|
915
|
+
"chart_ids": ["chart-detail", "chart-branch"],
|
|
916
|
+
"claim_refs": ["f-online", "f-recheck", "l-branch"],
|
|
917
|
+
"depends_on": ["ch-overview"],
|
|
918
|
+
}
|
|
919
|
+
)
|
|
920
|
+
return data
|
|
921
|
+
|
|
922
|
+
def _test_datazoom(self):
|
|
923
|
+
data = _sample_input()
|
|
924
|
+
months = [f"{i}月" for i in range(1, 15)]
|
|
925
|
+
data["cards"][0]["payload_rows"] = [["月份", "销售额"]] + [[month, 1000 * i] for i, month in enumerate(months, start=1)]
|
|
926
|
+
data["charts"][0]["categories"]["values"] = months
|
|
927
|
+
data["charts"][0]["series"][0]["data"] = [1000 * i for i in range(1, 15)]
|
|
928
|
+
# 派生系列与类目等长对齐。
|
|
929
|
+
data["derivations"][0]["keys"] = months
|
|
930
|
+
data["derivations"][0]["output_values"] = [None] + [1 / i for i in range(1, 14)]
|
|
931
|
+
data["charts"][0]["series"][1]["data"] = [None] + [1 / i for i in range(1, 14)]
|
|
932
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
933
|
+
payload = document.split("window.VIZ_REPORT_DATA=", 1)[1].split(";window.VIZ_APPENDIX_CARDS=", 1)[0]
|
|
934
|
+
trend = next(spec for spec in json.loads(payload) if spec["domId"] == "chart-trend")
|
|
935
|
+
self.assertIn("dataZoom", trend["option"])
|
|
936
|
+
|
|
937
|
+
def _test_table_number_formatting(self):
|
|
938
|
+
data = _sample_input()
|
|
939
|
+
data["cards"][1]["payload_rows"][2][1] = 52345678.9
|
|
940
|
+
data["charts"][1]["series"][0]["data"] = [600000, 52345678.9]
|
|
941
|
+
data["charts"][2]["rows"] = [["门店", 52345678.9]]
|
|
942
|
+
document = render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
943
|
+
# 正文表格千分位展示;附录A 账本保持原始写法。
|
|
944
|
+
self.assertIn("<td>52,345,678.9</td>", document)
|
|
945
|
+
self.assertIn("<td>52345678.9</td>", document)
|
|
946
|
+
|
|
947
|
+
def _test_rejects_invalid_series_extras(self):
|
|
948
|
+
cases = {
|
|
949
|
+
"bad_type": (lambda s: s.__setitem__("type", "area"), "type"),
|
|
950
|
+
"bad_y_axis": (lambda s: s.__setitem__("y_axis", "middle"), "y_axis"),
|
|
951
|
+
"bad_value_format": (lambda s: s.__setitem__("value_format", "currency"), "value_format"),
|
|
952
|
+
"bad_stack": (lambda s: s.__setitem__("stack", ""), "stack"),
|
|
953
|
+
}
|
|
954
|
+
for name, (mutate, fragment) in cases.items():
|
|
955
|
+
with self.subTest(case=name):
|
|
956
|
+
data = _sample_input()
|
|
957
|
+
mutate(data["charts"][0]["series"][0])
|
|
958
|
+
with self.assertRaises(ReportContractError) as ctx:
|
|
959
|
+
render_viz_report.render_report(data, echarts_js=STUB_ECHARTS)
|
|
960
|
+
self.assertIn(fragment, str(ctx.exception))
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
if __name__ == "__main__":
|
|
964
|
+
unittest.main()
|