
GO     := GO111MODULE=on go
GOTEST := CGO_ENABLED=1 $(GO) test -p 3 --race
GOBUILD := CGO_ENABLED=0 $(GO) build
CGOBUILD := CGO_ENABLED=1 $(GO) build
GOCOVER := $(GO) tool cover
TEST_ARTIFACTS_FILE_COVER := $(TEST_ARTIFACTS_DIR)/file_coverage.html
TEST_OPTIONS := -timeout 5m -cover -covermode=atomic -coverprofile="$(TEST_ARTIFACTS_OUT)"
FILES := $$(find . -name '*.go' -type f | grep -vE 'mock|vendor|generate|kv_gen|proto|pb\.go|pb\.gw\.go|test\.go')


FAIL_ON_STDOUT := awk '{ print } END { if (NR > 0) { exit 1  }  }'

UNAME := $(shell uname)
BROWSER_OPEN := "unknown"
ifeq ($(UNAME),Darwin)
    BROWSER_OPEN := open -a Safari
endif
ifeq ($(UNAME),Linux)
    BROWSER_OPEN := xdg-open
endif


OS := $(shell uname -s)
OS := $(shell echo $(OS) | tr '[:upper:]' '[:lower:]')
Arch := $(shell uname -m)

LOCAL_BIN=./bin
SWAGGER_BIN := ./bin/swagger
DEDICATED_CONSOLE_API_CLIENT_DIR := ./internal/tidbcloud/dedicated/consoleapi
OPEN_API_CLIENT_DIR := ./internal/tidbcloud/dedicated/openapi
PREMIUM_CONSOLE_API_CLIENT_DIR := ./internal/tidbcloud/premium/consoleapi
PREMIUM_OPEN_API_CLIENT_DIR := ./internal/tidbcloud/premium/openapi

## generate client code by swagger.json
# Variables
SWAGGER_VERSION := v0.30.5
SWAGGER_URL := https://github.com/go-swagger/go-swagger/releases/download/$(SWAGGER_VERSION)/swagger_$(OS)
ifeq ($(Arch), x86_64)
    SWAGGER_URL := $(SWAGGER_URL)_amd64
else ifeq ($(Arch), arm64)
    SWAGGER_URL := $(SWAGGER_URL)_arm64
else
    $(error Unsupported architecture: $(Arch))
endif

# Download swagger binary
$(SWAGGER_BIN):
	@echo "Ensuring directory $(LOCAL_BIN) exists..."
	@mkdir -p $(LOCAL_BIN)
	@echo "Installing swagger $(SWAGGER_VERSION) to $(SWAGGER_BIN)..."
	curl -L $(SWAGGER_URL) -o $(SWAGGER_BIN)
	chmod +x $(SWAGGER_BIN)
	@echo "Swagger installed at $(SWAGGER_BIN)"


# BEGIN Generate the Go client SDK
.PHONY: generate-api-client
generate-api-client: generate-dedicated-cdc-api-client generate-cluster-api-client generate-changefeed-open-api-client generate-premium-cdc-api-client generate-premium-changefeed-open-api-client generate-premium-dm-api-client generate-premium-import-api-client generate-br-api-client generate-br-open-api-client generate-crrf-api-client

.PHONY: generate-dedicated-cdc-api-client
generate-dedicated-cdc-api-client: $(SWAGGER_BIN)
	@echo "Generating Go client SDK using swagger..."
	@rm -rf $(DEDICATED_CONSOLE_API_CLIENT_DIR)/cdc
	@mkdir -p $(DEDICATED_CONSOLE_API_CLIENT_DIR)/cdc
	$(SWAGGER_BIN) generate client \
		-f ../../api/service.swagger.json \
		-A bizChangefeed \
		--tags=ConnectorService \
		--tags=ChangefeedInstanceTypeService \
		--tags=OpsInternalService \
		--tags=SinkEndpointService \
		--target=$(DEDICATED_CONSOLE_API_CLIENT_DIR)/cdc \
		--client-package=client \
		--model-package=model
	@rm -rf $(DEDICATED_CONSOLE_API_CLIENT_DIR)/conn_profile
	@mkdir -p $(DEDICATED_CONSOLE_API_CLIENT_DIR)/conn_profile
	$(SWAGGER_BIN) generate client \
		-f ../../api/dataflow.swagger.json \
		-A connProfile \
		--tags=ConnProfileService \
		--target=$(DEDICATED_CONSOLE_API_CLIENT_DIR)/conn_profile \
		--client-package=client \
		--model-package=model

