Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add app-hosted ensu inference integration test

Manav Rathi95ea394a93e7
diff --git a/mobile/native/darwin/Apps/ensu/EnsuTests/InferenceIntegrationTests.swift b/mobile/native/darwin/Apps/ensu/EnsuTests/InferenceIntegrationTests.swift
new file mode 100644
index 0000000000..8879b98b9a
--- /dev/null
+++ b/mobile/native/darwin/Apps/ensu/EnsuTests/InferenceIntegrationTests.swift
@@ -0,0 +1,148 @@
+import XCTest
+@testable import ensu
+
+private final class GenerateEventCollector: GenerateEventCallback {
+ private(set) var events: [GenerateEvent] = []
+
+ func onEvent(event: GenerateEvent) {
+ events.append(event)
+ }
+
+ var hasDone: Bool {
+ events.contains { event in
+ if case .done = event { return true }
+ return false
+ }
+ }
+
+ var lastError: String? {
+ events.compactMap { event in
+ if case let .error(_, message) = event { return message }
+ return nil
+ }.last
+ }
+
+ var textOutput: String {
+ events.compactMap { event in
+ if case let .text(_, text, _) = event { return text }
+ return nil
+ }.joined()
+ }
+}
+
+final class InferenceIntegrationTests: XCTestCase {
+ func testInitBackend() throws {
+ try initBackend()
+ }
+
+ func testGenerateChatStreamWhenModelProvided() throws {
+ let modelPath = try requiredEnv("ENSU_TEST_MODEL")
+
+ try initBackend()
+
+ let model = try loadModel(
+ params: ModelLoadParams(
+ modelPath: modelPath,
+ nGpuLayers: nil,
+ useMmap: nil,
+ useMlock: nil
+ )
+ )
+
+ let context = try createContext(
+ model: model,
+ params: ContextParams(contextSize: 2048, nThreads: nil, nBatch: nil)
+ )
+
+ let request = GenerateChatRequest(
+ messages: [
+ ChatMessage(role: "system", content: "You are a helpful assistant."),
+ ChatMessage(role: "user", content: "Say hello in one short sentence.")
+ ],
+ templateOverride: nil,
+ addAssistant: true,
+ imagePaths: nil,
+ mmprojPath: nil,
+ mediaMarker: nil,
+ maxTokens: 24,
+ temperature: 0.7,
+ topP: 0.9,
+ topK: nil,
+ repeatPenalty: nil,
+ frequencyPenalty: nil,
+ presencePenalty: nil,
+ seed: 42,
+ stopSequences: nil,
+ grammar: nil
+ )
+
+ let collector = GenerateEventCollector()
+ let summary = try generateChatStream(context: context, request: request, callback: collector)
+
+ XCTAssertGreaterThan(summary.jobId, 0)
+ XCTAssertTrue(collector.hasDone)
+ XCTAssertNil(collector.lastError)
+ XCTAssertFalse(collector.textOutput.isEmpty)
+ }
+
+ func testGenerateChatStreamWithImageWhenProvided() throws {
+ let modelPath = try requiredEnv("ENSU_TEST_MODEL")
+ let mmprojPath = try requiredEnv("ENSU_TEST_MMPROJ")
+ let imagePath = try requiredEnv("ENSU_TEST_IMAGE")
+
+ try initBackend()
+
+ let model = try loadModel(
+ params: ModelLoadParams(
+ modelPath: modelPath,
+ nGpuLayers: nil,
+ useMmap: nil,
+ useMlock: nil
+ )
+ )
+
+ let context = try createContext(
+ model: model,
+ params: ContextParams(contextSize: 2048, nThreads: nil, nBatch: nil)
+ )
+
+ let request = GenerateChatRequest(
+ messages: [
+ ChatMessage(role: "system", content: "You are a helpful assistant."),
+ ChatMessage(role: "user", content: "What color is the car? Answer in one word.")
+ ],
+ templateOverride: nil,
+ addAssistant: true,
+ imagePaths: [imagePath],
+ mmprojPath: mmprojPath,
+ mediaMarker: nil,
+ maxTokens: 24,
+ temperature: 0.2,
+ topP: 0.9,
+ topK: nil,
+ repeatPenalty: nil,
+ frequencyPenalty: nil,
+ presencePenalty: nil,
+ seed: 42,
+ stopSequences: nil,
+ grammar: nil
+ )
+
+ let collector = GenerateEventCollector()
+ let summary = try generateChatStream(context: context, request: request, callback: collector)
+
+ XCTAssertGreaterThan(summary.jobId, 0)
+ XCTAssertTrue(collector.hasDone)
+ XCTAssertNil(collector.lastError)
+ XCTAssertFalse(collector.textOutput.isEmpty)
+ }
+
+ private func requiredEnv(_ keys: String...) throws -> String {
+ let env = ProcessInfo.processInfo.environment
+ if let value = keys.compactMap({ env[$0] }).first(where: { !$0.isEmpty }) {
+ return value
+ }
+
+ throw XCTSkip("Set one of \(keys.joined(separator: ", ")) to run this test.")
+ }
+}
diff --git a/mobile/native/darwin/Apps/ensu/README.md b/mobile/native/darwin/Apps/ensu/README.md
index fc6277eb21..5d36359d69 100644
--- a/mobile/native/darwin/Apps/ensu/README.md
+++ b/mobile/native/darwin/Apps/ensu/README.md
@@ -46,6 +46,23 @@ xcodebuild \
-derivedDataPath build
```
+## Tests
+
+Run the app-hosted inference integration test from Terminal with `TEST_RUNNER_...` environment
+variables so they reach the XCTest runner process:
+
+```bash
+cd darwin/Apps/ensu
+TEST_RUNNER_ENSU_TEST_MODEL=/path/to/model.gguf \
+TEST_RUNNER_ENSU_TEST_MMPROJ=/path/to/mmproj.gguf \
+TEST_RUNNER_ENSU_TEST_IMAGE=/path/to/image.png \
+xcodebuild test \
+ -project ensu.xcodeproj \
+ -scheme ensu \
+ -destination 'platform=iOS Simulator,name=iPhone 17' \
+ SWIFT_ENABLE_EXPLICIT_MODULES=NO
+```
+
### Debug Build (Device)
```bash
cd darwin/Apps/ensu
diff --git a/mobile/native/darwin/Apps/ensu/ensu.xcodeproj/project.pbxproj b/mobile/native/darwin/Apps/ensu/ensu.xcodeproj/project.pbxproj
index 0979d567d9..8b03ebeaf6 100644
--- a/mobile/native/darwin/Apps/ensu/ensu.xcodeproj/project.pbxproj
+++ b/mobile/native/darwin/Apps/ensu/ensu.xcodeproj/project.pbxproj
@@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */
21BBA57867859DAD206D6A46 /* EnteNetwork in Frameworks */ = {isa = PBXBuildFile; productRef = DACC3DC82E618D6A00DAF993 /* EnteNetwork */; };
+ A10000012F11111100AA0001 /* InferenceIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A10000022F11111100AA0001 /* InferenceIntegrationTests.swift */; };
DACC3DC92E618D6A00DAF993 /* EnteNetwork in Frameworks */ = {isa = PBXBuildFile; productRef = DACC3DC82E618D6A00DAF993 /* EnteNetwork */; };
DAF584672E61BB8300F8A08F /* ZIPFoundation in Frameworks */ = {isa = PBXBuildFile; productRef = DAF584662E61BB8300F8A08F /* ZIPFoundation */; };
DAF584682E61BB8300F8A08F /* ZIPFoundation in Frameworks */ = {isa = PBXBuildFile; productRef = DAF584662E61BB8300F8A08F /* ZIPFoundation */; };
@@ -19,8 +20,20 @@
DBC1A0042FD0123400ABCD02 /* RiveRuntime in Frameworks */ = {isa = PBXBuildFile; productRef = DBC1A0022FD0123400ABCD02 /* RiveRuntime */; };
/* End PBXBuildFile section */
+/* Begin PBXContainerItemProxy section */
+ A10000072F11111100AA0001 /* PBXContainerItemProxy */ = {
+ isa = PBXContainerItemProxy;
+ containerPortal = DA72BC592E5F87A000D128FF /* Project object */;
+ proxyType = 1;
+ remoteGlobalIDString = DA72BC602E5F87A000D128FF;
+ remoteInfo = ensu;
+ };
+/* End PBXContainerItemProxy section */
+
/* Begin PBXFileReference section */
77E1DDDF900AB047CB554122 /* Ensu.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Ensu.app; sourceTree = BUILT_PRODUCTS_DIR; };
+ A10000022F11111100AA0001 /* InferenceIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InferenceIntegrationTests.swift; sourceTree = "<group>"; };
+ A10000032F11111100AA0001 /* EnsuTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = EnsuTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
DA72BC612E5F87A000D128FF /* ensu.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ensu.app; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
@@ -69,6 +82,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
+ A10000052F11111100AA0001 /* Frameworks */ = {
+ isa = PBXFrameworksBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
DA72BC5E2E5F87A000D128FF /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
@@ -84,10 +104,19 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
+ A10000042F11111100AA0001 /* EnsuTests */ = {
+ isa = PBXGroup;
+ children = (
+ A10000022F11111100AA0001 /* InferenceIntegrationTests.swift */,
+ );
+ path = EnsuTests;
+ sourceTree = "<group>";
+ };
DA72BC582E5F87A000D128FF = {
isa = PBXGroup;
children = (
DA72BC632E5F87A000D128FF /* ensu */,
+ A10000042F11111100AA0001 /* EnsuTests */,
DACC3DC72E618D6A00DAF993 /* Frameworks */,
DA72BC622E5F87A000D128FF /* Products */,
);
@@ -98,6 +127,7 @@
children = (
DA72BC612E5F87A000D128FF /* ensu.app */,
77E1DDDF900AB047CB554122 /* Ensu.app */,
+ A10000032F11111100AA0001 /* EnsuTests.xctest */,
);
name = Products;
sourceTree = "<group>";
@@ -112,6 +142,26 @@
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
+ A10000062F11111100AA0001 /* EnsuTests */ = {
+ isa = PBXNativeTarget;
+ buildConfigurationList = A100000B2F11111100AA0001 /* Build configuration list for PBXNativeTarget "EnsuTests" */;
+ buildPhases = (
+ A10000092F11111100AA0001 /* Sources */,
+ A10000052F11111100AA0001 /* Frameworks */,
+ A10000082F11111100AA0001 /* Resources */,
+ );
+ buildRules = (
+ );
+ dependencies = (
+ A100000A2F11111100AA0001 /* PBXTargetDependency */,
+ );
+ name = EnsuTests;
+ packageProductDependencies = (
+ );
+ productName = EnsuTests;
+ productReference = A10000032F11111100AA0001 /* EnsuTests.xctest */;
+ productType = "com.apple.product-type.bundle.unit-test";
+ };
DA72BC602E5F87A000D128FF /* ensu */ = {
isa = PBXNativeTarget;
buildConfigurationList = DA72BC872E5F87A100D128FF /* Build configuration list for PBXNativeTarget "ensu" */;
@@ -178,6 +228,10 @@
LastSwiftUpdateCheck = 1630;
LastUpgradeCheck = 1630;
TargetAttributes = {
+ A10000062F11111100AA0001 = {
+ CreatedOnToolsVersion = 16.3;
+ TestTargetID = DA72BC602E5F87A000D128FF;
+ };
DA72BC602E5F87A000D128FF = {
CreatedOnToolsVersion = 16.3;
};
@@ -207,6 +261,7 @@
projectDirPath = "";
projectRoot = "";
targets = (
+ A10000062F11111100AA0001 /* EnsuTests */,
DA72BC602E5F87A000D128FF /* ensu */,
EC53501B1BEF7BA36025B216 /* ensu-macos */,
);
@@ -221,6 +276,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
+ A10000082F11111100AA0001 /* Resources */ = {
+ isa = PBXResourcesBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
DA72BC5F2E5F87A000D128FF /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
@@ -294,6 +356,14 @@
/* End PBXShellScriptBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
+ A10000092F11111100AA0001 /* Sources */ = {
+ isa = PBXSourcesBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ A10000012F11111100AA0001 /* InferenceIntegrationTests.swift in Sources */,
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
BB83D829CC14232E7E8BDD51 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
@@ -310,6 +380,14 @@
};
/* End PBXSourcesBuildPhase section */
+/* Begin PBXTargetDependency section */
+ A100000A2F11111100AA0001 /* PBXTargetDependency */ = {
+ isa = PBXTargetDependency;
+ target = DA72BC602E5F87A000D128FF /* ensu */;
+ targetProxy = A10000072F11111100AA0001 /* PBXContainerItemProxy */;
+ };
+/* End PBXTargetDependency section */
+
/* Begin XCBuildConfiguration section */
903C407D9F16DA01750450CF /* Release */ = {
isa = XCBuildConfiguration;
@@ -423,6 +501,58 @@
};
name = Debug;
};
+ A100000C2F11111100AA0001 /* Debug */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ BUNDLE_LOADER = "$(TEST_HOST)";
+ CODE_SIGN_IDENTITY = "Apple Development";
+ CODE_SIGN_STYLE = Automatic;
+ CURRENT_PROJECT_VERSION = 31;
+ DEVELOPMENT_TEAM = 6Z68YJY9Q2;
+ GENERATE_INFOPLIST_FILE = YES;
+ HEADER_SEARCH_PATHS = (
+ "$(inherited)",
+ "$(SRCROOT)/ensu/Generated",
+ );
+ IPHONEOS_DEPLOYMENT_TARGET = 16.0;
+ MARKETING_VERSION = 0.1.15;
+ OTHER_SWIFT_FLAGS = "$(inherited) -Xcc -fmodule-map-file=$(SRCROOT)/ensu/Generated/coreFFI.modulemap -Xcc -fmodule-map-file=$(SRCROOT)/ensu/Generated/dbFFI.modulemap -Xcc -fmodule-map-file=$(SRCROOT)/ensu/Generated/syncFFI.modulemap -Xcc -fmodule-map-file=$(SRCROOT)/ensu/Generated/inferenceFFI.modulemap";
+ PRODUCT_BUNDLE_IDENTIFIER = io.ente.ensu.tests;
+ PRODUCT_NAME = "$(TARGET_NAME)";
+ SWIFT_EMIT_LOC_STRINGS = NO;
+ SWIFT_INCLUDE_PATHS = "$(inherited) $(SRCROOT)/ensu/Generated";
+ SWIFT_VERSION = 5.0;
+ TARGETED_DEVICE_FAMILY = "1,2";
+ TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ensu.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/ensu";
+ };
+ name = Debug;
+ };
+ A100000D2F11111100AA0001 /* Release */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ BUNDLE_LOADER = "$(TEST_HOST)";
+ CODE_SIGN_IDENTITY = "Apple Development";
+ CODE_SIGN_STYLE = Automatic;
+ CURRENT_PROJECT_VERSION = 31;
+ DEVELOPMENT_TEAM = 6Z68YJY9Q2;
+ GENERATE_INFOPLIST_FILE = YES;
+ HEADER_SEARCH_PATHS = (
+ "$(inherited)",
+ "$(SRCROOT)/ensu/Generated",
+ );
+ IPHONEOS_DEPLOYMENT_TARGET = 16.0;
+ MARKETING_VERSION = 0.1.15;
+ OTHER_SWIFT_FLAGS = "$(inherited) -Xcc -fmodule-map-file=$(SRCROOT)/ensu/Generated/coreFFI.modulemap -Xcc -fmodule-map-file=$(SRCROOT)/ensu/Generated/dbFFI.modulemap -Xcc -fmodule-map-file=$(SRCROOT)/ensu/Generated/syncFFI.modulemap -Xcc -fmodule-map-file=$(SRCROOT)/ensu/Generated/inferenceFFI.modulemap";
+ PRODUCT_BUNDLE_IDENTIFIER = io.ente.ensu.tests;
+ PRODUCT_NAME = "$(TARGET_NAME)";
+ SWIFT_EMIT_LOC_STRINGS = NO;
+ SWIFT_INCLUDE_PATHS = "$(inherited) $(SRCROOT)/ensu/Generated";
+ SWIFT_VERSION = 5.0;
+ TARGETED_DEVICE_FAMILY = "1,2";
+ TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ensu.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/ensu";
+ };
+ name = Release;
+ };
DA72BC852E5F87A100D128FF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
@@ -672,6 +802,15 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
+ A100000B2F11111100AA0001 /* Build configuration list for PBXNativeTarget "EnsuTests" */ = {
+ isa = XCConfigurationList;
+ buildConfigurations = (
+ A100000C2F11111100AA0001 /* Debug */,
+ A100000D2F11111100AA0001 /* Release */,
+ );
+ defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
+ };
DA72BC5C2E5F87A000D128FF /* Build configuration list for PBXProject "ensu" */ = {
isa = XCConfigurationList;
buildConfigurations = (