Metadata-Version: 2.4
Name: txtest
Version: 0.0.5
Summary: Unit testing for texts, courses, articles, and research.
Author-email: Thiago Nepomuceno <thiago@neps.academy>
License-Expression: Apache-2.0
Keywords: text,testing,validation,unit-testing
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development :: Testing
Classifier: Topic :: Text Processing
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: click>=8.1.7
Requires-Dist: rich>=13.9.4
Requires-Dist: Jinja2>=3.1.4
Requires-Dist: PyYAML>=6.0.2
Requires-Dist: python-dotenv>=1.0.1
Dynamic: license-file

# txtest

Unit tests for written content.

`txtest` evaluates a text file against criteria you define with the OpenAI Decisions API. Each test produces a `PASS`, `FAIL`, or `ERROR`, with a short description and its value and threshold shown in the terminal.

## Installation

Requires Python 3.11 or later. Install from [PyPI](https://pypi.org/project/txtest/):

```bash
python -m pip install --upgrade txtest
```

The CLI is included in version 0.0.2 and later. Run `txtest --help` to check the installation.

Set `OPENAI_API_KEY` in your environment or create a `.env` file:

```text
OPENAI_API_KEY=your-api-key
```

`txtest` looks for `.env` in the current directory and its parents. An existing environment variable takes precedence.

## Install the project skill

From your project's root, run:

```bash
txtest install
```

This copies the bundled skill to `.agents/skills/txtest/SKILL.md`. Run it again to update that file. Use `--target DIRECTORY` to select a different skills directory. The command is also available as `python -m txtest install`.

## Quickstart

The [repository](https://github.com/ThiNepo/txtest) includes a fictional library desk-reservation policy and two demonstration tests:

- [Cancellation steps](https://github.com/ThiNepo/txtest/blob/main/txtests/policies/01-cancellation-steps.txtest): checks whether the policy explains how to cancel a reservation.
- [Policy clarity](https://github.com/ThiNepo/txtest/blob/main/txtests/policies/02-policy-clarity.txtest): scores the policy against five rubric levels.

To try these examples, download their files by cloning the repository:

```bash
git clone https://github.com/ThiNepo/txtest.git
cd txtest
```

From the repository root, run:

```bash
txtest examples/reservation-policy.md --set policies
```

This evaluates [the complete policy](https://github.com/ThiNepo/txtest/blob/main/examples/reservation-policy.md) with every `.txtest` file in `txtests/policies/`, in filename order. Each test makes one request, sequentially, without automatic retries. A failed test or an API error does not stop the remaining tests. Results stay in the terminal.

For a demonstration of missing information, try [the incomplete policy](https://github.com/ThiNepo/txtest/blob/main/examples/reservation-policy-incomplete.md):

```bash
txtest examples/reservation-policy-incomplete.md --set policies
```

Its cancellation procedure is deliberately missing, so the cancellation test is intended to produce `FAIL`. Scores and probabilities are model judgments and may vary between runs; these examples do not prescribe fixed output values.

The module entry point works too:

```bash
python -m txtest examples/reservation-policy.md --set policies
```

## Selecting blocks

Use `--block TYPE:VALUE` to evaluate one reading of a file with conditional content.

```bash
txtest guide.md --set guides --block language:cpp
```

Write `[TYPE VALUE]` and `[endTYPE]` on their own lines. This command keeps the shared text and the body of each `[language cpp]` block. It removes the markers and skips other `language` blocks. Blocks of types you do not select stay as written, and markers inside fenced code are literal text.

Repeat `--block` to select more than one value or type. For example, `--block language:cpp --block language:python --block audience:beginner` keeps both programming languages and selects the beginner audience. A nested block is visible only when its enclosing blocks are visible. Without `--block`, the full file is evaluated.

## Writing tests

Keep each test in `txtests/<set>/<name>.txtest`. Sets are resolved from the current directory, and the input is one UTF-8 text file.

A test contains YAML frontmatter followed by Jinja templates in `<system>` and `<user>` blocks. For example:

```text
---
description: Explains how to cancel a reservation.
type: predicate
min_probability: 0.5
---
<system>
Does the policy give the reader a concrete procedure for cancelling a reservation?
</system>

<user>
{{ text }}
</user>
```

`description` is required and appears beside the result so the reader can understand what was checked. The `<system>` block becomes the question instructions; the rendered `<user>` block becomes the text evaluated by that question.

The templates have three variables:

| Variable | Value |
| --- | --- |
| `text` | The input text after applying any `--block` filters. |
| `filename` | The input file's name. |
| `path` | The input path provided to the command. |

An undefined Jinja variable produces a test error before a request is made.

### Predicate tests

Use `type: predicate` for a yes/no condition. The result is the model's estimated probability that the condition is true, from `0` to `1`. The test passes when the probability is at least `min_probability`, which defaults to `0.5`. Probabilities and their thresholds appear as percentages in the terminal.

### Score tests

Use `type: score` for a rubric with exactly five levels, ordered from lowest to highest. Each entry in `levels` needs a unique string `label` and a nonempty `description`. See the complete [policy clarity test](https://github.com/ThiNepo/txtest/blob/main/txtests/policies/02-policy-clarity.txtest) for a concrete rubric.

The result uses a **1–5 scale**: the API's zero-based score is shifted by one. Fractional scores are preserved and compared with `min_score`, which defaults to `4`, without rounding first. A score equal to the minimum passes. Each test passes or fails independently.

`score` is the default type when `type` is omitted. Predicate tests use `min_probability`; score tests use `min_score` and `levels`.

The accepted frontmatter fields are `description`, `type`, `min_probability`, `min_score`, `levels`, and `model`. This implementation uses `gpt-6-luna` by default and accepts that same value if `model` is explicitly set.

## Exit codes

| Code | Meaning |
| --- | --- |
| `0` | All tests passed. |
| `1` | At least one test failed, with no execution errors. |
| `2` | A configuration, input, API, or response error occurred. |

A model refusal is an error. An empty or missing test set is also an error.

## Development

Run the offline test suite with:

```bash
python -m unittest discover -s tests -v
```

## Build and publish

Install the build tool once with `python -m pip install build`. From the repository root, start with an empty `dist/` directory and run the build.

```bash
python -m build
```

The build creates a wheel (`.whl`) and a source distribution (`.tar.gz`) in `dist/`. To publish them to PyPI, run the upload separately.

```bash
twine upload dist/*
```

## License

Apache-2.0. See [LICENSE](https://github.com/ThiNepo/txtest/blob/main/LICENSE).
