package system

import (
	"context"
	"testing"

	sysModel "github.com/flipped-aurora/gin-vue-admin/server/model/system"
	"github.com/glebarez/sqlite"
	"github.com/stretchr/testify/require"
	"gorm.io/gorm"
)

func TestInitApiIncludesApiRoleManagementEndpoints(t *testing.T) {
	db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
	require.NoError(t, err)
	require.NoError(t, db.AutoMigrate(&sysModel.SysApi{}))

	_, err = (&initApi{}).InitializeData(context.WithValue(context.Background(), "db", db))
	require.NoError(t, err)

	requiredApis := []sysModel.SysApi{
		{Path: "/api/getApiRoles", Method: "GET"},
		{Path: "/api/setApiRoles", Method: "POST"},
	}
	for _, api := range requiredApis {
		var count int64
		require.NoError(t, db.Model(&sysModel.SysApi{}).Where("path = ? AND method = ?", api.Path, api.Method).Count(&count).Error)
		require.Equalf(t, int64(1), count, "missing api: %+v", api)
	}
}
