package invocation_test

import (
	"context"
	"os"
	"testing"

	"github.com/tidbcloud/AgentTeam/internal/coredb"
	"github.com/tidbcloud/AgentTeam/internal/coredb/testkit"
)

var workflowInvocationTiDB testkit.TiDBCoreDBFixture

func TestMain(m *testing.M) {
	os.Exit(testkit.RunTestsWithTiDBCoreDBFixture(m, "workflow_invocation_store", &workflowInvocationTiDB))
}

func startRollbackDB(t *testing.T, ctx context.Context) coredb.DB {
	t.Helper()
	// This path is limited to transaction-scoped state. The package boundary
	// guard rejects parallel tests and effects that rollback cannot isolate.
	return workflowInvocationTiDB.SerializedRollbackDB(t, ctx)
}

func startIsolatedInvocationDB(t *testing.T, ctx context.Context) coredb.DB {
	t.Helper()
	dsn := testkit.StartInitializedTiDBWithRootDSN(t, workflowInvocationTiDB.RootDSN(t))
	sqlDB, err := testkit.OpenTiDBContext(ctx, dsn)
	if err != nil {
		t.Fatalf("open isolated Workflow Invocation TiDB: %v", err)
	}
	sqlDB.SetMaxOpenConns(8)
	sqlDB.SetMaxIdleConns(8)
	t.Cleanup(func() {
		if err := sqlDB.Close(); err != nil {
			t.Errorf("close isolated Workflow Invocation TiDB: %v", err)
		}
	})
	return coredb.WrapSQLDB(sqlDB)
}