.PHONY: generate-premium-cdc-api-client
generate-premium-cdc-api-client: $(SWAGGER_BIN)
	@echo "Generating Premium Go client SDK using swagger..."
	@rm -rf $(PREMIUM_CONSOLE_API_CLIENT_DIR)/cdc
	@mkdir -p $(PREMIUM_CONSOLE_API_CLIENT_DIR)/cdc
	$(SWAGGER_BIN) generate client \
		-f ../../dataflow-service-ng/api/dataflow.swagger.json \
		-A bizChangefeed \
		--tags=ChangefeedService \
		--tags=DPFeatureService \
		--tags=ChangefeedSpecService \
		--tags=OpsInternalService \
		--tags=SinkEndpointService \
		--target=$(PREMIUM_CONSOLE_API_CLIENT_DIR)/cdc \
		--client-package=client \
		--model-package=model

.PHONY: generate-premium-changefeed-open-api-client
generate-premium-changefeed-open-api-client: $(SWAGGER_BIN)
	@echo "Generating Premium Changefeed OpenAPI Go client SDK using swagger..."
	@rm -rf $(PREMIUM_OPEN_API_CLIENT_DIR)/changefeed
	@mkdir -p $(PREMIUM_OPEN_API_CLIENT_DIR)/changefeed
	$(SWAGGER_BIN) generate client \
		-f ../../dataflow-service-ng/api/changefeed.swagger.internal.json \
		-A premiumChangefeed \
		--tags=ChangefeedService \
		--target=$(PREMIUM_OPEN_API_CLIENT_DIR)/changefeed \
		--client-package=client \
		--model-package=model

.PHONY: generate-changefeed-open-api-client
generate-changefeed-open-api-client: $(SWAGGER_BIN)
	@echo "Generating Changefeed Go client SDK using swagger..."
	@rm -rf $(OPEN_API_CLIENT_DIR)/changefeed
	@mkdir -p $(OPEN_API_CLIENT_DIR)/changefeed
	$(SWAGGER_BIN) generate client \
		-f ../../api/changefeed.swagger.json \
		-A Changefeed \
		--tags=ChangefeedService \
		--target=$(OPEN_API_CLIENT_DIR)/changefeed \
		--client-package=client \
		--model-package=model

.PHONY: generate-br-api-client
generate-br-api-client: $(SWAGGER_BIN)
	@echo "Generating BR Go client SDK (BackupService and BackupConsoleService merged)..."
	@rm -rf $(DEDICATED_CONSOLE_API_CLIENT_DIR)/br
	@mkdir -p $(DEDICATED_CONSOLE_API_CLIENT_DIR)/br
	$(SWAGGER_BIN) generate client \
		-f ../../api/service.swagger.json \
		-A br \
		--tags=BackupService \
		--target=$(DEDICATED_CONSOLE_API_CLIENT_DIR)/br \
		--client-package=client \
		--model-package=model
	$(SWAGGER_BIN) generate client \
		-f ../../api/backup.swagger.json \
		-A br \
		--tags=BackupConsoleService \
		--target=$(DEDICATED_CONSOLE_API_CLIENT_DIR)/br \
		--client-package=client \
		--model-package=model \
		--existing-models=github.com/tidbcloud/dataflow-service-e2e/internal/tidbcloud/dedicated/consoleapi/br/model \
		--skip-validation

