Skip to main content
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:
.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:
.testTarget(
    name: "MyAppTests",
    dependencies: [
        .product(name: "ChappieSDK", package: "chappie-sdk"),
        .product(name: "ChappieSDKRuntime", package: "chappie-sdk"),
        .product(name: "ChappieSDKTesting", package: "chappie-sdk")
    ]
)
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.

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:
import ChappieSDKTesting

try ChappieTestAuth.seedSignedInSession()
The default seeded account uses these values:
FieldValue
Emailpreview@example.com
User IDuser_test
Account IDaccount_test
Planplus
ExpiryOne hour from now

Customising the seeded account

Pass a ChappieAccountInfo to override any account metadata fields:
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:
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:
try ChappieTestAuth.clearSession()
Pass the same custom service name if you used one when seeding:
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:
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:
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:
#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:
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:
cd ChappieSDK
swift test
Build the demo apps to verify the full project compiles:
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