~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
97 lines · 5.4 KB Raw History
import Foundation
import XCTest

@testable import OldFella

final class DraftLocalMediaStoreTests: XCTestCase {
    private let accountID = "did:plc:media-test"

    private func directory() throws -> URL {
        let url = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
        addTeardownBlock { try? FileManager.default.removeItem(at: url) }
        return url
    }

    func testImagesAndReplySafetyPersistAcrossStoreInstances() async throws {
        let root = try directory()
        let store = DraftLocalMediaStore(directory: root)
        let image = ImageAttachment(data: Data([1, 2, 3]), altText: "A boat", mimeType: "image/png")
        let draft = ComposerDraft(
            text: "Remote text", attachments: [image], replyTo: "at://reply", publishResultUnknown: true)
        let prepared = try await store.prepare(draft, accountID: accountID)
        try await store.bind(prepared, remoteID: "remote", fingerprint: "fingerprint", accountID: accountID)
        let restored = DraftLocalMediaStore(directory: root)
        let found = try await restored.entry(remoteID: "remote", fingerprint: "fingerprint", accountID: accountID)
        let entry = try XCTUnwrap(found)
        XCTAssertEqual(entry.replyTo, draft.replyTo)
        XCTAssertTrue(entry.publishResultUnknown)
        XCTAssertEqual(entry.references, prepared.references)
        let images = try await restored.images(entry, altText: ["Current remote alt text"], accountID: accountID)
        XCTAssertEqual(images.first?.id, image.id)
        XCTAssertEqual(images.first?.data, image.data)
        XCTAssertEqual(images.first?.mimeType, image.mimeType)
        XCTAssertEqual(images.first?.altText, "Current remote alt text")
    }

    func testVideoDataAndFilesAreCopied() async throws {
        let root = try directory()
        let source = root.appendingPathComponent("temporary.mp4")
        let bytes = Data([4, 5, 6])
        try bytes.write(to: source)
        let store = DraftLocalMediaStore(directory: root.appendingPathComponent("media"))
        let videos = [VideoAttachment(data: bytes), VideoAttachment(fileURL: source)]
        for (index, video) in videos.enumerated() {
            let prepared = try await store.prepare(
                ComposerDraft(text: "Video", attachments: [], video: video), accountID: accountID)
            try await store.bind(prepared, remoteID: "video-\(index)", fingerprint: "same", accountID: accountID)
            let loaded = try await store.video(prepared, accountID: accountID)
            XCTAssertEqual(loaded?.id, video.id)
            XCTAssertEqual(loaded?.source, .data(bytes))
        }
        try FileManager.default.removeItem(at: source)
        let found = try await store.entry(remoteID: "video-1", fingerprint: "same", accountID: accountID)
        let entry = try XCTUnwrap(found)
        let loaded = try await store.video(entry, accountID: accountID)
        XCTAssertEqual(loaded?.source, .data(bytes))
    }

    func testAccountAndRemotePayloadChangesDoNotReusePrivateContext() async throws {
        let store = DraftLocalMediaStore(directory: try directory())
        let prepared = try await store.prepare(
            ComposerDraft(text: "Reply", attachments: [], replyTo: "at://reply"), accountID: accountID)
        try await store.bind(prepared, remoteID: "same", fingerprint: "original", accountID: accountID)
        let otherAccount = try await store.entry(remoteID: "same", fingerprint: "original", accountID: "../other")
        let changed = try await store.entry(remoteID: "same", fingerprint: "changed", accountID: accountID)
        XCTAssertNil(otherAccount)
        XCTAssertNil(changed)
    }

    func testFailedReplacementKeepsExistingMediaAndContext() async throws {
        let root = try directory()
        let store = DraftLocalMediaStore(directory: root)
        let draft = ComposerDraft(text: "Photo", attachments: [ImageAttachment(data: Data([1]))])
        let prepared = try await store.prepare(draft, accountID: accountID)
        try await store.bind(prepared, remoteID: "saved", fingerprint: "original", accountID: accountID)
        let missing = ComposerDraft(
            text: "Video", attachments: [], video: VideoAttachment(fileURL: root.appendingPathComponent("missing")))
        do {
            _ = try await store.prepare(missing, accountID: accountID)
            XCTFail("Expected the missing video to prevent local preparation")
        } catch {}
        let found = try await store.entry(remoteID: "saved", fingerprint: "original", accountID: accountID)
        let entry = try XCTUnwrap(found)
        let images = try await store.images(entry, altText: [""], accountID: accountID)
        XCTAssertEqual(images.first?.data, draft.attachments.first?.data)
    }

    func testDeletingBindingRemovesItsMedia() async throws {
        let store = DraftLocalMediaStore(directory: try directory())
        let prepared = try await store.prepare(
            ComposerDraft(text: "Photo", attachments: [ImageAttachment(data: Data([1]))]), accountID: accountID)
        try await store.bind(prepared, remoteID: "saved", fingerprint: "original", accountID: accountID)
        try await store.removeBinding(remoteID: "saved", accountID: accountID)
        let removed = try await store.entry(remoteID: "saved", fingerprint: "original", accountID: accountID)
        XCTAssertNil(removed)
    }
}