Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/example/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"expo-build-properties",
{
"android": {
"minSdkVersion": 26
"minSdkVersion": 26,
"kotlinVersion": "2.4.0"
}
}
],
Expand Down
17 changes: 12 additions & 5 deletions apps/example/jest.harness.config.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
const ignored = [
// Tests that need a physical device are left out on simulators
...(process.env.HARNESS_SIMULATOR === 'true'
? ['\\.device\\.harness\\.ts$']
: []),
// Each platform leaves out the providers that only run on the other one
...(process.env.HARNESS_PLATFORM === 'android'
? ['/harness/apple/', '/harness/core-ai/', '/harness/json-ui/apple\\.']
: ['/harness/android/']),
]

// The tests live in the harness folder at the monorepo root
export default {
preset: 'react-native-harness',
roots: ['<rootDir>/../../harness'],
testMatch: ['**/*.harness.ts'],
// Tests that need a physical device are left out on simulators
testPathIgnorePatterns:
process.env.HARNESS_SIMULATOR === 'true'
? ['\\.device\\.harness\\.ts$']
: [],
testPathIgnorePatterns: ignored,
setupFiles: ['<rootDir>/../../harness/setup.ts'],
// The test files are few, so a crawl is fast and does not depend on Watchman
watchman: false,
Expand Down
5 changes: 4 additions & 1 deletion apps/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
"ios": "expo run:ios",
"typecheck": "tsc --noEmit",
"test:harness:simulator": "scripts/test-harness.sh simulator",
"test:harness:device": "scripts/test-harness.sh device"
"test:harness:device": "scripts/test-harness.sh device",
"test:harness:android": "scripts/test-harness.sh android"
},
"dependencies": {
"@ai-sdk/provider": "^4.0.33",
"@azure/core-asynciterator-polyfill": "^1.0.2",
"@expo/ui": "~58.0.23",
"@expo/vector-icons": "^15.0.3",
"@legendapp/list": "^3.7.0",
"@react-native-ai/android": "workspace:*",
"@react-native-ai/apple": "workspace:*",
"@react-native-ai/core-ai": "workspace:*",
"@react-native-ai/json-ui": "workspace:*",
Expand Down Expand Up @@ -55,6 +57,7 @@
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@react-native-harness/platform-android": "1.6.0",
"@react-native-harness/platform-apple": "1.6.0",
"@types/react": "~19.3.0",
"@types/ungap__structured-clone": "^1.2.0",
Expand Down
20 changes: 19 additions & 1 deletion apps/example/rn-harness.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
androidPlatform,
physicalAndroidDevice,
} from '@react-native-harness/platform-android'
import {
applePhysicalDevice,
applePlatform,
Expand All @@ -8,9 +12,14 @@ const deviceName = process.env.HARNESS_IOS_DEVICE
// Builds signed with entitlements the default App ID lacks use another one
const bundleId = process.env.HARNESS_IOS_BUNDLE_ID ?? 'com.callstack.ai.example'

const androidDevice = process.env.HARNESS_ANDROID_DEVICE
const androidManufacturer = process.env.HARNESS_ANDROID_MANUFACTURER ?? 'Google'

// Tests run in a debug build of the app. The ios runner uses the physical
// device named with HARNESS_IOS_DEVICE (as `xcrun devicectl list devices`
// shows it), and ios-simulator a simulator
// shows it), and ios-simulator a simulator. The android runner uses the
// physical device whose model is HARNESS_ANDROID_DEVICE (as
// `adb shell getprop ro.product.model` shows it, such as Pixel 9 Pro)
export default {
entryPoint: './index.js',
appRegistryComponentName: 'main',
Expand All @@ -29,6 +38,15 @@ export default {
device: appleSimulator('iPhone 18 Pro', '27.0'),
bundleId: 'com.callstack.ai.example',
}),
...(androidDevice
? [
androidPlatform({
name: 'android',
device: physicalAndroidDevice(androidManufacturer, androidDevice),
bundleId: 'com.callstack.ai.example',
}),
]
: []),
],
defaultRunner: deviceName ? 'ios' : 'ios-simulator',
// On-device models load and generate slowly, but a test that hangs should
Expand Down
27 changes: 26 additions & 1 deletion apps/example/scripts/test-harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# bun run test:harness:simulator [test path]
# HARNESS_IOS_DEVICE="My iPhone" bun run test:harness:device [test path]
# HARNESS_ANDROID_DEVICE="Pixel 9 Pro" bun run test:harness:android [test path]

