# Development and release

Run these commands from a checkout of the GitHub repository.

```sh
mix deps.get --check-locked
mix check
mix test --cover --warnings-as-errors
mix quality
python3 scripts/generate.py --check
mix docs --warnings-as-errors
mix hex.audit
mix hex.publish --dry-run --yes
```

`mix check` checks formatting, compiles, and runs the suite in the test
environment. `mix quality` checks formatting, compiles, runs strict Credo,
and runs Dialyzer in the development environment. Dialyxir caches its PLTs
under `_build/dev/plts`. The first analysis on a runtime builds those tables.

The default test suite exercises Req with local mocks. It uses no Fatture in
Cloud credentials, creates no documents, and sends no invoices. Plug and
OpenApiSpex are test dependencies. Credo, Dialyxir, and ExDoc are development
dependencies.
The [SDK compatibility guide](sdk-parity.md) records which provider behavior
the suite exercises.

## Live read-only test

Run this file explicitly with an existing access token in `FIC_ACCESS_TOKEN`.
The test uses only `GET /user/info` and `GET /user/companies` on the official
HTTPS API. It checks the response structure and keeps credentials and response
contents out of its reports. The default suite and CI omit this file.

```sh
mix test scripts/live/read_only_test.exs
```

## OpenAPI contract tests

`ExFattureincloud.TestSupport.OpenAPIMock` reads the vendored specification
and extensions directly from `priv/`. The test helper caches the parsed
contract and its indexes with `:persistent_term`.

For all 124 operations, the contract suite builds input from the OpenAPI
schemas, calls the Elixir wrapper, and checks the HTTP request that passes
through Req. It verifies the method, path, authorization header, query
options, and JSON or multipart body. It validates the simulated response
against its schema and checks the SDK result, including empty bodies and
XML. It also checks the wrapper arities and required query parameters.

A wire-type check compares JSON values before and after OpenApiSpex casts
them. A JSON string in a numeric field fails validation. URL parameters are
strings on the wire and are cast according to the schema.

```elixir
alias ExFattureincloud.{Clients, TestSupport.OpenAPIMock}

client = ExFattureincloud.new(
  token: "test-token",
  req_options: [plug: {Req.Test, __MODULE__}]
)

OpenAPIMock.expect(__MODULE__, :list_clients, examples: false)
assert {:ok, _response} = Clients.list_clients(client, 12_345)
Req.Test.verify!(__MODULE__)
```

`examples: false` generates data with `OpenApiSpex.Schema.example/2`. It
strips example annotations from the generation copy and selects a non-null
enum value when the schema disallows null. Validation uses the original
schema. `examples: true` exercises the official examples and reports schema
mismatches.

The pinned sources contain 26 JSON media-type examples that contradict their
schemas. Each entry in `test/fixtures/openapi_example_issues.json` identifies
the original example and validation failure. The suite checks the SHA-256
hashes of the contract and extensions and verifies the complete set of
failing example locations. Review the differences when updating the contract
before updating the baseline.

Separate compatibility tests use Req.Test to verify that transport preserves
the original JSON values for those 26 examples. The contract tests continue
to enforce the original schemas. Run either suite by tag.

```sh
mix test --only contract --warnings-as-errors
mix test --only sdk_compatibility --warnings-as-errors
```

## Regenerate the API

`priv/openapi.json` contains the full enriched OpenAPI specification converted
from YAML. `priv/openapi-source.json` records the official commit, version,
and hashes. `priv/openapi-extensions.json` covers documented endpoints still
outside that pinned specification and records each extension's source.

Run the generator with Python 3.9 or later.

```sh
python3 scripts/generate.py
python3 scripts/generate.py --check
```

The generator writes the resource modules, operation catalog, OAuth scopes,
and endpoint guide, then formats the Elixir files. `--check` compares its
output with the committed files. Update the specification and generator to
change generated content. When upgrading the spec, pin an official commit,
verify the YAML-to-JSON conversion, update the hashes, and review the official
SDKs for helper changes. If a documented extension moves into the official
spec, compare its contract before removing the extension.

## GitHub Actions

The CI workflow runs its jobs only after a pull request is merged into `main`.
Every job runs on Elixir 1.19 / OTP 28 and Elixir 1.20 / OTP 29.

| Job | Checks |
| --- | --- |
| Tests | Formatting, warning-free compilation, reproducible generation, full test suite and at least 90% coverage. |
| Credo | Warning-free compilation and `mix credo --strict`. |
| Dialyzer | Warning-free compilation and type analysis. |
| Package and docs | Hex dependency audit, warning-free ExDoc, publishing dry run, package build and isolated production smoke test. |

Repository jobs use the locked dependency graph. The package job unpacks the
tarball in a temporary directory, resolves its production dependencies,
compiles it and runs `scripts/ci/package_smoke.exs`. The script starts the SDK
and checks packaged OpenAPI files, local request validation, and the absence
of development/test modules. GitHub retains coverage, generated docs, and
tarball artifacts for 14 days. CI has read-only repository access and the
publishing step uses Hex's dry-run mode.

## Publish on Hex

1. Update `@version` in `mix.exs`, the README dependency example, and
   `CHANGELOG.md` together. The initial release is `0.1.0`.
2. Once the release commit passes CI, tag it and push the tag so the source
   links generated by ExDoc resolve. For the first release, use `v0.1.0`.
3. From that commit, run `mix docs --warnings-as-errors` and
   `mix hex.publish --dry-run --yes` in the development environment. Inspect the
   package with `mix hex.build --unpack --output /tmp/ex_fattureincloud-package`
   if needed.
4. Authenticate with `mix hex.user auth`, then run `mix hex.publish` to upload
   the package and its documentation. The authenticated Hex account becomes the
   owner of the first release.
   [Hex publishing documentation](https://hexdocs.pm/hex/Mix.Tasks.Hex.Publish.html)
5. Check the [Hex package](https://hex.pm/packages/ex_fattureincloud) and
   [HexDocs](https://hexdocs.pm/ex_fattureincloud) for the published version.
