# Numscript specs format

Source: https://docs.formance.com/modules/numscript/specs

Stack: v3.2

The Numscript specs format is a conventional way to express unit tests about Numscript, using JSON. It can be used to define assertions over the results of a numscript run, given certain inputs. You can execute the tests using the [`numscript test`](https://docs.formance.com/modules/numscript/cli#test) command.

A JSON schema is available [online](https://raw.githubusercontent.com/formancehq/numscript/refs/heads/main/v1.specs.schema.json), so that you can have autocomplete and diagnostics in your editor. In many editors, such as VS Code, you can enable it by adding it to the JSON like this:

```json
{
  "$schema": "https://raw.githubusercontent.com/formancehq/numscript/refs/heads/main/v1.specs.schema.json"
}
```

This format is available from numscript\@0.0.25. If you're on an older version, use the [legacy specs schema](https://raw.githubusercontent.com/formancehq/numscript/refs/heads/main/specs.schema.json) instead — it uses maps instead of arrays for `balances` and `metadata`.

Here's the schema (using typescript notation):

```typescript
type Specs = {
  balances?: Balances;
  variables?: Vars;
  metadata?: AccountsMetadata;

  featureFlags?: Array<string>;
  testCases: Array<TestCase>
};

type TestCase = {
  balances?: Balances;
  variables?: Vars;
  metadata?: AccountsMetadata;

  it: string;
  "expect.error.missingFunds"?: boolean;
  "expect.error.negativeAmount"?: boolean;
  "expect.postings"?: Array<Posting>;
  "expect.txMetadata"?: TxMetadata;
  "expect.metadata"?: SetAccountsMetadata;
  "expect.endBalances"?: Balances;
  "expect.endBalances.include"?: Balances;
  "expect.movements"?: Movements;
}
```

```typescript
type Balances = Array<BalanceRow>;
type BalanceRow = {
  account: string;
  asset: string;
  amount: number;
  // Optional, for setups using asset colors or scoped accounts
  color?: string;
  scope?: string;
};

type Vars = {
  [name: string]: string
};

type AccountsMetadata = Array<AccountMetadataRow>;
type AccountMetadataRow = {
  account: string;
  key: string;
  value: string;
  scope?: string;
};

// Used by `expect.metadata`. The value is the metadata's rendered form (see
// the note under `expect.txMetadata`), same shape as the `metadata` precondition.
type SetAccountsMetadata = Array<SetAccountMetadataRow>;
type SetAccountMetadataRow = {
  account: string;
  key: string;
  value: string;
  scope?: string;
};

type TxMetadata = Array<TxMetadataRow>;
type TxMetadataRow = {
  key: string;
  value: string;
};

type Posting = {
  source: string;
  destination: string;
  asset: string;
  amount: number;
  sourceScope?: string;
  destinationScope?: string;
  color?: string;
};

type Movements = Array<Movement>;
type Movement = {
  source: string;
  destination: string;
  asset: string;
  amount: number;
  sourceScope?: string;
  destinationScope?: string;
  color?: string;
};
```

### Example

Say we have the following numscript:

```numscript
vars {
  monetary $cap
  account $source
  account $destination
}

send [EUR/2 *] (
  source = max $cap from $source
  destination = $destination
)
```

And we want to test that we never send more than `$cap`. We can express the relevant test cases in the following way:

```json
{
  "$schema": "https://raw.githubusercontent.com/formancehq/numscript/refs/heads/main/v1.specs.schema.json",
  "variables": {
    "source": "alice",
    "destination": "bob"
  },
  "balances": [
    { "account": "alice", "asset": "EUR/2", "amount": 500 }
  ],
  "testCases": [
    {
      "it": "sends all the available balance when it doesn't exceed the cap and @alice has enough balance",
      "variables": {
        "cap": "EUR/2 9999"
      },
      "expect.postings": [
        {
          "source": "alice",
          "destination": "bob",
          "amount": 500,
          "asset": "EUR/2"
        }
      ]
    },
    {
      "it": "caps the sent amt to $cap when lower than available balance",
      "variables": {
        "cap": "EUR/2 10"
      },
      "expect.postings": [
        {
          "source": "alice",
          "destination": "bob",
          "amount": 10,
          "asset": "EUR/2"
        }
      ]
    }
  ]
}
```

## Preconditions

The inputs of each test cases. You can set the preconditions top-level (in the outer object), and/or in each `testCase` . The preconditions in a testCase will be merged to the top-level preconditions (with the precedence being given to the inner preconditions).

Each entry in `balances` and `metadata` is keyed by its `account` (plus `asset` for balances, `key` for metadata, and optionally `color` / `scope`) — merging replaces entries with a matching key and keeps the rest.

For example, in the following specs:

```json
{
  "balances": [
    { "account": "alice", "asset": "EUR/2", "amount": 100 },
    { "account": "alice", "asset": "USD/2", "amount": 100 },
    { "account": "bob", "asset": "EUR/2", "amount": -2 }
  ],
  "testCases": [
    {
      "it": "example specs",
      "balances": [
        { "account": "alice", "asset": "EUR/2", "amount": 999 }
      ]
    }
  ]
}
```

The inner preconditions will only override `@alice` 's `EUR/2` balance, resulting in:

```json
[
  { "account": "alice", "asset": "EUR/2", "amount": 999 },
  { "account": "alice", "asset": "USD/2", "amount": 100 },
  { "account": "bob", "asset": "EUR/2", "amount": -2 }
]
```

### `variables`

The (stringified) value of each variable

```json
{
  "variables": {
     "amount": "USD/2 100"
  }
}
```

### `balances`

The initial accounts' balances.

```json
{
  "balances": [
    { "account": "alice", "asset": "USD/2", "amount": 200 },
    { "account": "bob", "asset": "USD/2", "amount": -42 }
  ]
}
```

### `metadata`

The initial accounts' metadata.

```json
{
  "metadata": [
    { "account": "alice", "key": "id", "value": "1234" }
  ]
}
```

## Assertions

Assertions are only run if explicitly defined.

The recommended assertion to use by default are `expect.postings` or `expect.error.missingFunds`, but there are also a few weaker assertion that might be useful when the exact postings are an implementation detail of your business logic.

### `expect.error.missingFunds`

Assert that the script failed because of missing funds. Even if this is set to true, the test will still fail if the script outputs a different error.

Defaults to `false`.

Note: this was called `expect.error` in earlier releases

### `expect.error.negativeAmount`

Assert that the script failed because of a send statement using a negative amount.

Defaults to `false`.

### `expect.postings`

Assert against the exact postings emitted by the script. To assert that there are no postings, you can use the empty array. To assert that no postings are produced because of a failure due to missing funds, you can use the `expect.error.missingFunds`  assertion instead.

```json
{
  "expect.postings": [
     { "source": "world", "destination": "user:001", "asset": "EUR/2", "amount": 100 }
  ]
}
```

### `expect.txMetadata`

Assert against the transaction meta emitted by the script (using `set_tx_meta`). It's an array of entries, one per metadata key. Each `value` is the metadata's rendered form — a monetary renders as `"USD/2 100"`, a portion as `"1/2"`, an account as its bare name, and so on — rather than a type-tagged object. Because the wire form is untyped, values of different types that render alike (a string `"42"` and the number `42`) are indistinguishable here.

```json
{
  "expect.txMetadata": [
    { "key": "senderAccount", "value": "user:5829" }
  ]
}
```

An unreleased build briefly represented this value as a type-tagged object, e.g. `{"type": "monetary", "asset": "USD/2", "amount": "100"}` instead of `"USD/2 100"`. That representation never reached a stable release and has been reverted — if a specs file still uses it, rewrite each `value` to its rendered string form.

### `expect.metadata`

Assert against the accounts metadata at the end of script execution (using `set_account_meta`). It's an array of entries, each keyed by `account` and `key`, with the same rendered `value` shape as `expect.txMetadata` (including the same short-lived tagged-object format, now reverted).

```json
{
  "expect.metadata": [
    { "account": "alice", "key": "id", "value": "1234" }
  ]
}
```

Note that it takes into account the values defined with the `metadata`  precondition as well.

### `expect.endBalances`

Assert against the balances at the end of the script

For example:

```json
{
  "expect.endBalances": [
    { "account": "alice", "asset": "EUR/2", "amount": 100 }
  ]
}
```

means that `@alice`  has `[EUR/2 100]`  balance after the script is applied.

You might consider using this assertion when you only care about the end balance of an account, for example if you need to bring an account to a certain value (not less, not more), so you don't care about how the postings are composed exactly.

Note: this was called `expect.volumes` in earlier releases

### `expect.endBalances.include`

A weaker version of `expect.endBalances` that allows defining a subset of the balances we assert against.

For example, the following:

```json
{
  "expect.endBalances.include": [
    { "account": "alice", "asset": "EUR/2", "amount": 100 }
  ]
}
```

passes even if there are more accounts in the involved balances, and if `alice` emit postings involving other currencies.

### `expect.movements`

Assert against the resulting movements. A movement is an array entry from a source account, to a destination account, for a given asset and amount.

For example, this assertion:

```json
{
  "expect.movements": [
    { "source": "alice", "destination": "bob", "asset": "EUR/2", "amount": 100 }
  ]
}
```

means that `@alice`  sent  `[EUR/2 100]` to `@bob`

You might consider using this assertion when you care about the movements graph from-to accounts, and you don't care about the order of the postings or the way they are split.

## Focus mode

You can select a subset of test to run by using the `focus` and `skip` modifiers on a test case definition. They are only meant to be used while developing, and will produce an error status code so that they aren't committed by mistake thus producing false positive tests.

### `focus`

If at least a test has a `focus` modifier, all the tests without the `focus` modifier will be skipped.

```json
{
  "testCases": [
	{
		"it": "only run this test!",
		"focus": true,
		"expect.postings": // ..
	},
	{
		"it": "this test is skipped",
		"expect.postings": // ..
	}
  ]
}
```

### `skip`

If a test is marked with the `skip` modifier, it will not be run.

```json
{
  "testCases": [
	{
		"it": "skip this test",
		"skip": true,
		"expect.postings": // ..
	}
  ]
}
```
