Buckets:
localhost1978/localhost / LOCALHOST1978 /Modules /SSK_Foundation /Tests /SSK_FoundationTests /AudioArtifactStoreTests.swift
| import Foundation | |
| import SSK_Abstractions | |
| @testable import SSK_Foundation | |
| import UniformTypeIdentifiers | |
| import XCTest | |
| @available(macOS 27.0, iOS 27.0, *) | |
| final class AudioArtifactStoreTests: XCTestCase { | |
| func testRoundTripsJobManifestAndBoundedWidget() async throws { | |
| let fixture = try await makeFixture() | |
| defer { try? FileManager.default.removeItem(at: fixture.root) } | |
| let fixtureDate = Date(timeIntervalSinceReferenceDate: 12_345) | |
| let job = AudioIngestionJobRecord( | |
| clientRequestURI: makeClientRequestURI(), | |
| sourceContentHash: "source-hash", | |
| createdAt: fixtureDate, | |
| updatedAt: fixtureDate | |
| ) | |
| try await fixture.coordinator.saveAudioJobRecord(job) | |
| let savedJob = try await fixture.coordinator.loadAudioJobRecord(id: job.id) | |
| XCTAssertEqual(savedJob, job) | |
| let sessionURI = makeSessionURI() | |
| let artifacts = AudioArtifactManifest.requiredReadyArtifactKinds.map { kind in | |
| AudioArtifactReference( | |
| kind: kind, | |
| uri: makeArtifactURI(kind.rawValue), | |
| contentHash: "hash-\(kind.rawValue)", | |
| byteCount: 1, | |
| createdAt: fixtureDate | |
| ) | |
| } | |
| let manifest = AudioArtifactManifest( | |
| jobID: job.id, | |
| sessionURI: sessionURI, | |
| sourceContentHash: "source-hash", | |
| pipelineVersion: "test", | |
| modelRevision: "test", | |
| cacheKey: "cache-key", | |
| artifacts: artifacts, | |
| createdAt: fixtureDate, | |
| updatedAt: fixtureDate, | |
| retentionClass: "test" | |
| ) | |
| try await fixture.coordinator.saveAudioArtifactManifest(manifest) | |
| let savedManifest = try await fixture.coordinator.loadAudioArtifactManifest(jobID: job.id) | |
| XCTAssertEqual(savedManifest, manifest) | |
| let widget = AudioIngestionWidgetModel( | |
| jobID: job.id, | |
| title: "Fixture", | |
| sourceName: "fixture.wav", | |
| state: .ready, | |
| phaseMessage: "Ready" | |
| ) | |
| try await fixture.coordinator.saveAudioWidgetModel(widget) | |
| let savedWidget = try await fixture.coordinator.loadAudioWidgetModel(jobID: job.id) | |
| XCTAssertEqual(savedWidget, widget) | |
| } | |
| func testReadsExistingTwentyThreeValueTelemetryFormat() async throws { | |
| let fixture = try await makeFixture() | |
| defer { try? FileManager.default.removeItem(at: fixture.root) } | |
| let sessionURI = makeSessionURI() | |
| let gaps = (0..<11).map(Double.init) | |
| let taus = (11..<22).map(Double.init) | |
| try await fixture.coordinator.appendTelemetry( | |
| timestamp: 12.5, | |
| gaps: gaps, | |
| tauChord: taus, | |
| sessionURI: sessionURI, | |
| type: .audio | |
| ) | |
| _ = try await fixture.coordinator.finalizeTelemetrySession(sessionURI: sessionURI) | |
| let frames = try await fixture.coordinator.readAudioTelemetryFrames( | |
| sessionURI: sessionURI | |
| ) | |
| XCTAssertEqual(frames.count, 1) | |
| XCTAssertEqual(frames[0].timestamp, 12.5) | |
| XCTAssertEqual(frames[0].gaps, gaps) | |
| XCTAssertEqual(frames[0].tauChord, taus) | |
| } | |
| func testFailsClosedWithoutMountedVault() async throws { | |
| let coordinator = StorageCoordinator(vaultProvider: TestAudioVaultProvider()) | |
| let job = AudioIngestionJobRecord( | |
| clientRequestURI: makeClientRequestURI(), | |
| sourceContentHash: "source-hash" | |
| ) | |
| do { | |
| try await coordinator.saveAudioJobRecord(job) | |
| XCTFail("Expected an unmounted-vault error") | |
| } catch let error as AudioArtifactStoreError { | |
| XCTAssertEqual(error, .vaultNotMounted) | |
| } | |
| } | |
| func testRemoteUploadKeepsTheLocalVaultMounted() async throws { | |
| let root = FileManager.default.temporaryDirectory | |
| .appendingPathComponent(UUID().uuidString, isDirectory: true) | |
| defer { try? FileManager.default.removeItem(at: root) } | |
| try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) | |
| let localProvider = TestAudioVaultProvider() | |
| let remoteProvider = TestAudioVaultProvider() | |
| let transport = TestRemoteAudioArtifactTransport() | |
| let coordinator = StorageCoordinator( | |
| vaultProvider: localProvider, | |
| remoteVaultProvider: remoteProvider, | |
| remoteAudioArtifactTransport: transport | |
| ) | |
| try await coordinator.mountSecureEnclave(volume: root) | |
| try await coordinator.connectToRemoteVault( | |
| endpoint: URL(string: "https://vault.example")!, | |
| configuration: nil | |
| ) | |
| let jobID = AudioIngestionJobID() | |
| let payload = Data("encrypted-stem".utf8) | |
| let localReference = try await coordinator.saveAudioArtifactData( | |
| payload, | |
| kind: .encryptedStem, | |
| jobID: jobID | |
| ) | |
| let remoteReference = try await coordinator.uploadAudioArtifactData( | |
| payload, | |
| kind: .encryptedStem, | |
| jobID: jobID | |
| ) | |
| XCTAssertEqual(coordinator.vaultURL, root) | |
| XCTAssertEqual(localReference.contentHash, remoteReference.contentHash) | |
| XCTAssertEqual(remoteReference.byteCount, payload.count) | |
| let uploadCount = await transport.uploadCount() | |
| XCTAssertEqual(uploadCount, 1) | |
| } | |
| func testRemoteUploadRejectsAnInsecureEndpoint() async throws { | |
| let root = FileManager.default.temporaryDirectory | |
| .appendingPathComponent(UUID().uuidString, isDirectory: true) | |
| defer { try? FileManager.default.removeItem(at: root) } | |
| try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) | |
| let coordinator = StorageCoordinator( | |
| vaultProvider: TestAudioVaultProvider(), | |
| remoteVaultProvider: TestAudioVaultProvider(), | |
| remoteAudioArtifactTransport: TestRemoteAudioArtifactTransport() | |
| ) | |
| try await coordinator.mountSecureEnclave(volume: root) | |
| do { | |
| try await coordinator.connectToRemoteVault( | |
| endpoint: URL(string: "http://vault.example")!, | |
| configuration: nil | |
| ) | |
| XCTFail("Expected the insecure remote endpoint to fail") | |
| } catch let error as AudioArtifactStoreError { | |
| XCTAssertEqual(error, .remoteWriterRequired) | |
| } | |
| } | |
| func testResolvesOnlyPersistedLocalRevealAndShareURLs() async throws { | |
| let fixture = try await makeFixture() | |
| defer { try? FileManager.default.removeItem(at: fixture.root) } | |
| let jobID = AudioIngestionJobID() | |
| var artifacts: [AudioArtifactReference] = [] | |
| for kind in AudioArtifactManifest.requiredReadyArtifactKinds { | |
| artifacts.append(try await fixture.coordinator.saveAudioArtifactData( | |
| Data(kind.rawValue.utf8), | |
| kind: kind, | |
| jobID: jobID | |
| )) | |
| } | |
| let sharePayload = Data("approved-share".utf8) | |
| let shareReference = try await fixture.coordinator.saveAudioArtifactData( | |
| sharePayload, | |
| kind: .shareArchive, | |
| jobID: jobID | |
| ) | |
| artifacts.append(shareReference) | |
| let manifest = AudioArtifactManifest( | |
| jobID: jobID, | |
| sessionURI: makeSessionURI(), | |
| sourceContentHash: "source-hash", | |
| pipelineVersion: "test", | |
| modelRevision: "test", | |
| cacheKey: "test", | |
| artifacts: artifacts, | |
| sharingState: .complete, | |
| retentionClass: "test" | |
| ) | |
| try await fixture.coordinator.saveAudioArtifactManifest(manifest) | |
| let revealURL = try await fixture.coordinator.revealableAudioJobURL(jobID: jobID) | |
| let shareURL = try await fixture.coordinator.shareableAudioArchiveURL(jobID: jobID) | |
| XCTAssertTrue(revealURL.path.hasPrefix(fixture.root.path + "/")) | |
| XCTAssertTrue(shareURL.path.hasPrefix(revealURL.path + "/")) | |
| XCTAssertEqual(try Data(contentsOf: shareURL), sharePayload) | |
| } | |
| func testTelemetryStoreRejectsInvalidDimensionsAndMissingMount() async throws { | |
| let store = TelemetryStore() | |
| let sessionURI = makeSessionURI() | |
| do { | |
| try await store.appendAudioTelemetry( | |
| timestamp: 0, | |
| gaps: [0], | |
| tauChord: Array(repeating: 0, count: 11), | |
| sessionURI: sessionURI | |
| ) | |
| XCTFail("Expected an invalid-dimensions error") | |
| } catch let error as TelemetryStoreError { | |
| XCTAssertEqual(error, .invalidDimensions(gaps: 1, tauChord: 11)) | |
| } | |
| do { | |
| try await store.appendAudioTelemetry( | |
| timestamp: 0, | |
| gaps: Array(repeating: 0, count: 11), | |
| tauChord: Array(repeating: 0, count: 11), | |
| sessionURI: sessionURI | |
| ) | |
| XCTFail("Expected a missing-mount error") | |
| } catch let error as TelemetryStoreError { | |
| XCTAssertEqual(error, .mountUnavailable) | |
| } | |
| } | |
| private func makeFixture() async throws -> (root: URL, coordinator: StorageCoordinator) { | |
| let root = FileManager.default.temporaryDirectory | |
| .appendingPathComponent(UUID().uuidString, isDirectory: true) | |
| try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) | |
| let coordinator = StorageCoordinator(vaultProvider: TestAudioVaultProvider()) | |
| try await coordinator.mountSecureEnclave(volume: root) | |
| return (root, coordinator) | |
| } | |
| } | |
| private func makeClientRequestURI() -> SSKURI { | |
| SSKURI( | |
| authority: .node(nodeID: SSKURI.NodeIdentifier(uuid: UUID())), | |
| domain: .identity, | |
| subdomain: "client-request" | |
| ) | |
| } | |
| private func makeSessionURI() -> SSKURI { | |
| SSKURI( | |
| authority: .node(nodeID: SSKURI.NodeIdentifier(uuid: UUID())), | |
| domain: .identity, | |
| subdomain: "audio-session" | |
| ) | |
| } | |
| private func makeArtifactURI(_ name: String) -> SSKURI { | |
| SSKURI( | |
| authority: .cas(hash: SSKURI.ContentHash(rawValue: "test")), | |
| domain: .media, | |
| subdomain: "audio-artifact", | |
| pathComponents: [SSKURI.PathComponent(rawValue: name)] | |
| ) | |
| } | |
| private final class TestAudioVaultProvider: VaultStorageProvider, @unchecked Sendable { | |
| private let lock = NSLock() | |
| private var mountedURL: URL? | |
| var currentMountPath: URL? { | |
| lock.withLock { mountedURL } | |
| } | |
| var isMounted: Bool { | |
| lock.withLock { mountedURL != nil } | |
| } | |
| func mount(volume: URL) async throws { | |
| lock.withLock { mountedURL = volume } | |
| } | |
| func unmount() async throws { | |
| lock.withLock { mountedURL = nil } | |
| } | |
| func createVault(at path: URL) async throws { | |
| try FileManager.default.createDirectory(at: path, withIntermediateDirectories: true) | |
| } | |
| func connectToRemoteVault(endpoint: URL, configuration: Data?) async throws { | |
| lock.withLock { mountedURL = endpoint } | |
| } | |
| } | |
| private actor TestRemoteAudioArtifactTransport: RemoteAudioArtifactTransporting { | |
| private var uploads = 0 | |
| func putAudioArtifact( | |
| endpoint: URL, | |
| jobID: AudioIngestionJobID, | |
| kind: AudioArtifactKind, | |
| contentHash: String, | |
| payload: Data | |
| ) async throws -> RemoteAudioArtifactReceipt { | |
| uploads += 1 | |
| return RemoteAudioArtifactReceipt( | |
| uri: makeArtifactURI(kind.rawValue), | |
| contentHash: contentHash, | |
| byteCount: payload.count | |
| ) | |
| } | |
| func getAudioArtifact( | |
| endpoint: URL, | |
| uri: SSKURI, | |
| expectedContentHash: String | |
| ) async throws -> Data { | |
| Data() | |
| } | |
| func deleteAudioArtifact(endpoint: URL, uri: SSKURI) async throws {} | |
| func uploadCount() -> Int { | |
| uploads | |
| } | |
| } | |
Xet Storage Details
- Size:
- 12 kB
- Xet hash:
- cdc874e4ab53878c7b1edbf6759abf9b8bb0607d22f30297e2dfaa92c135a469
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.