> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chappiesdk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing Your Chappie SDK Integration with Test Fixtures

> Use ChappieSDKTesting to seed auth fixtures, run unit tests with in-memory stores, and configure UI tests without live network calls.

Chappie ships a dedicated `ChappieSDKTesting` library target that provides seeded auth fixtures and an in-memory credential store for unit and UI tests. Keep it out of your app target — link it only to test targets so test-only code never ships to production.

## Adding ChappieSDKTesting to your test target

Add the package dependency and link both `ChappieSDK` and `ChappieSDKTesting` to your test target in `Package.swift`:

```swift theme={null}
.testTarget(
    name: "MyAppTests",
    dependencies: [
        .product(name: "ChappieSDK", package: "chappie-sdk"),
        .product(name: "ChappieSDKTesting", package: "chappie-sdk")
    ]
)
```

If your tests also exercise thread storage or runtime events, add `ChappieSDKRuntime` to the same target:

```swift theme={null}
.testTarget(
    name: "MyAppTests",
    dependencies: [
        .product(name: "ChappieSDK", package: "chappie-sdk"),
        .product(name: "ChappieSDKRuntime", package: "chappie-sdk"),
        .product(name: "ChappieSDKTesting", package: "chappie-sdk")
    ]
)
```

<Note>
  Never link `ChappieSDKTesting` from your app target. It is designed exclusively for test and UI-test targets — linking it to your app would bundle test fixtures and Keychain helpers into your production binary.
</Note>

## Seeding a signed-in session

Call `ChappieTestAuth.seedSignedInSession()` at the start of a test to write a fake-but-valid credential into the Keychain so `ChappieAuthSession` starts in the `.signedIn` state without requiring a real OAuth flow:

```swift theme={null}
import ChappieSDKTesting

try ChappieTestAuth.seedSignedInSession()
```

The default seeded account uses these values:

| Field      | Value                 |
| ---------- | --------------------- |
| Email      | `preview@example.com` |
| User ID    | `user_test`           |
| Account ID | `account_test`        |
| Plan       | `plus`                |
| Expiry     | One hour from now     |

### Customising the seeded account

Pass a `ChappieAccountInfo` to override any account metadata fields:

```swift theme={null}
try ChappieTestAuth.seedSignedInSession(
    accountInfo: ChappieAccountInfo(
        email: "tester@example.com",
        userID: "user_123",
        accountID: "account_123",
        plan: ChappiePlan(rawValue: "team")
    )
)
```

Pass a custom `keychainService` when the app under test uses a non-default service name:

```swift theme={null}
try ChappieTestAuth.seedSignedInSession(
    keychainService: "com.example.tests"
)
```

## Clearing a seeded session

Call `ChappieTestAuth.clearSession()` in your test tear-down to remove the seeded credential and return the app to a signed-out state:

```swift theme={null}
try ChappieTestAuth.clearSession()
```

Pass the same custom service name if you used one when seeding:

```swift theme={null}
try ChappieTestAuth.clearSession(keychainService: "com.example.tests")
```

## Using ChappieInMemoryThreadStore in unit tests

For unit tests that exercise conversation storage without touching the filesystem, use `ChappieInMemoryThreadStore` from `ChappieSDKRuntime`. It implements the full `ChappieThreadStore` protocol in memory and applies the same retention logic as `ChappieFileThreadStore`:

```swift theme={null}
import ChappieSDKRuntime

let store = ChappieInMemoryThreadStore()
let runtime = Chappie.client().runtime(store: store)

let reply = try await runtime.send("Hello from a test.")
let threads = try await runtime.listThreads()
XCTAssertFalse(threads.isEmpty)
```

Pass a custom `now` closure to test time-dependent retention behaviour without sleeping:

```swift theme={null}
var currentDate = Date()
let store = ChappieInMemoryThreadStore(now: { currentDate })
```

## UI tests: resetting auth state via launch arguments

The demo app pattern shows how to handle auth state in UI tests using launch arguments. In your app's `@main` entry point, check for the `-resetChappieAuthForUITests` launch argument (in a `#if DEBUG` block) and call `ChappieTestAuth.clearSession()` before creating the auth session:

```swift theme={null}
#if DEBUG
import ChappieSDKTesting

let arguments = ProcessInfo.processInfo.arguments
if arguments.contains("-resetChappieAuthForUITests") {
    try? ChappieTestAuth.clearSession()
}
if arguments.contains("-seedChappieAuthForUITests") {
    try? ChappieTestAuth.seedSignedInSession()
}
#endif
```

Then pass the argument from your `XCUIApplication` setup in the UI test target:

```swift theme={null}
let app = XCUIApplication()
app.launchArguments = ["-resetChappieAuthForUITests"]
app.launch()
```

Use `-seedChappieAuthForUITests` to start UI tests in the signed-in state without going through the actual OAuth flow.

## Running the SDK's test suite

Run all Swift package tests from the `ChappieSDK` directory:

```bash theme={null}
cd ChappieSDK
swift test
```

Build the demo apps to verify the full project compiles:

```bash theme={null}
xcodebuild \
  -project ChappieDemo/ChappieDemo.xcodeproj \
  -scheme ChappieDemo \
  -destination 'generic/platform=iOS Simulator' \
  build

xcodebuild \
  -project ChappieDemo/ChappieDemo.xcodeproj \
  -scheme ChappieExample \
  -destination 'generic/platform=iOS Simulator' \
  build
```
