> For the complete documentation index, see [llms.txt](https://triton.pharos.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritontestfactory.md).

# TritonTestFactory

Shared @IsTest helper for setting up, building, flushing, and asserting Triton logs in your own test classes.

## Overview

`TritonTestFactory` is an `@IsTest` utility that consolidates the setup, record-creation, flush, query, and assertion helpers used across Triton's own tests. You can reuse it in your test classes to reduce boilerplate when exercising Triton logging.

## Setup & State

#### initTestSetup()

{% code lineNumbers="true" %}

```apex
public static void initTestSetup()
```

{% endcode %}

Standard test setup — initializes Pharos test settings and clears the `Log_Level__mdt` cache. Call from your `@TestSetup`.

#### resetFlowState()

{% code lineNumbers="true" %}

```apex
public static void resetFlowState()
```

{% endcode %}

Resets Flow-related static state: the [`TritonFlowFlushBatch`](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritonflowflushbatch.md) pending buffer and the [`TritonCacheManager`](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritoncachemanager.md) mock cache.

## Fault Injection

#### failPublishing()

{% code lineNumbers="true" %}

```apex
public static void failPublishing()
```

{% endcode %}

Makes every Triton publish in the current test fail, at the single path all publishing goes through. Use it to prove your code survives a publish that Pharos refuses: the logging calls still return normally, and nothing is persisted.

{% code lineNumbers="true" %}

```apex
@IsTest
static void saveSurvivesAFailedPublish() {
    TritonTestFactory.failPublishing();
    Test.startTest();
    insert new Account(Name = 'Acme');   // the instrumented trigger logs and flushes
    Test.stopTest();
    System.assertEquals(1, [SELECT COUNT() FROM Account]);
    TritonTestFactory.assertNoLogs();
}
```

{% endcode %}

#### restrictLevel(TritonTypes.Level l)

{% code lineNumbers="true" %}

```apex
public static void restrictLevel(TritonTypes.Level l)
```

{% endcode %}

Installs a single global `Log_Level__mdt` rule at level `l`, so every log more verbose than it is filtered out. Tests start with no `Log_Level__mdt` rules (the org's own are never loaded), so this one rule is the only filter for the rest of the test.

## Record Creation

#### makeFlowLog(String summary)

{% code lineNumbers="true" %}

```apex
public static TritonFlow.FlowLog makeFlowLog(String summary)
```

{% endcode %}

Creates a [`TritonFlow.FlowLog`](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritonflow/flowlog.md) with standard test defaults (category `Flow`, type `type`, area `area`).

#### makeLogRecord(String summary)

{% code lineNumbers="true" %}

```apex
public static pharos__Log__c makeLogRecord(String summary)
```

{% endcode %}

Creates a `pharos__Log__c` record with standard test defaults.

## Flush

#### flushCachedFlowLogs()

{% code lineNumbers="true" %}

```apex
public static void flushCachedFlowLogs()
```

{% endcode %}

Manually drains cached Flow logs by constructing a `TritonFlowFlushBatch` and executing it — needed because a delayed `System.scheduleBatch` does not run during `Test.stopTest()`.

## Assertions

#### assertNoLogs()

{% code lineNumbers="true" %}

```apex
public static void assertNoLogs()
```

{% endcode %}

Asserts that no `pharos__Log__c` records exist.

#### assertLogCount(Integer expected)

{% code lineNumbers="true" %}

```apex
public static void assertLogCount(Integer expected)
```

{% endcode %}

Asserts that exactly `expected` log records exist.

#### assertSingleLog(TritonTypes.Category expectedCategory)

{% code lineNumbers="true" %}

```apex
public static pharos__Log__c assertSingleLog(TritonTypes.Category expectedCategory)
```

{% endcode %}

Asserts exactly one log exists with a populated hash and returns it.

## Query Helpers

#### queryLogs()

{% code lineNumbers="true" %}

```apex
public static List<pharos__Log__c> queryLogs()
```

{% endcode %}

Returns all `pharos__Log__c` records with the commonly queried fields, ordered by creation date.

#### countFlowFlushBatchJobs()

{% code lineNumbers="true" %}

```apex
public static Integer countFlowFlushBatchJobs()
```

{% endcode %}

Returns the count of `TritonFlowFlushBatch` async apex jobs.

#### countActiveFlowFlushBatchJobs()

{% code lineNumbers="true" %}

```apex
public static Integer countActiveFlowFlushBatchJobs()
```

{% endcode %}

Returns the count of active (queued/processing) `TritonFlowFlushBatch` jobs.