.PHONY: generate-crrf-api-client
generate-crrf-api-client: $(SWAGGER_BIN)
	@echo "Generating CRRF Go client SDK using swagger..."
	@rm -rf $(DEDICATED_CONSOLE_API_CLIENT_DIR)/crrf
	@mkdir -p $(DEDICATED_CONSOLE_API_CLIENT_DIR)/crrf
	$(SWAGGER_BIN) generate client \
		-f ../../api/crrf.swagger.json \
		-A crrf \
		--tags=CRRFConsoleService \
		--target=$(DEDICATED_CONSOLE_API_CLIENT_DIR)/crrf \
		--client-package=client \
		--model-package=model

.PHONY: generate-br-open-api-client
generate-br-open-api-client: $(SWAGGER_BIN)
	@echo "Generating BR Open API client SDK using swagger..."
	@rm -rf $(OPEN_API_CLIENT_DIR)/br
	@mkdir -p $(OPEN_API_CLIENT_DIR)/br
	$(SWAGGER_BIN) generate client \
		-f ../../idl/pbgen/proto/adminapi/v1beta1/backup.swagger.json \
		-A brOpenApi \
		--target=$(OPEN_API_CLIENT_DIR)/br \
		--client-package=client \
		--model-package=model

.PHONY: generate-dm-api-client
generate-dm-api-client: $(SWAGGER_BIN)
	@echo "Generating DM Go client SDK using swagger..."
	@rm -rf $(DEDICATED_CONSOLE_API_CLIENT_DIR)/dm
	@mkdir -p $(DEDICATED_CONSOLE_API_CLIENT_DIR)/dm
	$(SWAGGER_BIN) generate client \
		-f ../../api/dm.swagger.json \
		-A dm \
		--tags=Service \
		--target=$(DEDICATED_CONSOLE_API_CLIENT_DIR)/dm \
		--client-package=client \
		--model-package=model

.PHONY: generate-premium-dm-api-client
generate-premium-dm-api-client: $(SWAGGER_BIN)
	@echo "Generating Premium DM Go client SDK using swagger..."
	@rm -rf $(PREMIUM_CONSOLE_API_CLIENT_DIR)/dm
	@mkdir -p $(PREMIUM_CONSOLE_API_CLIENT_DIR)/dm
	$(SWAGGER_BIN) generate client \
		-f ../../dataflow-service-ng/api/premium_dm.ui.swagger.json \
		-A premiumDm \
		--tags=PremiumMigrationService \
		--target=$(PREMIUM_CONSOLE_API_CLIENT_DIR)/dm \
		--client-package=client \
		--model-package=model \
		--skip-validation

.PHONY: generate-premium-import-api-client
generate-premium-import-api-client: $(SWAGGER_BIN)
	@echo "Generating Premium Import Go client SDK using swagger..."
	@rm -rf $(PREMIUM_CONSOLE_API_CLIENT_DIR)/import
	@mkdir -p $(PREMIUM_CONSOLE_API_CLIENT_DIR)/import
	$(SWAGGER_BIN) generate client \
		-f ../../dataflow-service-ng/api/import_into.ui.swagger.json \
		-A premiumImport \
		--tags=ImportService \
		--target=$(PREMIUM_CONSOLE_API_CLIENT_DIR)/import \
		--client-package=client \
		--model-package=model

.PHONY: generate-import-api-client
generate-import-api-client: $(SWAGGER_BIN)
	@echo "Generating Import Go client SDK using swagger..."
	@rm -rf $(DEDICATED_CONSOLE_API_CLIENT_DIR)/import
	@mkdir -p $(DEDICATED_CONSOLE_API_CLIENT_DIR)/import
	$(SWAGGER_BIN) generate client \
		-f ../../api/service.swagger.json \
		-A import \
		--tags=ImportService \
		--target=$(DEDICATED_CONSOLE_API_CLIENT_DIR)/import \
		--client-package=client \
		--model-package=model

