Files
Travis Cline 99951bca92 httprr: improve test recording stability and add utilities (#1368)
* build: add examples-updater tool and update examples to v0.1.14-pre.1

Add a new tool to automate updating langchaingo version references in example
projects and use it to update all examples to v0.1.14-pre.1. The tool also
removes temporary replace directives that are no longer needed.

- Add update-examples Makefile target that runs the new tool
- Create internal/devtools/examples-updater implementation
- Update all example go.mod files to reference v0.1.14-pre.1
- Remove replace directives in googleai and vertex examples
- Update dependencies in main go.mod/go.sum

* httprr: improve header normalization and test container setup

Add header normalization to make HTTP recordings more stable:
- Add normalizeGoogleAPIClientHeader and normalizeVersionHeader functions
- Normalize User-Agent, x-goog-api-client and other version headers
- Remove OpenAI-Project header for consistency
- Preserve body for both replay lookup and recording
- Add comprehensive tests for header normalization

Improve testcontainer environment detection:
- Better handling of non-standard Docker socket paths
- Disable Ryuk reaper by default for resource efficiency
- Add verbose logging option via environment variable

* httputil: add ApiKeyTransport for query parameter API keys

Add a reusable ApiKeyTransport that adds API keys to URL query parameters:
- Create ApiKeyTransport implementation in httputil package
- Add comprehensive tests for the new transport
- Refactor googleai_test.go to use the shared transport
- Update chains/llm_test.go to use ApiKeyTransport with proper key scrubbing

This improves how API keys are handled with client libraries that don't
properly set keys when using custom HTTP clients, particularly useful
with httprr for testing Google API integrations.

* openai: remove duplicate MaxTokens field assignment

Remove redundant assignment where both MaxTokens and MaxCompletionTokens
were being set to the same value. MaxCompletionTokens is the preferred
field name that reflects OpenAI's API changes, while MaxTokens was
previously kept for backward compatibility.

* vectorstores/maridadb: add test infrastructure for MariaDB vectorstore

Add TestMain function for MariaDB vectorstore tests that ensures the proper
test environment is set up using the testctr package. This enables consistent
test container setup and teardown for MariaDB integration tests.

* all: normalize all httprr recordings for consistency

Update test recordings across all packages to use normalized headers:
- Standardize User-Agent to 'langchaingo-httprr'
- Normalize version information in x-goog-api-client headers
- Remove OpenAI-Project headers for consistency
- Fix deprecated Anthropic completion API tests
- Update HuggingFace test recordings with valid responses

These changes make test recordings stable across dependency updates
and different environments, preventing test failures from version changes.

* devtools: add utility tool for normalizing httprr test recordings

Add a command-line tool that standardizes version information in httprr recordings:
- Normalizes x-goog-api-client headers to use placeholder versions
- Standardizes x-amz-user-agent headers for consistency
- Replaces Go version strings with generic placeholders
- Supports dry-run mode to preview changes without modifying files
- Includes verbose output option for detailed change reporting

This tool helps maintain consistent test recordings across different environments
and dependency versions, preventing test failures from version changes.

* llms/openai: update OpenAI embedding test to use text-embedding-3-small model

Update the embedding test to use the current text-embedding-3-small model:
- Change model from text-embedding-ada-002 to text-embedding-3-small
- Adjust dimensions parameter from 1234 to 256 to match model capabilities
- Update test recording with appropriate response data

This change ensures tests remain compatible with OpenAI's current embedding
models and prevents test failures from API version changes.
2025-08-18 19:41:52 +02:00

165 lines
5.1 KiB
Makefile

# This file contains convenience targets for the project.
# It is not intended to be used as a build system.
# See the README for more information.
.PHONY: help
help:
@echo "Available targets:"
@echo ""
@echo "Testing:"
@echo " test - Run all tests with basic environment setup"
@echo " test-race - Run tests with race detection"
@echo " test-cover - Run tests with coverage reporting"
@echo " test-record - Run tests with re-recording of httprr files"
@echo ""
@echo "Code Quality:"
@echo " lint - Run linter with auto-installation if needed"
@echo " lint-fix - Run linter with automatic fixes"
@echo " lint-testing - Check test patterns and practices (httprr, etc.)"
@echo " lint-testing-fix - Check and attempt to fix test patterns"
@echo " lint-architecture - Check architectural rules and patterns"
@echo ""
@echo "Other:"
@echo " build-examples - Build all example projects to verify they compile"
@echo " update-examples - Update langchaingo version in all examples"
@echo " docs - Generate documentation"
@echo " clean - Clean lint cache"
@echo " help - Show this help message"
@echo ""
@echo "Git Hooks:"
@echo " pre-push - Run lint and fast tests (suitable for git pre-push hook)"
@echo " install-git-hooks - Install git hooks (sets up pre-push hook)"
.PHONY: test
test:
DOCKER_HOST=$$(docker context inspect -f='{{.Endpoints.docker.Host}}' 2>/dev/null || echo "unix:///var/run/docker.sock") \
TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE="/var/run/docker.sock" \
go test ./...
.PHONY: lint
lint: lint-deps
golangci-lint run --color=always ./...
.PHONY: lint-exp
lint-exp:
golangci-lint run --fix --config .golangci-exp.yaml ./...
.PHONY: lint-fix
lint-fix:
golangci-lint run --fix ./...
.PHONY: lint-all
lint-all:
golangci-lint run --color=always ./...
.PHONY: lint-deps
lint-deps:
@command -v golangci-lint >/dev/null 2>&1 || { \
echo >&2 "golangci-lint not found. Installing..."; \
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.3.0; \
command -v golangci-lint >/dev/null 2>&1 || { \
echo >&2 "Failed to detect golangci-lint after installation. Please check your Go installation and PATH."; \
exit 1; \
} \
}
@golangci-lint version | grep -qE "version v?2" || { echo "Error: golangci-lint v2.x.x required, found:" && golangci-lint version && exit 1; }
.PHONY: docs
docs:
@echo "Generating documentation..."
$(MAKE) -C docs build
.PHONY: test-race
test-race:
DOCKER_HOST=$$(docker context inspect -f='{{.Endpoints.docker.Host}}' 2>/dev/null || echo "unix:///var/run/docker.sock") \
TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE="/var/run/docker.sock" \
go test -race ./...
.PHONY: test-cover
test-cover:
DOCKER_HOST=$$(docker context inspect -f='{{.Endpoints.docker.Host}}' 2>/dev/null || echo "unix:///var/run/docker.sock") \
TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE="/var/run/docker.sock" \
go test -cover ./...
.PHONY: test-record
test-record:
@echo "Re-recording HTTP interactions for all packages using httprr..."
@echo "Note: Running with limited parallelism to avoid API rate limits"
PACKAGES=$$(go run ./internal/devtools/rrtool list-packages -format=paths) && \
echo "Recording HTTP interactions for packages:" && \
echo "$$PACKAGES" | tr ' ' '\n' | sed 's/^/ /' && \
echo "" && \
env DOCKER_HOST=$$(docker context inspect -f='{{.Endpoints.docker.Host}}' 2>/dev/null || echo "unix:///var/run/docker.sock") \
TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE="/var/run/docker.sock" \
go test $$PACKAGES -httprecord=. -httprecord-delay=1s -p 2 -parallel=2 -timeout=300s
.PHONY: run-pkgsite
run-pkgsite:
go run golang.org/x/pkgsite/cmd/pkgsite@latest
.PHONY: clean
clean: clean-lint-cache
.PHONY: clean-lint-cache
clean-lint-cache:
golangci-lint cache clean
.PHONY: build-examples
build-examples:
for example in $(shell find ./examples -mindepth 1 -maxdepth 1 -type d); do \
(cd $$example; echo Build $$example; go mod tidy; go build -o /dev/null) || exit 1; done
.PHONY: update-examples
update-examples:
@if [ -z "$(VERSION)" ]; then \
echo "Error: VERSION is required. Usage: make update-examples VERSION=v0.1.14-pre.1"; \
exit 1; \
fi
@echo "Updating examples to $(VERSION)..."
@go run ./internal/devtools/examples-updater -version $(VERSION)
.PHONY: add-go-work
add-go-work:
go work init .
go work use -r .
.PHONY: lint-devtools
lint-devtools:
go run ./internal/devtools/lint -v
.PHONY: lint-devtools-fix
lint-devtools-fix:
go run ./internal/devtools/lint -fix -v
.PHONY: lint-architecture
lint-architecture:
go run ./internal/devtools/lint -architecture -v
.PHONY: lint-prepush
lint-prepush:
go run ./internal/devtools/lint -prepush -v
.PHONY: lint-prepush-fix
lint-prepush-fix:
go run ./internal/devtools/lint -prepush -fix -v
.PHONY: lint-testing
lint-testing:
go run ./internal/devtools/lint -testing -v
.PHONY: lint-testing-fix
lint-testing-fix:
go run ./internal/devtools/lint -testing -fix -v
.PHONY: pre-push
pre-push:
@echo "Running pre-push checks..."
@$(MAKE) lint
@go test -short ./...
@echo "✅ Pre-push checks passed!"
.PHONY: install-git-hooks
install-git-hooks:
@./internal/devtools/git-hooks/install-git-hooks.sh