Files
felhom-controller/controller/internal/stacks/r591_copy_i18n_test.go
T

62 lines
2.5 KiB
Go

package stacks
import "testing"
// R-591: deepCopyStack is "a snapshot the caller may mutate". Before the fix the Meta.I18n map
// (and every pointer inside each overlay) was shared, so writing through the copy changed the
// original. The test asserts the CONSEQUENCE: after mutating the copy at every depth, the
// original still reads its own values.
func TestDeepCopyStackI18nIsNotShared(t *testing.T) {
s := func(v string) *string { return &v }
orig := &Stack{Name: "demo"}
orig.Meta.I18n = map[string]MetadataOverlay{
"en": {
Description: s("orig desc"),
AppInfo: &AppInfoOverlay{Tagline: s("orig tag"), UseCases: []string{"orig use"}},
DeployFields: []DeployFieldOverlay{{EnvVar: "A", Label: s("orig label"),
Options: []SelectOptionOverlay{{Value: "x", Label: s("orig opt")}}}},
OptionalConfig: []OptionalConfigGroupOverlay{{MatchGroup: "g", Group: s("orig group"),
Fields: []OptionalConfigFieldOverlay{{EnvVar: "B", HelpText: s("orig help")}}}},
Integrations: []IntegrationOverlay{{Target: "t", Label: s("orig int")}},
DataPaths: []DataPathOverlay{{Path: "/p", Label: s("orig path")}},
InitialCreds: &InitialCredentialsOverlay{Note: s("orig note")},
},
}
cp := deepCopyStack(orig)
en := cp.Meta.I18n["en"]
*en.Description = "MUT"
*en.AppInfo.Tagline = "MUT"
en.AppInfo.UseCases[0] = "MUT"
*en.DeployFields[0].Label = "MUT"
*en.DeployFields[0].Options[0].Label = "MUT"
*en.OptionalConfig[0].Group = "MUT"
*en.OptionalConfig[0].Fields[0].HelpText = "MUT"
*en.Integrations[0].Label = "MUT"
*en.DataPaths[0].Label = "MUT"
*en.InitialCreds.Note = "MUT"
cp.Meta.I18n["de"] = MetadataOverlay{Description: s("MUT")}
o := orig.Meta.I18n["en"]
checks := map[string]string{
"description": *o.Description,
"tagline": *o.AppInfo.Tagline,
"use case": o.AppInfo.UseCases[0],
"deploy label": *o.DeployFields[0].Label,
"option label": *o.DeployFields[0].Options[0].Label,
"optional group": *o.OptionalConfig[0].Group,
"optional help": *o.OptionalConfig[0].Fields[0].HelpText,
"integration": *o.Integrations[0].Label,
"data path": *o.DataPaths[0].Label,
"initial creds": *o.InitialCreds.Note,
}
for what, got := range checks {
if got == "MUT" {
t.Errorf("R-591: mutating the copy's %s overlay changed the ORIGINAL — the overlay is shared, not copied", what)
}
}
if _, ok := orig.Meta.I18n["de"]; ok {
t.Errorf("R-591: adding a language to the copy's I18n map added it to the ORIGINAL — the map is shared")
}
}