set -euo pipefail

Expand Down Expand Up @@ -35,8 +36,32 @@ case "$TARGET" in
npx expo run:ios --device "$HARNESS_IOS_DEVICE" --no-bundler
npx react-native-harness --harnessRunner ios "$@"
;;
android)
if [[ -z "${HARNESS_ANDROID_DEVICE:-}" ]]; then
echo 'Set HARNESS_ANDROID_DEVICE to the device model, as `adb shell getprop ro.product.model` shows it' >&2
exit 1
fi
# Harness runs the tests on the first connected device of that model and
# ignores ANDROID_SERIAL, so exactly one may be connected, and the build is
# installed on it
matching=()
for serial in $(adb devices | awk 'NR > 1 && $2 == "device" { print $1 }'); do
model="$(adb -s "$serial" shell getprop ro.product.model | tr -d '\r')"
if [[ "$model" == "$HARNESS_ANDROID_DEVICE" ]]; then
matching+=("$serial")
fi
done
if [[ "${#matching[@]}" != 1 ]]; then
echo "Connect exactly one $HARNESS_ANDROID_DEVICE, found ${#matching[@]}" >&2
exit 1
fi
export ANDROID_SERIAL="${matching[0]}"
npx expo prebuild --clean --platform android
npx expo run:android --no-bundler --device "$ANDROID_SERIAL"
HARNESS_PLATFORM=android npx react-native-harness --harnessRunner android "$@"
;;
*)
echo "Usage: $0 simulator|device [test path]" >&2
echo "Usage: $0 simulator|device|android [test path]" >&2
exit 1
;;
esac
3 changes: 3 additions & 0 deletions apps/example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
""
],
"paths": {
"@react-native-ai/android": [
"../../packages/android/src"
],
"@react-native-ai/apple": [
"../../packages/apple-llm/src"
],
Expand Down
22 changes: 22 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions harness/android/availability.harness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { android } from '@react-native-ai/android'
import { describe, expect, it } from 'react-native-harness'

import { nano4 } from './helpers'

// Gemini Nano runs only on physical devices with AICore, such as a Pixel 9
describe('android availability', () => {
it('reports Gemini Nano details', async () => {
const info = await android.getModelInfo()
console.log('[android] Gemini Nano:', JSON.stringify(info))

expect(['available', 'downloadable', 'downloading']).toContain(
info.availability
)
})

// On a fresh device this downloads Gemini Nano, which takes minutes
it('prepares Gemini Nano with download progress', async () => {
await android().prepare({ onProgress: () => {} })

const info = await android.getModelInfo()
expect(info.availability).toBe('available')
expect(info.contextSize).toBeGreaterThan(0)
expect(info.features).toBeDefined()
expect(android.isAvailable()).toBe(true)
})

it('reports Gemini Nano 4 preview details', async () => {
const info = await nano4.getModelInfo()
console.log('[android] Gemini Nano preview fast:', JSON.stringify(info))

expect(info.availability).not.toBe('unknown')
})

it('counts tokens with the Gemini Nano tokenizer', async () => {
expect(
await android().countTokens('How many tokens is this?')
).toBeGreaterThan(0)
})
})
32 changes: 32 additions & 0 deletions harness/android/embedding.harness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { android } from '@react-native-ai/android'
import { cosineSimilarity, embedMany } from 'ai'
import { describe, expect, it } from 'react-native-harness'

const model = android.embeddingModel({ taskType: 'semanticSimilarity' })

describe('android embeddings', () => {
it('reports the embedding model', async () => {
const info = await model.getModelInfo()
console.log('[android] embedding model:', JSON.stringify(info))

expect(info.availability).not.toBe('unknown')
})

it('embeds similar sentences closer together', async () => {
await model.prepare({ onProgress: () => {} })

const { embeddings } = await embedMany({
model,
values: [
'The cat sleeps on the sofa.',
'A kitten naps on the couch.',
'Stock markets fell sharply today.',
],
})

expect(embeddings[0]!.length).toBeGreaterThan(0)
expect(cosineSimilarity(embeddings[0]!, embeddings[1]!)).toBeGreaterThan(
cosineSimilarity(embeddings[0]!, embeddings[2]!)
)
})
})
Loading
Loading