Unify report module config traversal
This commit is contained in:
@@ -313,6 +313,37 @@ func TestValidateReportModuleKeysWithoutMutatingOptions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportModuleOverridesNormalizesConstructedOptionsWithoutMutatingConfig(t *testing.T) {
|
||||
cfg := Defaults()
|
||||
rawOptions := map[string]any{
|
||||
"sections": []any{"short_term"},
|
||||
}
|
||||
cfg.Reports = map[string]ReportConfig{
|
||||
"daily": {
|
||||
DeterministicModules: []ModuleConfigItem{
|
||||
{ID: module.Metadata},
|
||||
{ID: module.AreaForecastDiscussion, Options: rawOptions},
|
||||
},
|
||||
deterministicModulesSet: true,
|
||||
},
|
||||
}
|
||||
|
||||
overrides, err := cfg.ReportModuleOverrides()
|
||||
if err != nil {
|
||||
t.Fatalf("ReportModuleOverrides() error = %v", err)
|
||||
}
|
||||
options, ok := overrides[report.Daily][1].Options.(module.AreaForecastDiscussionOptions)
|
||||
if !ok {
|
||||
t.Fatalf("override options type = %T, want AreaForecastDiscussionOptions", overrides[report.Daily][1].Options)
|
||||
}
|
||||
if strings.Join(options.Sections, ",") != "short_term" {
|
||||
t.Fatalf("override sections = %#v, want short_term", options.Sections)
|
||||
}
|
||||
if got, ok := cfg.Reports["daily"].DeterministicModules[1].Options.(map[string]any); !ok || !reflect.DeepEqual(got, rawOptions) {
|
||||
t.Fatalf("config options after ReportModuleOverrides = %#v, want original raw map", cfg.Reports["daily"].DeterministicModules[1].Options)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateReportModuleAliasesDirectly(t *testing.T) {
|
||||
retiredDailyKey := retiredDailyReportKeyForTest()
|
||||
tests := []struct {
|
||||
@@ -510,6 +541,157 @@ reports:
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportModuleValidationConsistentForLoadedAndConstructedConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
yaml string
|
||||
reports map[string]ReportConfig
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "UnknownReport",
|
||||
yaml: `
|
||||
reports:
|
||||
moon:
|
||||
deterministic_modules:
|
||||
- metadata
|
||||
`,
|
||||
reports: map[string]ReportConfig{
|
||||
"moon": {
|
||||
DeterministicModules: []ModuleConfigItem{{ID: module.Metadata}},
|
||||
deterministicModulesSet: true,
|
||||
},
|
||||
},
|
||||
wantErr: "reports.moon",
|
||||
},
|
||||
{
|
||||
name: "DuplicateReportAlias",
|
||||
yaml: `
|
||||
reports:
|
||||
three-day:
|
||||
deterministic_modules:
|
||||
- metadata
|
||||
three_day:
|
||||
deterministic_modules:
|
||||
- metadata
|
||||
`,
|
||||
reports: map[string]ReportConfig{
|
||||
"three-day": {
|
||||
DeterministicModules: []ModuleConfigItem{{ID: module.Metadata}},
|
||||
deterministicModulesSet: true,
|
||||
},
|
||||
"three_day": {
|
||||
DeterministicModules: []ModuleConfigItem{{ID: module.Metadata}},
|
||||
deterministicModulesSet: true,
|
||||
},
|
||||
},
|
||||
wantErr: "duplicates report override",
|
||||
},
|
||||
{
|
||||
name: "UnknownModule",
|
||||
yaml: `
|
||||
reports:
|
||||
daily:
|
||||
deterministic_modules:
|
||||
- missing_module
|
||||
`,
|
||||
reports: map[string]ReportConfig{
|
||||
"daily": {
|
||||
DeterministicModules: []ModuleConfigItem{{ID: module.ID("missing_module")}},
|
||||
deterministicModulesSet: true,
|
||||
},
|
||||
},
|
||||
wantErr: `unknown module "missing_module"`,
|
||||
},
|
||||
{
|
||||
name: "DuplicateModule",
|
||||
yaml: `
|
||||
reports:
|
||||
daily:
|
||||
deterministic_modules:
|
||||
- metadata
|
||||
- metadata
|
||||
`,
|
||||
reports: map[string]ReportConfig{
|
||||
"daily": {
|
||||
DeterministicModules: []ModuleConfigItem{
|
||||
{ID: module.Metadata},
|
||||
{ID: module.Metadata},
|
||||
},
|
||||
deterministicModulesSet: true,
|
||||
},
|
||||
},
|
||||
wantErr: `duplicate module "metadata"`,
|
||||
},
|
||||
{
|
||||
name: "IncompatibleModule",
|
||||
yaml: `
|
||||
reports:
|
||||
daily:
|
||||
deterministic_modules:
|
||||
- tomorrow_planning
|
||||
`,
|
||||
reports: map[string]ReportConfig{
|
||||
"daily": {
|
||||
DeterministicModules: []ModuleConfigItem{{ID: module.TomorrowPlanning}},
|
||||
deterministicModulesSet: true,
|
||||
},
|
||||
},
|
||||
wantErr: `not compatible with report "daily"`,
|
||||
},
|
||||
{
|
||||
name: "InvalidOptions",
|
||||
yaml: `
|
||||
reports:
|
||||
daily:
|
||||
deterministic_modules:
|
||||
- id: metadata
|
||||
options:
|
||||
sections:
|
||||
- short_term
|
||||
`,
|
||||
reports: map[string]ReportConfig{
|
||||
"daily": {
|
||||
DeterministicModules: []ModuleConfigItem{
|
||||
{
|
||||
ID: module.Metadata,
|
||||
Options: map[string]any{
|
||||
"sections": []any{"short_term"},
|
||||
},
|
||||
},
|
||||
},
|
||||
deterministicModulesSet: true,
|
||||
},
|
||||
},
|
||||
wantErr: "options are invalid",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, loadErr := LoadFile(writeConfig(t, tt.yaml))
|
||||
assertReportModuleError(t, "LoadFile", loadErr, tt.wantErr)
|
||||
|
||||
cfg := Defaults()
|
||||
cfg.Reports = tt.reports
|
||||
assertReportModuleError(t, "Validate", Validate(cfg), tt.wantErr)
|
||||
|
||||
_, overrideErr := cfg.ReportModuleOverrides()
|
||||
assertReportModuleError(t, "ReportModuleOverrides", overrideErr, tt.wantErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertReportModuleError(t *testing.T, operation string, err error, want string) {
|
||||
t.Helper()
|
||||
if err == nil {
|
||||
t.Fatalf("%s error = nil, want %q", operation, want)
|
||||
}
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Fatalf("%s error = %q, want %q", operation, err.Error(), want)
|
||||
}
|
||||
}
|
||||
|
||||
func retiredDailyReportKeyForTest() string {
|
||||
return strings.Join([]string{"daily", "today"}, "_")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user