# GoodGo Platform — Pact Contract Tests

EN: Consumer-driven contract tests for microservice boundaries using Pact.io v3.
VI: Contract tests consumer-driven cho ranh giới microservice sử dụng Pact.io v3.

## Covered Boundaries

| Consumer | Provider | Test File |
|----------|----------|-----------|
| OrderService | CatalogService | `order-catalog.consumer.test.ts` |
| OrderService | WalletService | `order-wallet.consumer.test.ts` |
| OrderService | InventoryService | `order-inventory.consumer.test.ts` |
| MerchantService | IamService | `merchant-iam.consumer.test.ts` |
| PromotionService | OrderService | `promotion-order.consumer.test.ts` |

## Running Tests

```bash
cd tests/contract
pnpm install
pnpm test
```

EN: Pact files are generated in `tests/contract/pacts/`.
VI: Pact files được tạo trong `tests/contract/pacts/`.

## Provider Verification (CI)

EN: Each provider service must verify the Pact it receives. Add to the provider's CI workflow:
VI: Mỗi provider service phải verify Pact nhận được. Thêm vào CI workflow của provider:

```csharp
// EN: In provider test project — PactNet provider verification
// VI: Trong provider test project — PactNet provider verification
[Fact]
public async Task EnsureProviderApiHonoursPactWithOrderService()
{
    var config = new PactVerifierConfig
    {
        Outputters = new List<IOutput> { new XUnitOutput(_output) },
    };

    await new PactVerifier(config)
        .ServiceProvider("CatalogService", new Uri("http://localhost:5020"))
        .WithPactFile(new FileInfo("../../../../tests/contract/pacts/OrderService-CatalogService.json"))
        .WithProviderStateUrl(new Uri("http://localhost:5020/provider-states"))
        .Verify();
}
```

## Publishing Pacts (Optional — Pact Broker)

```bash
pnpm pact:publish
```

EN: Requires `PACT_BROKER_URL` and `PACT_BROKER_TOKEN` environment variables.
VI: Cần các biến môi trường `PACT_BROKER_URL` và `PACT_BROKER_TOKEN`.