# Notice: It's not easy to make it work, fallback to use raw http sdk directly
# Notice: this api only used for 3-layer arch
# At 2025-02-12, the swagger.json is copy from https://github.com/tidbcloud/tidb-management-service/blob/main/api/spec/tidb_cloud_open_api/dedicated/v1beta1/services.ui.swagger.json
# the file has some problem need to be fixed manually
# command to validate the swagger.json `./bin/swagger validate path/to/the/file`
# a example error is "- param "clusterIds" for "ClusterService_ListClusters" is a collection without an element type (array requires item definition)"
# root cause: array definition without type
# workaround: add type definition - "items": {"type": "string"}
.PHONY: generate-cluster-api-client
generate-cluster-api-client: $(SWAGGER_BIN)
	@rm -rf $(DEDICATED_CONSOLE_API_CLIENT_DIR)/cluster
	@mkdir -p $(DEDICATED_CONSOLE_API_CLIENT_DIR)/cluster
	$(SWAGGER_BIN) generate client \
			-f ./internal/tidbcloud/dedicated/consoleapi/copy-from-dedicated-cluster-services.ui.swagger.json \
			-A cluster \
			--skip-validation \
			--target=$(DEDICATED_CONSOLE_API_CLIENT_DIR)/cluster \
			--client-package=client \
			--model-package=model
	@rm -rf $(DEDICATED_CONSOLE_API_CLIENT_DIR)/serverless_cluster
	@mkdir -p $(DEDICATED_CONSOLE_API_CLIENT_DIR)/serverless_cluster
	$(SWAGGER_BIN) generate client \
			-f ./internal/tidbcloud/dedicated/consoleapi/copy-from-serverless-cluster-services.ui.swagger.json \
			-A serverless_cluster \
			--skip-validation \
			--target=$(DEDICATED_CONSOLE_API_CLIENT_DIR)/serverless_cluster \
			--client-package=client \
			--model-package=model
	@echo "Go client SDK generated in $(DEDICATED_CONSOLE_API_CLIENT_DIR)"

## END Generate the Go client SDK

