# Testing

## Test tiers

| Tier | Command | Prerequisites |
| --- | --- | --- |
| Unit | `make test` | None (uses miniredis and mocks) |
| Integration | `make test-integration` | `docker compose up redis mongodb -d` |
| Full local verify | `make test-all` | Unit + integration |

```sh
make test
make test-integration   # requires Redis + Mongo from compose
make test-all
```

## Current coverage

- HTTP health endpoints (including `/readyz` not-ready path)
- HTTP route registration smoke (`internal/http/server_test.go`)
- Config loading and validation
- API contract DTO mapping (`internal/contracts/api/*_test.go`)
- Redis client health checks (via miniredis in `internal/redis/`)
- Redis stream publisher (`internal/redis/publisher_test.go`) — XADD, validation, stream prefix
- Redis publisher integration (`internal/redis/publisher_integration_test.go`) — real Redis XADD/XRANGE
- Event contract validation
- Git contracts (`internal/contracts/git/`)
- Git provider (`internal/git/`) — URL building, classification, diff parsing, mock-runner sync flows
- SyncService orchestration (`internal/service/sync_test.go`) — git → publish workflow, status, MongoDB via mock store
- E2E sync flow (`internal/integration/sync_flow_test.go`) — mock Git + real Redis/Mongo
- MongoDB metadata store (`internal/store/mongo/store_integration_test.go`) — CRUD with `-tags=integration`
- Bitbucket webhook (`internal/webhook/`) — signature verify, parse, dedupe, handler (401/400/202 cases)
- Metadata REST APIs (`internal/http/repos_test.go`) — repository and snapshot GET handlers
- Admin sync API (`internal/http/admin_test.go`)

## Webhook tests

```sh
go test ./internal/webhook/ ./internal/contracts/webhook/ -v
```

Uses fixtures in `internal/webhook/testdata/`. No Bitbucket or running server required.

## Redis tests

Unit tests use [miniredis](https://github.com/alicebob/miniredis) — an in-process Redis server. No Docker required.

Integration tests (`-tags=integration`) connect to real Redis:

```sh
docker compose up redis -d
go test -tags=integration ./internal/redis/ -v
```

Default: `REDIS_URL=redis://localhost:6379`.

## Git provider tests

Unit tests in `internal/git/` use a mock `Runner` — no network or real Bitbucket required.

Optional integration tests (real `git` binary):

```sh
go test -tags=integration ./internal/git/ -v
```

Manual smoke against Bitbucket (local only):

```sh
export BITBUCKET_USERNAME=girish.chalumuri@search.com
export BITBUCKET_TOKEN=...
export GIT_WORKSPACE_PATH=./data/repos
go test -tags=integration ./internal/git/ -run TestProvider_RealBitbucket -v
```

## SyncService tests

Unit tests in `internal/service/sync_test.go` use mocked `GitProvider` and `EventPublisher` — no Redis or git required.

Covers: publish order, git/publish errors, partial file publish failure, context cancellation, status transitions, metadata persistence via mock store.

## E2E sync flow

Full pipeline with mock Git and real Redis/Mongo:

```sh
docker compose up redis mongodb -d
go test -tags=integration ./internal/integration/ -v
```

Verifies: Mongo repository/snapshot upsert, sync job completion, `repo.snapshot.ready` + `files.changed` stream lengths, deterministic event IDs.

## MongoDB store tests

Integration tests (requires local Mongo from compose):

```sh
docker compose up mongodb -d
go test -tags=integration ./internal/store/mongo/ -v
```

Uses `mongodb://localhost:27018/adpilot_repo_sync` by default.
