datarhei-dragonfork-core/config/config_test.go
Zac Gaetano 2d29dc9c4a
Some checks failed
tests / build (push) Failing after 3s
fix(config): preserve WebRTC section in Config.Clone()
Config.Clone() copied every top-level Data section except WebRTC.
Because api.go receives a clone (not the original), cfg.WebRTC.Enable
was always the zero value at runtime, the subsystem was skipped, and
the WHEP route was never mounted — regardless of CORE_WEBRTC_ENABLE.

Caught on the first live M2 TrueNAS deploy: env said enable=true,
container listened fine, but /api/v3/whep/:id returned Echo's default
JSON 404 (from router) instead of the handler's plain-text
'webrtc: stream not found' (which it would return for an unknown id).

- Add data.WebRTC = d.WebRTC in the struct-copy block.
- Deep-copy NAT1To1IPs alongside the other []string sections.
- Regression test TestConfigCopyWebRTC covers both.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 15:26:11 -04:00

111 lines
3.1 KiB
Go

package config
import (
"strings"
"testing"
"github.com/datarhei/core/v16/config/vars"
"github.com/datarhei/core/v16/io/fs"
"github.com/stretchr/testify/require"
)
func TestConfigCopy(t *testing.T) {
fs, _ := fs.NewMemFilesystem(fs.MemConfig{})
config1 := New(fs)
config1.Version = 42
config1.DB.Dir = "foo"
val1, _ := config1.Get("version")
val2, _ := config1.Get("db.dir")
val3, _ := config1.Get("host.name")
require.Equal(t, "42", val1)
require.Equal(t, "foo", val2)
require.Equal(t, "(empty)", val3)
config1.Set("host.name", "foo.com")
val3, _ = config1.Get("host.name")
require.Equal(t, "foo.com", val3)
config2 := config1.Clone()
require.Equal(t, int64(42), config2.Version)
require.Equal(t, "foo", config2.DB.Dir)
require.Equal(t, []string{"foo.com"}, config2.Host.Name)
config1.Set("version", "77")
require.Equal(t, int64(77), config1.Version)
require.Equal(t, int64(42), config2.Version)
config1.Set("db.dir", "bar")
require.Equal(t, "bar", config1.DB.Dir)
require.Equal(t, "foo", config2.DB.Dir)
config2.DB.Dir = "baz"
require.Equal(t, "bar", config1.DB.Dir)
require.Equal(t, "baz", config2.DB.Dir)
config1.Host.Name[0] = "bar.com"
require.Equal(t, []string{"bar.com"}, config1.Host.Name)
require.Equal(t, []string{"foo.com"}, config2.Host.Name)
}
// TestConfigCopyWebRTC is a regression test for Clone() silently dropping the
// WebRTC Data section. The first live M2 deploy surfaced this: env vars bound
// correctly onto the original Config, but Core handed the clone to app/api, so
// cfg.WebRTC.Enable was always the zero value and the subsystem was skipped.
func TestConfigCopyWebRTC(t *testing.T) {
fs, _ := fs.NewMemFilesystem(fs.MemConfig{})
config1 := New(fs)
config1.WebRTC.Enable = true
config1.WebRTC.PublicIP = "10.0.0.25"
config1.WebRTC.NAT1To1IPs = []string{"10.0.0.25", "203.0.113.10"}
config1.WebRTC.UDPMuxPort = 45000
config2 := config1.Clone()
require.Equal(t, true, config2.WebRTC.Enable)
require.Equal(t, "10.0.0.25", config2.WebRTC.PublicIP)
require.Equal(t, []string{"10.0.0.25", "203.0.113.10"}, config2.WebRTC.NAT1To1IPs)
require.Equal(t, 45000, config2.WebRTC.UDPMuxPort)
// NAT1To1IPs is a slice — mutating the clone must not affect the
// source, which is what every other section guarantees via
// copy.Slice. Same contract for WebRTC.
config2.WebRTC.NAT1To1IPs[0] = "mutated"
require.Equal(t, "10.0.0.25", config1.WebRTC.NAT1To1IPs[0])
}
func TestValidateDefault(t *testing.T) {
fs, err := fs.NewMemFilesystem(fs.MemConfig{})
require.NoError(t, err)
size, fresh, err := fs.WriteFileReader("./mime.types", strings.NewReader("xxxxx"))
require.Equal(t, int64(5), size)
require.Equal(t, true, fresh)
require.NoError(t, err)
_, _, err = fs.WriteFileReader("/bin/ffmpeg", strings.NewReader("xxxxx"))
require.NoError(t, err)
cfg := New(fs)
cfg.Validate(true)
errors := []string{}
cfg.Messages(func(level string, v vars.Variable, message string) {
if level == "error" {
errors = append(errors, message)
}
})
require.Equal(t, 0, len(cfg.Overrides()))
require.Equal(t, false, cfg.HasErrors(), errors)
}