# Essential v1 clients are generated from the authoritative serverless-service
# contracts vendored under api/essential. The Console surface has Export, BR,
# Import, CDC, DM, and PrivateLink clients; v1beta1 has Export, Import, CDC,
# DM, and PrivateLink clients.
.PHONY: generate-essential-api-client
generate-essential-api-client: $(SWAGGER_BIN)
	@rm -rf ./internal/tidbcloud/essential/serverless_console/export ./internal/tidbcloud/essential/serverless_console/br ./internal/tidbcloud/essential/serverless_console/import ./internal/tidbcloud/essential/serverless_console/cdc ./internal/tidbcloud/essential/serverless_console/dm ./internal/tidbcloud/essential/serverless_console/privatelink ./internal/tidbcloud/essential/serverless_openapi_v1beta1/export ./internal/tidbcloud/essential/serverless_openapi_v1beta1/import ./internal/tidbcloud/essential/serverless_openapi_v1beta1/cdc ./internal/tidbcloud/essential/serverless_openapi_v1beta1/dm ./internal/tidbcloud/essential/serverless_openapi_v1beta1/privatelink
	@mkdir -p ./internal/tidbcloud/essential/serverless_console/export ./internal/tidbcloud/essential/serverless_console/br ./internal/tidbcloud/essential/serverless_console/import ./internal/tidbcloud/essential/serverless_console/cdc ./internal/tidbcloud/essential/serverless_console/dm ./internal/tidbcloud/essential/serverless_console/privatelink ./internal/tidbcloud/essential/serverless_openapi_v1beta1/export ./internal/tidbcloud/essential/serverless_openapi_v1beta1/import ./internal/tidbcloud/essential/serverless_openapi_v1beta1/cdc ./internal/tidbcloud/essential/serverless_openapi_v1beta1/dm ./internal/tidbcloud/essential/serverless_openapi_v1beta1/privatelink
	$(SWAGGER_BIN) generate client -f ./api/essential/export-console.swagger.json -A essentialExportConsole --tags=ExportService --target=./internal/tidbcloud/essential/serverless_console/export --client-package=client --model-package=model --skip-validation
	$(SWAGGER_BIN) generate client -f ./api/essential/br-console.swagger.json -A essentialBRConsole --tags=BackupRestoreService --target=./internal/tidbcloud/essential/serverless_console/br --client-package=client --model-package=model --skip-validation
	$(SWAGGER_BIN) generate client -f ./api/essential/import-console.swagger.json -A essentialImportConsole --tags=ImportService --target=./internal/tidbcloud/essential/serverless_console/import --client-package=client --model-package=model --skip-validation
	$(SWAGGER_BIN) generate client -f ./api/essential/cdc-console.swagger.json -A essentialCDCConsole --tags=ChangefeedService --target=./internal/tidbcloud/essential/serverless_console/cdc --client-package=client --model-package=model --skip-validation
	$(SWAGGER_BIN) generate client -f ./api/essential/dm-console.swagger.json -A essentialDMConsole --tags=MigrationService --target=./internal/tidbcloud/essential/serverless_console/dm --client-package=client --model-package=model --skip-validation
	$(SWAGGER_BIN) generate client -f ./api/essential/privatelink-console.swagger.json -A essentialPrivateLinkConsole --tags=PrivateLinkConnectionService --target=./internal/tidbcloud/essential/serverless_console/privatelink --client-package=client --model-package=model --skip-validation
	$(SWAGGER_BIN) generate client -f ./api/essential/export-openapi.swagger.json -A essentialExportOpenAPI --tags=Export --tags=ExportService --target=./internal/tidbcloud/essential/serverless_openapi_v1beta1/export --client-package=client --model-package=model --skip-validation
	$(SWAGGER_BIN) generate client -f ./api/essential/import-openapi.swagger.json -A essentialImportOpenAPI --tags=Import --tags=ImportService --target=./internal/tidbcloud/essential/serverless_openapi_v1beta1/import --client-package=client --model-package=model --skip-validation
	$(SWAGGER_BIN) generate client -f ./api/essential/cdc-openapi.swagger.json -A essentialCDCOpenAPI --tags=ChangefeedService --target=./internal/tidbcloud/essential/serverless_openapi_v1beta1/cdc --client-package=client --model-package=model --skip-validation
	$(SWAGGER_BIN) generate client -f ./api/essential/dm-openapi.swagger.json -A essentialDMOpenAPI --tags=Migration --target=./internal/tidbcloud/essential/serverless_openapi_v1beta1/dm --client-package=client --model-package=model --skip-validation
	$(SWAGGER_BIN) generate client -f ./api/essential/privatelink-openapi.swagger.json -A essentialPrivateLinkOpenAPI --tags=PrivateLinkConnectionService --target=./internal/tidbcloud/essential/serverless_openapi_v1beta1/privatelink --client-package=client --model-package=model --skip-validation

# this image serve for two purposes: 1. login in local; 2. run case in CI machine
.PHONY: build-e2e-image
build-e2e-image:
	# 1. remove `exit 1`
	# 2. go to https://github.com/settings/tokens to create a new token(classic) with `write:packages`
	# 3. replace the token in the docker command
	# 4. run this target
	# 5. delete token from https://github.com/settings/tokens to avoid security problem
	exit 1
	docker build --build-arg GITHUB_TOKEN={replace-me} -t dataflow-e2e-runner -f ./Dockerfile .

.PHONY: install-gotestsum
install-gotestsum:
	@if ! command -v gotestsum >/dev/null 2>&1; then \
		go install gotest.tools/gotestsum@latest; \
	fi

ENV ?= "local"
E2E_REPORT := ./e2e-report.xml
LOG_FILE ?= ./run_test.log

