import Foundation import ImageIO import Testing @testable import EquipoShareCore @Test func filePreparationBoundsAndScopedMetadata() throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: directory) } let input = directory.appendingPathComponent("hello.txt") try Data("hello".utf8).write(to: input) let access = ScopedAccess() let prepared = try AttachmentFile.prepareFile(input, directory: directory, access: access) #expect(try Data(contentsOf: prepared.url) == Data("hello".utf8)) #expect(!access.started) #expect(throws: AttachmentFailure.type) { try AttachmentFile.prepareFile(directory, directory: directory) } #expect(throws: AttachmentFailure.tooLarge) { try AttachmentFile.stage(input, directory: directory, maximumBytes: 4, access: access) } } private final class ScopedAccess: AttachmentFileAccess, @unchecked Sendable { var started = false func start(_ url: URL) -> Bool { started = true; return true } func stop(_ url: URL) { started = false } func isRegularFile(_ url: URL) throws -> Bool { #expect(started) if !started { throw AttachmentFailure.type } return try url.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile == true } }