// SPDX-License-Identifier: GPL-3.0-or-later

package chartengine

import (
	"testing"

	"github.com/netdata/netdata/go/plugins/plugin/framework/chartengine/internal/program"
	"github.com/stretchr/testify/assert"
)

func TestEnforceChartInstanceCapsSoftWhenAllExistingAreActive(t *testing.T) {
	tests := map[string]struct{}{"all existing active instances are kept": {}}

	for name := range tests {
		t.Run(name, func(t *testing.T) {
			const currentSuccessSeq = 42
			const templateID = "g0.c0"

			lifecycle := program.LifecyclePolicy{
				MaxInstances: 1,
			}
			state := newMaterializedState()
			state.charts["win_nic_traffic_eth0"] = &materializedChartState{
				templateID:         templateID,
				lifecycle:          lifecycle,
				lastSeenSuccessSeq: currentSuccessSeq,
				dimensions:         make(map[string]*materializedDimensionState),
			}
			state.charts["win_nic_traffic_eth1"] = &materializedChartState{
				templateID:         templateID,
				lifecycle:          lifecycle,
				lastSeenSuccessSeq: currentSuccessSeq,
				dimensions:         make(map[string]*materializedDimensionState),
			}

			chartsByID := map[string]*chartState{
				"win_nic_traffic_eth0": {
					templateID: templateID,
					lifecycle:  lifecycle,
					entries:    make(map[string]*dimBuildEntry),
				},
				"win_nic_traffic_eth1": {
					templateID: templateID,
					lifecycle:  lifecycle,
					entries:    make(map[string]*dimBuildEntry),
				},
			}

			removeCharts := enforceChartInstanceCaps(currentSuccessSeq, chartsByID, &state)
			assert.Empty(t, removeCharts)
			assert.Len(t, chartsByID, 2)
			assert.Len(t, state.charts, 2)
		})
	}
}