.PHONY: dedicated-e2e-test
dedicated-e2e-test: SHELL := /bin/bash
dedicated-e2e-test: .SHELLFLAGS := -o pipefail -c
dedicated-e2e-test: install-gotestsum
	GOMAXPROCS=8 gotestsum --format=standard-verbose --junitfile $(E2E_REPORT) -- -v -gcflags="all=-N -l" -timeout 3h ./run_dedicated_test.go ./run_test.go -args -env=$(ENV) $(if $(DOMAIN),-domain=$(DOMAIN)) $(if $(API_MODE),-api-mode=$(API_MODE)) $(if $(CONFIG_FILE),-config-file=$(CONFIG_FILE)) $(if $(EXECUTOR),-executor=$(EXECUTOR)) $(if $(CASES),-cases="$(CASES)") $(if $(CASE_GROUP),-case-group="$(CASE_GROUP)") $(if $(ASYNC_DEPS),-async-deps=$(ASYNC_DEPS)) 2>&1 | tee $(LOG_FILE)
	@if [ "$(OS)" = "darwin" ]; then \
		sed -i '' 's/\[31m//g' $(E2E_REPORT) && \
		sed -i '' 's/\[32m//g' $(E2E_REPORT) && \
		sed -i '' 's/\[33m//g' $(E2E_REPORT) && \
		sed -i '' 's/\[0m//g' $(E2E_REPORT); \
	elif [ "$(OS)" = "linux" ]; then \
		sed -i 's/\[31m//g' $(E2E_REPORT) && \
		sed -i 's/\[32m//g' $(E2E_REPORT) && \
		sed -i 's/\[33m//g' $(E2E_REPORT) && \
		sed -i 's/\[0m//g' $(E2E_REPORT); \
	else \
		echo "Unsupported OS: $(OS)"; \
	fi

.PHONY: dedicated-e2e-test-local
dedicated-e2e-test-local:
	exec env GOMAXPROCS=8 go test -v -gcflags="all=-N -l" -timeout 3h ./run_dedicated_test.go ./run_test.go -args -env=local $(if $(DOMAIN),-domain=$(DOMAIN)) $(if $(API_MODE),-api-mode=$(API_MODE)) $(if $(CONFIG_FILE),-config-file=$(CONFIG_FILE)) $(if $(EXECUTOR),-executor=$(EXECUTOR)) $(if $(CASES),-cases="$(CASES)") $(if $(CASE_GROUP),-case-group="$(CASE_GROUP)") $(if $(ASYNC_DEPS),-async-deps=$(ASYNC_DEPS))

.PHONY: dedicated-e2e-test-dev
dedicated-e2e-test-dev:
	$(MAKE) dedicated-e2e-test ENV=dev

.PHONY: dedicated-e2e-test-staging
dedicated-e2e-test-staging:
	$(MAKE) dedicated-e2e-test ENV=staging

.PHONY: dedicated-e2e-test-prod
dedicated-e2e-test-prod:
	$(MAKE) dedicated-e2e-test ENV=prod


.PHONY: premium-e2e-test
premium-e2e-test: SHELL := /bin/bash
premium-e2e-test: .SHELLFLAGS := -o pipefail -c
premium-e2e-test: install-gotestsum
	go test ./internal/tidbcloud/premium/consoleapi -run TestPremiumImportClientPackageCompile -count=1
	GOMAXPROCS=8 gotestsum --format=standard-verbose --junitfile $(E2E_REPORT) -- -v -gcflags="all=-N -l" -timeout 3h ./run_premium_test.go ./run_test.go -args -env=$(ENV) $(if $(DOMAIN),-domain=$(DOMAIN)) $(if $(API_MODE),-api-mode=$(API_MODE)) $(if $(CONFIG_FILE),-config-file=$(CONFIG_FILE)) $(if $(EXECUTOR),-executor=$(EXECUTOR)) $(if $(CASES),-cases="$(CASES)") $(if $(CASE_GROUP),-case-group="$(CASE_GROUP)") $(if $(ASYNC_DEPS),-async-deps=$(ASYNC_DEPS)) 2>&1 | tee $(LOG_FILE)
	@if [ "$(OS)" = "darwin" ]; then \
		sed -i '' 's/\[31m//g' $(E2E_REPORT) && \
		sed -i '' 's/\[32m//g' $(E2E_REPORT) && \
		sed -i '' 's/\[33m//g' $(E2E_REPORT) && \
		sed -i '' 's/\[0m//g' $(E2E_REPORT); \
	elif [ "$(OS)" = "linux" ]; then \
		sed -i 's/\[31m//g' $(E2E_REPORT) && \
		sed -i 's/\[32m//g' $(E2E_REPORT) && \
		sed -i 's/\[33m//g' $(E2E_REPORT) && \
		sed -i 's/\[0m//g' $(E2E_REPORT); \
	else \
		echo "Unsupported OS: $(OS)"; \
	fi

