Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more
test: cover isolated file export payloads
Alex Efremov4cb4097764a9
diff --git a/Fluid.xcodeproj/project.pbxproj b/Fluid.xcodeproj/project.pbxprojindex 6ca82216..9ec6dee1 100644--- a/Fluid.xcodeproj/project.pbxproj+++ b/Fluid.xcodeproj/project.pbxproj@@ -7,6 +7,7 @@objects = {/* Begin PBXBuildFile section */+ F1E2026102900000000000002 /* FileExportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1E2026102900000000000001 /* FileExportTests.swift */; };AD1130000000000000000004 /* OverlayCaptionPresentationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD1130000000000000000003 /* OverlayCaptionPresentationTests.swift */; };AD1030000000000000000004 /* HoldToMutePresentationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD1030000000000000000003 /* HoldToMutePresentationTests.swift */; };AD1030000000000000000002 /* HoldToMuteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD1030000000000000000001 /* HoldToMuteTests.swift */; };@@ -203,6 +204,7 @@/* End PBXCopyFilesBuildPhase section *//* Begin PBXFileReference section */+ F1E2026102900000000000001 /* FileExportTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileExportTests.swift; sourceTree = "<group>"; };AD1130000000000000000003 /* OverlayCaptionPresentationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverlayCaptionPresentationTests.swift; sourceTree = "<group>"; };AD1030000000000000000003 /* HoldToMutePresentationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HoldToMutePresentationTests.swift; sourceTree = "<group>"; };AD1030000000000000000001 /* HoldToMuteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HoldToMuteTests.swift; sourceTree = "<group>"; };@@ -510,6 +512,7 @@F5E000000000000000000020 /* FluidServeTests.swift */,BE24123456789ABCDEF00003 /* ModelTemperatureSupportTests.swift */,B1016A0000000000000000A1 /* LLMClientIdleConnectionTests.swift */,+ F1E2026102900000000000001 /* FileExportTests.swift */,C2B100000000000000000001 /* MeetingTranscriptAssemblerTests.swift */,D9B100000000000000000001 /* VoiceModelDeletionTests.swift */,C2B200000000000000000001 /* MeetingCanonicalPipelineTests.swift */,@@ -917,6 +920,7 @@F5E000000000000000000021 /* FluidServeTests.swift in Sources */,BE24123456789ABCDEF00004 /* ModelTemperatureSupportTests.swift in Sources */,B1016A0000000000000000A2 /* LLMClientIdleConnectionTests.swift in Sources */,+ F1E2026102900000000000002 /* FileExportTests.swift in Sources */,C2B100000000000000000002 /* MeetingTranscriptAssemblerTests.swift in Sources */,D9B100000000000000000002 /* VoiceModelDeletionTests.swift in Sources */,C2B200000000000000000002 /* MeetingCanonicalPipelineTests.swift in Sources */,diff --git a/Sources/Fluid/UI/FileTranscriptionView.swift b/Sources/Fluid/UI/FileTranscriptionView.swiftindex 613c8405..e961963f 100644--- a/Sources/Fluid/UI/FileTranscriptionView.swift+++ b/Sources/Fluid/UI/FileTranscriptionView.swift@@ -879,6 +879,10 @@ struct TranscriptionDocument: FileDocument {}func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {+ try self.makeFileWrapper()+ }++ nonisolated func makeFileWrapper() throws -> FileWrapper {let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent("temp.\(self.format.fileExtension)")switch self.format {diff --git a/Tests/FluidDictationIntegrationTests/FileExportTests.swift b/Tests/FluidDictationIntegrationTests/FileExportTests.swiftnew file mode 100644index 00000000..e32ad0fd--- /dev/null+++ b/Tests/FluidDictationIntegrationTests/FileExportTests.swift@@ -0,0 +1,151 @@+@testable import FluidVoice_Debug+import Foundation+import XCTest++@MainActor+final class FileExportTests: XCTestCase {+ private func requireOwnedTemporaryDirectory() throws -> URL {+ let directory = FileManager.default.temporaryDirectory.resolvingSymlinksInPath().standardizedFileURL+ let configured = ProcessInfo.processInfo.environment["FLUID_EXPORT_TEST_SANDBOX_TEMP"]+ guard let configured,+ directory.path == URL(fileURLWithPath: configured).resolvingSymlinksInPath().standardizedFileURL.path,+ directory.path.contains("fluid-export-tests-")+ else {+ throw XCTSkip("Launch the test host with an owned fluid-export-tests-* TMPDIR and matching FLUID_EXPORT_TEST_SANDBOX_TEMP")+ }+ return directory+ }++ private func fixture(_ index: Int = 0, confidence: Float = 0.95) -> TranscriptionResult {+ TranscriptionResult(+ text: "Synthetic transcript \(index): Привет, Ёж!",+ confidence: confidence,+ duration: 12.5,+ processingTime: 0.75,+ fileName: "fixture-\(index).wav",+ timestamp: Date(timeIntervalSince1970: 1_700_000_000),+ speakerSegments: [SpeakerTranscriptSegment(+ speaker: "Speaker 1", startSeconds: 0, endSeconds: 2, text: "Synthetic speech"+ )],+ speakerLabelingNotice: "Synthetic notice",+ speakerLabelingGaps: [SpeakerTranscriptGap(startSeconds: 2, endSeconds: 2.5)]+ )+ }++ private func document(+ _ result: TranscriptionResult,+ format: FileTranscriptionView.ExportFormat+ ) throws -> TranscriptionDocument {+ _ = try self.requireOwnedTemporaryDirectory()+ return TranscriptionDocument(result: result, format: format, service: FileTranscriptionService(asrService: ASRService()))+ }++ func testExportLeavesExistingTemporaryFilesUntouched() throws {+ let directory = try self.requireOwnedTemporaryDirectory()+ let urls = ["temp.txt", "temp.json"].map { directory.appendingPathComponent($0) }+ guard urls.allSatisfy({ !FileManager.default.fileExists(atPath: $0.path) }) else {+ throw XCTSkip("Refusing to replace pre-existing temporary files")+ }+ let sentinel = Data("Owned export test sentinel".utf8)+ for url in urls { try sentinel.write(to: url, options: .withoutOverwriting) }+ defer { for url in urls { try? FileManager.default.removeItem(at: url) } }++ for format in FileTranscriptionView.ExportFormat.allCases {+ _ = try self.document(self.fixture(), format: format).makeFileWrapper()+ for url in urls {+ XCTAssertEqual(try? Data(contentsOf: url), sentinel, "Export changed \(url.lastPathComponent)")+ }+ }+ }++ func testConcurrentDocumentsKeepTheirOwnPayloads() throws {+ let directory = try self.requireOwnedTemporaryDirectory()+ guard ["temp.txt", "temp.json"].allSatisfy({+ !FileManager.default.fileExists(atPath: directory.appendingPathComponent($0).path)+ }) else {+ throw XCTSkip("Refusing to replace pre-existing temporary files")+ }+ let documents = try (0..<128).map { try self.document(self.fixture($0), format: .text) }+ let expected = (0..<128).map { Data(self.fixture($0).textExport.utf8) }+ let outcomes = ExportOutcomes()++ DispatchQueue.concurrentPerform(iterations: documents.count) { index in+ do {+ let bytes = try documents[index].makeFileWrapper().regularFileContents+ if bytes != expected[index] { outcomes.record("Wrong payload for document \(index)") }+ } catch {+ outcomes.record("Document \(index) failed: \(error)")+ }+ }++ XCTAssertEqual(outcomes.failures, [])+ XCTAssertFalse(FileManager.default.fileExists(atPath: directory.appendingPathComponent("temp.txt").path))+ }++ func testTextPreservesExistingMetadataAndUTF8() throws {+ let result = self.fixture()+ let wrapper = try self.document(result, format: .text).makeFileWrapper()+ XCTAssertTrue(wrapper.isRegularFile)+ XCTAssertEqual(wrapper.regularFileContents, Data(result.textExport.utf8))+ XCTAssertNil(wrapper.preferredFilename)+ }++ func testJSONPreservesExistingSchemaAndFormatting() throws {+ let result = self.fixture()+ let wrapper = try self.document(result, format: .json).makeFileWrapper()+ let bytes = try XCTUnwrap(wrapper.regularFileContents)+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.prettyPrinted, .sortedKeys]+ encoder.dateEncodingStrategy = .iso8601+ XCTAssertEqual(bytes, try encoder.encode(result))+ let json = try XCTUnwrap(JSONSerialization.jsonObject(with: bytes) as? [String: Any])+ XCTAssertEqual(Set(json.keys), [+ "text", "confidence", "duration", "processingTime", "fileName", "timestamp",+ "speakerSegments", "speakerLabelingNotice", "speakerLabelingGaps",+ ])+ XCTAssertEqual(json["fileName"] as? String, "fixture-0.wav")+ XCTAssertEqual(json["timestamp"] as? String, "2023-11-14T22:13:20Z")+ XCTAssertNil(json["id"])+ }++ func testDocumentRetainsCapturedResultAndFormat() throws {+ var selectedResult = self.fixture()+ var selectedFormat = FileTranscriptionView.ExportFormat.text+ let expected = Data(selectedResult.textExport.utf8)+ let document = try self.document(selectedResult, format: selectedFormat)+ selectedResult = self.fixture(1)+ selectedFormat = .json++ _ = try self.document(selectedResult, format: selectedFormat).makeFileWrapper()+ XCTAssertEqual(try document.makeFileWrapper().regularFileContents, expected)+ XCTAssertEqual(try document.makeFileWrapper().regularFileContents, expected)+ }++ func testJSONEncodingFailureReachesDocumentWriter() throws {+ let document = try self.document(self.fixture(confidence: .nan), format: .json)+ for _ in 0..<2 {+ XCTAssertThrowsError(try document.makeFileWrapper()) { error in+ guard case EncodingError.invalidValue = error else {+ return XCTFail("Expected JSON encoding failure, got \(error)")+ }+ }+ }+ }+}++private final class ExportOutcomes: @unchecked Sendable {+ private let lock = NSLock()+ private var storage: [String] = []++ nonisolated func record(_ failure: String) {+ self.lock.lock()+ defer { self.lock.unlock() }+ self.storage.append(failure)+ }++ nonisolated var failures: [String] {+ self.lock.lock()+ defer { self.lock.unlock() }+ return self.storage+ }+}