1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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)
}
}