.PHONY: premium-e2e-test-local
premium-e2e-test-local:
	go test ./internal/tidbcloud/premium/consoleapi -run TestPremiumImportClientPackageCompile -count=1
	exec env GOMAXPROCS=8 go test -v -gcflags="all=-N -l" -timeout 3h ./run_premium_test.go ./run_test.go -args -env=local $(if $(DOMAIN),-domain=$(DOMAIN)) $(if $(API_MODE),-api-mode=$(API_MODE)) $(if $(CONFIG_FILE),-config-file=$(CONFIG_FILE)) $(if $(EXECUTOR),-executor=$(EXECUTOR)) $(if $(CASES),-cases="$(CASES)") $(if $(CASE_GROUP),-case-group="$(CASE_GROUP)") $(if $(ASYNC_DEPS),-async-deps=$(ASYNC_DEPS))

.PHONY: premium-e2e-test-dev
premium-e2e-test-dev:
	$(MAKE) premium-e2e-test ENV=dev

.PHONY: premium-e2e-test-staging
premium-e2e-test-staging:
	$(MAKE) premium-e2e-test ENV=staging

.PHONY: premium-e2e-test-prod
premium-e2e-test-prod:
	$(MAKE) premium-e2e-test ENV=prod

.PHONY: essential-e2e-test
essential-e2e-test: SHELL := /bin/bash
essential-e2e-test: .SHELLFLAGS := -o pipefail -c
essential-e2e-test: install-gotestsum
	GOMAXPROCS=8 gotestsum --format=standard-verbose --junitfile $(E2E_REPORT) -- -v -gcflags="all=-N -l" -timeout 3h ./run_essential_test.go ./run_test.go -args -env=$(ENV) $(if $(DOMAIN),-domain=$(DOMAIN),-domain=all) $(if $(API_MODE),-api-mode=$(API_MODE)) $(if $(CONFIG_FILE),-config-file=$(CONFIG_FILE)) $(if $(EXECUTOR),-executor=$(EXECUTOR)) $(if $(CASES),-cases="$(CASES)") $(if $(CASE_GROUP),-case-group="$(CASE_GROUP)") $(if $(ASYNC_DEPS),-async-deps=$(ASYNC_DEPS)) 2>&1 | tee $(LOG_FILE)

.PHONY: essential-e2e-test-local
essential-e2e-test-local:
	exec env GOMAXPROCS=8 go test -v -gcflags="all=-N -l" -timeout 3h ./run_essential_test.go ./run_test.go -args -env=local $(if $(DOMAIN),-domain=$(DOMAIN),-domain=all) $(if $(API_MODE),-api-mode=$(API_MODE)) $(if $(CONFIG_FILE),-config-file=$(CONFIG_FILE)) $(if $(EXECUTOR),-executor=$(EXECUTOR)) $(if $(CASES),-cases="$(CASES)") $(if $(CASE_GROUP),-case-group="$(CASE_GROUP)") $(if $(ASYNC_DEPS),-async-deps=$(ASYNC_DEPS))

.PHONY: install-junit2html
install-junit2html:
	pip install junit2html

.PHONY: show-e2e-report
browse-e2e-report:
	junit2html ./e2e-report.xml  ./e2e-report.html
	$(BROWSER_OPEN) ./e2e-report.html
